1 /* Collect static initialization info into data structures
2 that can be traversed by C++ initialization and finalization
5 Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
6 Contributed by Chris Smith (csmith@convex.com).
7 Heavily modified by Michael Meissner (meissner@cygnus.com),
8 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
10 This file is part of GNU CC.
12 GNU CC is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 GNU CC is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GNU CC; see the file COPYING. If not, write to
24 the Free Software Foundation, 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. */
28 /* Build tables of static constructors and destructors and run ld. */
31 #include <sys/types.h>
43 #include "gansidecl.h"
51 extern const char *const sys_errlist[];
53 extern char *sys_errlist[];
60 /* Obstack allocation and deallocation routines. */
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
75 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
78 #define WTERMSIG(S) ((S) & 0x7f)
81 #define WIFEXITED(S) (((S) & 0xff) == 0)
84 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
87 extern char *choose_temp_base ();
89 /* On certain systems, we have code that works by scanning the object file
90 directly. But this code uses system-specific header files and library
91 functions, so turn it off in a cross-compiler. Likewise, the names of
92 the utilities aren't correct for a cross-compiler; we have to hope that
93 cross-versions are in the proper directories. */
96 #undef SUNOS4_SHARED_LIBRARIES
97 #undef OBJECT_FORMAT_COFF
98 #undef OBJECT_FORMAT_ROSE
100 #undef REAL_LD_FILE_NAME
101 #undef REAL_NM_FILE_NAME
102 #undef REAL_STRIP_FILE_NAME
105 /* If we can't use a special method, use the ordinary one:
106 run nm to find what symbols are present.
107 In a cross-compiler, this means you need a cross nm,
108 but that isn't quite as unpleasant as special headers. */
110 #if !defined (OBJECT_FORMAT_COFF) && !defined (OBJECT_FORMAT_ROSE)
111 #define OBJECT_FORMAT_NONE
114 #ifdef OBJECT_FORMAT_COFF
123 /* Many versions of ldfcn.h define these. */
131 /* Some systems have an ISCOFF macro, but others do not. In some cases
132 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
133 that either do not have an ISCOFF macro in /usr/include or for those
134 where it is wrong. */
137 #define MY_ISCOFF(X) ISCOFF (X)
140 #ifdef XCOFF_DEBUGGING_INFO
141 #define XCOFF_SCAN_LIBS
144 #endif /* OBJECT_FORMAT_COFF */
146 #ifdef OBJECT_FORMAT_ROSE
153 #include <sys/mman.h>
157 #include <mach_o_format.h>
158 #include <mach_o_header.h>
159 #include <mach_o_vals.h>
160 #include <mach_o_types.h>
162 #endif /* OBJECT_FORMAT_ROSE */
164 #ifdef OBJECT_FORMAT_NONE
166 /* Default flags to pass to nm. */
168 #define NM_FLAGS "-p"
171 #endif /* OBJECT_FORMAT_NONE */
173 /* Some systems use __main in a way incompatible with its use in gcc, in these
174 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
175 give the same symbol without quotes for an alternative entry point. You
176 must define both, or neither. */
178 #define NAME__MAIN "__main"
179 #define SYMBOL__MAIN __main
182 #if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES || defined(XCOFF_SCAN_LIBS)
183 #define SCAN_LIBRARIES
187 int do_collecting = 1;
189 int do_collecting = 0;
192 /* Linked lists of constructor and destructor names. */
208 /* Enumeration giving which pass this is for scanning the program file. */
211 PASS_FIRST, /* without constructors */
212 PASS_OBJ, /* individual objects */
213 PASS_LIB, /* looking for shared libraries */
214 PASS_SECOND /* with constructors linked in */
217 #ifndef NO_SYS_SIGLIST
218 #ifndef DONT_DECLARE_SYS_SIGLIST
219 extern char *sys_siglist[];
222 extern char *version_string;
224 int vflag; /* true if -v */
225 static int rflag; /* true if -r */
226 static int strip_flag; /* true if -s */
228 int debug; /* true if -debug */
230 static int shared_obj; /* true if -shared */
232 static int temp_filename_length; /* Length of temp_filename */
233 static char *temp_filename; /* Base of temp filenames */
234 static char *c_file; /* <xxx>.c for constructor/destructor list. */
235 static char *o_file; /* <xxx>.o for constructor/destructor list. */
236 static char *export_file; /* <xxx>.x for AIX export list. */
237 char *ldout; /* File for ld errors. */
238 static char *output_file; /* Output file for ld. */
239 static char *nm_file_name; /* pathname of nm */
240 static char *ldd_file_name; /* pathname of ldd (or equivalent) */
241 static char *strip_file_name; /* pathname of strip */
242 char *c_file_name; /* pathname of gcc */
243 static char *initname, *fininame; /* names of init and fini funcs */
245 static struct head constructors; /* list of constructors found */
246 static struct head destructors; /* list of destructors found */
247 static struct head exports; /* list of exported symbols */
249 struct obstack temporary_obstack;
250 struct obstack permanent_obstack;
251 char * temporary_firstobj;
253 /* Defined in the automatically-generated underscore.c. */
254 extern int prepends_underscore;
256 extern char *getenv ();
257 extern char *mktemp ();
258 extern FILE *fdopen ();
260 /* Structure to hold all the directories in which to search for files to
265 char *prefix; /* String to prepend to the path. */
266 struct prefix_list *next; /* Next in linked list. */
271 struct prefix_list *plist; /* List of prefixes to try */
272 int max_len; /* Max length of a prefix in PLIST */
273 char *name; /* Name of this list (used in config stuff) */
276 void collect_exit PROTO((int));
277 void collect_execute PROTO((char *, char **, char *));
278 void dump_file PROTO((char *));
279 static void handler PROTO((int));
280 static int is_ctor_dtor PROTO((char *));
281 static int is_in_prefix_list PROTO((struct path_prefix *, char *, int));
282 static char *find_a_file PROTO((struct path_prefix *, char *));
283 static void add_prefix PROTO((struct path_prefix *, char *));
284 static void prefix_from_env PROTO((char *, struct path_prefix *));
285 static void prefix_from_string PROTO((char *, struct path_prefix *));
286 static void do_wait PROTO((char *));
287 static void fork_execute PROTO((char *, char **));
288 static void maybe_unlink PROTO((char *));
289 static void add_to_list PROTO((struct head *, char *));
290 static void write_list PROTO((FILE *, char *, struct id *));
291 static void write_list_with_asm PROTO((FILE *, char *, struct id *));
292 static void write_c_file PROTO((FILE *, char *));
293 static void write_export_file PROTO((FILE *));
294 static void scan_prog_file PROTO((char *, enum pass));
295 static void scan_libraries PROTO((char *));
300 extern char *index ();
301 extern char *rindex ();
317 while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
320 close (fdtmp[--fdx]);
336 static char buffer[30];
340 if (e > 0 && e < sys_nerr)
341 return sys_errlist[e];
343 sprintf (buffer, "Unknown error %d", e);
348 /* Delete tempfiles and exit function. */
351 collect_exit (status)
354 if (c_file != 0 && c_file[0])
355 maybe_unlink (c_file);
357 if (o_file != 0 && o_file[0])
358 maybe_unlink (o_file);
360 if (export_file != 0 && export_file[0])
361 maybe_unlink (export_file);
363 if (ldout != 0 && ldout[0])
366 maybe_unlink (ldout);
369 if (status != 0 && output_file != 0 && output_file[0])
370 maybe_unlink (output_file);
376 /* Die when sys call fails. */
379 fatal_perror (string, arg1, arg2, arg3)
380 char *string, *arg1, *arg2, *arg3;
384 fprintf (stderr, "collect2: ");
385 fprintf (stderr, string, arg1, arg2, arg3);
386 fprintf (stderr, ": %s\n", my_strerror (e));
393 fatal (string, arg1, arg2, arg3)
394 char *string, *arg1, *arg2, *arg3;
396 fprintf (stderr, "collect2: ");
397 fprintf (stderr, string, arg1, arg2, arg3);
398 fprintf (stderr, "\n");
402 /* Write error message. */
405 error (string, arg1, arg2, arg3, arg4)
406 char *string, *arg1, *arg2, *arg3, *arg4;
408 fprintf (stderr, "collect2: ");
409 fprintf (stderr, string, arg1, arg2, arg3, arg4);
410 fprintf (stderr, "\n");
413 /* In case obstack is linked in, and abort is defined to fancy_abort,
414 provide a default entry. */
419 fatal ("internal error");
427 if (c_file != 0 && c_file[0])
428 maybe_unlink (c_file);
430 if (o_file != 0 && o_file[0])
431 maybe_unlink (o_file);
433 if (ldout != 0 && ldout[0])
434 maybe_unlink (ldout);
436 signal (signo, SIG_DFL);
437 kill (getpid (), signo);
442 xcalloc (size1, size2)
445 char *ptr = (char *) calloc (size1, size2);
449 fatal ("out of memory");
457 char *ptr = (char *) malloc (size);
461 fatal ("out of memory");
470 register char *value = (char *) realloc (ptr, size);
472 fatal ("virtual memory exhausted");
480 return access (name, R_OK) == 0;
483 /* Make a copy of a string INPUT with size SIZE. */
486 savestring (input, size)
490 char *output = (char *) xmalloc (size + 1);
491 bcopy (input, output, size);
500 FILE *stream = fopen (name, "r");
501 int no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
508 while (c = getc (stream),
509 c != EOF && (isalnum (c) || c == '_' || c == '$' || c == '.'))
510 obstack_1grow (&temporary_obstack, c);
511 if (obstack_object_size (&temporary_obstack) > 0)
513 char *word, *p, *result;
514 obstack_1grow (&temporary_obstack, '\0');
515 word = obstack_finish (&temporary_obstack);
518 ++word, putc ('.', stderr);
520 if (*p == '_' && prepends_underscore)
526 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI);
531 fputs (result, stderr);
533 diff = strlen (word) - strlen (result);
535 --diff, putc (' ', stderr);
536 while (diff < 0 && c == ' ')
537 ++diff, c = getc (stream);
542 fputs (word, stderr);
545 obstack_free (&temporary_obstack, temporary_firstobj);
553 /* Decide whether the given symbol is:
554 a constructor (1), a destructor (2), or neither (0). */
560 struct names { char *name; int len; int ret; int two_underscores; };
562 register struct names *p;
564 register char *orig_s = s;
566 static struct names special[] = {
567 #ifdef NO_DOLLAR_IN_LABEL
568 #ifdef NO_DOT_IN_LABEL
569 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
570 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
572 { "GLOBAL_.I.", sizeof ("GLOBAL_.I.")-1, 1, 0 },
573 { "GLOBAL_.D.", sizeof ("GLOBAL_.D.")-1, 2, 0 },
576 { "GLOBAL_$I$", sizeof ("GLOBAL_$I$")-1, 1, 0 },
577 { "GLOBAL_$D$", sizeof ("GLOBAL_$D$")-1, 2, 0 },
579 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
580 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
581 #ifdef CFRONT_LOSSAGE /* Don't collect cfront initialization functions.
582 cfront has its own linker procedure to collect them;
583 if collect2 gets them too, they get collected twice
584 when the cfront procedure is run and the compiler used
585 for linking happens to be GCC. */
586 { "sti__", sizeof ("sti__")-1, 1, 1 },
587 { "std__", sizeof ("std__")-1, 2, 1 },
588 #endif /* CFRONT_LOSSAGE */
592 while ((ch = *s) == '_')
598 for (p = &special[0]; p->len > 0; p++)
601 && (!p->two_underscores || ((s - orig_s) >= 2))
602 && strncmp(s, p->name, p->len) == 0)
610 /* Routine to add variables to the environment. */
618 #ifndef VMS /* nor about VMS */
620 extern char **environ;
621 char **old_environ = environ;
628 while ((ch = *p++) != '\0' && ch != '=')
634 /* Search for replacing an existing environment variable, and
635 count the number of total environment variables. */
636 for (envp = old_environ; *envp; envp++)
639 if (!strncmp (str, *envp, name_len))
646 /* Add a new environment variable */
647 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
649 bcopy ((char *) old_environ, (char *) (environ + 1),
650 sizeof (char *) * (num_envs+1));
656 #endif /* HAVE_PUTENV */
658 /* By default, colon separates directories in a path. */
659 #ifndef PATH_SEPARATOR
660 #define PATH_SEPARATOR ':'
663 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
664 and one from the PATH variable. */
666 static struct path_prefix cpath, path;
669 /* This is the name of the target machine. We use it to form the name
670 of the files to execute. */
672 static char *target_machine = TARGET_MACHINE;
675 /* Names under which we were executed. Never return one of those files in our
678 static struct path_prefix our_file_names;
680 /* Determine if STRING is in PPREFIX.
682 This utility is currently only used to look up file names. Prefix lists
683 record directory names. This matters to us because the latter has a
684 trailing slash, so I've added a flag to handle both. */
687 is_in_prefix_list (pprefix, string, filep)
688 struct path_prefix *pprefix;
692 struct prefix_list *pl;
696 int len = strlen (string);
698 for (pl = pprefix->plist; pl; pl = pl->next)
700 if (strncmp (pl->prefix, string, len) == 0
701 && strcmp (pl->prefix + len, "/") == 0)
707 for (pl = pprefix->plist; pl; pl = pl->next)
709 if (strcmp (pl->prefix, string) == 0)
717 /* Search for NAME using prefix list PPREFIX. We only look for executable
720 Return 0 if not found, otherwise return its name, allocated with malloc. */
723 find_a_file (pprefix, name)
724 struct path_prefix *pprefix;
728 struct prefix_list *pl;
729 int len = pprefix->max_len + strlen (name) + 1;
731 #ifdef EXECUTABLE_SUFFIX
732 len += strlen (EXECUTABLE_SUFFIX);
735 temp = xmalloc (len);
737 /* Determine the filename to execute (special case for absolute paths). */
741 if (access (name, X_OK) == 0)
748 for (pl = pprefix->plist; pl; pl = pl->next)
750 strcpy (temp, pl->prefix);
752 if (! is_in_prefix_list (&our_file_names, temp, 1)
753 /* This is a kludge, but there seems no way around it. */
754 && strcmp (temp, "./ld") != 0
755 && access (temp, X_OK) == 0)
758 #ifdef EXECUTABLE_SUFFIX
759 /* Some systems have a suffix for executable files.
760 So try appending that. */
761 strcat (temp, EXECUTABLE_SUFFIX);
762 if (! is_in_prefix_list (&our_file_names, temp, 1)
763 && access (temp, X_OK) == 0)
772 /* Add an entry for PREFIX to prefix list PPREFIX. */
775 add_prefix (pprefix, prefix)
776 struct path_prefix *pprefix;
779 struct prefix_list *pl, **prev;
784 for (pl = pprefix->plist; pl->next; pl = pl->next)
789 prev = &pprefix->plist;
791 /* Keep track of the longest prefix */
793 len = strlen (prefix);
794 if (len > pprefix->max_len)
795 pprefix->max_len = len;
797 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
798 pl->prefix = savestring (prefix, len);
803 pl->next = (struct prefix_list *) 0;
807 /* Take the value of the environment variable ENV, break it into a path, and
808 add of the entries to PPREFIX. */
811 prefix_from_env (env, pprefix)
813 struct path_prefix *pprefix;
815 char *p = getenv (env);
818 prefix_from_string (p, pprefix);
822 prefix_from_string (p, pprefix)
824 struct path_prefix *pprefix;
827 char *nstore = (char *) xmalloc (strlen (p) + 3);
832 if (*endp == PATH_SEPARATOR || *endp == 0)
834 strncpy (nstore, startp, endp-startp);
837 strcpy (nstore, "./");
839 else if (endp[-1] != '/')
841 nstore[endp-startp] = '/';
842 nstore[endp-startp+1] = 0;
845 nstore[endp-startp] = 0;
847 add_prefix (pprefix, nstore);
850 endp = startp = endp + 1;
864 char *ld_suffix = "ld";
865 char *full_ld_suffix = ld_suffix;
866 char *real_ld_suffix = "real-ld";
867 char *full_real_ld_suffix = real_ld_suffix;
868 char *collect_ld_suffix = "collect-ld";
869 char *nm_suffix = "nm";
870 char *full_nm_suffix = nm_suffix;
871 char *gnm_suffix = "gnm";
872 char *full_gnm_suffix = gnm_suffix;
874 char *ldd_suffix = LDD_SUFFIX;
875 char *full_ldd_suffix = ldd_suffix;
877 char *strip_suffix = "strip";
878 char *full_strip_suffix = strip_suffix;
879 char *gstrip_suffix = "gstrip";
880 char *full_gstrip_suffix = gstrip_suffix;
882 FILE *outf, *exportf;
889 char **ld1_argv = (char **) xcalloc (sizeof (char *), argc+3);
890 char **ld1 = ld1_argv;
891 char **ld2_argv = (char **) xcalloc (sizeof (char *), argc+6);
892 char **ld2 = ld2_argv;
893 char **object_lst = (char **) xcalloc (sizeof (char *), argc);
894 char **object = object_lst;
896 int num_c_args = argc+7;
903 #ifndef DEFAULT_A_OUT_NAME
904 output_file = "a.out";
906 output_file = DEFAULT_A_OUT_NAME;
909 obstack_begin (&temporary_obstack, 0);
910 obstack_begin (&permanent_obstack, 0);
911 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
912 current_demangling_style = gnu_demangling;
914 /* We must check that we do not call ourselves in an infinite
915 recursion loop. We append the name used for us to the COLLECT_NAMES
916 environment variable.
918 In practice, collect will rarely invoke itself. This can happen now
919 that we are no longer called gld. A perfect example is when running
920 gcc in a build directory that has been installed. When looking for
921 ld's, we'll find our installed version and believe that's the real ld. */
923 /* We must also append COLLECT_NAME to COLLECT_NAMES to watch for the
924 previous version of collect (the one that used COLLECT_NAME and only
925 handled two levels of recursion). If we don't we may mutually recurse
926 forever. This can happen (I think) when bootstrapping the old version
927 and a new one is installed (rare, but we should handle it).
928 ??? Hopefully references to COLLECT_NAME can be removed at some point. */
930 collect_name = (char *) getenv ("COLLECT_NAME");
931 collect_names = (char *) getenv ("COLLECT_NAMES");
933 p = (char *) xmalloc (strlen ("COLLECT_NAMES=")
934 + (collect_name ? strlen (collect_name) + 1 : 0)
935 + (collect_names ? strlen (collect_names) + 1 : 0)
936 + strlen (argv[0]) + 1);
937 strcpy (p, "COLLECT_NAMES=");
938 if (collect_name != 0)
939 sprintf (p + strlen (p), "%s%c", collect_name, PATH_SEPARATOR);
940 if (collect_names != 0)
941 sprintf (p + strlen (p), "%s%c", collect_names, PATH_SEPARATOR);
945 prefix_from_env ("COLLECT_NAMES", &our_file_names);
947 /* Set environment variable COLLECT_NAME to our name so the previous version
948 of collect won't find us. If it does we'll mutually recurse forever.
949 This can happen when bootstrapping the new version and an old version is
951 ??? Hopefully this bit of code can be removed at some point. */
953 p = xmalloc (strlen ("COLLECT_NAME=") + strlen (argv[0]) + 1);
954 sprintf (p, "COLLECT_NAME=%s", argv[0]);
957 p = (char *) getenv ("COLLECT_GCC_OPTIONS");
962 while (*q && *q != ' ') q++;
963 if (*p == '-' && p[1] == 'm')
970 c_ptr = c_argv = (char **) xcalloc (sizeof (char *), num_c_args);
973 fatal ("no arguments");
976 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
977 signal (SIGQUIT, handler);
979 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
980 signal (SIGINT, handler);
982 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
983 signal (SIGALRM, handler);
986 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
987 signal (SIGHUP, handler);
989 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
990 signal (SIGSEGV, handler);
992 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
993 signal (SIGBUS, handler);
996 /* Extract COMPILER_PATH and PATH into our prefix list. */
997 prefix_from_env ("COMPILER_PATH", &cpath);
998 prefix_from_env ("PATH", &path);
1000 #ifdef CROSS_COMPILE
1001 /* If we look for a program in the compiler directories, we just use
1002 the short name, since these directories are already system-specific.
1003 But it we look for a took in the system directories, we need to
1004 qualify the program name with the target machine. */
1007 = xcalloc (strlen (ld_suffix) + strlen (target_machine) + 2, 1);
1008 strcpy (full_ld_suffix, target_machine);
1009 strcat (full_ld_suffix, "-");
1010 strcat (full_ld_suffix, ld_suffix);
1013 = xcalloc (strlen (real_ld_suffix) + strlen (target_machine) + 2, 1);
1014 strcpy (full_real_ld_suffix, target_machine);
1015 strcat (full_real_ld_suffix, "-");
1016 strcat (full_real_ld_suffix, real_ld_suffix);
1020 = xcalloc (strlen (gld_suffix) + strlen (target_machine) + 2, 1);
1021 strcpy (full_gld_suffix, target_machine);
1022 strcat (full_gld_suffix, "-");
1023 strcat (full_gld_suffix, gld_suffix);
1027 = xcalloc (strlen (nm_suffix) + strlen (target_machine) + 2, 1);
1028 strcpy (full_nm_suffix, target_machine);
1029 strcat (full_nm_suffix, "-");
1030 strcat (full_nm_suffix, nm_suffix);
1033 = xcalloc (strlen (gnm_suffix) + strlen (target_machine) + 2, 1);
1034 strcpy (full_gnm_suffix, target_machine);
1035 strcat (full_gnm_suffix, "-");
1036 strcat (full_gnm_suffix, gnm_suffix);
1040 = xcalloc (strlen (ldd_suffix) + strlen (target_machine) + 2, 1);
1041 strcpy (full_ldd_suffix, target_machine);
1042 strcat (full_ldd_suffix, "-");
1043 strcat (full_ldd_suffix, ldd_suffix);
1047 = xcalloc (strlen (strip_suffix) + strlen (target_machine) + 2, 1);
1048 strcpy (full_strip_suffix, target_machine);
1049 strcat (full_strip_suffix, "-");
1050 strcat (full_strip_suffix, strip_suffix);
1053 = xcalloc (strlen (gstrip_suffix) + strlen (target_machine) + 2, 1);
1054 strcpy (full_gstrip_suffix, target_machine);
1055 strcat (full_gstrip_suffix, "-");
1056 strcat (full_gstrip_suffix, gstrip_suffix);
1057 #endif /* CROSS_COMPILE */
1059 /* Try to discover a valid linker/nm/strip to use. */
1061 /* Maybe we know the right file to use (if not cross). */
1062 #ifdef REAL_LD_FILE_NAME
1063 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
1064 if (ld_file_name == 0)
1066 /* Search the (target-specific) compiler dirs for ld'. */
1067 ld_file_name = find_a_file (&cpath, real_ld_suffix);
1068 /* Likewise for `collect-ld'. */
1069 if (ld_file_name == 0)
1070 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
1071 /* Search the compiler directories for `ld'. We have protection against
1072 recursive calls in find_a_file. */
1073 if (ld_file_name == 0)
1074 ld_file_name = find_a_file (&cpath, ld_suffix);
1075 /* Search the ordinary system bin directories
1076 for `ld' (if native linking) or `TARGET-ld' (if cross). */
1077 if (ld_file_name == 0)
1078 ld_file_name = find_a_file (&path, full_ld_suffix);
1080 /* If we've invoked ourselves, try again with LD_FILE_NAME. */
1082 if (collect_names != 0)
1084 if (ld_file_name != 0)
1086 argv[0] = ld_file_name;
1087 execvp (argv[0], argv);
1089 fatal ("cannot find `ld'");
1092 #ifdef REAL_NM_FILE_NAME
1093 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
1094 if (nm_file_name == 0)
1096 nm_file_name = find_a_file (&cpath, gnm_suffix);
1097 if (nm_file_name == 0)
1098 nm_file_name = find_a_file (&path, full_gnm_suffix);
1099 if (nm_file_name == 0)
1100 nm_file_name = find_a_file (&cpath, nm_suffix);
1101 if (nm_file_name == 0)
1102 nm_file_name = find_a_file (&path, full_nm_suffix);
1105 ldd_file_name = find_a_file (&cpath, ldd_suffix);
1106 if (ldd_file_name == 0)
1107 ldd_file_name = find_a_file (&path, full_ldd_suffix);
1110 #ifdef REAL_STRIP_FILE_NAME
1111 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
1112 if (strip_file_name == 0)
1114 strip_file_name = find_a_file (&cpath, gstrip_suffix);
1115 if (strip_file_name == 0)
1116 strip_file_name = find_a_file (&path, full_gstrip_suffix);
1117 if (strip_file_name == 0)
1118 strip_file_name = find_a_file (&cpath, strip_suffix);
1119 if (strip_file_name == 0)
1120 strip_file_name = find_a_file (&path, full_strip_suffix);
1122 /* Determine the full path name of the C compiler to use. */
1123 c_file_name = getenv ("COLLECT_GCC");
1124 if (c_file_name == 0)
1126 #ifdef CROSS_COMPILE
1127 c_file_name = xcalloc (sizeof ("gcc-") + strlen (target_machine) + 1, 1);
1128 strcpy (c_file_name, target_machine);
1129 strcat (c_file_name, "-gcc");
1131 c_file_name = "gcc";
1135 p = find_a_file (&cpath, c_file_name);
1137 /* Here it should be safe to use the system search path since we should have
1138 already qualified the name of the compiler when it is needed. */
1140 p = find_a_file (&path, c_file_name);
1145 *ld1++ = *ld2++ = ld_file_name;
1147 /* Make temp file names. */
1148 temp_filename = choose_temp_base ();
1149 temp_filename_length = strlen (temp_filename);
1150 c_file = xcalloc (temp_filename_length + sizeof (".c"), 1);
1151 o_file = xcalloc (temp_filename_length + sizeof (".o"), 1);
1152 export_file = xmalloc (temp_filename_length + sizeof (".x"));
1153 ldout = xmalloc (temp_filename_length + sizeof (".ld"));
1154 sprintf (ldout, "%s.ld", temp_filename);
1155 sprintf (c_file, "%s.c", temp_filename);
1156 sprintf (o_file, "%s.o", temp_filename);
1157 sprintf (export_file, "%s.x", temp_filename);
1158 *c_ptr++ = c_file_name;
1163 /* !!! When GCC calls collect2,
1164 it does not know whether it is calling collect2 or ld.
1165 So collect2 cannot meaningfully understand any options
1166 except those ld understands.
1167 If you propose to make GCC pass some other option,
1168 just imagine what will happen if ld is really ld!!! */
1170 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1171 /* After the first file, put in the c++ rt0. */
1174 while ((arg = *++argv) != (char *)0)
1176 *ld1++ = *ld2++ = arg;
1183 if (!strcmp (arg, "-debug"))
1195 /* place o_file BEFORE this argument! */
1205 output_file = *ld1++ = *ld2++ = *++argv;
1207 output_file = &arg[2];
1216 if (arg[2] == '\0' && do_collecting)
1218 /* We must strip after the nm run, otherwise C++ linking
1219 won't work. Thus we strip in the second ld run, or
1220 else with strip if there is no second ld run. */
1232 else if ((p = rindex (arg, '.')) != (char *)0
1233 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0))
1242 /* place o_file BEFORE this argument! */
1253 /* Get any options that the upper GCC wants to pass to the sub-GCC. */
1254 p = (char *) getenv ("COLLECT_GCC_OPTIONS");
1259 while (*q && *q != ' ') q++;
1260 if (*p == '-' && (p[1] == 'm' || p[1] == 'f'))
1261 *c_ptr++ = savestring (p, q - p);
1262 if (strncmp (p, "-shared", sizeof ("shared") - 1) == 0)
1269 #ifdef COLLECT_EXPORT_LIST
1270 /* The AIX linker will discard static constructors in object files if
1271 nothing else in the file is referenced, so look at them first. */
1272 while (object_lst < object)
1273 scan_prog_file (*object_lst++, PASS_OBJ);
1276 char *buf = alloca (strlen (export_file) + 5);
1277 sprintf (buf, "-bE:%s", export_file);
1280 exportf = fopen (export_file, "w");
1281 if (exportf == (FILE *)0)
1282 fatal_perror ("%s", export_file);
1283 write_export_file (exportf);
1284 if (fclose (exportf))
1285 fatal_perror ("closing %s", export_file);
1290 *object = *c_ptr = *ld1 = (char *)0;
1294 fprintf (stderr, "collect2 version %s", version_string);
1295 #ifdef TARGET_VERSION
1298 fprintf (stderr, "\n");
1304 fprintf (stderr, "ld_file_name = %s\n",
1305 (ld_file_name ? ld_file_name : "not found"));
1306 fprintf (stderr, "c_file_name = %s\n",
1307 (c_file_name ? c_file_name : "not found"));
1308 fprintf (stderr, "nm_file_name = %s\n",
1309 (nm_file_name ? nm_file_name : "not found"));
1311 fprintf (stderr, "ldd_file_name = %s\n",
1312 (ldd_file_name ? ldd_file_name : "not found"));
1314 fprintf (stderr, "strip_file_name = %s\n",
1315 (strip_file_name ? strip_file_name : "not found"));
1316 fprintf (stderr, "c_file = %s\n",
1317 (c_file ? c_file : "not found"));
1318 fprintf (stderr, "o_file = %s\n",
1319 (o_file ? o_file : "not found"));
1321 ptr = getenv ("COLLECT_NAMES");
1323 fprintf (stderr, "COLLECT_NAMES = %s\n", ptr);
1325 ptr = getenv ("COLLECT_GCC_OPTIONS");
1327 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1329 ptr = getenv ("COLLECT_GCC");
1331 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1333 ptr = getenv ("COMPILER_PATH");
1335 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1337 ptr = getenv ("LIBRARY_PATH");
1339 fprintf (stderr, "LIBRARY_PATH = %s\n", ptr);
1341 fprintf (stderr, "\n");
1344 /* Load the program, searching all libraries. */
1346 collect_execute ("ld", ld1_argv, ldout);
1351 /* If -r or they'll be run via some other method, don't build the
1352 constructor or destructor list, just return now. */
1353 if (rflag || ! do_collecting)
1356 /* Examine the namelist with nm and search it for static constructors
1357 and destructors to call.
1358 Write the constructor and destructor tables to a .s file and reload. */
1360 scan_prog_file (output_file, PASS_FIRST);
1362 #ifdef SCAN_LIBRARIES
1363 scan_libraries (output_file);
1368 fprintf (stderr, "%d constructor(s) found\n", constructors.number);
1369 fprintf (stderr, "%d destructor(s) found\n", destructors.number);
1372 if (constructors.number == 0 && destructors.number == 0
1374 /* If we will be running these functions ourselves, we want to emit
1375 stubs into the shared library so that we don't have to relink
1376 dependent programs when we add static objects. */
1381 /* Strip now if it was requested on the command line. */
1384 char **strip_argv = (char **) xcalloc (sizeof (char *), 3);
1385 strip_argv[0] = strip_file_name;
1386 strip_argv[1] = output_file;
1387 strip_argv[2] = (char *) 0;
1388 fork_execute ("strip", strip_argv);
1391 #ifdef COLLECT_EXPORT_LIST
1392 maybe_unlink (export_file);
1397 maybe_unlink(output_file);
1398 outf = fopen (c_file, "w");
1399 if (outf == (FILE *)0)
1400 fatal_perror ("%s", c_file);
1402 write_c_file (outf, c_file);
1405 fatal_perror ("closing %s", c_file);
1407 /* Tell the linker that we have initializer and finalizer functions. */
1408 #ifdef LD_INIT_SWITCH
1409 *ld2++ = LD_INIT_SWITCH;
1411 *ld2++ = LD_FINI_SWITCH;
1416 #ifdef COLLECT_EXPORT_LIST
1419 add_to_list (&exports, initname);
1420 add_to_list (&exports, fininame);
1421 add_to_list (&exports, "_GLOBAL__DI");
1422 add_to_list (&exports, "_GLOBAL__DD");
1423 exportf = fopen (export_file, "w");
1424 if (exportf == (FILE *)0)
1425 fatal_perror ("%s", export_file);
1426 write_export_file (exportf);
1427 if (fclose (exportf))
1428 fatal_perror ("closing %s", export_file);
1434 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1435 output_file, c_file);
1436 write_c_file (stderr, "stderr");
1437 fprintf (stderr, "========== end of c_file\n\n");
1438 #ifdef COLLECT_EXPORT_LIST
1439 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1440 write_export_file (stderr);
1441 fprintf (stderr, "========== end of export_file\n\n");
1445 /* Assemble the constructor and destructor tables.
1446 Link the tables in with the rest of the program. */
1448 fork_execute ("gcc", c_argv);
1449 fork_execute ("ld", ld2_argv);
1451 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1452 constructors/destructors in shared libraries. */
1453 scan_prog_file (output_file, PASS_SECOND);
1455 maybe_unlink (c_file);
1456 maybe_unlink (o_file);
1457 maybe_unlink (export_file);
1462 /* Wait for a process to finish, and exit if a non-zero status is found. */
1473 if (WIFSIGNALED (status))
1475 int sig = WTERMSIG (status);
1476 #ifdef NO_SYS_SIGLIST
1477 error ("%s terminated with signal %d %s",
1480 (status & 0200) ? ", core dumped" : "");
1482 error ("%s terminated with signal %d [%s]%s",
1486 (status & 0200) ? ", core dumped" : "");
1492 if (WIFEXITED (status))
1493 return WEXITSTATUS (status);
1502 int ret = collect_wait (prog);
1505 error ("%s returned %d exit status", prog, ret);
1511 /* Fork and execute a program, and wait for the reply. */
1514 collect_execute (prog, argv, redir)
1527 fprintf (stderr, "%s", argv[0]);
1529 fprintf (stderr, "[cannot find %s]", prog);
1531 for (p_argv = &argv[1]; (str = *p_argv) != (char *)0; p_argv++)
1532 fprintf (stderr, " %s", str);
1534 fprintf (stderr, "\n");
1540 /* If we can't find a program we need, complain error. Do this here
1541 since we might not end up needing something that we couldn't find. */
1544 fatal ("cannot find `%s'", prog);
1550 fatal_perror ("fork");
1552 fatal_perror ("vfork");
1556 if (pid == 0) /* child context */
1561 if (freopen (redir, "a", stdout) == NULL)
1562 fatal_perror ("redirecting stdout");
1563 if (freopen (redir, "a", stderr) == NULL)
1564 fatal_perror ("redirecting stderr");
1567 execvp (argv[0], argv);
1568 fatal_perror ("executing %s", prog);
1573 fork_execute (prog, argv)
1577 collect_execute (prog, argv, NULL);
1581 /* Unlink a file unless we are debugging. */
1590 fprintf (stderr, "[Leaving %s]\n", file);
1594 /* Add a name to a linked list. */
1597 add_to_list (head_ptr, name)
1598 struct head *head_ptr;
1602 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1604 static long sequence_number = 0;
1605 strcpy (newid->name, name);
1607 if (head_ptr->first)
1608 head_ptr->last->next = newid;
1610 head_ptr->first = newid;
1612 /* Check for duplicate symbols. */
1613 for (p = head_ptr->first;
1614 strcmp (name, p->name) != 0;
1619 head_ptr->last->next = 0;
1624 newid->sequence = ++sequence_number;
1625 head_ptr->last = newid;
1629 /* Write: `prefix', the names on list LIST, `suffix'. */
1632 write_list (stream, prefix, list)
1639 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1645 write_list_with_asm (stream, prefix, list)
1652 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1653 prefix, list->sequence, list->name);
1658 /* Write out the constructor and destructor tables statically (for a shared
1659 object), along with the functions to execute them. */
1662 write_c_file_stat (stream, name)
1666 char *prefix, *p, *q;
1668 /* Figure out name of output_file, stripping off .so version. */
1669 p = rindex (output_file, '/');
1671 p = (char *) output_file;
1685 if (strncmp (q, ".so", 3) == 0)
1694 /* q points to null at end of the string (or . of the .so version) */
1695 prefix = xmalloc (q - p + 1);
1696 strncpy (prefix, p, q - p);
1698 for (q = prefix; *q; q++)
1702 fprintf (stderr, "\nwrite_c_file - output name is %s, prefix is %s\n",
1703 output_file, prefix);
1705 #define INIT_NAME_FORMAT "_GLOBAL__FI_%s"
1706 initname = xmalloc (strlen (prefix) + sizeof (INIT_NAME_FORMAT) - 2);
1707 sprintf (initname, INIT_NAME_FORMAT, prefix);
1709 #define FINI_NAME_FORMAT "_GLOBAL__FD_%s"
1710 fininame = xmalloc (strlen (prefix) + sizeof (FINI_NAME_FORMAT) - 2);
1711 sprintf (fininame, FINI_NAME_FORMAT, prefix);
1715 /* Write the tables as C code */
1717 fprintf (stream, "static int count;\n");
1718 fprintf (stream, "typedef void entry_pt();\n");
1719 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1720 fprintf (stream, "void %s() {\n", initname);
1721 if (constructors.number > 0)
1723 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1724 write_list (stream, "\t\t", constructors.first);
1725 fprintf (stream, "\t};\n");
1726 fprintf (stream, "\tentry_pt **p;\n");
1727 fprintf (stream, "\tif (count++ != 0) return;\n");
1728 fprintf (stream, "\tp = ctors + %d;\n", constructors.number);
1729 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1732 fprintf (stream, "\t++count;\n");
1733 fprintf (stream, "}\n");
1734 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1735 fprintf (stream, "void %s() {\n", fininame);
1736 if (destructors.number > 0)
1738 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1739 write_list (stream, "\t\t", destructors.first);
1740 fprintf (stream, "\t};\n");
1741 fprintf (stream, "\tentry_pt **p;\n");
1742 fprintf (stream, "\tif (--count != 0) return;\n");
1743 fprintf (stream, "\tp = dtors;\n");
1744 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1745 destructors.number);
1747 fprintf (stream, "}\n");
1751 fprintf (stream, "void _GLOBAL__DI() {\n\t%s();\n}\n", initname);
1752 fprintf (stream, "void _GLOBAL__DD() {\n\t%s();\n}\n", fininame);
1756 /* Write the constructor/destructor tables. */
1759 write_c_file_glob (stream, name)
1763 /* Write the tables as C code */
1765 fprintf (stream, "typedef void entry_pt();\n\n");
1767 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1769 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1770 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number);
1771 write_list (stream, "\t", constructors.first);
1772 fprintf (stream, "\t0\n};\n\n");
1774 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1776 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1777 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number);
1778 write_list (stream, "\t", destructors.first);
1779 fprintf (stream, "\t0\n};\n\n");
1781 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1782 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1786 write_c_file (stream, name)
1790 #ifndef LD_INIT_SWITCH
1792 write_c_file_glob (stream, name);
1795 write_c_file_stat (stream, name);
1799 write_export_file (stream)
1802 struct id *list = exports.first;
1803 for (; list; list = list->next)
1804 fprintf (stream, "%s\n", list->name);
1807 #ifdef OBJECT_FORMAT_NONE
1809 /* Generic version to scan the name list of the loaded program for
1810 the symbols g++ uses for static constructors and destructors.
1812 The constructor table begins at __CTOR_LIST__ and contains a count
1813 of the number of pointers (or -1 if the constructors are built in a
1814 separate section by the linker), followed by the pointers to the
1815 constructor functions, terminated with a null pointer. The
1816 destructor table has the same format, and begins at __DTOR_LIST__. */
1819 scan_prog_file (prog_name, which_pass)
1821 enum pass which_pass;
1823 void (*int_handler) ();
1824 void (*quit_handler) ();
1832 if (which_pass == PASS_SECOND)
1835 /* If we don't have an `nm', complain. */
1836 if (nm_file_name == 0)
1837 fatal ("cannot find `nm'");
1839 nm_argv[argc++] = nm_file_name;
1840 if (NM_FLAGS[0] != '\0')
1841 nm_argv[argc++] = NM_FLAGS;
1843 nm_argv[argc++] = prog_name;
1844 nm_argv[argc++] = (char *)0;
1846 if (pipe (pipe_fd) < 0)
1847 fatal_perror ("pipe");
1849 inf = fdopen (pipe_fd[0], "r");
1850 if (inf == (FILE *)0)
1851 fatal_perror ("fdopen");
1853 /* Trace if needed. */
1859 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *)0; p_argv++)
1860 fprintf (stderr, " %s", str);
1862 fprintf (stderr, "\n");
1868 /* Spawn child nm on pipe */
1873 fatal_perror ("fork");
1875 fatal_perror ("vfork");
1879 if (pid == 0) /* child context */
1882 if (dup2 (pipe_fd[1], 1) < 0)
1883 fatal_perror ("dup2 (%d, 1)", pipe_fd[1]);
1885 if (close (pipe_fd[0]) < 0)
1886 fatal_perror ("close (%d)", pipe_fd[0]);
1888 if (close (pipe_fd[1]) < 0)
1889 fatal_perror ("close (%d)", pipe_fd[1]);
1891 execv (nm_file_name, nm_argv);
1892 fatal_perror ("executing %s", nm_file_name);
1895 /* Parent context from here on. */
1896 int_handler = (void (*) ())signal (SIGINT, SIG_IGN);
1898 quit_handler = (void (*) ())signal (SIGQUIT, SIG_IGN);
1901 if (close (pipe_fd[1]) < 0)
1902 fatal_perror ("close (%d)", pipe_fd[1]);
1905 fprintf (stderr, "\nnm output with constructors/destructors.\n");
1907 /* Read each line of nm output. */
1908 while (fgets (buf, sizeof buf, inf) != (char *)0)
1913 /* If it contains a constructor or destructor name, add the name
1914 to the appropriate list. */
1916 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
1917 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
1924 /* Find the end of the symbol name.
1925 Don't include `|', because Encore nm can tack that on the end. */
1926 for (end = p; (ch2 = *end) != '\0' && !isspace (ch2) && ch2 != '|';
1932 switch (is_ctor_dtor (name))
1935 if (which_pass != PASS_LIB)
1936 add_to_list (&constructors, name);
1940 if (which_pass != PASS_LIB)
1941 add_to_list (&destructors, name);
1945 if (which_pass != PASS_LIB)
1946 fatal ("init function found in object %s", prog_name);
1947 #ifndef LD_INIT_SWITCH
1948 add_to_list (&constructors, name);
1953 if (which_pass != PASS_LIB)
1954 fatal ("fini function found in object %s", prog_name);
1955 #ifndef LD_FINI_SWITCH
1956 add_to_list (&destructors, name);
1960 default: /* not a constructor or destructor */
1965 fprintf (stderr, "\t%s\n", buf);
1969 fprintf (stderr, "\n");
1971 if (fclose (inf) != 0)
1972 fatal_perror ("fclose of pipe");
1974 do_wait (nm_file_name);
1976 signal (SIGINT, int_handler);
1978 signal (SIGQUIT, quit_handler);
1982 #if SUNOS4_SHARED_LIBRARIES
1984 /* Routines to scan the SunOS 4 _DYNAMIC structure to find shared libraries
1985 that the output file depends upon and their initialization/finalization
1986 routines, if any. */
1991 #include <sys/mman.h>
1992 #include <sys/param.h>
1993 #include <sys/unistd.h>
1994 #include <sys/dir.h>
1996 /* pointers to the object file */
1997 unsigned object; /* address of memory mapped file */
1998 unsigned objsize; /* size of memory mapped to file */
1999 char * code; /* pointer to code segment */
2000 char * data; /* pointer to data segment */
2001 struct nlist *symtab; /* pointer to symbol table */
2002 struct link_dynamic *ld;
2003 struct link_dynamic_2 *ld_2;
2004 struct head libraries;
2006 /* Map the file indicated by NAME into memory and store its address. */
2014 if ((fp = open (name, O_RDONLY)) == -1)
2015 fatal ("unable to open file '%s'", name);
2016 if (fstat (fp, &s) == -1)
2017 fatal ("unable to stat file '%s'", name);
2019 objsize = s.st_size;
2020 object = (unsigned) mmap (0, objsize, PROT_READ|PROT_WRITE, MAP_PRIVATE,
2023 fatal ("unable to mmap file '%s'", name);
2028 /* Helpers for locatelib. */
2030 static char *libname;
2036 return (strncmp (libname, d->d_name, strlen (libname)) == 0);
2039 /* If one file has an additional numeric extension past LIBNAME, then put
2040 that one first in the sort. If both files have additional numeric
2041 extensions, then put the one with the higher number first in the sort.
2043 We must verify that the extension is numeric, because Sun saves the
2044 original versions of patched libraries with a .FCS extension. Files with
2045 invalid extensions must go last in the sort, so that they won't be used. */
2049 struct direct **d1, **d2;
2051 int i1, i2 = strlen (libname);
2052 char *e1 = (*d1)->d_name + i2;
2053 char *e2 = (*d2)->d_name + i2;
2055 while (*e1 && *e2 && *e1 == '.' && *e2 == '.'
2056 && e1[1] && isdigit (e1[1]) && e2[1] && isdigit (e2[1]))
2060 i1 = strtol (e1, &e1, 10);
2061 i2 = strtol (e2, &e2, 10);
2068 /* It has a valid numeric extension, prefer this one. */
2069 if (*e1 == '.' && e1[1] && isdigit (e1[1]))
2071 /* It has a invalid numeric extension, must prefer the other one. */
2077 /* It has a valid numeric extension, prefer this one. */
2078 if (*e2 == '.' && e2[1] && isdigit (e2[1]))
2080 /* It has a invalid numeric extension, must prefer the other one. */
2088 /* Given the name NAME of a dynamic dependency, find its pathname and add
2089 it to the list of libraries. */
2097 char buf[MAXPATHLEN];
2105 /* counting elements in array, need 1 extra for null */
2107 ld_rules = (char *) (ld_2->ld_rules + code);
2111 for (; *ld_rules != 0; ld_rules++)
2112 if (*ld_rules == ':')
2114 ld_rules = (char *) (ld_2->ld_rules + code);
2115 ldr = (char *) malloc (strlen (ld_rules) + 1);
2116 strcpy (ldr, ld_rules);
2118 p = getenv ("LD_LIBRARY_PATH");
2123 for (q = p ; *q != 0; q++)
2126 q = (char *) malloc (strlen (p) + 1);
2129 l = (char **) malloc ((cnt + 3) * sizeof (char *));
2134 for (; *ldr != 0; ldr++)
2144 for (; *q != 0; q++)
2151 /* built in directories are /lib, /usr/lib, and /usr/local/lib */
2154 *pp++ = "/usr/local/lib";
2158 for (pp = l; *pp != 0 ; pp++)
2160 struct direct **namelist;
2162 if ((entries = scandir (*pp, &namelist, libselect, libcompare)) > 0)
2164 sprintf (buf, "%s/%s", *pp, namelist[entries - 1]->d_name);
2165 add_to_list (&libraries, buf);
2167 fprintf (stderr, "%s\n", buf);
2174 fprintf (stderr, "not found\n");
2176 fatal ("dynamic dependency %s not found", name);
2180 /* Scan the _DYNAMIC structure of the output file to find shared libraries
2181 that it depends upon and any constructors or destructors they contain. */
2184 scan_libraries (prog_name)
2187 struct exec *header;
2189 struct link_object *lo;
2190 char buff[MAXPATHLEN];
2193 mapfile (prog_name);
2194 header = (struct exec *)object;
2195 if (N_BADMAG (*header))
2196 fatal ("bad magic number in file '%s'", prog_name);
2197 if (header->a_dynamic == 0)
2200 code = (char *) (N_TXTOFF (*header) + (long) header);
2201 data = (char *) (N_DATOFF (*header) + (long) header);
2202 symtab = (struct nlist *) (N_SYMOFF (*header) + (long) header);
2204 if (header->a_magic == ZMAGIC && header->a_entry == 0x20)
2207 ld = (struct link_dynamic *) (symtab->n_value + code);
2213 ld = (struct link_dynamic *) data;
2218 fprintf (stderr, "dynamic dependencies.\n");
2220 ld_2 = (struct link_dynamic_2 *) ((long) ld->ld_un.ld_2 + (long)base);
2221 for (lo = (struct link_object *) ld_2->ld_need; lo;
2222 lo = (struct link_object *) lo->lo_next)
2225 lo = (struct link_object *) ((long) lo + code);
2226 name = (char *) (code + lo->lo_name);
2230 fprintf (stderr, "\t-l%s.%d => ", name, lo->lo_major);
2231 sprintf (buff, "lib%s.so.%d.%d", name, lo->lo_major, lo->lo_minor);
2237 fprintf (stderr, "\t%s\n", name);
2238 add_to_list (&libraries, name);
2243 fprintf (stderr, "\n");
2245 /* now iterate through the library list adding their symbols to
2247 for (list = libraries.first; list; list = list->next)
2248 scan_prog_file (list->name, PASS_LIB);
2251 #else /* SUNOS4_SHARED_LIBRARIES */
2254 /* Use the List Dynamic Dependencies program to find shared libraries that
2255 the output file depends upon and their initialization/finalization
2256 routines, if any. */
2259 scan_libraries (prog_name)
2262 static struct head libraries; /* list of shared libraries found */
2264 void (*int_handler) ();
2265 void (*quit_handler) ();
2273 /* If we don't have an `ldd', complain. */
2274 if (ldd_file_name == 0)
2276 error ("cannot find `ldd'");
2280 ldd_argv[argc++] = ldd_file_name;
2281 ldd_argv[argc++] = prog_name;
2282 ldd_argv[argc++] = (char *) 0;
2284 if (pipe (pipe_fd) < 0)
2285 fatal_perror ("pipe");
2287 inf = fdopen (pipe_fd[0], "r");
2288 if (inf == (FILE *) 0)
2289 fatal_perror ("fdopen");
2291 /* Trace if needed. */
2297 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2298 fprintf (stderr, " %s", str);
2300 fprintf (stderr, "\n");
2306 /* Spawn child ldd on pipe */
2311 fatal_perror ("fork");
2313 fatal_perror ("vfork");
2317 if (pid == 0) /* child context */
2320 if (dup2 (pipe_fd[1], 1) < 0)
2321 fatal_perror ("dup2 (%d, 1)", pipe_fd[1]);
2323 if (close (pipe_fd[0]) < 0)
2324 fatal_perror ("close (%d)", pipe_fd[0]);
2326 if (close (pipe_fd[1]) < 0)
2327 fatal_perror ("close (%d)", pipe_fd[1]);
2329 execv (ldd_file_name, ldd_argv);
2330 fatal_perror ("executing %s", ldd_file_name);
2333 /* Parent context from here on. */
2334 int_handler = (void (*) ()) signal (SIGINT, SIG_IGN);
2336 quit_handler = (void (*) ()) signal (SIGQUIT, SIG_IGN);
2339 if (close (pipe_fd[1]) < 0)
2340 fatal_perror ("close (%d)", pipe_fd[1]);
2343 fprintf (stderr, "\nldd output with constructors/destructors.\n");
2345 /* Read each line of ldd output. */
2346 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2349 char *name, *end, *p = buf;
2351 /* Extract names of libraries and add to list. */
2352 PARSE_LDD_OUTPUT (p);
2357 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2358 fatal ("dynamic dependency %s not found", buf);
2360 /* Find the end of the symbol name. */
2362 (ch2 = *end) != '\0' && ch2 != '\n' && !isspace (ch2) && ch2 != '|';
2367 if (access (name, R_OK) == 0)
2368 add_to_list (&libraries, name);
2370 fatal ("unable to open dynamic dependency '%s'", buf);
2373 fprintf (stderr, "\t%s\n", buf);
2376 fprintf (stderr, "\n");
2378 if (fclose (inf) != 0)
2379 fatal_perror ("fclose of pipe");
2381 do_wait (ldd_file_name);
2383 signal (SIGINT, int_handler);
2385 signal (SIGQUIT, quit_handler);
2388 /* now iterate through the library list adding their symbols to
2390 for (list = libraries.first; list; list = list->next)
2391 scan_prog_file (list->name, PASS_LIB);
2394 #endif /* LDD_SUFFIX */
2395 #endif /* SUNOS4_SHARED_LIBRARIES */
2397 #endif /* OBJECT_FORMAT_NONE */
2401 * COFF specific stuff.
2404 #ifdef OBJECT_FORMAT_COFF
2406 #if defined(EXTENDED_COFF)
2407 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2408 # define GCC_SYMENT SYMR
2409 # define GCC_OK_SYMBOL(X) ((X).st == stProc && (X).sc == scText)
2410 # define GCC_SYMINC(X) (1)
2411 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2412 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2414 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2415 # define GCC_SYMENT SYMENT
2416 # define GCC_OK_SYMBOL(X) \
2417 (((X).n_sclass == C_EXT) && \
2418 (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) || \
2419 ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT)))
2420 # define GCC_SYMINC(X) ((X).n_numaux+1)
2421 # define GCC_SYMZERO(X) 0
2422 # define GCC_CHECK_HDR(X) (1)
2425 extern char *ldgetname ();
2427 /* COFF version to scan the name list of the loaded program for
2428 the symbols g++ uses for static constructors and destructors.
2430 The constructor table begins at __CTOR_LIST__ and contains a count
2431 of the number of pointers (or -1 if the constructors are built in a
2432 separate section by the linker), followed by the pointers to the
2433 constructor functions, terminated with a null pointer. The
2434 destructor table has the same format, and begins at __DTOR_LIST__. */
2437 scan_prog_file (prog_name, which_pass)
2439 enum pass which_pass;
2441 LDFILE *ldptr = NULL;
2442 int sym_index, sym_count;
2444 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2447 if ((ldptr = ldopen (prog_name, ldptr)) == NULL)
2448 fatal ("%s: can't open as COFF file", prog_name);
2450 if (!MY_ISCOFF (HEADER (ldptr).f_magic))
2451 fatal ("%s: not a COFF file", prog_name);
2453 if (GCC_CHECK_HDR (ldptr))
2455 sym_count = GCC_SYMBOLS (ldptr);
2456 sym_index = GCC_SYMZERO (ldptr);
2457 while (sym_index < sym_count)
2461 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2463 sym_index += GCC_SYMINC (symbol);
2465 if (GCC_OK_SYMBOL (symbol))
2469 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2470 continue; /* should never happen */
2472 #ifdef XCOFF_DEBUGGING_INFO
2473 /* All AIX function names have a duplicate entry beginning
2479 switch (is_ctor_dtor (name))
2482 add_to_list (&constructors, name);
2483 if (which_pass == PASS_OBJ)
2484 add_to_list (&exports, name);
2488 add_to_list (&destructors, name);
2489 if (which_pass == PASS_OBJ)
2490 add_to_list (&exports, name);
2493 default: /* not a constructor or destructor */
2497 #if !defined(EXTENDED_COFF)
2499 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2500 symbol.n_scnum, symbol.n_sclass,
2501 (symbol.n_type ? "0" : ""), symbol.n_type,
2505 fprintf (stderr, "\tiss = %5d, value = %5d, index = %5d, name = %s\n",
2506 symbol.iss, symbol.value, symbol.index, name);
2512 (void) ldclose(ldptr);
2515 #ifdef XCOFF_SCAN_LIBS
2516 /* Scan imported AIX libraries for GCC static ctors and dtors.
2517 FIXME: it is possible to link an executable without the actual import
2518 library by using an "import file" - a text file listing symbols
2519 exported by a library. To support this, we would have to scan
2520 import files as well as actual shared binaries to find GCC ctors.
2521 TODO: use memory mapping instead of 'ld' routines, files are already
2522 memory mapped, but we could eliminate the extra in-memory copies.
2523 Is it worth the effort? */
2526 scan_libraries (prog_name)
2531 static struct path_prefix libpath; /* we should only do this once */
2533 if ((ldptr = ldopen (prog_name, ldptr)) == NULL)
2534 fatal ("%s: can't open as COFF file", prog_name);
2536 if (!MY_ISCOFF (HEADER (ldptr).f_magic))
2537 fatal ("%s: not a COFF file", prog_name);
2539 /* find and read loader section */
2540 if (ldnshread (ldptr, _LOADER, &ldsh))
2546 FSEEK (ldptr, ldsh.s_scnptr, BEGINNING);
2547 FREAD (&ldh, sizeof (ldh), 1, ldptr);
2548 /* read import library list */
2549 impbuf = alloca (ldh.l_istlen);
2550 FSEEK (ldptr, ldh.l_impoff + ldsh.s_scnptr, BEGINNING);
2551 FREAD (impbuf, ldh.l_istlen, 1, ldptr);
2554 fprintf (stderr, "LIBPATH=%s\n", impbuf);
2555 prefix_from_string (impbuf, &libpath);
2557 /* skip LIBPATH and empty base and member fields */
2558 impbuf += strlen (impbuf) + 3;
2559 for (entry = 1; entry < ldh.l_nimpid; ++entry)
2561 char *impath = impbuf;
2562 char *implib = impath + strlen (impath) + 1;
2563 char *impmem = implib + strlen (implib) + 1;
2564 char *soname = NULL;
2567 LDFILE *libptr = NULL;
2568 struct prefix_list *pl;
2571 impbuf = impmem + strlen (impmem) + 1;
2573 fprintf (stderr, "PATH+BASE=%s%s\n", impath, implib);
2574 /* Skip AIX kernel exports */
2575 if (*impath == '/' && *(impath+1) == '\0'
2576 && strcmp (implib, "unix") == 0)
2578 pathlen = strlen (impath);
2579 trial = alloca (MAX (pathlen + 1, libpath.max_len)
2580 + strlen (implib) + 1);
2583 strcpy (trial, impath);
2584 if (impath[pathlen - 1] != '/')
2585 trial[pathlen++] = '/';
2586 strcpy (trial + pathlen, implib);
2587 if (access (trial, R_OK) == 0)
2591 for (pl = libpath.plist; pl; pl = pl->next)
2593 strcpy (trial, pl->prefix);
2594 strcat (trial, implib);
2595 if (access (trial, R_OK) == 0)
2603 fatal ("%s: library not found", implib);
2606 fprintf (stderr, "%s (%s)\n", soname, impmem);
2608 fprintf (stderr, "%s\n", soname);
2612 /* scan imported shared objects for GCC GLOBAL ctors */
2614 if ((libptr = ldopen (soname, libptr)) == NULL)
2615 fatal ("%s: can't open import library", soname);
2616 if (TYPE (libptr) == ARTYPE)
2620 fatal ("%s: no archive member specified", soname);
2621 ldahread (libptr, &ah);
2622 if (strcmp (ah.ar_name, impmem))
2625 type = HEADER (libptr).f_magic;
2626 if (HEADER (libptr).f_flags & F_SHROBJ)
2633 if (!ldnshread (libptr, _LOADER, &soldsh))
2634 fatal ("%s: not an import library", soname);
2635 FSEEK (libptr, soldsh.s_scnptr, BEGINNING);
2636 if (FREAD (&soldh, sizeof (soldh), 1, libptr) != 1)
2637 fatal ("%s: can't read loader section", soname);
2638 /*fprintf (stderr, "\tscanning %s\n", soname);*/
2639 symcnt = soldh.l_nsyms;
2640 lsyms = (LDSYM*) alloca (symcnt * sizeof (*lsyms));
2641 symcnt = FREAD (lsyms, sizeof (*lsyms), symcnt, libptr);
2642 ldstrings = alloca (soldh.l_stlen);
2643 FSEEK (libptr, soldsh.s_scnptr+soldh.l_stoff, BEGINNING);
2644 FREAD (ldstrings, soldh.l_stlen, 1, libptr);
2645 for (i = 0; i < symcnt; ++i)
2647 LDSYM *l = lsyms + i;
2648 if (LDR_EXPORT (*l))
2652 expname = l->l_name;
2653 else if (l->l_offset < soldh.l_stlen)
2654 expname = ldstrings + l->l_offset;
2655 switch (is_ctor_dtor (expname))
2659 fprintf (stderr, "\t%s\n", expname);
2660 add_to_list (&constructors, expname);
2664 add_to_list (&destructors, expname);
2667 default: /* not a constructor or destructor */
2674 fprintf (stderr, "%s: type = %04X flags = %04X\n",
2675 ah.ar_name, type, HEADER (libptr).f_flags);
2677 while (ldclose (libptr) == FAILURE);
2678 /* printf (stderr, "closed %s\n", soname); */
2682 #endif /* XCOFF_SCAN_LIBS */
2684 #endif /* OBJECT_FORMAT_COFF */
2688 * OSF/rose specific stuff.
2691 #ifdef OBJECT_FORMAT_ROSE
2693 /* Union of the various load commands */
2695 typedef union load_union
2697 ldc_header_t hdr; /* common header */
2698 load_cmd_map_command_t map; /* map indexing other load cmds */
2699 interpreter_command_t iprtr; /* interpreter pathname */
2700 strings_command_t str; /* load commands strings section */
2701 region_command_t region; /* region load command */
2702 reloc_command_t reloc; /* relocation section */
2703 package_command_t pkg; /* package load command */
2704 symbols_command_t sym; /* symbol sections */
2705 entry_command_t ent; /* program start section */
2706 gen_info_command_t info; /* object information */
2707 func_table_command_t func; /* function constructors/destructors */
2710 /* Structure to point to load command and data section in memory. */
2712 typedef struct load_all
2714 load_union_t *load; /* load command */
2715 char *section; /* pointer to section */
2718 /* Structure to contain information about a file mapped into memory. */
2722 char *start; /* start of map */
2723 char *name; /* filename */
2724 long size; /* size of the file */
2725 long rounded_size; /* size rounded to page boundary */
2726 int fd; /* file descriptor */
2727 int rw; /* != 0 if opened read/write */
2728 int use_mmap; /* != 0 if mmap'ed */
2731 extern int decode_mach_o_hdr ();
2732 extern int encode_mach_o_hdr ();
2734 static void add_func_table PROTO((mo_header_t *, load_all_t *,
2735 symbol_info_t *, int));
2736 static void print_header PROTO((mo_header_t *));
2737 static void print_load_command PROTO((load_union_t*, size_t, int));
2738 static void bad_header PROTO((int));
2739 static struct file_info *read_file PROTO((char *, int, int));
2740 static void end_file PROTO((struct file_info *));
2742 /* OSF/rose specific version to scan the name list of the loaded
2743 program for the symbols g++ uses for static constructors and
2746 The constructor table begins at __CTOR_LIST__ and contains a count
2747 of the number of pointers (or -1 if the constructors are built in a
2748 separate section by the linker), followed by the pointers to the
2749 constructor functions, terminated with a null pointer. The
2750 destructor table has the same format, and begins at __DTOR_LIST__. */
2753 scan_prog_file (prog_name, which_pass)
2755 enum pass which_pass;
2759 load_all_t *load_array;
2760 load_all_t *load_end;
2761 load_all_t *load_cmd;
2762 int symbol_load_cmds;
2768 struct file_info *obj_file;
2770 mo_lcid_t cmd_strings = -1;
2771 symbol_info_t *main_sym = 0;
2772 int rw = (which_pass != PASS_FIRST);
2774 prog_fd = open (prog_name, (rw) ? O_RDWR : O_RDONLY);
2776 fatal_perror ("can't read %s", prog_name);
2778 obj_file = read_file (prog_name, prog_fd, rw);
2779 obj = obj_file->start;
2781 status = decode_mach_o_hdr (obj, MO_SIZEOF_RAW_HDR, MOH_HEADER_VERSION, &hdr);
2782 if (status != MO_HDR_CONV_SUCCESS)
2783 bad_header (status);
2786 /* Do some basic sanity checks. Note we explicitly use the big endian magic number,
2787 since the hardware will automatically swap bytes for us on loading little endian
2790 #ifndef CROSS_COMPILE
2791 if (hdr.moh_magic != MOH_MAGIC_MSB
2792 || hdr.moh_header_version != MOH_HEADER_VERSION
2793 || hdr.moh_byte_order != OUR_BYTE_ORDER
2794 || hdr.moh_data_rep_id != OUR_DATA_REP_ID
2795 || hdr.moh_cpu_type != OUR_CPU_TYPE
2796 || hdr.moh_cpu_subtype != OUR_CPU_SUBTYPE
2797 || hdr.moh_vendor_type != OUR_VENDOR_TYPE)
2799 fatal ("incompatibilities between object file & expected values");
2804 print_header (&hdr);
2806 offset = hdr.moh_first_cmd_off;
2807 load_end = load_array
2808 = (load_all_t *) xcalloc (sizeof (load_all_t), hdr.moh_n_load_cmds + 2);
2810 /* Build array of load commands, calculating the offsets */
2811 for (i = 0; i < hdr.moh_n_load_cmds; i++)
2813 load_union_t *load_hdr; /* load command header */
2815 load_cmd = load_end++;
2816 load_hdr = (load_union_t *) (obj + offset);
2818 /* If modifying the program file, copy the header. */
2821 load_union_t *ptr = (load_union_t *) xmalloc (load_hdr->hdr.ldci_cmd_size);
2822 bcopy ((char *)load_hdr, (char *)ptr, load_hdr->hdr.ldci_cmd_size);
2825 /* null out old command map, because we will rewrite at the end. */
2826 if (ptr->hdr.ldci_cmd_type == LDC_CMD_MAP)
2828 cmd_strings = ptr->map.lcm_ld_cmd_strings;
2829 ptr->hdr.ldci_cmd_type = LDC_UNDEFINED;
2833 load_cmd->load = load_hdr;
2834 if (load_hdr->hdr.ldci_section_off > 0)
2835 load_cmd->section = obj + load_hdr->hdr.ldci_section_off;
2838 print_load_command (load_hdr, offset, i);
2840 offset += load_hdr->hdr.ldci_cmd_size;
2843 /* If the last command is the load command map and is not undefined,
2844 decrement the count of load commands. */
2845 if (rw && load_end[-1].load->hdr.ldci_cmd_type == LDC_UNDEFINED)
2848 hdr.moh_n_load_cmds--;
2851 /* Go through and process each symbol table section. */
2852 symbol_load_cmds = 0;
2853 for (load_cmd = load_array; load_cmd < load_end; load_cmd++)
2855 load_union_t *load_hdr = load_cmd->load;
2857 if (load_hdr->hdr.ldci_cmd_type == LDC_SYMBOLS)
2863 char *kind = "unknown";
2865 switch (load_hdr->sym.symc_kind)
2867 case SYMC_IMPORTS: kind = "imports"; break;
2868 case SYMC_DEFINED_SYMBOLS: kind = "defined"; break;
2869 case SYMC_STABS: kind = "stabs"; break;
2872 fprintf (stderr, "\nProcessing symbol table #%d, offset = 0x%.8lx, kind = %s\n",
2873 symbol_load_cmds, load_hdr->hdr.ldci_section_off, kind);
2876 if (load_hdr->sym.symc_kind != SYMC_DEFINED_SYMBOLS)
2879 str_sect = load_array[load_hdr->sym.symc_strings_section].section;
2880 if (str_sect == (char *)0)
2881 fatal ("string section missing");
2883 if (load_cmd->section == (char *)0)
2884 fatal ("section pointer missing");
2886 num_syms = load_hdr->sym.symc_nentries;
2887 for (i = 0; i < num_syms; i++)
2889 symbol_info_t *sym = ((symbol_info_t *) load_cmd->section) + i;
2890 char *name = sym->si_name.symbol_name + str_sect;
2897 char *n = name + strlen (name) - strlen (NAME__MAIN);
2899 if ((n - name) < 0 || strcmp (n, NAME__MAIN))
2909 switch (is_ctor_dtor (name))
2912 add_to_list (&constructors, name);
2916 add_to_list (&destructors, name);
2919 default: /* not a constructor or destructor */
2925 fprintf (stderr, "\ttype = 0x%.4x, sc = 0x%.2x, flags = 0x%.8x, name = %.30s\n",
2926 sym->si_type, sym->si_sc_type, sym->si_flags, name);
2931 if (symbol_load_cmds == 0)
2932 fatal ("no symbol table found");
2934 /* Update the program file now, rewrite header and load commands. At present,
2935 we assume that there is enough space after the last load command to insert
2936 one more. Since the first section written out is page aligned, and the
2937 number of load commands is small, this is ok for the present. */
2941 load_union_t *load_map;
2944 if (cmd_strings == -1)
2945 fatal ("no cmd_strings found");
2947 /* Add __main to initializer list.
2948 If we are building a program instead of a shared library, don't
2949 do anything, since in the current version, you cannot do mallocs
2950 and such in the constructors. */
2952 if (main_sym != (symbol_info_t *)0
2953 && ((hdr.moh_flags & MOH_EXECABLE_F) == 0))
2954 add_func_table (&hdr, load_array, main_sym, FNTC_INITIALIZATION);
2957 fprintf (stderr, "\nUpdating header and load commands.\n\n");
2959 hdr.moh_n_load_cmds++;
2960 size = sizeof (load_cmd_map_command_t) + (sizeof (mo_offset_t) * (hdr.moh_n_load_cmds - 1));
2962 /* Create new load command map. */
2964 fprintf (stderr, "load command map, %d cmds, new size %ld.\n",
2965 (int)hdr.moh_n_load_cmds, (long)size);
2967 load_map = (load_union_t *) xcalloc (1, size);
2968 load_map->map.ldc_header.ldci_cmd_type = LDC_CMD_MAP;
2969 load_map->map.ldc_header.ldci_cmd_size = size;
2970 load_map->map.lcm_ld_cmd_strings = cmd_strings;
2971 load_map->map.lcm_nentries = hdr.moh_n_load_cmds;
2972 load_array[hdr.moh_n_load_cmds-1].load = load_map;
2974 offset = hdr.moh_first_cmd_off;
2975 for (i = 0; i < hdr.moh_n_load_cmds; i++)
2977 load_map->map.lcm_map[i] = offset;
2978 if (load_array[i].load->hdr.ldci_cmd_type == LDC_CMD_MAP)
2979 hdr.moh_load_map_cmd_off = offset;
2981 offset += load_array[i].load->hdr.ldci_cmd_size;
2984 hdr.moh_sizeofcmds = offset - MO_SIZEOF_RAW_HDR;
2987 print_header (&hdr);
2990 status = encode_mach_o_hdr (&hdr, obj, MO_SIZEOF_RAW_HDR);
2991 if (status != MO_HDR_CONV_SUCCESS)
2992 bad_header (status);
2995 fprintf (stderr, "writing load commands.\n\n");
2997 /* Write load commands */
2998 offset = hdr.moh_first_cmd_off;
2999 for (i = 0; i < hdr.moh_n_load_cmds; i++)
3001 load_union_t *load_hdr = load_array[i].load;
3002 size_t size = load_hdr->hdr.ldci_cmd_size;
3005 print_load_command (load_hdr, offset, i);
3007 bcopy ((char *)load_hdr, (char *)(obj + offset), size);
3012 end_file (obj_file);
3014 if (close (prog_fd))
3015 fatal_perror ("closing %s", prog_name);
3018 fprintf (stderr, "\n");
3022 /* Add a function table to the load commands to call a function
3023 on initiation or termination of the process. */
3026 add_func_table (hdr_p, load_array, sym, type)
3027 mo_header_t *hdr_p; /* pointer to global header */
3028 load_all_t *load_array; /* array of ptrs to load cmds */
3029 symbol_info_t *sym; /* pointer to symbol entry */
3030 int type; /* fntc_type value */
3032 /* Add a new load command. */
3033 int num_cmds = ++hdr_p->moh_n_load_cmds;
3034 int load_index = num_cmds - 1;
3035 size_t size = sizeof (func_table_command_t) + sizeof (mo_addr_t);
3036 load_union_t *ptr = xcalloc (1, size);
3037 load_all_t *load_cmd;
3040 /* Set the unresolved address bit in the header to force the loader to be
3041 used, since kernel exec does not call the initialization functions. */
3042 hdr_p->moh_flags |= MOH_UNRESOLVED_F;
3044 load_cmd = &load_array[load_index];
3045 load_cmd->load = ptr;
3046 load_cmd->section = (char *)0;
3048 /* Fill in func table load command. */
3049 ptr->func.ldc_header.ldci_cmd_type = LDC_FUNC_TABLE;
3050 ptr->func.ldc_header.ldci_cmd_size = size;
3051 ptr->func.ldc_header.ldci_section_off = 0;
3052 ptr->func.ldc_header.ldci_section_len = 0;
3053 ptr->func.fntc_type = type;
3054 ptr->func.fntc_nentries = 1;
3056 /* copy address, turn it from abs. address to (region,offset) if necessary. */
3057 /* Is the symbol already expressed as (region, offset)? */
3058 if ((sym->si_flags & SI_ABSOLUTE_VALUE_F) == 0)
3060 ptr->func.fntc_entry_loc[i].adr_lcid = sym->si_value.def_val.adr_lcid;
3061 ptr->func.fntc_entry_loc[i].adr_sctoff = sym->si_value.def_val.adr_sctoff;
3064 /* If not, figure out which region it's in. */
3067 mo_vm_addr_t addr = sym->si_value.abs_val;
3070 for (i = 0; i < load_index; i++)
3072 if (load_array[i].load->hdr.ldci_cmd_type == LDC_REGION)
3074 region_command_t *region_ptr = &load_array[i].load->region;
3076 if ((region_ptr->regc_flags & REG_ABS_ADDR_F) != 0
3077 && addr >= region_ptr->regc_addr.vm_addr
3078 && addr <= region_ptr->regc_addr.vm_addr + region_ptr->regc_vm_size)
3080 ptr->func.fntc_entry_loc[0].adr_lcid = i;
3081 ptr->func.fntc_entry_loc[0].adr_sctoff = addr - region_ptr->regc_addr.vm_addr;
3089 fatal ("could not convert 0x%l.8x into a region", addr);
3094 "%s function, region %d, offset = %ld (0x%.8lx)\n",
3095 (type == FNTC_INITIALIZATION) ? "init" : "term",
3096 (int)ptr->func.fntc_entry_loc[i].adr_lcid,
3097 (long)ptr->func.fntc_entry_loc[i].adr_sctoff,
3098 (long)ptr->func.fntc_entry_loc[i].adr_sctoff);
3103 /* Print the global header for an OSF/rose object. */
3106 print_header (hdr_ptr)
3107 mo_header_t *hdr_ptr;
3109 fprintf (stderr, "\nglobal header:\n");
3110 fprintf (stderr, "\tmoh_magic = 0x%.8lx\n", hdr_ptr->moh_magic);
3111 fprintf (stderr, "\tmoh_major_version = %d\n", (int)hdr_ptr->moh_major_version);
3112 fprintf (stderr, "\tmoh_minor_version = %d\n", (int)hdr_ptr->moh_minor_version);
3113 fprintf (stderr, "\tmoh_header_version = %d\n", (int)hdr_ptr->moh_header_version);
3114 fprintf (stderr, "\tmoh_max_page_size = %d\n", (int)hdr_ptr->moh_max_page_size);
3115 fprintf (stderr, "\tmoh_byte_order = %d\n", (int)hdr_ptr->moh_byte_order);
3116 fprintf (stderr, "\tmoh_data_rep_id = %d\n", (int)hdr_ptr->moh_data_rep_id);
3117 fprintf (stderr, "\tmoh_cpu_type = %d\n", (int)hdr_ptr->moh_cpu_type);
3118 fprintf (stderr, "\tmoh_cpu_subtype = %d\n", (int)hdr_ptr->moh_cpu_subtype);
3119 fprintf (stderr, "\tmoh_vendor_type = %d\n", (int)hdr_ptr->moh_vendor_type);
3120 fprintf (stderr, "\tmoh_load_map_cmd_off = %d\n", (int)hdr_ptr->moh_load_map_cmd_off);
3121 fprintf (stderr, "\tmoh_first_cmd_off = %d\n", (int)hdr_ptr->moh_first_cmd_off);
3122 fprintf (stderr, "\tmoh_sizeofcmds = %d\n", (int)hdr_ptr->moh_sizeofcmds);
3123 fprintf (stderr, "\tmon_n_load_cmds = %d\n", (int)hdr_ptr->moh_n_load_cmds);
3124 fprintf (stderr, "\tmoh_flags = 0x%.8lx", (long)hdr_ptr->moh_flags);
3126 if (hdr_ptr->moh_flags & MOH_RELOCATABLE_F)
3127 fprintf (stderr, ", relocatable");
3129 if (hdr_ptr->moh_flags & MOH_LINKABLE_F)
3130 fprintf (stderr, ", linkable");
3132 if (hdr_ptr->moh_flags & MOH_EXECABLE_F)
3133 fprintf (stderr, ", execable");
3135 if (hdr_ptr->moh_flags & MOH_EXECUTABLE_F)
3136 fprintf (stderr, ", executable");
3138 if (hdr_ptr->moh_flags & MOH_UNRESOLVED_F)
3139 fprintf (stderr, ", unresolved");
3141 fprintf (stderr, "\n\n");
3146 /* Print a short summary of a load command. */
3149 print_load_command (load_hdr, offset, number)
3150 load_union_t *load_hdr;
3154 mo_long_t type = load_hdr->hdr.ldci_cmd_type;
3155 char *type_str = (char *)0;
3159 case LDC_UNDEFINED: type_str = "UNDEFINED"; break;
3160 case LDC_CMD_MAP: type_str = "CMD_MAP"; break;
3161 case LDC_INTERPRETER: type_str = "INTERPRETER"; break;
3162 case LDC_STRINGS: type_str = "STRINGS"; break;
3163 case LDC_REGION: type_str = "REGION"; break;
3164 case LDC_RELOC: type_str = "RELOC"; break;
3165 case LDC_PACKAGE: type_str = "PACKAGE"; break;
3166 case LDC_SYMBOLS: type_str = "SYMBOLS"; break;
3167 case LDC_ENTRY: type_str = "ENTRY"; break;
3168 case LDC_FUNC_TABLE: type_str = "FUNC_TABLE"; break;
3169 case LDC_GEN_INFO: type_str = "GEN_INFO"; break;
3173 "cmd %2d, sz: 0x%.2lx, coff: 0x%.3lx, doff: 0x%.6lx, dlen: 0x%.6lx",
3175 (long) load_hdr->hdr.ldci_cmd_size,
3177 (long) load_hdr->hdr.ldci_section_off,
3178 (long) load_hdr->hdr.ldci_section_len);
3180 if (type_str == (char *)0)
3181 fprintf (stderr, ", ty: unknown (%ld)\n", (long) type);
3183 else if (type != LDC_REGION)
3184 fprintf (stderr, ", ty: %s\n", type_str);
3189 switch (load_hdr->region.regc_usage_type)
3191 case REG_TEXT_T: region = ", .text"; break;
3192 case REG_DATA_T: region = ", .data"; break;
3193 case REG_BSS_T: region = ", .bss"; break;
3194 case REG_GLUE_T: region = ", .glue"; break;
3195 #if defined (REG_RDATA_T) && defined (REG_SDATA_T) && defined (REG_SBSS_T) /*mips*/
3196 case REG_RDATA_T: region = ", .rdata"; break;
3197 case REG_SDATA_T: region = ", .sdata"; break;
3198 case REG_SBSS_T: region = ", .sbss"; break;
3202 fprintf (stderr, ", ty: %s, vaddr: 0x%.8lx, vlen: 0x%.6lx%s\n",
3204 (long) load_hdr->region.regc_vm_addr,
3205 (long) load_hdr->region.regc_vm_size,
3213 /* Fatal error when {en,de}code_mach_o_header fails. */
3219 char *msg = (char *)0;
3223 case MO_ERROR_BAD_MAGIC: msg = "bad magic number"; break;
3224 case MO_ERROR_BAD_HDR_VERS: msg = "bad header version"; break;
3225 case MO_ERROR_BAD_RAW_HDR_VERS: msg = "bad raw header version"; break;
3226 case MO_ERROR_BUF2SML: msg = "raw header buffer too small"; break;
3227 case MO_ERROR_OLD_RAW_HDR_FILE: msg = "old raw header file"; break;
3228 case MO_ERROR_UNSUPPORTED_VERS: msg = "unsupported version"; break;
3231 if (msg == (char *)0)
3232 fatal ("unknown {de,en}code_mach_o_hdr return value %d", status);
3238 /* Read a file into a memory buffer. */
3240 static struct file_info *
3241 read_file (name, fd, rw)
3242 char *name; /* filename */
3243 int fd; /* file descriptor */
3244 int rw; /* read/write */
3246 struct stat stat_pkt;
3247 struct file_info *p = (struct file_info *) xcalloc (sizeof (struct file_info), 1);
3249 static int page_size;
3252 if (fstat (fd, &stat_pkt) < 0)
3253 fatal_perror ("fstat %s", name);
3256 p->size = stat_pkt.st_size;
3257 p->rounded_size = stat_pkt.st_size;
3263 fprintf (stderr, "mmap %s, %s\n", name, (rw) ? "read/write" : "read-only");
3266 page_size = sysconf (_SC_PAGE_SIZE);
3268 p->rounded_size = ((p->size + page_size - 1) / page_size) * page_size;
3269 p->start = mmap ((caddr_t)0,
3270 (rw) ? p->rounded_size : p->size,
3271 (rw) ? (PROT_READ | PROT_WRITE) : PROT_READ,
3272 MAP_FILE | MAP_VARIABLE | MAP_SHARED,
3276 if (p->start != (char *)0 && p->start != (char *)-1)
3280 #endif /* USE_MMAP */
3285 fprintf (stderr, "read %s\n", name);
3288 p->start = xmalloc (p->size);
3289 if (lseek (fd, 0L, SEEK_SET) < 0)
3290 fatal_perror ("lseek to 0 on %s", name);
3292 len = read (fd, p->start, p->size);
3294 fatal_perror ("read %s", name);
3297 fatal ("read %ld bytes, expected %ld, from %s", len, p->size, name);
3303 /* Do anything necessary to write a file back from memory. */
3307 struct file_info *ptr; /* file information block */
3315 fprintf (stderr, "msync %s\n", ptr->name);
3317 if (msync (ptr->start, ptr->rounded_size, MS_ASYNC))
3318 fatal_perror ("msync %s", ptr->name);
3322 fprintf (stderr, "munmap %s\n", ptr->name);
3324 if (munmap (ptr->start, ptr->size))
3325 fatal_perror ("munmap %s", ptr->name);
3328 #endif /* USE_MMAP */
3335 fprintf (stderr, "write %s\n", ptr->name);
3337 if (lseek (ptr->fd, 0L, SEEK_SET) < 0)
3338 fatal_perror ("lseek to 0 on %s", ptr->name);
3340 len = write (ptr->fd, ptr->start, ptr->size);
3342 fatal_perror ("write %s", ptr->name);
3344 if (len != ptr->size)
3345 fatal ("wrote %ld bytes, expected %ld, to %s", len, ptr->size, ptr->name);
3354 #endif /* OBJECT_FORMAT_ROSE */