1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4 Contributed by Chris Smith (csmith@convex.com).
5 Heavily modified by Michael Meissner (meissner@cygnus.com),
6 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
8 This file is part of GNU CC.
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
26 /* Build tables of static constructors and destructors and run ld. */
29 #include <sys/types.h>
41 #include "gansidecl.h"
48 extern char *sys_errlist[];
54 /* Obstack allocation and deallocation routines. */
55 #define obstack_chunk_alloc xmalloc
56 #define obstack_chunk_free free
69 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
72 #define WTERMSIG(S) ((S) & 0x7f)
75 #define WIFEXITED(S) (((S) & 0xff) == 0)
78 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
81 extern char *choose_temp_base ();
83 /* On certain systems, we have code that works by scanning the object file
84 directly. But this code uses system-specific header files and library
85 functions, so turn it off in a cross-compiler. Likewise, the names of
86 the utilities aren't correct for a cross-compiler; we have to hope that
87 cross-versions are in the proper directories. */
90 #undef SUNOS4_SHARED_LIBRARIES
91 #undef OBJECT_FORMAT_COFF
92 #undef OBJECT_FORMAT_ROSE
94 #undef REAL_LD_FILE_NAME
95 #undef REAL_NM_FILE_NAME
96 #undef REAL_STRIP_FILE_NAME
99 /* If we can't use a special method, use the ordinary one:
100 run nm to find what symbols are present.
101 In a cross-compiler, this means you need a cross nm,
102 but that isn't quite as unpleasant as special headers. */
104 #if !defined (OBJECT_FORMAT_COFF) && !defined (OBJECT_FORMAT_ROSE)
105 #define OBJECT_FORMAT_NONE
108 #ifdef OBJECT_FORMAT_COFF
117 /* Many versions of ldfcn.h define these. */
125 /* Some systems have an ISCOFF macro, but others do not. In some cases
126 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
127 that either do not have an ISCOFF macro in /usr/include or for those
128 where it is wrong. */
131 #define MY_ISCOFF(X) ISCOFF (X)
134 #ifdef XCOFF_DEBUGGING_INFO
135 #define XCOFF_SCAN_LIBS
138 #endif /* OBJECT_FORMAT_COFF */
140 #ifdef OBJECT_FORMAT_ROSE
147 #include <sys/mman.h>
151 #include <mach_o_format.h>
152 #include <mach_o_header.h>
153 #include <mach_o_vals.h>
154 #include <mach_o_types.h>
156 #endif /* OBJECT_FORMAT_ROSE */
158 #ifdef OBJECT_FORMAT_NONE
160 /* Default flags to pass to nm. */
162 #define NM_FLAGS "-p"
165 #endif /* OBJECT_FORMAT_NONE */
167 /* Some systems use __main in a way incompatible with its use in gcc, in these
168 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
169 give the same symbol without quotes for an alternative entry point. You
170 must define both, or neither. */
172 #define NAME__MAIN "__main"
173 #define SYMBOL__MAIN __main
176 #if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES || defined(XCOFF_SCAN_LIBS)
177 #define SCAN_LIBRARIES
181 int do_collecting = 1;
183 int do_collecting = 0;
186 /* Linked lists of constructor and destructor names. */
202 /* Enumeration giving which pass this is for scanning the program file. */
205 PASS_FIRST, /* without constructors */
206 PASS_OBJ, /* individual objects */
207 PASS_LIB, /* looking for shared libraries */
208 PASS_SECOND /* with constructors linked in */
211 #ifndef NO_SYS_SIGLIST
212 #ifndef SYS_SIGLIST_DECLARED
213 extern char *sys_siglist[];
216 extern char *version_string;
218 int vflag; /* true if -v */
219 static int rflag; /* true if -r */
220 static int strip_flag; /* true if -s */
222 int debug; /* true if -debug */
224 static int shared_obj; /* true if -shared */
226 static int temp_filename_length; /* Length of temp_filename */
227 static char *temp_filename; /* Base of temp filenames */
228 static char *c_file; /* <xxx>.c for constructor/destructor list. */
229 static char *o_file; /* <xxx>.o for constructor/destructor list. */
230 static char *export_file; /* <xxx>.x for AIX export list. */
231 char *ldout; /* File for ld errors. */
232 static char *output_file; /* Output file for ld. */
233 static char *nm_file_name; /* pathname of nm */
234 static char *ldd_file_name; /* pathname of ldd (or equivalent) */
235 static char *strip_file_name; /* pathname of strip */
236 char *c_file_name; /* pathname of gcc */
237 static char *initname, *fininame; /* names of init and fini funcs */
239 static struct head constructors; /* list of constructors found */
240 static struct head destructors; /* list of destructors found */
241 static struct head exports; /* list of exported symbols */
243 struct obstack temporary_obstack;
244 struct obstack permanent_obstack;
245 char * temporary_firstobj;
247 /* Defined in the automatically-generated underscore.c. */
248 extern int prepends_underscore;
250 extern char *getenv ();
251 extern char *mktemp ();
252 extern FILE *fdopen ();
254 /* Structure to hold all the directories in which to search for files to
259 char *prefix; /* String to prepend to the path. */
260 struct prefix_list *next; /* Next in linked list. */
265 struct prefix_list *plist; /* List of prefixes to try */
266 int max_len; /* Max length of a prefix in PLIST */
267 char *name; /* Name of this list (used in config stuff) */
270 void collect_exit PROTO((int));
271 void collect_execute PROTO((char *, char **, char *));
272 void dump_file PROTO((char *));
273 static void handler PROTO((int));
274 static int is_ctor_dtor PROTO((char *));
275 static int is_in_prefix_list PROTO((struct path_prefix *, char *, int));
276 static char *find_a_file PROTO((struct path_prefix *, char *));
277 static void add_prefix PROTO((struct path_prefix *, char *));
278 static void prefix_from_env PROTO((char *, struct path_prefix *));
279 static void prefix_from_string PROTO((char *, struct path_prefix *));
280 static void do_wait PROTO((char *));
281 static void fork_execute PROTO((char *, char **));
282 static void maybe_unlink PROTO((char *));
283 static void add_to_list PROTO((struct head *, char *));
284 static void write_list PROTO((FILE *, char *, struct id *));
285 static void write_list_with_asm PROTO((FILE *, char *, struct id *));
286 static void write_c_file PROTO((FILE *, char *));
287 static void write_export_file PROTO((FILE *));
288 static void scan_prog_file PROTO((char *, enum pass));
289 static void scan_libraries PROTO((char *));
294 extern char *index ();
295 extern char *rindex ();
311 while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
314 close (fdtmp[--fdx]);
330 static char buffer[30];
334 if (e > 0 && e < sys_nerr)
335 return sys_errlist[e];
337 sprintf (buffer, "Unknown error %d", e);
342 /* Delete tempfiles and exit function. */
345 collect_exit (status)
348 if (c_file != 0 && c_file[0])
349 maybe_unlink (c_file);
351 if (o_file != 0 && o_file[0])
352 maybe_unlink (o_file);
354 if (export_file != 0 && export_file[0])
355 maybe_unlink (export_file);
357 if (ldout != 0 && ldout[0])
360 maybe_unlink (ldout);
363 if (status != 0 && output_file != 0 && output_file[0])
364 maybe_unlink (output_file);
370 /* Die when sys call fails. */
373 fatal_perror (string, arg1, arg2, arg3)
374 char *string, *arg1, *arg2, *arg3;
378 fprintf (stderr, "collect2: ");
379 fprintf (stderr, string, arg1, arg2, arg3);
380 fprintf (stderr, ": %s\n", my_strerror (e));
381 collect_exit (FATAL_EXIT_CODE);
387 fatal (string, arg1, arg2, arg3)
388 char *string, *arg1, *arg2, *arg3;
390 fprintf (stderr, "collect2: ");
391 fprintf (stderr, string, arg1, arg2, arg3);
392 fprintf (stderr, "\n");
393 collect_exit (FATAL_EXIT_CODE);
396 /* Write error message. */
399 error (string, arg1, arg2, arg3, arg4)
400 char *string, *arg1, *arg2, *arg3, *arg4;
402 fprintf (stderr, "collect2: ");
403 fprintf (stderr, string, arg1, arg2, arg3, arg4);
404 fprintf (stderr, "\n");
407 /* In case obstack is linked in, and abort is defined to fancy_abort,
408 provide a default entry. */
413 fatal ("internal error");
421 if (c_file != 0 && c_file[0])
422 maybe_unlink (c_file);
424 if (o_file != 0 && o_file[0])
425 maybe_unlink (o_file);
427 if (ldout != 0 && ldout[0])
428 maybe_unlink (ldout);
430 if (export_file != 0 && export_file[0])
431 maybe_unlink (export_file);
433 signal (signo, SIG_DFL);
434 kill (getpid (), signo);
439 xcalloc (size1, size2)
442 char *ptr = (char *) calloc (size1, size2);
446 fatal ("out of memory");
454 char *ptr = (char *) malloc (size);
458 fatal ("out of memory");
467 register char *value = (char *) realloc (ptr, size);
469 fatal ("virtual memory exhausted");
477 return access (name, R_OK) == 0;
480 /* Make a copy of a string INPUT with size SIZE. */
483 savestring (input, size)
487 char *output = (char *) xmalloc (size + 1);
488 bcopy (input, output, size);
493 /* Parse a reasonable subset of shell quoting syntax. */
510 obstack_1grow (&temporary_obstack, c);
511 else if (! inside && c == ' ')
513 else if (! inside && c == '\\')
518 obstack_1grow (&temporary_obstack, c);
521 obstack_1grow (&temporary_obstack, '\0');
523 return obstack_finish (&temporary_obstack);
530 FILE *stream = fopen (name, "r");
531 int no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
538 while (c = getc (stream),
539 c != EOF && (isalnum (c) || c == '_' || c == '$' || c == '.'))
540 obstack_1grow (&temporary_obstack, c);
541 if (obstack_object_size (&temporary_obstack) > 0)
543 char *word, *p, *result;
544 obstack_1grow (&temporary_obstack, '\0');
545 word = obstack_finish (&temporary_obstack);
548 ++word, putc ('.', stderr);
550 if (*p == '_' && prepends_underscore)
556 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI);
561 fputs (result, stderr);
563 diff = strlen (word) - strlen (result);
565 --diff, putc (' ', stderr);
566 while (diff < 0 && c == ' ')
567 ++diff, c = getc (stream);
572 fputs (word, stderr);
575 obstack_free (&temporary_obstack, temporary_firstobj);
584 /* Decide whether the given symbol is:
585 a constructor (1), a destructor (2), or neither (0). */
591 struct names { char *name; int len; int ret; int two_underscores; };
593 register struct names *p;
595 register char *orig_s = s;
597 static struct names special[] = {
598 #ifdef NO_DOLLAR_IN_LABEL
599 #ifdef NO_DOT_IN_LABEL
600 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
601 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
603 { "GLOBAL_.I.", sizeof ("GLOBAL_.I.")-1, 1, 0 },
604 { "GLOBAL_.D.", sizeof ("GLOBAL_.D.")-1, 2, 0 },
607 { "GLOBAL_$I$", sizeof ("GLOBAL_$I$")-1, 1, 0 },
608 { "GLOBAL_$D$", sizeof ("GLOBAL_$D$")-1, 2, 0 },
610 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
611 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
612 #ifdef CFRONT_LOSSAGE /* Don't collect cfront initialization functions.
613 cfront has its own linker procedure to collect them;
614 if collect2 gets them too, they get collected twice
615 when the cfront procedure is run and the compiler used
616 for linking happens to be GCC. */
617 { "sti__", sizeof ("sti__")-1, 1, 1 },
618 { "std__", sizeof ("std__")-1, 2, 1 },
619 #endif /* CFRONT_LOSSAGE */
623 while ((ch = *s) == '_')
629 for (p = &special[0]; p->len > 0; p++)
632 && (!p->two_underscores || ((s - orig_s) >= 2))
633 && strncmp(s, p->name, p->len) == 0)
641 /* Routine to add variables to the environment. */
649 #ifndef VMS /* nor about VMS */
651 extern char **environ;
652 char **old_environ = environ;
659 while ((ch = *p++) != '\0' && ch != '=')
665 /* Search for replacing an existing environment variable, and
666 count the number of total environment variables. */
667 for (envp = old_environ; *envp; envp++)
670 if (!strncmp (str, *envp, name_len))
677 /* Add a new environment variable */
678 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
680 bcopy ((char *) old_environ, (char *) (environ + 1),
681 sizeof (char *) * (num_envs+1));
687 #endif /* HAVE_PUTENV */
689 /* By default, colon separates directories in a path. */
690 #ifndef PATH_SEPARATOR
691 #define PATH_SEPARATOR ':'
694 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
695 and one from the PATH variable. */
697 static struct path_prefix cpath, path;
700 /* This is the name of the target machine. We use it to form the name
701 of the files to execute. */
703 static char *target_machine = TARGET_MACHINE;
706 /* Names under which we were executed. Never return one of those files in our
709 static struct path_prefix our_file_names;
711 /* Determine if STRING is in PPREFIX.
713 This utility is currently only used to look up file names. Prefix lists
714 record directory names. This matters to us because the latter has a
715 trailing slash, so I've added a flag to handle both. */
718 is_in_prefix_list (pprefix, string, filep)
719 struct path_prefix *pprefix;
723 struct prefix_list *pl;
727 int len = strlen (string);
729 for (pl = pprefix->plist; pl; pl = pl->next)
731 if (strncmp (pl->prefix, string, len) == 0
732 && strcmp (pl->prefix + len, "/") == 0)
738 for (pl = pprefix->plist; pl; pl = pl->next)
740 if (strcmp (pl->prefix, string) == 0)
748 /* Search for NAME using prefix list PPREFIX. We only look for executable
751 Return 0 if not found, otherwise return its name, allocated with malloc. */
754 find_a_file (pprefix, name)
755 struct path_prefix *pprefix;
759 struct prefix_list *pl;
760 int len = pprefix->max_len + strlen (name) + 1;
762 #ifdef EXECUTABLE_SUFFIX
763 len += strlen (EXECUTABLE_SUFFIX);
766 temp = xmalloc (len);
768 /* Determine the filename to execute (special case for absolute paths). */
772 if (access (name, X_OK) == 0)
779 for (pl = pprefix->plist; pl; pl = pl->next)
781 strcpy (temp, pl->prefix);
783 if (! is_in_prefix_list (&our_file_names, temp, 1)
784 /* This is a kludge, but there seems no way around it. */
785 && strcmp (temp, "./ld") != 0
786 && access (temp, X_OK) == 0)
789 #ifdef EXECUTABLE_SUFFIX
790 /* Some systems have a suffix for executable files.
791 So try appending that. */
792 strcat (temp, EXECUTABLE_SUFFIX);
793 if (! is_in_prefix_list (&our_file_names, temp, 1)
794 && access (temp, X_OK) == 0)
803 /* Add an entry for PREFIX to prefix list PPREFIX. */
806 add_prefix (pprefix, prefix)
807 struct path_prefix *pprefix;
810 struct prefix_list *pl, **prev;
815 for (pl = pprefix->plist; pl->next; pl = pl->next)
820 prev = &pprefix->plist;
822 /* Keep track of the longest prefix */
824 len = strlen (prefix);
825 if (len > pprefix->max_len)
826 pprefix->max_len = len;
828 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
829 pl->prefix = savestring (prefix, len);
834 pl->next = (struct prefix_list *) 0;
838 /* Take the value of the environment variable ENV, break it into a path, and
839 add of the entries to PPREFIX. */
842 prefix_from_env (env, pprefix)
844 struct path_prefix *pprefix;
846 char *p = getenv (env);
849 prefix_from_string (p, pprefix);
853 prefix_from_string (p, pprefix)
855 struct path_prefix *pprefix;
858 char *nstore = (char *) xmalloc (strlen (p) + 3);
863 if (*endp == PATH_SEPARATOR || *endp == 0)
865 strncpy (nstore, startp, endp-startp);
868 strcpy (nstore, "./");
870 else if (endp[-1] != '/')
872 nstore[endp-startp] = '/';
873 nstore[endp-startp+1] = 0;
876 nstore[endp-startp] = 0;
878 add_prefix (pprefix, nstore);
881 endp = startp = endp + 1;
895 char *ld_suffix = "ld";
896 char *full_ld_suffix = ld_suffix;
897 char *real_ld_suffix = "real-ld";
898 char *full_real_ld_suffix = real_ld_suffix;
899 char *collect_ld_suffix = "collect-ld";
900 char *nm_suffix = "nm";
901 char *full_nm_suffix = nm_suffix;
902 char *gnm_suffix = "gnm";
903 char *full_gnm_suffix = gnm_suffix;
905 char *ldd_suffix = LDD_SUFFIX;
906 char *full_ldd_suffix = ldd_suffix;
908 char *strip_suffix = "strip";
909 char *full_strip_suffix = strip_suffix;
910 char *gstrip_suffix = "gstrip";
911 char *full_gstrip_suffix = gstrip_suffix;
913 FILE *outf, *exportf;
920 char **ld1_argv = (char **) xcalloc (sizeof (char *), argc+3);
921 char **ld1 = ld1_argv;
922 char **ld2_argv = (char **) xcalloc (sizeof (char *), argc+6);
923 char **ld2 = ld2_argv;
924 char **object_lst = (char **) xcalloc (sizeof (char *), argc);
925 char **object = object_lst;
927 int num_c_args = argc+7;
934 #ifndef DEFAULT_A_OUT_NAME
935 output_file = "a.out";
937 output_file = DEFAULT_A_OUT_NAME;
940 obstack_begin (&temporary_obstack, 0);
941 obstack_begin (&permanent_obstack, 0);
942 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
943 current_demangling_style = gnu_demangling;
945 /* We must check that we do not call ourselves in an infinite
946 recursion loop. We append the name used for us to the COLLECT_NAMES
947 environment variable.
949 In practice, collect will rarely invoke itself. This can happen now
950 that we are no longer called gld. A perfect example is when running
951 gcc in a build directory that has been installed. When looking for
952 ld's, we'll find our installed version and believe that's the real ld. */
954 /* We must also append COLLECT_NAME to COLLECT_NAMES to watch for the
955 previous version of collect (the one that used COLLECT_NAME and only
956 handled two levels of recursion). If we don't we may mutually recurse
957 forever. This can happen (I think) when bootstrapping the old version
958 and a new one is installed (rare, but we should handle it).
959 ??? Hopefully references to COLLECT_NAME can be removed at some point. */
961 collect_name = (char *) getenv ("COLLECT_NAME");
962 collect_names = (char *) getenv ("COLLECT_NAMES");
964 p = (char *) xmalloc (strlen ("COLLECT_NAMES=")
965 + (collect_name ? strlen (collect_name) + 1 : 0)
966 + (collect_names ? strlen (collect_names) + 1 : 0)
967 + strlen (argv[0]) + 1);
968 strcpy (p, "COLLECT_NAMES=");
969 if (collect_name != 0)
970 sprintf (p + strlen (p), "%s%c", collect_name, PATH_SEPARATOR);
971 if (collect_names != 0)
972 sprintf (p + strlen (p), "%s%c", collect_names, PATH_SEPARATOR);
976 prefix_from_env ("COLLECT_NAMES", &our_file_names);
978 /* Set environment variable COLLECT_NAME to our name so the previous version
979 of collect won't find us. If it does we'll mutually recurse forever.
980 This can happen when bootstrapping the new version and an old version is
982 ??? Hopefully this bit of code can be removed at some point. */
984 p = xmalloc (strlen ("COLLECT_NAME=") + strlen (argv[0]) + 1);
985 sprintf (p, "COLLECT_NAME=%s", argv[0]);
988 p = (char *) getenv ("COLLECT_GCC_OPTIONS");
991 char *q = extract_string (&p);
992 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
995 obstack_free (&temporary_obstack, temporary_firstobj);
997 c_ptr = c_argv = (char **) xcalloc (sizeof (char *), num_c_args);
1000 fatal ("no arguments");
1003 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
1004 signal (SIGQUIT, handler);
1006 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1007 signal (SIGINT, handler);
1009 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
1010 signal (SIGALRM, handler);
1013 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1014 signal (SIGHUP, handler);
1016 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
1017 signal (SIGSEGV, handler);
1019 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
1020 signal (SIGBUS, handler);
1023 /* Extract COMPILER_PATH and PATH into our prefix list. */
1024 prefix_from_env ("COMPILER_PATH", &cpath);
1025 prefix_from_env ("PATH", &path);
1027 #ifdef CROSS_COMPILE
1028 /* If we look for a program in the compiler directories, we just use
1029 the short name, since these directories are already system-specific.
1030 But it we look for a took in the system directories, we need to
1031 qualify the program name with the target machine. */
1034 = xcalloc (strlen (ld_suffix) + strlen (target_machine) + 2, 1);
1035 strcpy (full_ld_suffix, target_machine);
1036 strcat (full_ld_suffix, "-");
1037 strcat (full_ld_suffix, ld_suffix);
1040 = xcalloc (strlen (real_ld_suffix) + strlen (target_machine) + 2, 1);
1041 strcpy (full_real_ld_suffix, target_machine);
1042 strcat (full_real_ld_suffix, "-");
1043 strcat (full_real_ld_suffix, real_ld_suffix);
1047 = xcalloc (strlen (gld_suffix) + strlen (target_machine) + 2, 1);
1048 strcpy (full_gld_suffix, target_machine);
1049 strcat (full_gld_suffix, "-");
1050 strcat (full_gld_suffix, gld_suffix);
1054 = xcalloc (strlen (nm_suffix) + strlen (target_machine) + 2, 1);
1055 strcpy (full_nm_suffix, target_machine);
1056 strcat (full_nm_suffix, "-");
1057 strcat (full_nm_suffix, nm_suffix);
1060 = xcalloc (strlen (gnm_suffix) + strlen (target_machine) + 2, 1);
1061 strcpy (full_gnm_suffix, target_machine);
1062 strcat (full_gnm_suffix, "-");
1063 strcat (full_gnm_suffix, gnm_suffix);
1067 = xcalloc (strlen (ldd_suffix) + strlen (target_machine) + 2, 1);
1068 strcpy (full_ldd_suffix, target_machine);
1069 strcat (full_ldd_suffix, "-");
1070 strcat (full_ldd_suffix, ldd_suffix);
1074 = xcalloc (strlen (strip_suffix) + strlen (target_machine) + 2, 1);
1075 strcpy (full_strip_suffix, target_machine);
1076 strcat (full_strip_suffix, "-");
1077 strcat (full_strip_suffix, strip_suffix);
1080 = xcalloc (strlen (gstrip_suffix) + strlen (target_machine) + 2, 1);
1081 strcpy (full_gstrip_suffix, target_machine);
1082 strcat (full_gstrip_suffix, "-");
1083 strcat (full_gstrip_suffix, gstrip_suffix);
1084 #endif /* CROSS_COMPILE */
1086 /* Try to discover a valid linker/nm/strip to use. */
1088 /* Maybe we know the right file to use (if not cross). */
1089 #ifdef REAL_LD_FILE_NAME
1090 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
1091 if (ld_file_name == 0)
1093 /* Search the (target-specific) compiler dirs for ld'. */
1094 ld_file_name = find_a_file (&cpath, real_ld_suffix);
1095 /* Likewise for `collect-ld'. */
1096 if (ld_file_name == 0)
1097 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
1098 /* Search the compiler directories for `ld'. We have protection against
1099 recursive calls in find_a_file. */
1100 if (ld_file_name == 0)
1101 ld_file_name = find_a_file (&cpath, ld_suffix);
1102 /* Search the ordinary system bin directories
1103 for `ld' (if native linking) or `TARGET-ld' (if cross). */
1104 if (ld_file_name == 0)
1105 ld_file_name = find_a_file (&path, full_ld_suffix);
1107 /* If we've invoked ourselves, try again with LD_FILE_NAME. */
1109 if (collect_names != 0)
1111 if (ld_file_name != 0)
1113 argv[0] = ld_file_name;
1114 execvp (argv[0], argv);
1116 fatal ("cannot find `ld'");
1119 #ifdef REAL_NM_FILE_NAME
1120 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
1121 if (nm_file_name == 0)
1123 nm_file_name = find_a_file (&cpath, gnm_suffix);
1124 if (nm_file_name == 0)
1125 nm_file_name = find_a_file (&path, full_gnm_suffix);
1126 if (nm_file_name == 0)
1127 nm_file_name = find_a_file (&cpath, nm_suffix);
1128 if (nm_file_name == 0)
1129 nm_file_name = find_a_file (&path, full_nm_suffix);
1132 ldd_file_name = find_a_file (&cpath, ldd_suffix);
1133 if (ldd_file_name == 0)
1134 ldd_file_name = find_a_file (&path, full_ldd_suffix);
1137 #ifdef REAL_STRIP_FILE_NAME
1138 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
1139 if (strip_file_name == 0)
1141 strip_file_name = find_a_file (&cpath, gstrip_suffix);
1142 if (strip_file_name == 0)
1143 strip_file_name = find_a_file (&path, full_gstrip_suffix);
1144 if (strip_file_name == 0)
1145 strip_file_name = find_a_file (&cpath, strip_suffix);
1146 if (strip_file_name == 0)
1147 strip_file_name = find_a_file (&path, full_strip_suffix);
1149 /* Determine the full path name of the C compiler to use. */
1150 c_file_name = getenv ("COLLECT_GCC");
1151 if (c_file_name == 0)
1153 #ifdef CROSS_COMPILE
1154 c_file_name = xcalloc (sizeof ("gcc-") + strlen (target_machine) + 1, 1);
1155 strcpy (c_file_name, target_machine);
1156 strcat (c_file_name, "-gcc");
1158 c_file_name = "gcc";
1162 p = find_a_file (&cpath, c_file_name);
1164 /* Here it should be safe to use the system search path since we should have
1165 already qualified the name of the compiler when it is needed. */
1167 p = find_a_file (&path, c_file_name);
1172 *ld1++ = *ld2++ = ld_file_name;
1174 /* Make temp file names. */
1175 temp_filename = choose_temp_base ();
1176 temp_filename_length = strlen (temp_filename);
1177 c_file = xcalloc (temp_filename_length + sizeof (".c"), 1);
1178 o_file = xcalloc (temp_filename_length + sizeof (".o"), 1);
1179 export_file = xmalloc (temp_filename_length + sizeof (".x"));
1180 ldout = xmalloc (temp_filename_length + sizeof (".ld"));
1181 sprintf (ldout, "%s.ld", temp_filename);
1182 sprintf (c_file, "%s.c", temp_filename);
1183 sprintf (o_file, "%s.o", temp_filename);
1184 sprintf (export_file, "%s.x", temp_filename);
1185 *c_ptr++ = c_file_name;
1190 /* !!! When GCC calls collect2,
1191 it does not know whether it is calling collect2 or ld.
1192 So collect2 cannot meaningfully understand any options
1193 except those ld understands.
1194 If you propose to make GCC pass some other option,
1195 just imagine what will happen if ld is really ld!!! */
1197 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1198 /* After the first file, put in the c++ rt0. */
1201 while ((arg = *++argv) != (char *) 0)
1203 *ld1++ = *ld2++ = arg;
1210 if (!strcmp (arg, "-debug"))
1222 /* place o_file BEFORE this argument! */
1232 output_file = *ld1++ = *ld2++ = *++argv;
1234 output_file = &arg[2];
1243 if (arg[2] == '\0' && do_collecting)
1245 /* We must strip after the nm run, otherwise C++ linking
1246 won't work. Thus we strip in the second ld run, or
1247 else with strip if there is no second ld run. */
1259 else if ((p = rindex (arg, '.')) != (char *) 0
1260 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0))
1269 /* place o_file BEFORE this argument! */
1280 /* Get any options that the upper GCC wants to pass to the sub-GCC. */
1281 p = (char *) getenv ("COLLECT_GCC_OPTIONS");
1284 char *q = extract_string (&p);
1285 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1286 *c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
1287 if (strncmp (q, "-shared", sizeof ("shared") - 1) == 0)
1290 obstack_free (&temporary_obstack, temporary_firstobj);
1292 #ifdef COLLECT_EXPORT_LIST
1293 /* The AIX linker will discard static constructors in object files if
1294 nothing else in the file is referenced, so look at them first. */
1295 while (object_lst < object)
1296 scan_prog_file (*object_lst++, PASS_OBJ);
1299 char *buf = alloca (strlen (export_file) + 5);
1300 sprintf (buf, "-bE:%s", export_file);
1303 exportf = fopen (export_file, "w");
1304 if (exportf == (FILE *) 0)
1305 fatal_perror ("%s", export_file);
1306 write_export_file (exportf);
1307 if (fclose (exportf))
1308 fatal_perror ("closing %s", export_file);
1313 *object = *c_ptr = *ld1 = (char *) 0;
1317 fprintf (stderr, "collect2 version %s", version_string);
1318 #ifdef TARGET_VERSION
1321 fprintf (stderr, "\n");
1327 fprintf (stderr, "ld_file_name = %s\n",
1328 (ld_file_name ? ld_file_name : "not found"));
1329 fprintf (stderr, "c_file_name = %s\n",
1330 (c_file_name ? c_file_name : "not found"));
1331 fprintf (stderr, "nm_file_name = %s\n",
1332 (nm_file_name ? nm_file_name : "not found"));
1334 fprintf (stderr, "ldd_file_name = %s\n",
1335 (ldd_file_name ? ldd_file_name : "not found"));
1337 fprintf (stderr, "strip_file_name = %s\n",
1338 (strip_file_name ? strip_file_name : "not found"));
1339 fprintf (stderr, "c_file = %s\n",
1340 (c_file ? c_file : "not found"));
1341 fprintf (stderr, "o_file = %s\n",
1342 (o_file ? o_file : "not found"));
1344 ptr = getenv ("COLLECT_NAMES");
1346 fprintf (stderr, "COLLECT_NAMES = %s\n", ptr);
1348 ptr = getenv ("COLLECT_GCC_OPTIONS");
1350 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1352 ptr = getenv ("COLLECT_GCC");
1354 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1356 ptr = getenv ("COMPILER_PATH");
1358 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1360 ptr = getenv ("LIBRARY_PATH");
1362 fprintf (stderr, "LIBRARY_PATH = %s\n", ptr);
1364 fprintf (stderr, "\n");
1367 /* Load the program, searching all libraries and attempting to provide
1368 undefined symbols from repository information. */
1370 do_tlink (ld1_argv, object_lst);
1372 /* If -r or they'll be run via some other method, don't build the
1373 constructor or destructor list, just return now. */
1374 if (rflag || ! do_collecting)
1376 /* But make sure we delete the export file we may have created. */
1377 if (export_file != 0 && export_file[0])
1378 maybe_unlink (export_file);
1382 /* Examine the namelist with nm and search it for static constructors
1383 and destructors to call.
1384 Write the constructor and destructor tables to a .s file and reload. */
1386 scan_prog_file (output_file, PASS_FIRST);
1388 #ifdef SCAN_LIBRARIES
1389 scan_libraries (output_file);
1394 fprintf (stderr, "%d constructor(s) found\n", constructors.number);
1395 fprintf (stderr, "%d destructor(s) found\n", destructors.number);
1398 if (constructors.number == 0 && destructors.number == 0
1400 /* If we will be running these functions ourselves, we want to emit
1401 stubs into the shared library so that we don't have to relink
1402 dependent programs when we add static objects. */
1407 /* Strip now if it was requested on the command line. */
1410 char **strip_argv = (char **) xcalloc (sizeof (char *), 3);
1411 strip_argv[0] = strip_file_name;
1412 strip_argv[1] = output_file;
1413 strip_argv[2] = (char *) 0;
1414 fork_execute ("strip", strip_argv);
1417 #ifdef COLLECT_EXPORT_LIST
1418 maybe_unlink (export_file);
1423 maybe_unlink(output_file);
1424 outf = fopen (c_file, "w");
1425 if (outf == (FILE *) 0)
1426 fatal_perror ("%s", c_file);
1428 write_c_file (outf, c_file);
1431 fatal_perror ("closing %s", c_file);
1433 /* Tell the linker that we have initializer and finalizer functions. */
1434 #ifdef LD_INIT_SWITCH
1435 *ld2++ = LD_INIT_SWITCH;
1437 *ld2++ = LD_FINI_SWITCH;
1442 #ifdef COLLECT_EXPORT_LIST
1445 add_to_list (&exports, initname);
1446 add_to_list (&exports, fininame);
1447 add_to_list (&exports, "_GLOBAL__DI");
1448 add_to_list (&exports, "_GLOBAL__DD");
1449 exportf = fopen (export_file, "w");
1450 if (exportf == (FILE *) 0)
1451 fatal_perror ("%s", export_file);
1452 write_export_file (exportf);
1453 if (fclose (exportf))
1454 fatal_perror ("closing %s", export_file);
1460 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1461 output_file, c_file);
1462 write_c_file (stderr, "stderr");
1463 fprintf (stderr, "========== end of c_file\n\n");
1464 #ifdef COLLECT_EXPORT_LIST
1465 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1466 write_export_file (stderr);
1467 fprintf (stderr, "========== end of export_file\n\n");
1471 /* Assemble the constructor and destructor tables.
1472 Link the tables in with the rest of the program. */
1474 fork_execute ("gcc", c_argv);
1475 fork_execute ("ld", ld2_argv);
1477 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1478 constructors/destructors in shared libraries. */
1479 scan_prog_file (output_file, PASS_SECOND);
1481 maybe_unlink (c_file);
1482 maybe_unlink (o_file);
1483 maybe_unlink (export_file);
1488 /* Wait for a process to finish, and exit if a non-zero status is found. */
1499 if (WIFSIGNALED (status))
1501 int sig = WTERMSIG (status);
1502 #ifdef NO_SYS_SIGLIST
1503 error ("%s terminated with signal %d %s",
1506 (status & 0200) ? ", core dumped" : "");
1508 error ("%s terminated with signal %d [%s]%s",
1512 (status & 0200) ? ", core dumped" : "");
1515 collect_exit (FATAL_EXIT_CODE);
1518 if (WIFEXITED (status))
1519 return WEXITSTATUS (status);
1528 int ret = collect_wait (prog);
1531 error ("%s returned %d exit status", prog, ret);
1537 /* Fork and execute a program, and wait for the reply. */
1540 collect_execute (prog, argv, redir)
1553 fprintf (stderr, "%s", argv[0]);
1555 fprintf (stderr, "[cannot find %s]", prog);
1557 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1558 fprintf (stderr, " %s", str);
1560 fprintf (stderr, "\n");
1566 /* If we can't find a program we need, complain error. Do this here
1567 since we might not end up needing something that we couldn't find. */
1570 fatal ("cannot find `%s'", prog);
1576 fatal_perror ("fork");
1578 fatal_perror ("vfork");
1582 if (pid == 0) /* child context */
1587 if (freopen (redir, "a", stdout) == NULL)
1588 fatal_perror ("redirecting stdout");
1589 if (freopen (redir, "a", stderr) == NULL)
1590 fatal_perror ("redirecting stderr");
1593 execvp (argv[0], argv);
1594 fatal_perror ("executing %s", prog);
1599 fork_execute (prog, argv)
1603 collect_execute (prog, argv, NULL);
1607 /* Unlink a file unless we are debugging. */
1616 fprintf (stderr, "[Leaving %s]\n", file);
1620 /* Add a name to a linked list. */
1623 add_to_list (head_ptr, name)
1624 struct head *head_ptr;
1628 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1630 static long sequence_number = 0;
1631 strcpy (newid->name, name);
1633 if (head_ptr->first)
1634 head_ptr->last->next = newid;
1636 head_ptr->first = newid;
1638 /* Check for duplicate symbols. */
1639 for (p = head_ptr->first;
1640 strcmp (name, p->name) != 0;
1645 head_ptr->last->next = 0;
1650 newid->sequence = ++sequence_number;
1651 head_ptr->last = newid;
1655 /* Write: `prefix', the names on list LIST, `suffix'. */
1658 write_list (stream, prefix, list)
1665 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1671 write_list_with_asm (stream, prefix, list)
1678 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1679 prefix, list->sequence, list->name);
1684 /* Write out the constructor and destructor tables statically (for a shared
1685 object), along with the functions to execute them. */
1688 write_c_file_stat (stream, name)
1692 char *prefix, *p, *q;
1694 /* Figure out name of output_file, stripping off .so version. */
1695 p = rindex (output_file, '/');
1697 p = (char *) output_file;
1711 if (strncmp (q, ".so", 3) == 0)
1720 /* q points to null at end of the string (or . of the .so version) */
1721 prefix = xmalloc (q - p + 1);
1722 strncpy (prefix, p, q - p);
1724 for (q = prefix; *q; q++)
1728 fprintf (stderr, "\nwrite_c_file - output name is %s, prefix is %s\n",
1729 output_file, prefix);
1731 #define INIT_NAME_FORMAT "_GLOBAL__FI_%s"
1732 initname = xmalloc (strlen (prefix) + sizeof (INIT_NAME_FORMAT) - 2);
1733 sprintf (initname, INIT_NAME_FORMAT, prefix);
1735 #define FINI_NAME_FORMAT "_GLOBAL__FD_%s"
1736 fininame = xmalloc (strlen (prefix) + sizeof (FINI_NAME_FORMAT) - 2);
1737 sprintf (fininame, FINI_NAME_FORMAT, prefix);
1741 /* Write the tables as C code */
1743 fprintf (stream, "static int count;\n");
1744 fprintf (stream, "typedef void entry_pt();\n");
1745 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1746 fprintf (stream, "void %s() {\n", initname);
1747 if (constructors.number > 0)
1749 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1750 write_list (stream, "\t\t", constructors.first);
1751 fprintf (stream, "\t};\n");
1752 fprintf (stream, "\tentry_pt **p;\n");
1753 fprintf (stream, "\tif (count++ != 0) return;\n");
1754 fprintf (stream, "\tp = ctors + %d;\n", constructors.number);
1755 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1758 fprintf (stream, "\t++count;\n");
1759 fprintf (stream, "}\n");
1760 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1761 fprintf (stream, "void %s() {\n", fininame);
1762 if (destructors.number > 0)
1764 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1765 write_list (stream, "\t\t", destructors.first);
1766 fprintf (stream, "\t};\n");
1767 fprintf (stream, "\tentry_pt **p;\n");
1768 fprintf (stream, "\tif (--count != 0) return;\n");
1769 fprintf (stream, "\tp = dtors;\n");
1770 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1771 destructors.number);
1773 fprintf (stream, "}\n");
1777 fprintf (stream, "void _GLOBAL__DI() {\n\t%s();\n}\n", initname);
1778 fprintf (stream, "void _GLOBAL__DD() {\n\t%s();\n}\n", fininame);
1782 /* Write the constructor/destructor tables. */
1785 write_c_file_glob (stream, name)
1789 /* Write the tables as C code */
1791 fprintf (stream, "typedef void entry_pt();\n\n");
1793 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1795 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1796 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number);
1797 write_list (stream, "\t", constructors.first);
1798 fprintf (stream, "\t0\n};\n\n");
1800 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1802 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1803 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number);
1804 write_list (stream, "\t", destructors.first);
1805 fprintf (stream, "\t0\n};\n\n");
1807 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1808 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1812 write_c_file (stream, name)
1816 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1817 #ifndef LD_INIT_SWITCH
1819 write_c_file_glob (stream, name);
1822 write_c_file_stat (stream, name);
1823 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
1827 write_export_file (stream)
1830 struct id *list = exports.first;
1831 for (; list; list = list->next)
1832 fprintf (stream, "%s\n", list->name);
1835 #ifdef OBJECT_FORMAT_NONE
1837 /* Generic version to scan the name list of the loaded program for
1838 the symbols g++ uses for static constructors and destructors.
1840 The constructor table begins at __CTOR_LIST__ and contains a count
1841 of the number of pointers (or -1 if the constructors are built in a
1842 separate section by the linker), followed by the pointers to the
1843 constructor functions, terminated with a null pointer. The
1844 destructor table has the same format, and begins at __DTOR_LIST__. */
1847 scan_prog_file (prog_name, which_pass)
1849 enum pass which_pass;
1851 void (*int_handler) ();
1852 void (*quit_handler) ();
1860 if (which_pass == PASS_SECOND)
1863 /* If we don't have an `nm', complain. */
1864 if (nm_file_name == 0)
1865 fatal ("cannot find `nm'");
1867 nm_argv[argc++] = nm_file_name;
1868 if (NM_FLAGS[0] != '\0')
1869 nm_argv[argc++] = NM_FLAGS;
1871 nm_argv[argc++] = prog_name;
1872 nm_argv[argc++] = (char *) 0;
1874 if (pipe (pipe_fd) < 0)
1875 fatal_perror ("pipe");
1877 inf = fdopen (pipe_fd[0], "r");
1878 if (inf == (FILE *) 0)
1879 fatal_perror ("fdopen");
1881 /* Trace if needed. */
1887 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
1888 fprintf (stderr, " %s", str);
1890 fprintf (stderr, "\n");
1896 /* Spawn child nm on pipe */
1901 fatal_perror ("fork");
1903 fatal_perror ("vfork");
1907 if (pid == 0) /* child context */
1910 if (dup2 (pipe_fd[1], 1) < 0)
1911 fatal_perror ("dup2 (%d, 1)", pipe_fd[1]);
1913 if (close (pipe_fd[0]) < 0)
1914 fatal_perror ("close (%d)", pipe_fd[0]);
1916 if (close (pipe_fd[1]) < 0)
1917 fatal_perror ("close (%d)", pipe_fd[1]);
1919 execv (nm_file_name, nm_argv);
1920 fatal_perror ("executing %s", nm_file_name);
1923 /* Parent context from here on. */
1924 int_handler = (void (*) ())signal (SIGINT, SIG_IGN);
1926 quit_handler = (void (*) ())signal (SIGQUIT, SIG_IGN);
1929 if (close (pipe_fd[1]) < 0)
1930 fatal_perror ("close (%d)", pipe_fd[1]);
1933 fprintf (stderr, "\nnm output with constructors/destructors.\n");
1935 /* Read each line of nm output. */
1936 while (fgets (buf, sizeof buf, inf) != (char *) 0)
1941 /* If it contains a constructor or destructor name, add the name
1942 to the appropriate list. */
1944 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
1945 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
1952 /* Find the end of the symbol name.
1953 Don't include `|', because Encore nm can tack that on the end. */
1954 for (end = p; (ch2 = *end) != '\0' && !isspace (ch2) && ch2 != '|';
1960 switch (is_ctor_dtor (name))
1963 if (which_pass != PASS_LIB)
1964 add_to_list (&constructors, name);
1968 if (which_pass != PASS_LIB)
1969 add_to_list (&destructors, name);
1973 if (which_pass != PASS_LIB)
1974 fatal ("init function found in object %s", prog_name);
1975 #ifndef LD_INIT_SWITCH
1976 add_to_list (&constructors, name);
1981 if (which_pass != PASS_LIB)
1982 fatal ("fini function found in object %s", prog_name);
1983 #ifndef LD_FINI_SWITCH
1984 add_to_list (&destructors, name);
1988 default: /* not a constructor or destructor */
1993 fprintf (stderr, "\t%s\n", buf);
1997 fprintf (stderr, "\n");
1999 if (fclose (inf) != 0)
2000 fatal_perror ("fclose of pipe");
2002 do_wait (nm_file_name);
2004 signal (SIGINT, int_handler);
2006 signal (SIGQUIT, quit_handler);
2010 #if SUNOS4_SHARED_LIBRARIES
2012 /* Routines to scan the SunOS 4 _DYNAMIC structure to find shared libraries
2013 that the output file depends upon and their initialization/finalization
2014 routines, if any. */
2019 #include <sys/mman.h>
2020 #include <sys/param.h>
2022 #include <sys/dir.h>
2024 /* pointers to the object file */
2025 unsigned object; /* address of memory mapped file */
2026 unsigned objsize; /* size of memory mapped to file */
2027 char * code; /* pointer to code segment */
2028 char * data; /* pointer to data segment */
2029 struct nlist *symtab; /* pointer to symbol table */
2030 struct link_dynamic *ld;
2031 struct link_dynamic_2 *ld_2;
2032 struct head libraries;
2034 /* Map the file indicated by NAME into memory and store its address. */
2042 if ((fp = open (name, O_RDONLY)) == -1)
2043 fatal ("unable to open file '%s'", name);
2044 if (fstat (fp, &s) == -1)
2045 fatal ("unable to stat file '%s'", name);
2047 objsize = s.st_size;
2048 object = (unsigned) mmap (0, objsize, PROT_READ|PROT_WRITE, MAP_PRIVATE,
2051 fatal ("unable to mmap file '%s'", name);
2056 /* Helpers for locatelib. */
2058 static char *libname;
2064 return (strncmp (libname, d->d_name, strlen (libname)) == 0);
2067 /* If one file has an additional numeric extension past LIBNAME, then put
2068 that one first in the sort. If both files have additional numeric
2069 extensions, then put the one with the higher number first in the sort.
2071 We must verify that the extension is numeric, because Sun saves the
2072 original versions of patched libraries with a .FCS extension. Files with
2073 invalid extensions must go last in the sort, so that they won't be used. */
2077 struct direct **d1, **d2;
2079 int i1, i2 = strlen (libname);
2080 char *e1 = (*d1)->d_name + i2;
2081 char *e2 = (*d2)->d_name + i2;
2083 while (*e1 && *e2 && *e1 == '.' && *e2 == '.'
2084 && e1[1] && isdigit (e1[1]) && e2[1] && isdigit (e2[1]))
2088 i1 = strtol (e1, &e1, 10);
2089 i2 = strtol (e2, &e2, 10);
2096 /* It has a valid numeric extension, prefer this one. */
2097 if (*e1 == '.' && e1[1] && isdigit (e1[1]))
2099 /* It has a invalid numeric extension, must prefer the other one. */
2105 /* It has a valid numeric extension, prefer this one. */
2106 if (*e2 == '.' && e2[1] && isdigit (e2[1]))
2108 /* It has a invalid numeric extension, must prefer the other one. */
2116 /* Given the name NAME of a dynamic dependency, find its pathname and add
2117 it to the list of libraries. */
2125 char buf[MAXPATHLEN];
2133 /* counting elements in array, need 1 extra for null */
2135 ld_rules = (char *) (ld_2->ld_rules + code);
2139 for (; *ld_rules != 0; ld_rules++)
2140 if (*ld_rules == ':')
2142 ld_rules = (char *) (ld_2->ld_rules + code);
2143 ldr = (char *) malloc (strlen (ld_rules) + 1);
2144 strcpy (ldr, ld_rules);
2146 p = getenv ("LD_LIBRARY_PATH");
2151 for (q = p ; *q != 0; q++)
2154 q = (char *) malloc (strlen (p) + 1);
2157 l = (char **) malloc ((cnt + 3) * sizeof (char *));
2162 for (; *ldr != 0; ldr++)
2172 for (; *q != 0; q++)
2179 /* built in directories are /lib, /usr/lib, and /usr/local/lib */
2182 *pp++ = "/usr/local/lib";
2186 for (pp = l; *pp != 0 ; pp++)
2188 struct direct **namelist;
2190 if ((entries = scandir (*pp, &namelist, libselect, libcompare)) > 0)
2192 sprintf (buf, "%s/%s", *pp, namelist[entries - 1]->d_name);
2193 add_to_list (&libraries, buf);
2195 fprintf (stderr, "%s\n", buf);
2202 fprintf (stderr, "not found\n");
2204 fatal ("dynamic dependency %s not found", name);
2208 /* Scan the _DYNAMIC structure of the output file to find shared libraries
2209 that it depends upon and any constructors or destructors they contain. */
2212 scan_libraries (prog_name)
2215 struct exec *header;
2217 struct link_object *lo;
2218 char buff[MAXPATHLEN];
2221 mapfile (prog_name);
2222 header = (struct exec *)object;
2223 if (N_BADMAG (*header))
2224 fatal ("bad magic number in file '%s'", prog_name);
2225 if (header->a_dynamic == 0)
2228 code = (char *) (N_TXTOFF (*header) + (long) header);
2229 data = (char *) (N_DATOFF (*header) + (long) header);
2230 symtab = (struct nlist *) (N_SYMOFF (*header) + (long) header);
2232 if (header->a_magic == ZMAGIC && header->a_entry == 0x20)
2235 ld = (struct link_dynamic *) (symtab->n_value + code);
2241 ld = (struct link_dynamic *) data;
2246 fprintf (stderr, "dynamic dependencies.\n");
2248 ld_2 = (struct link_dynamic_2 *) ((long) ld->ld_un.ld_2 + (long)base);
2249 for (lo = (struct link_object *) ld_2->ld_need; lo;
2250 lo = (struct link_object *) lo->lo_next)
2253 lo = (struct link_object *) ((long) lo + code);
2254 name = (char *) (code + lo->lo_name);
2258 fprintf (stderr, "\t-l%s.%d => ", name, lo->lo_major);
2259 sprintf (buff, "lib%s.so.%d.%d", name, lo->lo_major, lo->lo_minor);
2265 fprintf (stderr, "\t%s\n", name);
2266 add_to_list (&libraries, name);
2271 fprintf (stderr, "\n");
2273 /* now iterate through the library list adding their symbols to
2275 for (list = libraries.first; list; list = list->next)
2276 scan_prog_file (list->name, PASS_LIB);
2279 #else /* SUNOS4_SHARED_LIBRARIES */
2282 /* Use the List Dynamic Dependencies program to find shared libraries that
2283 the output file depends upon and their initialization/finalization
2284 routines, if any. */
2287 scan_libraries (prog_name)
2290 static struct head libraries; /* list of shared libraries found */
2292 void (*int_handler) ();
2293 void (*quit_handler) ();
2301 /* If we don't have an `ldd', complain. */
2302 if (ldd_file_name == 0)
2304 error ("cannot find `ldd'");
2308 ldd_argv[argc++] = ldd_file_name;
2309 ldd_argv[argc++] = prog_name;
2310 ldd_argv[argc++] = (char *) 0;
2312 if (pipe (pipe_fd) < 0)
2313 fatal_perror ("pipe");
2315 inf = fdopen (pipe_fd[0], "r");
2316 if (inf == (FILE *) 0)
2317 fatal_perror ("fdopen");
2319 /* Trace if needed. */
2325 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2326 fprintf (stderr, " %s", str);
2328 fprintf (stderr, "\n");
2334 /* Spawn child ldd on pipe */
2339 fatal_perror ("fork");
2341 fatal_perror ("vfork");
2345 if (pid == 0) /* child context */
2348 if (dup2 (pipe_fd[1], 1) < 0)
2349 fatal_perror ("dup2 (%d, 1)", pipe_fd[1]);
2351 if (close (pipe_fd[0]) < 0)
2352 fatal_perror ("close (%d)", pipe_fd[0]);
2354 if (close (pipe_fd[1]) < 0)
2355 fatal_perror ("close (%d)", pipe_fd[1]);
2357 execv (ldd_file_name, ldd_argv);
2358 fatal_perror ("executing %s", ldd_file_name);
2361 /* Parent context from here on. */
2362 int_handler = (void (*) ()) signal (SIGINT, SIG_IGN);
2364 quit_handler = (void (*) ()) signal (SIGQUIT, SIG_IGN);
2367 if (close (pipe_fd[1]) < 0)
2368 fatal_perror ("close (%d)", pipe_fd[1]);
2371 fprintf (stderr, "\nldd output with constructors/destructors.\n");
2373 /* Read each line of ldd output. */
2374 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2377 char *name, *end, *p = buf;
2379 /* Extract names of libraries and add to list. */
2380 PARSE_LDD_OUTPUT (p);
2385 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2386 fatal ("dynamic dependency %s not found", buf);
2388 /* Find the end of the symbol name. */
2390 (ch2 = *end) != '\0' && ch2 != '\n' && !isspace (ch2) && ch2 != '|';
2395 if (access (name, R_OK) == 0)
2396 add_to_list (&libraries, name);
2398 fatal ("unable to open dynamic dependency '%s'", buf);
2401 fprintf (stderr, "\t%s\n", buf);
2404 fprintf (stderr, "\n");
2406 if (fclose (inf) != 0)
2407 fatal_perror ("fclose of pipe");
2409 do_wait (ldd_file_name);
2411 signal (SIGINT, int_handler);
2413 signal (SIGQUIT, quit_handler);
2416 /* now iterate through the library list adding their symbols to
2418 for (list = libraries.first; list; list = list->next)
2419 scan_prog_file (list->name, PASS_LIB);
2422 #endif /* LDD_SUFFIX */
2423 #endif /* SUNOS4_SHARED_LIBRARIES */
2425 #endif /* OBJECT_FORMAT_NONE */
2429 * COFF specific stuff.
2432 #ifdef OBJECT_FORMAT_COFF
2434 #if defined(EXTENDED_COFF)
2435 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2436 # define GCC_SYMENT SYMR
2437 # define GCC_OK_SYMBOL(X) ((X).st == stProc && (X).sc == scText)
2438 # define GCC_SYMINC(X) (1)
2439 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2440 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2442 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2443 # define GCC_SYMENT SYMENT
2444 # define GCC_OK_SYMBOL(X) \
2445 (((X).n_sclass == C_EXT) && \
2446 (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) || \
2447 ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT)))
2448 # define GCC_SYMINC(X) ((X).n_numaux+1)
2449 # define GCC_SYMZERO(X) 0
2450 # define GCC_CHECK_HDR(X) (1)
2453 extern char *ldgetname ();
2455 /* COFF version to scan the name list of the loaded program for
2456 the symbols g++ uses for static constructors and destructors.
2458 The constructor table begins at __CTOR_LIST__ and contains a count
2459 of the number of pointers (or -1 if the constructors are built in a
2460 separate section by the linker), followed by the pointers to the
2461 constructor functions, terminated with a null pointer. The
2462 destructor table has the same format, and begins at __DTOR_LIST__. */
2465 scan_prog_file (prog_name, which_pass)
2467 enum pass which_pass;
2469 LDFILE *ldptr = NULL;
2470 int sym_index, sym_count;
2472 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2475 if ((ldptr = ldopen (prog_name, ldptr)) == NULL)
2476 fatal ("%s: can't open as COFF file", prog_name);
2478 if (!MY_ISCOFF (HEADER (ldptr).f_magic))
2479 fatal ("%s: not a COFF file", prog_name);
2481 if (GCC_CHECK_HDR (ldptr))
2483 sym_count = GCC_SYMBOLS (ldptr);
2484 sym_index = GCC_SYMZERO (ldptr);
2485 while (sym_index < sym_count)
2489 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2491 sym_index += GCC_SYMINC (symbol);
2493 if (GCC_OK_SYMBOL (symbol))
2497 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2498 continue; /* should never happen */
2500 #ifdef XCOFF_DEBUGGING_INFO
2501 /* All AIX function names have a duplicate entry beginning
2507 switch (is_ctor_dtor (name))
2510 add_to_list (&constructors, name);
2511 if (which_pass == PASS_OBJ)
2512 add_to_list (&exports, name);
2516 add_to_list (&destructors, name);
2517 if (which_pass == PASS_OBJ)
2518 add_to_list (&exports, name);
2521 default: /* not a constructor or destructor */
2525 #if !defined(EXTENDED_COFF)
2527 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2528 symbol.n_scnum, symbol.n_sclass,
2529 (symbol.n_type ? "0" : ""), symbol.n_type,
2533 fprintf (stderr, "\tiss = %5d, value = %5d, index = %5d, name = %s\n",
2534 symbol.iss, symbol.value, symbol.index, name);
2540 (void) ldclose(ldptr);
2543 #ifdef XCOFF_SCAN_LIBS
2544 /* Scan imported AIX libraries for GCC static ctors and dtors.
2545 FIXME: it is possible to link an executable without the actual import
2546 library by using an "import file" - a text file listing symbols
2547 exported by a library. To support this, we would have to scan
2548 import files as well as actual shared binaries to find GCC ctors.
2549 TODO: use memory mapping instead of 'ld' routines, files are already
2550 memory mapped, but we could eliminate the extra in-memory copies.
2551 Is it worth the effort? */
2554 scan_libraries (prog_name)
2559 static struct path_prefix libpath; /* we should only do this once */
2561 if ((ldptr = ldopen (prog_name, ldptr)) == NULL)
2562 fatal ("%s: can't open as COFF file", prog_name);
2564 if (!MY_ISCOFF (HEADER (ldptr).f_magic))
2565 fatal ("%s: not a COFF file", prog_name);
2567 /* find and read loader section */
2568 if (ldnshread (ldptr, _LOADER, &ldsh))
2574 FSEEK (ldptr, ldsh.s_scnptr, BEGINNING);
2575 FREAD (&ldh, sizeof (ldh), 1, ldptr);
2576 /* read import library list */
2577 impbuf = alloca (ldh.l_istlen);
2578 FSEEK (ldptr, ldh.l_impoff + ldsh.s_scnptr, BEGINNING);
2579 FREAD (impbuf, ldh.l_istlen, 1, ldptr);
2582 fprintf (stderr, "LIBPATH=%s\n", impbuf);
2583 prefix_from_string (impbuf, &libpath);
2585 /* skip LIBPATH and empty base and member fields */
2586 impbuf += strlen (impbuf) + 3;
2587 for (entry = 1; entry < ldh.l_nimpid; ++entry)
2589 char *impath = impbuf;
2590 char *implib = impath + strlen (impath) + 1;
2591 char *impmem = implib + strlen (implib) + 1;
2592 char *soname = NULL;
2595 LDFILE *libptr = NULL;
2596 struct prefix_list *pl;
2599 impbuf = impmem + strlen (impmem) + 1;
2601 fprintf (stderr, "PATH+BASE=%s%s\n", impath, implib);
2602 /* Skip AIX kernel exports */
2603 if (*impath == '/' && *(impath+1) == '\0'
2604 && strcmp (implib, "unix") == 0)
2606 pathlen = strlen (impath);
2607 trial = alloca (MAX (pathlen + 1, libpath.max_len)
2608 + strlen (implib) + 1);
2611 strcpy (trial, impath);
2612 if (impath[pathlen - 1] != '/')
2613 trial[pathlen++] = '/';
2614 strcpy (trial + pathlen, implib);
2615 if (access (trial, R_OK) == 0)
2619 for (pl = libpath.plist; pl; pl = pl->next)
2621 strcpy (trial, pl->prefix);
2622 strcat (trial, implib);
2623 if (access (trial, R_OK) == 0)
2631 fatal ("%s: library not found", implib);
2634 fprintf (stderr, "%s (%s)\n", soname, impmem);
2636 fprintf (stderr, "%s\n", soname);
2640 /* scan imported shared objects for GCC GLOBAL ctors */
2642 if ((libptr = ldopen (soname, libptr)) == NULL)
2643 fatal ("%s: can't open import library", soname);
2644 if (TYPE (libptr) == ARTYPE)
2648 fatal ("%s: no archive member specified", soname);
2649 ldahread (libptr, &ah);
2650 if (strcmp (ah.ar_name, impmem))
2653 type = HEADER (libptr).f_magic;
2654 if (HEADER (libptr).f_flags & F_SHROBJ)
2661 if (!ldnshread (libptr, _LOADER, &soldsh))
2662 fatal ("%s: not an import library", soname);
2663 FSEEK (libptr, soldsh.s_scnptr, BEGINNING);
2664 if (FREAD (&soldh, sizeof (soldh), 1, libptr) != 1)
2665 fatal ("%s: can't read loader section", soname);
2666 /*fprintf (stderr, "\tscanning %s\n", soname);*/
2667 symcnt = soldh.l_nsyms;
2668 lsyms = (LDSYM *) alloca (symcnt * sizeof (*lsyms));
2669 symcnt = FREAD (lsyms, sizeof (*lsyms), symcnt, libptr);
2670 ldstrings = alloca (soldh.l_stlen);
2671 FSEEK (libptr, soldsh.s_scnptr+soldh.l_stoff, BEGINNING);
2672 FREAD (ldstrings, soldh.l_stlen, 1, libptr);
2673 for (i = 0; i < symcnt; ++i)
2675 LDSYM *l = lsyms + i;
2676 if (LDR_EXPORT (*l))
2680 expname = l->l_name;
2681 else if (l->l_offset < soldh.l_stlen)
2682 expname = ldstrings + l->l_offset;
2683 switch (is_ctor_dtor (expname))
2687 fprintf (stderr, "\t%s\n", expname);
2688 add_to_list (&constructors, expname);
2692 add_to_list (&destructors, expname);
2695 default: /* not a constructor or destructor */
2702 fprintf (stderr, "%s: type = %04X flags = %04X\n",
2703 ah.ar_name, type, HEADER (libptr).f_flags);
2705 while (ldclose (libptr) == FAILURE);
2706 /* printf (stderr, "closed %s\n", soname); */
2710 #endif /* XCOFF_SCAN_LIBS */
2712 #endif /* OBJECT_FORMAT_COFF */
2716 * OSF/rose specific stuff.
2719 #ifdef OBJECT_FORMAT_ROSE
2721 /* Union of the various load commands */
2723 typedef union load_union
2725 ldc_header_t hdr; /* common header */
2726 load_cmd_map_command_t map; /* map indexing other load cmds */
2727 interpreter_command_t iprtr; /* interpreter pathname */
2728 strings_command_t str; /* load commands strings section */
2729 region_command_t region; /* region load command */
2730 reloc_command_t reloc; /* relocation section */
2731 package_command_t pkg; /* package load command */
2732 symbols_command_t sym; /* symbol sections */
2733 entry_command_t ent; /* program start section */
2734 gen_info_command_t info; /* object information */
2735 func_table_command_t func; /* function constructors/destructors */
2738 /* Structure to point to load command and data section in memory. */
2740 typedef struct load_all
2742 load_union_t *load; /* load command */
2743 char *section; /* pointer to section */
2746 /* Structure to contain information about a file mapped into memory. */
2750 char *start; /* start of map */
2751 char *name; /* filename */
2752 long size; /* size of the file */
2753 long rounded_size; /* size rounded to page boundary */
2754 int fd; /* file descriptor */
2755 int rw; /* != 0 if opened read/write */
2756 int use_mmap; /* != 0 if mmap'ed */
2759 extern int decode_mach_o_hdr ();
2760 extern int encode_mach_o_hdr ();
2762 static void add_func_table PROTO((mo_header_t *, load_all_t *,
2763 symbol_info_t *, int));
2764 static void print_header PROTO((mo_header_t *));
2765 static void print_load_command PROTO((load_union_t *, size_t, int));
2766 static void bad_header PROTO((int));
2767 static struct file_info *read_file PROTO((char *, int, int));
2768 static void end_file PROTO((struct file_info *));
2770 /* OSF/rose specific version to scan the name list of the loaded
2771 program for the symbols g++ uses for static constructors and
2774 The constructor table begins at __CTOR_LIST__ and contains a count
2775 of the number of pointers (or -1 if the constructors are built in a
2776 separate section by the linker), followed by the pointers to the
2777 constructor functions, terminated with a null pointer. The
2778 destructor table has the same format, and begins at __DTOR_LIST__. */
2781 scan_prog_file (prog_name, which_pass)
2783 enum pass which_pass;
2787 load_all_t *load_array;
2788 load_all_t *load_end;
2789 load_all_t *load_cmd;
2790 int symbol_load_cmds;
2796 struct file_info *obj_file;
2798 mo_lcid_t cmd_strings = -1;
2799 symbol_info_t *main_sym = 0;
2800 int rw = (which_pass != PASS_FIRST);
2802 prog_fd = open (prog_name, (rw) ? O_RDWR : O_RDONLY);
2804 fatal_perror ("can't read %s", prog_name);
2806 obj_file = read_file (prog_name, prog_fd, rw);
2807 obj = obj_file->start;
2809 status = decode_mach_o_hdr (obj, MO_SIZEOF_RAW_HDR, MOH_HEADER_VERSION, &hdr);
2810 if (status != MO_HDR_CONV_SUCCESS)
2811 bad_header (status);
2814 /* Do some basic sanity checks. Note we explicitly use the big endian magic number,
2815 since the hardware will automatically swap bytes for us on loading little endian
2818 #ifndef CROSS_COMPILE
2819 if (hdr.moh_magic != MOH_MAGIC_MSB
2820 || hdr.moh_header_version != MOH_HEADER_VERSION
2821 || hdr.moh_byte_order != OUR_BYTE_ORDER
2822 || hdr.moh_data_rep_id != OUR_DATA_REP_ID
2823 || hdr.moh_cpu_type != OUR_CPU_TYPE
2824 || hdr.moh_cpu_subtype != OUR_CPU_SUBTYPE
2825 || hdr.moh_vendor_type != OUR_VENDOR_TYPE)
2827 fatal ("incompatibilities between object file & expected values");
2832 print_header (&hdr);
2834 offset = hdr.moh_first_cmd_off;
2835 load_end = load_array
2836 = (load_all_t *) xcalloc (sizeof (load_all_t), hdr.moh_n_load_cmds + 2);
2838 /* Build array of load commands, calculating the offsets */
2839 for (i = 0; i < hdr.moh_n_load_cmds; i++)
2841 load_union_t *load_hdr; /* load command header */
2843 load_cmd = load_end++;
2844 load_hdr = (load_union_t *) (obj + offset);
2846 /* If modifying the program file, copy the header. */
2849 load_union_t *ptr = (load_union_t *) xmalloc (load_hdr->hdr.ldci_cmd_size);
2850 bcopy ((char *)load_hdr, (char *)ptr, load_hdr->hdr.ldci_cmd_size);
2853 /* null out old command map, because we will rewrite at the end. */
2854 if (ptr->hdr.ldci_cmd_type == LDC_CMD_MAP)
2856 cmd_strings = ptr->map.lcm_ld_cmd_strings;
2857 ptr->hdr.ldci_cmd_type = LDC_UNDEFINED;
2861 load_cmd->load = load_hdr;
2862 if (load_hdr->hdr.ldci_section_off > 0)
2863 load_cmd->section = obj + load_hdr->hdr.ldci_section_off;
2866 print_load_command (load_hdr, offset, i);
2868 offset += load_hdr->hdr.ldci_cmd_size;
2871 /* If the last command is the load command map and is not undefined,
2872 decrement the count of load commands. */
2873 if (rw && load_end[-1].load->hdr.ldci_cmd_type == LDC_UNDEFINED)
2876 hdr.moh_n_load_cmds--;
2879 /* Go through and process each symbol table section. */
2880 symbol_load_cmds = 0;
2881 for (load_cmd = load_array; load_cmd < load_end; load_cmd++)
2883 load_union_t *load_hdr = load_cmd->load;
2885 if (load_hdr->hdr.ldci_cmd_type == LDC_SYMBOLS)
2891 char *kind = "unknown";
2893 switch (load_hdr->sym.symc_kind)
2895 case SYMC_IMPORTS: kind = "imports"; break;
2896 case SYMC_DEFINED_SYMBOLS: kind = "defined"; break;
2897 case SYMC_STABS: kind = "stabs"; break;
2900 fprintf (stderr, "\nProcessing symbol table #%d, offset = 0x%.8lx, kind = %s\n",
2901 symbol_load_cmds, load_hdr->hdr.ldci_section_off, kind);
2904 if (load_hdr->sym.symc_kind != SYMC_DEFINED_SYMBOLS)
2907 str_sect = load_array[load_hdr->sym.symc_strings_section].section;
2908 if (str_sect == (char *) 0)
2909 fatal ("string section missing");
2911 if (load_cmd->section == (char *) 0)
2912 fatal ("section pointer missing");
2914 num_syms = load_hdr->sym.symc_nentries;
2915 for (i = 0; i < num_syms; i++)
2917 symbol_info_t *sym = ((symbol_info_t *) load_cmd->section) + i;
2918 char *name = sym->si_name.symbol_name + str_sect;
2925 char *n = name + strlen (name) - strlen (NAME__MAIN);
2927 if ((n - name) < 0 || strcmp (n, NAME__MAIN))
2937 switch (is_ctor_dtor (name))
2940 add_to_list (&constructors, name);
2944 add_to_list (&destructors, name);
2947 default: /* not a constructor or destructor */
2953 fprintf (stderr, "\ttype = 0x%.4x, sc = 0x%.2x, flags = 0x%.8x, name = %.30s\n",
2954 sym->si_type, sym->si_sc_type, sym->si_flags, name);
2959 if (symbol_load_cmds == 0)
2960 fatal ("no symbol table found");
2962 /* Update the program file now, rewrite header and load commands. At present,
2963 we assume that there is enough space after the last load command to insert
2964 one more. Since the first section written out is page aligned, and the
2965 number of load commands is small, this is ok for the present. */
2969 load_union_t *load_map;
2972 if (cmd_strings == -1)
2973 fatal ("no cmd_strings found");
2975 /* Add __main to initializer list.
2976 If we are building a program instead of a shared library, don't
2977 do anything, since in the current version, you cannot do mallocs
2978 and such in the constructors. */
2980 if (main_sym != (symbol_info_t *) 0
2981 && ((hdr.moh_flags & MOH_EXECABLE_F) == 0))
2982 add_func_table (&hdr, load_array, main_sym, FNTC_INITIALIZATION);
2985 fprintf (stderr, "\nUpdating header and load commands.\n\n");
2987 hdr.moh_n_load_cmds++;
2988 size = sizeof (load_cmd_map_command_t) + (sizeof (mo_offset_t) * (hdr.moh_n_load_cmds - 1));
2990 /* Create new load command map. */
2992 fprintf (stderr, "load command map, %d cmds, new size %ld.\n",
2993 (int)hdr.moh_n_load_cmds, (long)size);
2995 load_map = (load_union_t *) xcalloc (1, size);
2996 load_map->map.ldc_header.ldci_cmd_type = LDC_CMD_MAP;
2997 load_map->map.ldc_header.ldci_cmd_size = size;
2998 load_map->map.lcm_ld_cmd_strings = cmd_strings;
2999 load_map->map.lcm_nentries = hdr.moh_n_load_cmds;
3000 load_array[hdr.moh_n_load_cmds-1].load = load_map;
3002 offset = hdr.moh_first_cmd_off;
3003 for (i = 0; i < hdr.moh_n_load_cmds; i++)
3005 load_map->map.lcm_map[i] = offset;
3006 if (load_array[i].load->hdr.ldci_cmd_type == LDC_CMD_MAP)
3007 hdr.moh_load_map_cmd_off = offset;
3009 offset += load_array[i].load->hdr.ldci_cmd_size;
3012 hdr.moh_sizeofcmds = offset - MO_SIZEOF_RAW_HDR;
3015 print_header (&hdr);
3018 status = encode_mach_o_hdr (&hdr, obj, MO_SIZEOF_RAW_HDR);
3019 if (status != MO_HDR_CONV_SUCCESS)
3020 bad_header (status);
3023 fprintf (stderr, "writing load commands.\n\n");
3025 /* Write load commands */
3026 offset = hdr.moh_first_cmd_off;
3027 for (i = 0; i < hdr.moh_n_load_cmds; i++)
3029 load_union_t *load_hdr = load_array[i].load;
3030 size_t size = load_hdr->hdr.ldci_cmd_size;
3033 print_load_command (load_hdr, offset, i);
3035 bcopy ((char *) load_hdr, (char *) (obj + offset), size);
3040 end_file (obj_file);
3042 if (close (prog_fd))
3043 fatal_perror ("closing %s", prog_name);
3046 fprintf (stderr, "\n");
3050 /* Add a function table to the load commands to call a function
3051 on initiation or termination of the process. */
3054 add_func_table (hdr_p, load_array, sym, type)
3055 mo_header_t *hdr_p; /* pointer to global header */
3056 load_all_t *load_array; /* array of ptrs to load cmds */
3057 symbol_info_t *sym; /* pointer to symbol entry */
3058 int type; /* fntc_type value */
3060 /* Add a new load command. */
3061 int num_cmds = ++hdr_p->moh_n_load_cmds;
3062 int load_index = num_cmds - 1;
3063 size_t size = sizeof (func_table_command_t) + sizeof (mo_addr_t);
3064 load_union_t *ptr = xcalloc (1, size);
3065 load_all_t *load_cmd;
3068 /* Set the unresolved address bit in the header to force the loader to be
3069 used, since kernel exec does not call the initialization functions. */
3070 hdr_p->moh_flags |= MOH_UNRESOLVED_F;
3072 load_cmd = &load_array[load_index];
3073 load_cmd->load = ptr;
3074 load_cmd->section = (char *) 0;
3076 /* Fill in func table load command. */
3077 ptr->func.ldc_header.ldci_cmd_type = LDC_FUNC_TABLE;
3078 ptr->func.ldc_header.ldci_cmd_size = size;
3079 ptr->func.ldc_header.ldci_section_off = 0;
3080 ptr->func.ldc_header.ldci_section_len = 0;
3081 ptr->func.fntc_type = type;
3082 ptr->func.fntc_nentries = 1;
3084 /* copy address, turn it from abs. address to (region,offset) if necessary. */
3085 /* Is the symbol already expressed as (region, offset)? */
3086 if ((sym->si_flags & SI_ABSOLUTE_VALUE_F) == 0)
3088 ptr->func.fntc_entry_loc[i].adr_lcid = sym->si_value.def_val.adr_lcid;
3089 ptr->func.fntc_entry_loc[i].adr_sctoff = sym->si_value.def_val.adr_sctoff;
3092 /* If not, figure out which region it's in. */
3095 mo_vm_addr_t addr = sym->si_value.abs_val;
3098 for (i = 0; i < load_index; i++)
3100 if (load_array[i].load->hdr.ldci_cmd_type == LDC_REGION)
3102 region_command_t *region_ptr = &load_array[i].load->region;
3104 if ((region_ptr->regc_flags & REG_ABS_ADDR_F) != 0
3105 && addr >= region_ptr->regc_addr.vm_addr
3106 && addr <= region_ptr->regc_addr.vm_addr + region_ptr->regc_vm_size)
3108 ptr->func.fntc_entry_loc[0].adr_lcid = i;
3109 ptr->func.fntc_entry_loc[0].adr_sctoff = addr - region_ptr->regc_addr.vm_addr;
3117 fatal ("could not convert 0x%l.8x into a region", addr);
3122 "%s function, region %d, offset = %ld (0x%.8lx)\n",
3123 (type == FNTC_INITIALIZATION) ? "init" : "term",
3124 (int)ptr->func.fntc_entry_loc[i].adr_lcid,
3125 (long)ptr->func.fntc_entry_loc[i].adr_sctoff,
3126 (long)ptr->func.fntc_entry_loc[i].adr_sctoff);
3131 /* Print the global header for an OSF/rose object. */
3134 print_header (hdr_ptr)
3135 mo_header_t *hdr_ptr;
3137 fprintf (stderr, "\nglobal header:\n");
3138 fprintf (stderr, "\tmoh_magic = 0x%.8lx\n", hdr_ptr->moh_magic);
3139 fprintf (stderr, "\tmoh_major_version = %d\n", (int)hdr_ptr->moh_major_version);
3140 fprintf (stderr, "\tmoh_minor_version = %d\n", (int)hdr_ptr->moh_minor_version);
3141 fprintf (stderr, "\tmoh_header_version = %d\n", (int)hdr_ptr->moh_header_version);
3142 fprintf (stderr, "\tmoh_max_page_size = %d\n", (int)hdr_ptr->moh_max_page_size);
3143 fprintf (stderr, "\tmoh_byte_order = %d\n", (int)hdr_ptr->moh_byte_order);
3144 fprintf (stderr, "\tmoh_data_rep_id = %d\n", (int)hdr_ptr->moh_data_rep_id);
3145 fprintf (stderr, "\tmoh_cpu_type = %d\n", (int)hdr_ptr->moh_cpu_type);
3146 fprintf (stderr, "\tmoh_cpu_subtype = %d\n", (int)hdr_ptr->moh_cpu_subtype);
3147 fprintf (stderr, "\tmoh_vendor_type = %d\n", (int)hdr_ptr->moh_vendor_type);
3148 fprintf (stderr, "\tmoh_load_map_cmd_off = %d\n", (int)hdr_ptr->moh_load_map_cmd_off);
3149 fprintf (stderr, "\tmoh_first_cmd_off = %d\n", (int)hdr_ptr->moh_first_cmd_off);
3150 fprintf (stderr, "\tmoh_sizeofcmds = %d\n", (int)hdr_ptr->moh_sizeofcmds);
3151 fprintf (stderr, "\tmon_n_load_cmds = %d\n", (int)hdr_ptr->moh_n_load_cmds);
3152 fprintf (stderr, "\tmoh_flags = 0x%.8lx", (long)hdr_ptr->moh_flags);
3154 if (hdr_ptr->moh_flags & MOH_RELOCATABLE_F)
3155 fprintf (stderr, ", relocatable");
3157 if (hdr_ptr->moh_flags & MOH_LINKABLE_F)
3158 fprintf (stderr, ", linkable");
3160 if (hdr_ptr->moh_flags & MOH_EXECABLE_F)
3161 fprintf (stderr, ", execable");
3163 if (hdr_ptr->moh_flags & MOH_EXECUTABLE_F)
3164 fprintf (stderr, ", executable");
3166 if (hdr_ptr->moh_flags & MOH_UNRESOLVED_F)
3167 fprintf (stderr, ", unresolved");
3169 fprintf (stderr, "\n\n");
3174 /* Print a short summary of a load command. */
3177 print_load_command (load_hdr, offset, number)
3178 load_union_t *load_hdr;
3182 mo_long_t type = load_hdr->hdr.ldci_cmd_type;
3183 char *type_str = (char *) 0;
3187 case LDC_UNDEFINED: type_str = "UNDEFINED"; break;
3188 case LDC_CMD_MAP: type_str = "CMD_MAP"; break;
3189 case LDC_INTERPRETER: type_str = "INTERPRETER"; break;
3190 case LDC_STRINGS: type_str = "STRINGS"; break;
3191 case LDC_REGION: type_str = "REGION"; break;
3192 case LDC_RELOC: type_str = "RELOC"; break;
3193 case LDC_PACKAGE: type_str = "PACKAGE"; break;
3194 case LDC_SYMBOLS: type_str = "SYMBOLS"; break;
3195 case LDC_ENTRY: type_str = "ENTRY"; break;
3196 case LDC_FUNC_TABLE: type_str = "FUNC_TABLE"; break;
3197 case LDC_GEN_INFO: type_str = "GEN_INFO"; break;
3201 "cmd %2d, sz: 0x%.2lx, coff: 0x%.3lx, doff: 0x%.6lx, dlen: 0x%.6lx",
3203 (long) load_hdr->hdr.ldci_cmd_size,
3205 (long) load_hdr->hdr.ldci_section_off,
3206 (long) load_hdr->hdr.ldci_section_len);
3208 if (type_str == (char *) 0)
3209 fprintf (stderr, ", ty: unknown (%ld)\n", (long) type);
3211 else if (type != LDC_REGION)
3212 fprintf (stderr, ", ty: %s\n", type_str);
3217 switch (load_hdr->region.regc_usage_type)
3219 case REG_TEXT_T: region = ", .text"; break;
3220 case REG_DATA_T: region = ", .data"; break;
3221 case REG_BSS_T: region = ", .bss"; break;
3222 case REG_GLUE_T: region = ", .glue"; break;
3223 #if defined (REG_RDATA_T) && defined (REG_SDATA_T) && defined (REG_SBSS_T) /*mips*/
3224 case REG_RDATA_T: region = ", .rdata"; break;
3225 case REG_SDATA_T: region = ", .sdata"; break;
3226 case REG_SBSS_T: region = ", .sbss"; break;
3230 fprintf (stderr, ", ty: %s, vaddr: 0x%.8lx, vlen: 0x%.6lx%s\n",
3232 (long) load_hdr->region.regc_vm_addr,
3233 (long) load_hdr->region.regc_vm_size,
3241 /* Fatal error when {en,de}code_mach_o_header fails. */
3247 char *msg = (char *) 0;
3251 case MO_ERROR_BAD_MAGIC: msg = "bad magic number"; break;
3252 case MO_ERROR_BAD_HDR_VERS: msg = "bad header version"; break;
3253 case MO_ERROR_BAD_RAW_HDR_VERS: msg = "bad raw header version"; break;
3254 case MO_ERROR_BUF2SML: msg = "raw header buffer too small"; break;
3255 case MO_ERROR_OLD_RAW_HDR_FILE: msg = "old raw header file"; break;
3256 case MO_ERROR_UNSUPPORTED_VERS: msg = "unsupported version"; break;
3259 if (msg == (char *) 0)
3260 fatal ("unknown {de,en}code_mach_o_hdr return value %d", status);
3266 /* Read a file into a memory buffer. */
3268 static struct file_info *
3269 read_file (name, fd, rw)
3270 char *name; /* filename */
3271 int fd; /* file descriptor */
3272 int rw; /* read/write */
3274 struct stat stat_pkt;
3275 struct file_info *p = (struct file_info *) xcalloc (sizeof (struct file_info), 1);
3277 static int page_size;
3280 if (fstat (fd, &stat_pkt) < 0)
3281 fatal_perror ("fstat %s", name);
3284 p->size = stat_pkt.st_size;
3285 p->rounded_size = stat_pkt.st_size;
3291 fprintf (stderr, "mmap %s, %s\n", name, (rw) ? "read/write" : "read-only");
3294 page_size = sysconf (_SC_PAGE_SIZE);
3296 p->rounded_size = ((p->size + page_size - 1) / page_size) * page_size;
3297 p->start = mmap ((caddr_t) 0,
3298 (rw) ? p->rounded_size : p->size,
3299 (rw) ? (PROT_READ | PROT_WRITE) : PROT_READ,
3300 MAP_FILE | MAP_VARIABLE | MAP_SHARED,
3304 if (p->start != (char *) 0 && p->start != (char *) -1)
3308 #endif /* USE_MMAP */
3313 fprintf (stderr, "read %s\n", name);
3316 p->start = xmalloc (p->size);
3317 if (lseek (fd, 0L, SEEK_SET) < 0)
3318 fatal_perror ("lseek to 0 on %s", name);
3320 len = read (fd, p->start, p->size);
3322 fatal_perror ("read %s", name);
3325 fatal ("read %ld bytes, expected %ld, from %s", len, p->size, name);
3331 /* Do anything necessary to write a file back from memory. */
3335 struct file_info *ptr; /* file information block */
3343 fprintf (stderr, "msync %s\n", ptr->name);
3345 if (msync (ptr->start, ptr->rounded_size, MS_ASYNC))
3346 fatal_perror ("msync %s", ptr->name);
3350 fprintf (stderr, "munmap %s\n", ptr->name);
3352 if (munmap (ptr->start, ptr->size))
3353 fatal_perror ("munmap %s", ptr->name);
3356 #endif /* USE_MMAP */
3363 fprintf (stderr, "write %s\n", ptr->name);
3365 if (lseek (ptr->fd, 0L, SEEK_SET) < 0)
3366 fatal_perror ("lseek to 0 on %s", ptr->name);
3368 len = write (ptr->fd, ptr->start, ptr->size);
3370 fatal_perror ("write %s", ptr->name);
3372 if (len != ptr->size)
3373 fatal ("wrote %ld bytes, expected %ld, to %s", len, ptr->size, ptr->name);
3382 #endif /* OBJECT_FORMAT_ROSE */