1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 93-97, 1998 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>
57 #include "gansidecl.h"
64 extern char *sys_errlist[];
70 /* Obstack allocation and deallocation routines. */
71 #define obstack_chunk_alloc xmalloc
72 #define obstack_chunk_free free
85 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
88 #define WTERMSIG(S) ((S) & 0x7f)
91 #define WIFEXITED(S) (((S) & 0xff) == 0)
94 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
97 extern char *choose_temp_base ();
99 /* On certain systems, we have code that works by scanning the object file
100 directly. But this code uses system-specific header files and library
101 functions, so turn it off in a cross-compiler. Likewise, the names of
102 the utilities aren't correct for a cross-compiler; we have to hope that
103 cross-versions are in the proper directories. */
106 #undef SUNOS4_SHARED_LIBRARIES
107 #undef OBJECT_FORMAT_COFF
108 #undef OBJECT_FORMAT_ROSE
109 #undef MD_EXEC_PREFIX
110 #undef REAL_LD_FILE_NAME
111 #undef REAL_NM_FILE_NAME
112 #undef REAL_STRIP_FILE_NAME
115 /* If we can't use a special method, use the ordinary one:
116 run nm to find what symbols are present.
117 In a cross-compiler, this means you need a cross nm,
118 but that isn't quite as unpleasant as special headers. */
120 #if !defined (OBJECT_FORMAT_COFF) && !defined (OBJECT_FORMAT_ROSE)
121 #define OBJECT_FORMAT_NONE
124 #ifdef OBJECT_FORMAT_COFF
133 /* Many versions of ldfcn.h define these. */
141 /* Some systems have an ISCOFF macro, but others do not. In some cases
142 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
143 that either do not have an ISCOFF macro in /usr/include or for those
144 where it is wrong. */
147 #define MY_ISCOFF(X) ISCOFF (X)
150 #ifdef XCOFF_DEBUGGING_INFO
151 #define XCOFF_SCAN_LIBS
154 #endif /* OBJECT_FORMAT_COFF */
156 #ifdef OBJECT_FORMAT_ROSE
163 #include <sys/mman.h>
167 #include <mach_o_format.h>
168 #include <mach_o_header.h>
169 #include <mach_o_vals.h>
170 #include <mach_o_types.h>
172 #endif /* OBJECT_FORMAT_ROSE */
174 #ifdef OBJECT_FORMAT_NONE
176 /* Default flags to pass to nm. */
178 #define NM_FLAGS "-p"
181 #endif /* OBJECT_FORMAT_NONE */
183 /* Some systems use __main in a way incompatible with its use in gcc, in these
184 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
185 give the same symbol without quotes for an alternative entry point. You
186 must define both, or neither. */
188 #define NAME__MAIN "__main"
189 #define SYMBOL__MAIN __main
192 #if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES || defined(XCOFF_SCAN_LIBS)
193 #define SCAN_LIBRARIES
197 int do_collecting = 1;
199 int do_collecting = 0;
202 /* Linked lists of constructor and destructor names. */
218 /* Enumeration giving which pass this is for scanning the program file. */
221 PASS_FIRST, /* without constructors */
222 PASS_OBJ, /* individual objects */
223 PASS_LIB, /* looking for shared libraries */
224 PASS_SECOND /* with constructors linked in */
227 #ifndef NO_SYS_SIGLIST
228 #ifndef SYS_SIGLIST_DECLARED
229 extern char *sys_siglist[];
232 extern char *version_string;
234 int vflag; /* true if -v */
235 static int rflag; /* true if -r */
236 static int strip_flag; /* true if -s */
238 int debug; /* true if -debug */
240 static int shared_obj; /* true if -shared */
242 static int temp_filename_length; /* Length of temp_filename */
243 static char *temp_filename; /* Base of temp filenames */
244 static char *c_file; /* <xxx>.c for constructor/destructor list. */
245 static char *o_file; /* <xxx>.o for constructor/destructor list. */
246 static char *export_file; /* <xxx>.x for AIX export list. */
247 char *ldout; /* File for ld errors. */
248 static char *output_file; /* Output file for ld. */
249 static char *nm_file_name; /* pathname of nm */
250 static char *ldd_file_name; /* pathname of ldd (or equivalent) */
251 static char *strip_file_name; /* pathname of strip */
252 char *c_file_name; /* pathname of gcc */
253 static char *initname, *fininame; /* names of init and fini funcs */
255 static struct head constructors; /* list of constructors found */
256 static struct head destructors; /* list of destructors found */
257 static struct head exports; /* list of exported symbols */
258 static struct head frame_tables; /* list of frame unwind info tables */
260 struct obstack temporary_obstack;
261 struct obstack permanent_obstack;
262 char * temporary_firstobj;
264 /* Defined in the automatically-generated underscore.c. */
265 extern int prepends_underscore;
267 extern char *getenv ();
268 extern char *mktemp ();
269 extern FILE *fdopen ();
271 /* Structure to hold all the directories in which to search for files to
276 char *prefix; /* String to prepend to the path. */
277 struct prefix_list *next; /* Next in linked list. */
282 struct prefix_list *plist; /* List of prefixes to try */
283 int max_len; /* Max length of a prefix in PLIST */
284 char *name; /* Name of this list (used in config stuff) */
287 void collect_exit PROTO((int));
288 void collect_execute PROTO((char *, char **, char *));
289 void dump_file PROTO((char *));
290 static void handler PROTO((int));
291 static int is_ctor_dtor PROTO((char *));
292 static int is_in_prefix_list PROTO((struct path_prefix *, char *, int));
293 static char *find_a_file PROTO((struct path_prefix *, char *));
294 static void add_prefix PROTO((struct path_prefix *, char *));
295 static void prefix_from_env PROTO((char *, struct path_prefix *));
296 static void prefix_from_string PROTO((char *, struct path_prefix *));
297 static void do_wait PROTO((char *));
298 static void fork_execute PROTO((char *, char **));
299 static void maybe_unlink PROTO((char *));
300 static void add_to_list PROTO((struct head *, char *));
301 static void write_list PROTO((FILE *, char *, struct id *));
302 static void write_list_with_asm PROTO((FILE *, char *, struct id *));
303 static void write_c_file PROTO((FILE *, char *));
304 static void write_export_file PROTO((FILE *));
305 static void scan_prog_file PROTO((char *, enum pass));
306 #ifdef SCAN_LIBRARIES
307 static void scan_libraries PROTO((char *));
313 #ifdef NEED_DECLARATION_INDEX
314 extern char *index ();
317 #ifdef NEED_DECLARATION_RINDEX
318 extern char *rindex ();
321 #ifdef NEED_DECLARATION_FREE
338 while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
341 close (fdtmp[--fdx]);
357 static char buffer[30];
361 if (e > 0 && e < sys_nerr)
362 return sys_errlist[e];
364 sprintf (buffer, "Unknown error %d", e);
369 /* Delete tempfiles and exit function. */
372 collect_exit (status)
375 if (c_file != 0 && c_file[0])
376 maybe_unlink (c_file);
378 if (o_file != 0 && o_file[0])
379 maybe_unlink (o_file);
381 if (export_file != 0 && export_file[0])
382 maybe_unlink (export_file);
384 if (ldout != 0 && ldout[0])
387 maybe_unlink (ldout);
390 if (status != 0 && output_file != 0 && output_file[0])
391 maybe_unlink (output_file);
397 /* Die when sys call fails. */
400 fatal_perror (string, arg1, arg2, arg3)
401 char *string, *arg1, *arg2, *arg3;
405 fprintf (stderr, "collect2: ");
406 fprintf (stderr, string, arg1, arg2, arg3);
407 fprintf (stderr, ": %s\n", my_strerror (e));
408 collect_exit (FATAL_EXIT_CODE);
414 fatal (string, arg1, arg2, arg3)
415 char *string, *arg1, *arg2, *arg3;
417 fprintf (stderr, "collect2: ");
418 fprintf (stderr, string, arg1, arg2, arg3);
419 fprintf (stderr, "\n");
420 collect_exit (FATAL_EXIT_CODE);
423 /* Write error message. */
426 error (string, arg1, arg2, arg3, arg4)
427 char *string, *arg1, *arg2, *arg3, *arg4;
429 fprintf (stderr, "collect2: ");
430 fprintf (stderr, string, arg1, arg2, arg3, arg4);
431 fprintf (stderr, "\n");
434 /* In case obstack is linked in, and abort is defined to fancy_abort,
435 provide a default entry. */
440 fatal ("internal error");
448 if (c_file != 0 && c_file[0])
449 maybe_unlink (c_file);
451 if (o_file != 0 && o_file[0])
452 maybe_unlink (o_file);
454 if (ldout != 0 && ldout[0])
455 maybe_unlink (ldout);
457 if (export_file != 0 && export_file[0])
458 maybe_unlink (export_file);
460 signal (signo, SIG_DFL);
461 kill (getpid (), signo);
466 xcalloc (size1, size2)
469 char *ptr = (char *) calloc (size1, size2);
473 fatal ("out of memory");
481 char *ptr = (char *) malloc (size);
485 fatal ("out of memory");
494 register char *value = (char *) realloc (ptr, size);
496 fatal ("virtual memory exhausted");
504 return access (name, R_OK) == 0;
507 /* Make a copy of a string INPUT with size SIZE. */
510 savestring (input, size)
514 char *output = (char *) xmalloc (size + 1);
515 bcopy (input, output, size);
520 /* Parse a reasonable subset of shell quoting syntax. */
537 obstack_1grow (&temporary_obstack, c);
538 else if (! inside && c == ' ')
540 else if (! inside && c == '\\')
545 obstack_1grow (&temporary_obstack, c);
548 obstack_1grow (&temporary_obstack, '\0');
550 return obstack_finish (&temporary_obstack);
557 FILE *stream = fopen (name, "r");
558 int no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
565 while (c = getc (stream),
566 c != EOF && (isalnum (c) || c == '_' || c == '$' || c == '.'))
567 obstack_1grow (&temporary_obstack, c);
568 if (obstack_object_size (&temporary_obstack) > 0)
570 char *word, *p, *result;
571 obstack_1grow (&temporary_obstack, '\0');
572 word = obstack_finish (&temporary_obstack);
575 ++word, putc ('.', stderr);
577 if (*p == '_' && prepends_underscore)
583 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI);
588 fputs (result, stderr);
590 diff = strlen (word) - strlen (result);
592 --diff, putc (' ', stderr);
593 while (diff < 0 && c == ' ')
594 ++diff, c = getc (stream);
599 fputs (word, stderr);
602 obstack_free (&temporary_obstack, temporary_firstobj);
611 /* Decide whether the given symbol is:
612 a constructor (1), a destructor (2), or neither (0). */
618 struct names { char *name; int len; int ret; int two_underscores; };
620 register struct names *p;
622 register char *orig_s = s;
624 static struct names special[] = {
625 #ifdef NO_DOLLAR_IN_LABEL
626 #ifdef NO_DOT_IN_LABEL
627 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
628 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
629 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
631 { "GLOBAL_.I.", sizeof ("GLOBAL_.I.")-1, 1, 0 },
632 { "GLOBAL_.D.", sizeof ("GLOBAL_.D.")-1, 2, 0 },
633 { "GLOBAL_.F.", sizeof ("GLOBAL_.F.")-1, 5, 0 },
636 { "GLOBAL_$I$", sizeof ("GLOBAL_$I$")-1, 1, 0 },
637 { "GLOBAL_$D$", sizeof ("GLOBAL_$D$")-1, 2, 0 },
638 { "GLOBAL_$F$", sizeof ("GLOBAL_$F$")-1, 5, 0 },
640 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
641 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
642 #ifdef CFRONT_LOSSAGE /* Don't collect cfront initialization functions.
643 cfront has its own linker procedure to collect them;
644 if collect2 gets them too, they get collected twice
645 when the cfront procedure is run and the compiler used
646 for linking happens to be GCC. */
647 { "sti__", sizeof ("sti__")-1, 1, 1 },
648 { "std__", sizeof ("std__")-1, 2, 1 },
649 #endif /* CFRONT_LOSSAGE */
653 while ((ch = *s) == '_')
659 for (p = &special[0]; p->len > 0; p++)
662 && (!p->two_underscores || ((s - orig_s) >= 2))
663 && strncmp(s, p->name, p->len) == 0)
671 /* Routine to add variables to the environment. */
679 #ifndef VMS /* nor about VMS */
681 extern char **environ;
682 char **old_environ = environ;
689 while ((ch = *p++) != '\0' && ch != '=')
695 /* Search for replacing an existing environment variable, and
696 count the number of total environment variables. */
697 for (envp = old_environ; *envp; envp++)
700 if (!strncmp (str, *envp, name_len))
707 /* Add a new environment variable */
708 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
710 bcopy ((char *) old_environ, (char *) (environ + 1),
711 sizeof (char *) * (num_envs+1));
717 #endif /* HAVE_PUTENV */
719 /* By default, colon separates directories in a path. */
720 #ifndef PATH_SEPARATOR
721 #define PATH_SEPARATOR ':'
724 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
725 and one from the PATH variable. */
727 static struct path_prefix cpath, path;
730 /* This is the name of the target machine. We use it to form the name
731 of the files to execute. */
733 static char *target_machine = TARGET_MACHINE;
736 /* Names under which we were executed. Never return one of those files in our
739 static struct path_prefix our_file_names;
741 /* Determine if STRING is in PPREFIX.
743 This utility is currently only used to look up file names. Prefix lists
744 record directory names. This matters to us because the latter has a
745 trailing slash, so I've added a flag to handle both. */
748 is_in_prefix_list (pprefix, string, filep)
749 struct path_prefix *pprefix;
753 struct prefix_list *pl;
757 int len = strlen (string);
759 for (pl = pprefix->plist; pl; pl = pl->next)
761 if (strncmp (pl->prefix, string, len) == 0
762 && strcmp (pl->prefix + len, "/") == 0)
768 for (pl = pprefix->plist; pl; pl = pl->next)
770 if (strcmp (pl->prefix, string) == 0)
778 /* Search for NAME using prefix list PPREFIX. We only look for executable
781 Return 0 if not found, otherwise return its name, allocated with malloc. */
784 find_a_file (pprefix, name)
785 struct path_prefix *pprefix;
789 struct prefix_list *pl;
790 int len = pprefix->max_len + strlen (name) + 1;
792 #ifdef EXECUTABLE_SUFFIX
793 len += strlen (EXECUTABLE_SUFFIX);
796 temp = xmalloc (len);
798 /* Determine the filename to execute (special case for absolute paths). */
802 if (access (name, X_OK) == 0)
809 for (pl = pprefix->plist; pl; pl = pl->next)
811 strcpy (temp, pl->prefix);
813 if (! is_in_prefix_list (&our_file_names, temp, 1)
814 /* This is a kludge, but there seems no way around it. */
815 && strcmp (temp, "./ld") != 0
816 && access (temp, X_OK) == 0)
819 #ifdef EXECUTABLE_SUFFIX
820 /* Some systems have a suffix for executable files.
821 So try appending that. */
822 strcat (temp, EXECUTABLE_SUFFIX);
823 if (! is_in_prefix_list (&our_file_names, temp, 1)
824 && access (temp, X_OK) == 0)
833 /* Add an entry for PREFIX to prefix list PPREFIX. */
836 add_prefix (pprefix, prefix)
837 struct path_prefix *pprefix;
840 struct prefix_list *pl, **prev;
845 for (pl = pprefix->plist; pl->next; pl = pl->next)
850 prev = &pprefix->plist;
852 /* Keep track of the longest prefix */
854 len = strlen (prefix);
855 if (len > pprefix->max_len)
856 pprefix->max_len = len;
858 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
859 pl->prefix = savestring (prefix, len);
864 pl->next = (struct prefix_list *) 0;
868 /* Take the value of the environment variable ENV, break it into a path, and
869 add of the entries to PPREFIX. */
872 prefix_from_env (env, pprefix)
874 struct path_prefix *pprefix;
876 char *p = getenv (env);
879 prefix_from_string (p, pprefix);
883 prefix_from_string (p, pprefix)
885 struct path_prefix *pprefix;
888 char *nstore = (char *) xmalloc (strlen (p) + 3);
893 if (*endp == PATH_SEPARATOR || *endp == 0)
895 strncpy (nstore, startp, endp-startp);
898 strcpy (nstore, "./");
900 else if (endp[-1] != '/')
902 nstore[endp-startp] = '/';
903 nstore[endp-startp+1] = 0;
906 nstore[endp-startp] = 0;
908 add_prefix (pprefix, nstore);
911 endp = startp = endp + 1;
925 char *ld_suffix = "ld";
926 char *full_ld_suffix = ld_suffix;
927 char *real_ld_suffix = "real-ld";
929 char *full_real_ld_suffix = real_ld_suffix;
931 char *collect_ld_suffix = "collect-ld";
932 char *nm_suffix = "nm";
933 char *full_nm_suffix = nm_suffix;
934 char *gnm_suffix = "gnm";
935 char *full_gnm_suffix = gnm_suffix;
937 char *ldd_suffix = LDD_SUFFIX;
938 char *full_ldd_suffix = ldd_suffix;
940 char *strip_suffix = "strip";
941 char *full_strip_suffix = strip_suffix;
942 char *gstrip_suffix = "gstrip";
943 char *full_gstrip_suffix = gstrip_suffix;
946 #ifdef COLLECT_EXPORT_LIST
948 #endif /* COLLECT_EXPORT_LIST */
955 char **ld1_argv = (char **) xcalloc (sizeof (char *), argc+3);
956 char **ld1 = ld1_argv;
957 char **ld2_argv = (char **) xcalloc (sizeof (char *), argc+6);
958 char **ld2 = ld2_argv;
959 char **object_lst = (char **) xcalloc (sizeof (char *), argc);
960 char **object = object_lst;
962 int num_c_args = argc+7;
969 #ifndef DEFAULT_A_OUT_NAME
970 output_file = "a.out";
972 output_file = DEFAULT_A_OUT_NAME;
975 obstack_begin (&temporary_obstack, 0);
976 obstack_begin (&permanent_obstack, 0);
977 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
978 current_demangling_style = gnu_demangling;
980 /* We must check that we do not call ourselves in an infinite
981 recursion loop. We append the name used for us to the COLLECT_NAMES
982 environment variable.
984 In practice, collect will rarely invoke itself. This can happen now
985 that we are no longer called gld. A perfect example is when running
986 gcc in a build directory that has been installed. When looking for
987 ld's, we'll find our installed version and believe that's the real ld. */
989 /* We must also append COLLECT_NAME to COLLECT_NAMES to watch for the
990 previous version of collect (the one that used COLLECT_NAME and only
991 handled two levels of recursion). If we don't we may mutually recurse
992 forever. This can happen (I think) when bootstrapping the old version
993 and a new one is installed (rare, but we should handle it).
994 ??? Hopefully references to COLLECT_NAME can be removed at some point. */
996 collect_name = (char *) getenv ("COLLECT_NAME");
997 collect_names = (char *) getenv ("COLLECT_NAMES");
999 p = (char *) xmalloc (strlen ("COLLECT_NAMES=")
1000 + (collect_name ? strlen (collect_name) + 1 : 0)
1001 + (collect_names ? strlen (collect_names) + 1 : 0)
1002 + strlen (argv[0]) + 1);
1003 strcpy (p, "COLLECT_NAMES=");
1004 if (collect_name != 0)
1005 sprintf (p + strlen (p), "%s%c", collect_name, PATH_SEPARATOR);
1006 if (collect_names != 0)
1007 sprintf (p + strlen (p), "%s%c", collect_names, PATH_SEPARATOR);
1008 strcat (p, argv[0]);
1011 prefix_from_env ("COLLECT_NAMES", &our_file_names);
1013 /* Set environment variable COLLECT_NAME to our name so the previous version
1014 of collect won't find us. If it does we'll mutually recurse forever.
1015 This can happen when bootstrapping the new version and an old version is
1017 ??? Hopefully this bit of code can be removed at some point. */
1019 p = xmalloc (strlen ("COLLECT_NAME=") + strlen (argv[0]) + 1);
1020 sprintf (p, "COLLECT_NAME=%s", argv[0]);
1023 p = (char *) getenv ("COLLECT_GCC_OPTIONS");
1026 char *q = extract_string (&p);
1027 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1030 obstack_free (&temporary_obstack, temporary_firstobj);
1033 c_ptr = c_argv = (char **) xcalloc (sizeof (char *), num_c_args);
1036 fatal ("no arguments");
1039 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
1040 signal (SIGQUIT, handler);
1042 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1043 signal (SIGINT, handler);
1045 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
1046 signal (SIGALRM, handler);
1049 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1050 signal (SIGHUP, handler);
1052 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
1053 signal (SIGSEGV, handler);
1055 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
1056 signal (SIGBUS, handler);
1059 /* Extract COMPILER_PATH and PATH into our prefix list. */
1060 prefix_from_env ("COMPILER_PATH", &cpath);
1061 prefix_from_env ("PATH", &path);
1063 #ifdef CROSS_COMPILE
1064 /* If we look for a program in the compiler directories, we just use
1065 the short name, since these directories are already system-specific.
1066 But it we look for a took in the system directories, we need to
1067 qualify the program name with the target machine. */
1070 = xcalloc (strlen (ld_suffix) + strlen (target_machine) + 2, 1);
1071 strcpy (full_ld_suffix, target_machine);
1072 strcat (full_ld_suffix, "-");
1073 strcat (full_ld_suffix, ld_suffix);
1076 = xcalloc (strlen (real_ld_suffix) + strlen (target_machine) + 2, 1);
1077 strcpy (full_real_ld_suffix, target_machine);
1078 strcat (full_real_ld_suffix, "-");
1079 strcat (full_real_ld_suffix, real_ld_suffix);
1083 = xcalloc (strlen (gld_suffix) + strlen (target_machine) + 2, 1);
1084 strcpy (full_gld_suffix, target_machine);
1085 strcat (full_gld_suffix, "-");
1086 strcat (full_gld_suffix, gld_suffix);
1090 = xcalloc (strlen (nm_suffix) + strlen (target_machine) + 2, 1);
1091 strcpy (full_nm_suffix, target_machine);
1092 strcat (full_nm_suffix, "-");
1093 strcat (full_nm_suffix, nm_suffix);
1096 = xcalloc (strlen (gnm_suffix) + strlen (target_machine) + 2, 1);
1097 strcpy (full_gnm_suffix, target_machine);
1098 strcat (full_gnm_suffix, "-");
1099 strcat (full_gnm_suffix, gnm_suffix);
1103 = xcalloc (strlen (ldd_suffix) + strlen (target_machine) + 2, 1);
1104 strcpy (full_ldd_suffix, target_machine);
1105 strcat (full_ldd_suffix, "-");
1106 strcat (full_ldd_suffix, ldd_suffix);
1110 = xcalloc (strlen (strip_suffix) + strlen (target_machine) + 2, 1);
1111 strcpy (full_strip_suffix, target_machine);
1112 strcat (full_strip_suffix, "-");
1113 strcat (full_strip_suffix, strip_suffix);
1116 = xcalloc (strlen (gstrip_suffix) + strlen (target_machine) + 2, 1);
1117 strcpy (full_gstrip_suffix, target_machine);
1118 strcat (full_gstrip_suffix, "-");
1119 strcat (full_gstrip_suffix, gstrip_suffix);
1120 #endif /* CROSS_COMPILE */
1122 /* Try to discover a valid linker/nm/strip to use. */
1124 /* Maybe we know the right file to use (if not cross). */
1125 #ifdef REAL_LD_FILE_NAME
1126 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
1127 if (ld_file_name == 0)
1129 /* Search the (target-specific) compiler dirs for ld'. */
1130 ld_file_name = find_a_file (&cpath, real_ld_suffix);
1131 /* Likewise for `collect-ld'. */
1132 if (ld_file_name == 0)
1133 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
1134 /* Search the compiler directories for `ld'. We have protection against
1135 recursive calls in find_a_file. */
1136 if (ld_file_name == 0)
1137 ld_file_name = find_a_file (&cpath, ld_suffix);
1138 /* Search the ordinary system bin directories
1139 for `ld' (if native linking) or `TARGET-ld' (if cross). */
1140 if (ld_file_name == 0)
1141 ld_file_name = find_a_file (&path, full_ld_suffix);
1143 /* If we've invoked ourselves, try again with LD_FILE_NAME. */
1145 if (collect_names != 0)
1147 if (ld_file_name != 0)
1149 argv[0] = ld_file_name;
1150 execvp (argv[0], argv);
1152 fatal ("cannot find `ld'");
1155 #ifdef REAL_NM_FILE_NAME
1156 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
1157 if (nm_file_name == 0)
1159 nm_file_name = find_a_file (&cpath, gnm_suffix);
1160 if (nm_file_name == 0)
1161 nm_file_name = find_a_file (&path, full_gnm_suffix);
1162 if (nm_file_name == 0)
1163 nm_file_name = find_a_file (&cpath, nm_suffix);
1164 if (nm_file_name == 0)
1165 nm_file_name = find_a_file (&path, full_nm_suffix);
1168 ldd_file_name = find_a_file (&cpath, ldd_suffix);
1169 if (ldd_file_name == 0)
1170 ldd_file_name = find_a_file (&path, full_ldd_suffix);
1173 #ifdef REAL_STRIP_FILE_NAME
1174 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
1175 if (strip_file_name == 0)
1177 strip_file_name = find_a_file (&cpath, gstrip_suffix);
1178 if (strip_file_name == 0)
1179 strip_file_name = find_a_file (&path, full_gstrip_suffix);
1180 if (strip_file_name == 0)
1181 strip_file_name = find_a_file (&cpath, strip_suffix);
1182 if (strip_file_name == 0)
1183 strip_file_name = find_a_file (&path, full_strip_suffix);
1185 /* Determine the full path name of the C compiler to use. */
1186 c_file_name = getenv ("COLLECT_GCC");
1187 if (c_file_name == 0)
1189 #ifdef CROSS_COMPILE
1190 c_file_name = xcalloc (sizeof ("gcc-") + strlen (target_machine) + 1, 1);
1191 strcpy (c_file_name, target_machine);
1192 strcat (c_file_name, "-gcc");
1194 c_file_name = "gcc";
1198 p = find_a_file (&cpath, c_file_name);
1200 /* Here it should be safe to use the system search path since we should have
1201 already qualified the name of the compiler when it is needed. */
1203 p = find_a_file (&path, c_file_name);
1208 *ld1++ = *ld2++ = ld_file_name;
1210 /* Make temp file names. */
1211 temp_filename = choose_temp_base ();
1212 temp_filename_length = strlen (temp_filename);
1213 c_file = xcalloc (temp_filename_length + sizeof (".c"), 1);
1214 o_file = xcalloc (temp_filename_length + sizeof (".o"), 1);
1215 export_file = xmalloc (temp_filename_length + sizeof (".x"));
1216 ldout = xmalloc (temp_filename_length + sizeof (".ld"));
1217 sprintf (ldout, "%s.ld", temp_filename);
1218 sprintf (c_file, "%s.c", temp_filename);
1219 sprintf (o_file, "%s.o", temp_filename);
1220 sprintf (export_file, "%s.x", temp_filename);
1221 *c_ptr++ = c_file_name;
1226 /* !!! When GCC calls collect2,
1227 it does not know whether it is calling collect2 or ld.
1228 So collect2 cannot meaningfully understand any options
1229 except those ld understands.
1230 If you propose to make GCC pass some other option,
1231 just imagine what will happen if ld is really ld!!! */
1233 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1234 /* After the first file, put in the c++ rt0. */
1237 while ((arg = *++argv) != (char *) 0)
1239 *ld1++ = *ld2++ = arg;
1246 if (!strcmp (arg, "-debug"))
1258 /* place o_file BEFORE this argument! */
1268 output_file = *ld1++ = *ld2++ = *++argv;
1270 output_file = &arg[2];
1279 if (arg[2] == '\0' && do_collecting)
1281 /* We must strip after the nm run, otherwise C++ linking
1282 won't work. Thus we strip in the second ld run, or
1283 else with strip if there is no second ld run. */
1295 else if ((p = rindex (arg, '.')) != (char *) 0
1296 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0))
1305 /* place o_file BEFORE this argument! */
1316 /* Get any options that the upper GCC wants to pass to the sub-GCC. */
1317 p = (char *) getenv ("COLLECT_GCC_OPTIONS");
1320 char *q = extract_string (&p);
1321 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1322 *c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
1323 if (strncmp (q, "-shared", sizeof ("shared") - 1) == 0)
1326 obstack_free (&temporary_obstack, temporary_firstobj);
1327 *c_ptr++ = "-fno-exceptions";
1329 #ifdef COLLECT_EXPORT_LIST
1330 /* The AIX linker will discard static constructors in object files if
1331 nothing else in the file is referenced, so look at them first. */
1332 while (object_lst < object)
1333 scan_prog_file (*object_lst++, PASS_OBJ);
1336 char *buf = alloca (strlen (export_file) + 5);
1337 sprintf (buf, "-bE:%s", export_file);
1340 exportf = fopen (export_file, "w");
1341 if (exportf == (FILE *) 0)
1342 fatal_perror ("%s", export_file);
1343 write_export_file (exportf);
1344 if (fclose (exportf))
1345 fatal_perror ("closing %s", export_file);
1350 *object = *c_ptr = *ld1 = (char *) 0;
1354 fprintf (stderr, "collect2 version %s", version_string);
1355 #ifdef TARGET_VERSION
1358 fprintf (stderr, "\n");
1364 fprintf (stderr, "ld_file_name = %s\n",
1365 (ld_file_name ? ld_file_name : "not found"));
1366 fprintf (stderr, "c_file_name = %s\n",
1367 (c_file_name ? c_file_name : "not found"));
1368 fprintf (stderr, "nm_file_name = %s\n",
1369 (nm_file_name ? nm_file_name : "not found"));
1371 fprintf (stderr, "ldd_file_name = %s\n",
1372 (ldd_file_name ? ldd_file_name : "not found"));
1374 fprintf (stderr, "strip_file_name = %s\n",
1375 (strip_file_name ? strip_file_name : "not found"));
1376 fprintf (stderr, "c_file = %s\n",
1377 (c_file ? c_file : "not found"));
1378 fprintf (stderr, "o_file = %s\n",
1379 (o_file ? o_file : "not found"));
1381 ptr = getenv ("COLLECT_NAMES");
1383 fprintf (stderr, "COLLECT_NAMES = %s\n", ptr);
1385 ptr = getenv ("COLLECT_GCC_OPTIONS");
1387 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1389 ptr = getenv ("COLLECT_GCC");
1391 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1393 ptr = getenv ("COMPILER_PATH");
1395 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1397 ptr = getenv ("LIBRARY_PATH");
1399 fprintf (stderr, "LIBRARY_PATH = %s\n", ptr);
1401 fprintf (stderr, "\n");
1404 /* Load the program, searching all libraries and attempting to provide
1405 undefined symbols from repository information. */
1407 do_tlink (ld1_argv, object_lst);
1409 /* If -r or they'll be run via some other method, don't build the
1410 constructor or destructor list, just return now. */
1411 if (rflag || ! do_collecting)
1413 /* But make sure we delete the export file we may have created. */
1414 if (export_file != 0 && export_file[0])
1415 maybe_unlink (export_file);
1419 /* Examine the namelist with nm and search it for static constructors
1420 and destructors to call.
1421 Write the constructor and destructor tables to a .s file and reload. */
1423 scan_prog_file (output_file, PASS_FIRST);
1425 #ifdef SCAN_LIBRARIES
1426 scan_libraries (output_file);
1431 fprintf (stderr, "%d constructor(s) found\n", constructors.number);
1432 fprintf (stderr, "%d destructor(s) found\n", destructors.number);
1435 if (constructors.number == 0 && destructors.number == 0
1436 && frame_tables.number == 0
1437 #ifdef SCAN_LIBRARIES
1438 /* If we will be running these functions ourselves, we want to emit
1439 stubs into the shared library so that we don't have to relink
1440 dependent programs when we add static objects. */
1445 /* Strip now if it was requested on the command line. */
1448 char **strip_argv = (char **) xcalloc (sizeof (char *), 3);
1449 strip_argv[0] = strip_file_name;
1450 strip_argv[1] = output_file;
1451 strip_argv[2] = (char *) 0;
1452 fork_execute ("strip", strip_argv);
1455 #ifdef COLLECT_EXPORT_LIST
1456 maybe_unlink (export_file);
1461 maybe_unlink(output_file);
1462 outf = fopen (c_file, "w");
1463 if (outf == (FILE *) 0)
1464 fatal_perror ("%s", c_file);
1466 write_c_file (outf, c_file);
1469 fatal_perror ("closing %s", c_file);
1471 /* Tell the linker that we have initializer and finalizer functions. */
1472 #ifdef LD_INIT_SWITCH
1473 *ld2++ = LD_INIT_SWITCH;
1475 *ld2++ = LD_FINI_SWITCH;
1480 #ifdef COLLECT_EXPORT_LIST
1483 add_to_list (&exports, initname);
1484 add_to_list (&exports, fininame);
1485 add_to_list (&exports, "_GLOBAL__DI");
1486 add_to_list (&exports, "_GLOBAL__DD");
1487 exportf = fopen (export_file, "w");
1488 if (exportf == (FILE *) 0)
1489 fatal_perror ("%s", export_file);
1490 write_export_file (exportf);
1491 if (fclose (exportf))
1492 fatal_perror ("closing %s", export_file);
1498 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1499 output_file, c_file);
1500 write_c_file (stderr, "stderr");
1501 fprintf (stderr, "========== end of c_file\n\n");
1502 #ifdef COLLECT_EXPORT_LIST
1503 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1504 write_export_file (stderr);
1505 fprintf (stderr, "========== end of export_file\n\n");
1509 /* Assemble the constructor and destructor tables.
1510 Link the tables in with the rest of the program. */
1512 fork_execute ("gcc", c_argv);
1513 fork_execute ("ld", ld2_argv);
1515 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1516 constructors/destructors in shared libraries. */
1517 scan_prog_file (output_file, PASS_SECOND);
1519 maybe_unlink (c_file);
1520 maybe_unlink (o_file);
1521 maybe_unlink (export_file);
1526 /* Wait for a process to finish, and exit if a non-zero status is found. */
1537 if (WIFSIGNALED (status))
1539 int sig = WTERMSIG (status);
1540 #ifdef NO_SYS_SIGLIST
1541 error ("%s terminated with signal %d %s",
1544 (status & 0200) ? ", core dumped" : "");
1546 error ("%s terminated with signal %d [%s]%s",
1550 (status & 0200) ? ", core dumped" : "");
1553 collect_exit (FATAL_EXIT_CODE);
1556 if (WIFEXITED (status))
1557 return WEXITSTATUS (status);
1566 int ret = collect_wait (prog);
1569 error ("%s returned %d exit status", prog, ret);
1575 /* Fork and execute a program, and wait for the reply. */
1578 collect_execute (prog, argv, redir)
1591 fprintf (stderr, "%s", argv[0]);
1593 fprintf (stderr, "[cannot find %s]", prog);
1595 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1596 fprintf (stderr, " %s", str);
1598 fprintf (stderr, "\n");
1604 /* If we can't find a program we need, complain error. Do this here
1605 since we might not end up needing something that we couldn't find. */
1608 fatal ("cannot find `%s'", prog);
1614 fatal_perror ("fork");
1616 fatal_perror ("vfork");
1620 if (pid == 0) /* child context */
1625 if (freopen (redir, "a", stdout) == NULL)
1626 fatal_perror ("redirecting stdout: %s", redir);
1627 if (freopen (redir, "a", stderr) == NULL)
1628 fatal_perror ("redirecting stderr: %s", redir);
1631 execvp (argv[0], argv);
1632 fatal_perror ("executing %s", prog);
1637 fork_execute (prog, argv)
1641 collect_execute (prog, argv, NULL);
1645 /* Unlink a file unless we are debugging. */
1654 fprintf (stderr, "[Leaving %s]\n", file);
1658 /* Add a name to a linked list. */
1661 add_to_list (head_ptr, name)
1662 struct head *head_ptr;
1666 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1668 static long sequence_number = 0;
1669 strcpy (newid->name, name);
1671 if (head_ptr->first)
1672 head_ptr->last->next = newid;
1674 head_ptr->first = newid;
1676 /* Check for duplicate symbols. */
1677 for (p = head_ptr->first;
1678 strcmp (name, p->name) != 0;
1683 head_ptr->last->next = 0;
1688 newid->sequence = ++sequence_number;
1689 head_ptr->last = newid;
1693 /* Write: `prefix', the names on list LIST, `suffix'. */
1696 write_list (stream, prefix, list)
1703 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1709 write_list_with_asm (stream, prefix, list)
1716 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1717 prefix, list->sequence, list->name);
1722 /* Write out the constructor and destructor tables statically (for a shared
1723 object), along with the functions to execute them. */
1726 write_c_file_stat (stream, name)
1730 char *prefix, *p, *q;
1731 int frames = (frame_tables.number > 0);
1733 /* Figure out name of output_file, stripping off .so version. */
1734 p = rindex (output_file, '/');
1736 p = (char *) output_file;
1750 if (strncmp (q, ".so", 3) == 0)
1759 /* q points to null at end of the string (or . of the .so version) */
1760 prefix = xmalloc (q - p + 1);
1761 strncpy (prefix, p, q - p);
1763 for (q = prefix; *q; q++)
1767 fprintf (stderr, "\nwrite_c_file - output name is %s, prefix is %s\n",
1768 output_file, prefix);
1770 #define INIT_NAME_FORMAT "_GLOBAL__FI_%s"
1771 initname = xmalloc (strlen (prefix) + sizeof (INIT_NAME_FORMAT) - 2);
1772 sprintf (initname, INIT_NAME_FORMAT, prefix);
1774 #define FINI_NAME_FORMAT "_GLOBAL__FD_%s"
1775 fininame = xmalloc (strlen (prefix) + sizeof (FINI_NAME_FORMAT) - 2);
1776 sprintf (fininame, FINI_NAME_FORMAT, prefix);
1780 /* Write the tables as C code */
1782 fprintf (stream, "static int count;\n");
1783 fprintf (stream, "typedef void entry_pt();\n");
1784 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1788 write_list_with_asm (stream, "extern void *", frame_tables.first);
1790 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1791 write_list (stream, "\t\t&", frame_tables.first);
1792 fprintf (stream, "\t0\n};\n");
1794 /* This must match what's in frame.h. */
1795 fprintf (stream, "struct object {\n");
1796 fprintf (stream, " void *pc_begin;\n");
1797 fprintf (stream, " void *pc_end;\n");
1798 fprintf (stream, " void *fde_begin;\n");
1799 fprintf (stream, " void *fde_array;\n");
1800 fprintf (stream, " __SIZE_TYPE__ count;\n");
1801 fprintf (stream, " struct object *next;\n");
1802 fprintf (stream, "};\n");
1804 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1805 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1807 fprintf (stream, "static void reg_frame () {\n");
1808 fprintf (stream, "\tstatic struct object ob;\n");
1809 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1810 fprintf (stream, "\t}\n");
1812 fprintf (stream, "static void dereg_frame () {\n");
1813 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1814 fprintf (stream, "\t}\n");
1817 fprintf (stream, "void %s() {\n", initname);
1818 if (constructors.number > 0 || frames)
1820 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1821 write_list (stream, "\t\t", constructors.first);
1823 fprintf (stream, "\treg_frame,\n");
1824 fprintf (stream, "\t};\n");
1825 fprintf (stream, "\tentry_pt **p;\n");
1826 fprintf (stream, "\tif (count++ != 0) return;\n");
1827 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1828 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1831 fprintf (stream, "\t++count;\n");
1832 fprintf (stream, "}\n");
1833 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1834 fprintf (stream, "void %s() {\n", fininame);
1835 if (destructors.number > 0 || frames)
1837 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1838 write_list (stream, "\t\t", destructors.first);
1840 fprintf (stream, "\tdereg_frame,\n");
1841 fprintf (stream, "\t};\n");
1842 fprintf (stream, "\tentry_pt **p;\n");
1843 fprintf (stream, "\tif (--count != 0) return;\n");
1844 fprintf (stream, "\tp = dtors;\n");
1845 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1846 destructors.number + frames);
1848 fprintf (stream, "}\n");
1852 fprintf (stream, "void _GLOBAL__DI() {\n\t%s();\n}\n", initname);
1853 fprintf (stream, "void _GLOBAL__DD() {\n\t%s();\n}\n", fininame);
1857 /* Write the constructor/destructor tables. */
1860 write_c_file_glob (stream, name)
1864 /* Write the tables as C code */
1866 int frames = (frame_tables.number > 0);
1868 fprintf (stream, "typedef void entry_pt();\n\n");
1870 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1874 write_list_with_asm (stream, "extern void *", frame_tables.first);
1876 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1877 write_list (stream, "\t\t&", frame_tables.first);
1878 fprintf (stream, "\t0\n};\n");
1880 /* This must match what's in frame.h. */
1881 fprintf (stream, "struct object {\n");
1882 fprintf (stream, " void *pc_begin;\n");
1883 fprintf (stream, " void *pc_end;\n");
1884 fprintf (stream, " void *fde_begin;\n");
1885 fprintf (stream, " void *fde_array;\n");
1886 fprintf (stream, " __SIZE_TYPE__ count;\n");
1887 fprintf (stream, " struct object *next;\n");
1888 fprintf (stream, "};\n");
1890 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1891 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1893 fprintf (stream, "static void reg_frame () {\n");
1894 fprintf (stream, "\tstatic struct object ob;\n");
1895 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1896 fprintf (stream, "\t}\n");
1898 fprintf (stream, "static void dereg_frame () {\n");
1899 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1900 fprintf (stream, "\t}\n");
1903 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1904 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
1905 write_list (stream, "\t", constructors.first);
1907 fprintf (stream, "\treg_frame,\n");
1908 fprintf (stream, "\t0\n};\n\n");
1910 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1912 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1913 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
1914 write_list (stream, "\t", destructors.first);
1916 fprintf (stream, "\tdereg_frame,\n");
1917 fprintf (stream, "\t0\n};\n\n");
1919 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1920 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1924 write_c_file (stream, name)
1928 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1929 #ifndef LD_INIT_SWITCH
1931 write_c_file_glob (stream, name);
1934 write_c_file_stat (stream, name);
1935 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
1939 write_export_file (stream)
1942 struct id *list = exports.first;
1943 for (; list; list = list->next)
1944 fprintf (stream, "%s\n", list->name);
1947 #ifdef OBJECT_FORMAT_NONE
1949 /* Generic version to scan the name list of the loaded program for
1950 the symbols g++ uses for static constructors and destructors.
1952 The constructor table begins at __CTOR_LIST__ and contains a count
1953 of the number of pointers (or -1 if the constructors are built in a
1954 separate section by the linker), followed by the pointers to the
1955 constructor functions, terminated with a null pointer. The
1956 destructor table has the same format, and begins at __DTOR_LIST__. */
1959 scan_prog_file (prog_name, which_pass)
1961 enum pass which_pass;
1963 void (*int_handler) ();
1964 void (*quit_handler) ();
1972 if (which_pass == PASS_SECOND)
1975 /* If we don't have an `nm', complain. */
1976 if (nm_file_name == 0)
1977 fatal ("cannot find `nm'");
1979 nm_argv[argc++] = nm_file_name;
1980 if (NM_FLAGS[0] != '\0')
1981 nm_argv[argc++] = NM_FLAGS;
1983 nm_argv[argc++] = prog_name;
1984 nm_argv[argc++] = (char *) 0;
1986 if (pipe (pipe_fd) < 0)
1987 fatal_perror ("pipe");
1989 inf = fdopen (pipe_fd[0], "r");
1990 if (inf == (FILE *) 0)
1991 fatal_perror ("fdopen");
1993 /* Trace if needed. */
1999 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2000 fprintf (stderr, " %s", str);
2002 fprintf (stderr, "\n");
2008 /* Spawn child nm on pipe */
2013 fatal_perror ("fork");
2015 fatal_perror ("vfork");
2019 if (pid == 0) /* child context */
2022 if (dup2 (pipe_fd[1], 1) < 0)
2023 fatal_perror ("dup2 (%d, 1)", pipe_fd[1]);
2025 if (close (pipe_fd[0]) < 0)
2026 fatal_perror ("close (%d)", pipe_fd[0]);
2028 if (close (pipe_fd[1]) < 0)
2029 fatal_perror ("close (%d)", pipe_fd[1]);
2031 execv (nm_file_name, nm_argv);
2032 fatal_perror ("executing %s", nm_file_name);
2035 /* Parent context from here on. */
2036 int_handler = (void (*) ())signal (SIGINT, SIG_IGN);
2038 quit_handler = (void (*) ())signal (SIGQUIT, SIG_IGN);
2041 if (close (pipe_fd[1]) < 0)
2042 fatal_perror ("close (%d)", pipe_fd[1]);
2045 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2047 /* Read each line of nm output. */
2048 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2053 /* If it contains a constructor or destructor name, add the name
2054 to the appropriate list. */
2056 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2057 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2064 /* Find the end of the symbol name.
2065 Don't include `|', because Encore nm can tack that on the end. */
2066 for (end = p; (ch2 = *end) != '\0' && !isspace (ch2) && ch2 != '|';
2072 switch (is_ctor_dtor (name))
2075 if (which_pass != PASS_LIB)
2076 add_to_list (&constructors, name);
2080 if (which_pass != PASS_LIB)
2081 add_to_list (&destructors, name);
2085 if (which_pass != PASS_LIB)
2086 fatal ("init function found in object %s", prog_name);
2087 #ifndef LD_INIT_SWITCH
2088 add_to_list (&constructors, name);
2093 if (which_pass != PASS_LIB)
2094 fatal ("fini function found in object %s", prog_name);
2095 #ifndef LD_FINI_SWITCH
2096 add_to_list (&destructors, name);
2101 if (which_pass != PASS_LIB)
2102 add_to_list (&frame_tables, name);
2104 default: /* not a constructor or destructor */
2109 fprintf (stderr, "\t%s\n", buf);
2113 fprintf (stderr, "\n");
2115 if (fclose (inf) != 0)
2116 fatal_perror ("fclose of pipe");
2118 do_wait (nm_file_name);
2120 signal (SIGINT, int_handler);
2122 signal (SIGQUIT, quit_handler);
2126 #if SUNOS4_SHARED_LIBRARIES
2128 /* Routines to scan the SunOS 4 _DYNAMIC structure to find shared libraries
2129 that the output file depends upon and their initialization/finalization
2130 routines, if any. */
2135 #include <sys/mman.h>
2136 #include <sys/param.h>
2138 #include <sys/dir.h>
2140 /* pointers to the object file */
2141 unsigned object; /* address of memory mapped file */
2142 unsigned objsize; /* size of memory mapped to file */
2143 char * code; /* pointer to code segment */
2144 char * data; /* pointer to data segment */
2145 struct nlist *symtab; /* pointer to symbol table */
2146 struct link_dynamic *ld;
2147 struct link_dynamic_2 *ld_2;
2148 struct head libraries;
2150 /* Map the file indicated by NAME into memory and store its address. */
2158 if ((fp = open (name, O_RDONLY)) == -1)
2159 fatal ("unable to open file '%s'", name);
2160 if (fstat (fp, &s) == -1)
2161 fatal ("unable to stat file '%s'", name);
2163 objsize = s.st_size;
2164 object = (unsigned) mmap (0, objsize, PROT_READ|PROT_WRITE, MAP_PRIVATE,
2167 fatal ("unable to mmap file '%s'", name);
2172 /* Helpers for locatelib. */
2174 static char *libname;
2180 return (strncmp (libname, d->d_name, strlen (libname)) == 0);
2183 /* If one file has an additional numeric extension past LIBNAME, then put
2184 that one first in the sort. If both files have additional numeric
2185 extensions, then put the one with the higher number first in the sort.
2187 We must verify that the extension is numeric, because Sun saves the
2188 original versions of patched libraries with a .FCS extension. Files with
2189 invalid extensions must go last in the sort, so that they won't be used. */
2193 struct direct **d1, **d2;
2195 int i1, i2 = strlen (libname);
2196 char *e1 = (*d1)->d_name + i2;
2197 char *e2 = (*d2)->d_name + i2;
2199 while (*e1 && *e2 && *e1 == '.' && *e2 == '.'
2200 && e1[1] && isdigit (e1[1]) && e2[1] && isdigit (e2[1]))
2204 i1 = strtol (e1, &e1, 10);
2205 i2 = strtol (e2, &e2, 10);
2212 /* It has a valid numeric extension, prefer this one. */
2213 if (*e1 == '.' && e1[1] && isdigit (e1[1]))
2215 /* It has a invalid numeric extension, must prefer the other one. */
2221 /* It has a valid numeric extension, prefer this one. */
2222 if (*e2 == '.' && e2[1] && isdigit (e2[1]))
2224 /* It has a invalid numeric extension, must prefer the other one. */
2232 /* Given the name NAME of a dynamic dependency, find its pathname and add
2233 it to the list of libraries. */
2241 char buf[MAXPATHLEN];
2249 /* counting elements in array, need 1 extra for null */
2251 ld_rules = (char *) (ld_2->ld_rules + code);
2255 for (; *ld_rules != 0; ld_rules++)
2256 if (*ld_rules == ':')
2258 ld_rules = (char *) (ld_2->ld_rules + code);
2259 ldr = (char *) malloc (strlen (ld_rules) + 1);
2260 strcpy (ldr, ld_rules);
2262 p = getenv ("LD_LIBRARY_PATH");
2267 for (q = p ; *q != 0; q++)
2270 q = (char *) malloc (strlen (p) + 1);
2273 l = (char **) malloc ((cnt + 3) * sizeof (char *));
2278 for (; *ldr != 0; ldr++)
2288 for (; *q != 0; q++)
2295 /* built in directories are /lib, /usr/lib, and /usr/local/lib */
2298 *pp++ = "/usr/local/lib";
2302 for (pp = l; *pp != 0 ; pp++)
2304 struct direct **namelist;
2306 if ((entries = scandir (*pp, &namelist, libselect, libcompare)) > 0)
2308 sprintf (buf, "%s/%s", *pp, namelist[entries - 1]->d_name);
2309 add_to_list (&libraries, buf);
2311 fprintf (stderr, "%s\n", buf);
2318 fprintf (stderr, "not found\n");
2320 fatal ("dynamic dependency %s not found", name);
2324 /* Scan the _DYNAMIC structure of the output file to find shared libraries
2325 that it depends upon and any constructors or destructors they contain. */
2328 scan_libraries (prog_name)
2331 struct exec *header;
2333 struct link_object *lo;
2334 char buff[MAXPATHLEN];
2337 mapfile (prog_name);
2338 header = (struct exec *)object;
2339 if (N_BADMAG (*header))
2340 fatal ("bad magic number in file '%s'", prog_name);
2341 if (header->a_dynamic == 0)
2344 code = (char *) (N_TXTOFF (*header) + (long) header);
2345 data = (char *) (N_DATOFF (*header) + (long) header);
2346 symtab = (struct nlist *) (N_SYMOFF (*header) + (long) header);
2348 if (header->a_magic == ZMAGIC && header->a_entry == 0x20)
2351 ld = (struct link_dynamic *) (symtab->n_value + code);
2357 ld = (struct link_dynamic *) data;
2362 fprintf (stderr, "dynamic dependencies.\n");
2364 ld_2 = (struct link_dynamic_2 *) ((long) ld->ld_un.ld_2 + (long)base);
2365 for (lo = (struct link_object *) ld_2->ld_need; lo;
2366 lo = (struct link_object *) lo->lo_next)
2369 lo = (struct link_object *) ((long) lo + code);
2370 name = (char *) (code + lo->lo_name);
2374 fprintf (stderr, "\t-l%s.%d => ", name, lo->lo_major);
2375 sprintf (buff, "lib%s.so.%d.%d", name, lo->lo_major, lo->lo_minor);
2381 fprintf (stderr, "\t%s\n", name);
2382 add_to_list (&libraries, name);
2387 fprintf (stderr, "\n");
2389 /* now iterate through the library list adding their symbols to
2391 for (list = libraries.first; list; list = list->next)
2392 scan_prog_file (list->name, PASS_LIB);
2395 #else /* SUNOS4_SHARED_LIBRARIES */
2398 /* Use the List Dynamic Dependencies program to find shared libraries that
2399 the output file depends upon and their initialization/finalization
2400 routines, if any. */
2403 scan_libraries (prog_name)
2406 static struct head libraries; /* list of shared libraries found */
2408 void (*int_handler) ();
2409 void (*quit_handler) ();
2417 /* If we don't have an `ldd', complain. */
2418 if (ldd_file_name == 0)
2420 error ("cannot find `ldd'");
2424 ldd_argv[argc++] = ldd_file_name;
2425 ldd_argv[argc++] = prog_name;
2426 ldd_argv[argc++] = (char *) 0;
2428 if (pipe (pipe_fd) < 0)
2429 fatal_perror ("pipe");
2431 inf = fdopen (pipe_fd[0], "r");
2432 if (inf == (FILE *) 0)
2433 fatal_perror ("fdopen");
2435 /* Trace if needed. */
2441 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2442 fprintf (stderr, " %s", str);
2444 fprintf (stderr, "\n");
2450 /* Spawn child ldd on pipe */
2455 fatal_perror ("fork");
2457 fatal_perror ("vfork");
2461 if (pid == 0) /* child context */
2464 if (dup2 (pipe_fd[1], 1) < 0)
2465 fatal_perror ("dup2 (%d, 1)", pipe_fd[1]);
2467 if (close (pipe_fd[0]) < 0)
2468 fatal_perror ("close (%d)", pipe_fd[0]);
2470 if (close (pipe_fd[1]) < 0)
2471 fatal_perror ("close (%d)", pipe_fd[1]);
2473 execv (ldd_file_name, ldd_argv);
2474 fatal_perror ("executing %s", ldd_file_name);
2477 /* Parent context from here on. */
2478 int_handler = (void (*) ()) signal (SIGINT, SIG_IGN);
2480 quit_handler = (void (*) ()) signal (SIGQUIT, SIG_IGN);
2483 if (close (pipe_fd[1]) < 0)
2484 fatal_perror ("close (%d)", pipe_fd[1]);
2487 fprintf (stderr, "\nldd output with constructors/destructors.\n");
2489 /* Read each line of ldd output. */
2490 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2493 char *name, *end, *p = buf;
2495 /* Extract names of libraries and add to list. */
2496 PARSE_LDD_OUTPUT (p);
2501 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2502 fatal ("dynamic dependency %s not found", buf);
2504 /* Find the end of the symbol name. */
2506 (ch2 = *end) != '\0' && ch2 != '\n' && !isspace (ch2) && ch2 != '|';
2511 if (access (name, R_OK) == 0)
2512 add_to_list (&libraries, name);
2514 fatal ("unable to open dynamic dependency '%s'", buf);
2517 fprintf (stderr, "\t%s\n", buf);
2520 fprintf (stderr, "\n");
2522 if (fclose (inf) != 0)
2523 fatal_perror ("fclose of pipe");
2525 do_wait (ldd_file_name);
2527 signal (SIGINT, int_handler);
2529 signal (SIGQUIT, quit_handler);
2532 /* now iterate through the library list adding their symbols to
2534 for (list = libraries.first; list; list = list->next)
2535 scan_prog_file (list->name, PASS_LIB);
2538 #endif /* LDD_SUFFIX */
2539 #endif /* SUNOS4_SHARED_LIBRARIES */
2541 #endif /* OBJECT_FORMAT_NONE */
2545 * COFF specific stuff.
2548 #ifdef OBJECT_FORMAT_COFF
2550 #if defined(EXTENDED_COFF)
2551 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2552 # define GCC_SYMENT SYMR
2553 # define GCC_OK_SYMBOL(X) ((X).st == stProc && (X).sc == scText)
2554 # define GCC_SYMINC(X) (1)
2555 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2556 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2558 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2559 # define GCC_SYMENT SYMENT
2560 # define GCC_OK_SYMBOL(X) \
2561 (((X).n_sclass == C_EXT) && \
2562 (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) || \
2563 ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT)))
2564 # define GCC_SYMINC(X) ((X).n_numaux+1)
2565 # define GCC_SYMZERO(X) 0
2566 # define GCC_CHECK_HDR(X) (1)
2569 extern char *ldgetname ();
2571 /* COFF version to scan the name list of the loaded program for
2572 the symbols g++ uses for static constructors and destructors.
2574 The constructor table begins at __CTOR_LIST__ and contains a count
2575 of the number of pointers (or -1 if the constructors are built in a
2576 separate section by the linker), followed by the pointers to the
2577 constructor functions, terminated with a null pointer. The
2578 destructor table has the same format, and begins at __DTOR_LIST__. */
2581 scan_prog_file (prog_name, which_pass)
2583 enum pass which_pass;
2585 LDFILE *ldptr = NULL;
2586 int sym_index, sym_count;
2588 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2591 if ((ldptr = ldopen (prog_name, ldptr)) == NULL)
2592 fatal ("%s: can't open as COFF file", prog_name);
2594 if (!MY_ISCOFF (HEADER (ldptr).f_magic))
2595 fatal ("%s: not a COFF file", prog_name);
2597 if (GCC_CHECK_HDR (ldptr))
2599 sym_count = GCC_SYMBOLS (ldptr);
2600 sym_index = GCC_SYMZERO (ldptr);
2601 while (sym_index < sym_count)
2605 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2607 sym_index += GCC_SYMINC (symbol);
2609 if (GCC_OK_SYMBOL (symbol))
2613 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2614 continue; /* should never happen */
2616 #ifdef XCOFF_DEBUGGING_INFO
2617 /* All AIX function names have a duplicate entry beginning
2623 switch (is_ctor_dtor (name))
2626 add_to_list (&constructors, name);
2627 if (which_pass == PASS_OBJ)
2628 add_to_list (&exports, name);
2632 add_to_list (&destructors, name);
2633 if (which_pass == PASS_OBJ)
2634 add_to_list (&exports, name);
2637 default: /* not a constructor or destructor */
2641 #if !defined(EXTENDED_COFF)
2643 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2644 symbol.n_scnum, symbol.n_sclass,
2645 (symbol.n_type ? "0" : ""), symbol.n_type,
2649 fprintf (stderr, "\tiss = %5d, value = %5d, index = %5d, name = %s\n",
2650 symbol.iss, symbol.value, symbol.index, name);
2656 (void) ldclose(ldptr);
2659 #ifdef XCOFF_SCAN_LIBS
2660 /* Scan imported AIX libraries for GCC static ctors and dtors.
2661 FIXME: it is possible to link an executable without the actual import
2662 library by using an "import file" - a text file listing symbols
2663 exported by a library. To support this, we would have to scan
2664 import files as well as actual shared binaries to find GCC ctors.
2665 TODO: use memory mapping instead of 'ld' routines, files are already
2666 memory mapped, but we could eliminate the extra in-memory copies.
2667 Is it worth the effort? */
2670 scan_libraries (prog_name)
2675 static struct path_prefix libpath; /* we should only do this once */
2677 if ((ldptr = ldopen (prog_name, ldptr)) == NULL)
2678 fatal ("%s: can't open as COFF file", prog_name);
2680 if (!MY_ISCOFF (HEADER (ldptr).f_magic))
2681 fatal ("%s: not a COFF file", prog_name);
2683 /* find and read loader section */
2684 if (ldnshread (ldptr, _LOADER, &ldsh))
2690 FSEEK (ldptr, ldsh.s_scnptr, BEGINNING);
2691 FREAD (&ldh, sizeof (ldh), 1, ldptr);
2692 /* read import library list */
2693 impbuf = alloca (ldh.l_istlen);
2694 FSEEK (ldptr, ldh.l_impoff + ldsh.s_scnptr, BEGINNING);
2695 FREAD (impbuf, ldh.l_istlen, 1, ldptr);
2698 fprintf (stderr, "LIBPATH=%s\n", impbuf);
2699 prefix_from_string (impbuf, &libpath);
2701 /* skip LIBPATH and empty base and member fields */
2702 impbuf += strlen (impbuf) + 3;
2703 for (entry = 1; entry < ldh.l_nimpid; ++entry)
2705 char *impath = impbuf;
2706 char *implib = impath + strlen (impath) + 1;
2707 char *impmem = implib + strlen (implib) + 1;
2708 char *soname = NULL;
2711 LDFILE *libptr = NULL;
2712 struct prefix_list *pl;
2715 impbuf = impmem + strlen (impmem) + 1;
2717 fprintf (stderr, "PATH+BASE=%s%s\n", impath, implib);
2718 /* Skip AIX kernel exports */
2719 if (*impath == '/' && *(impath+1) == '\0'
2720 && strcmp (implib, "unix") == 0)
2722 pathlen = strlen (impath);
2723 trial = alloca (MAX (pathlen + 1, libpath.max_len)
2724 + strlen (implib) + 1);
2727 strcpy (trial, impath);
2728 if (impath[pathlen - 1] != '/')
2729 trial[pathlen++] = '/';
2730 strcpy (trial + pathlen, implib);
2731 if (access (trial, R_OK) == 0)
2735 for (pl = libpath.plist; pl; pl = pl->next)
2737 strcpy (trial, pl->prefix);
2738 strcat (trial, implib);
2739 if (access (trial, R_OK) == 0)
2747 fatal ("%s: library not found", implib);
2750 fprintf (stderr, "%s (%s)\n", soname, impmem);
2752 fprintf (stderr, "%s\n", soname);
2756 /* scan imported shared objects for GCC GLOBAL ctors */
2758 if ((libptr = ldopen (soname, libptr)) == NULL)
2759 fatal ("%s: can't open import library", soname);
2760 if (TYPE (libptr) == ARTYPE)
2764 fatal ("%s: no archive member specified", soname);
2765 ldahread (libptr, &ah);
2766 if (strcmp (ah.ar_name, impmem))
2769 type = HEADER (libptr).f_magic;
2770 if (HEADER (libptr).f_flags & F_SHROBJ)
2777 if (!ldnshread (libptr, _LOADER, &soldsh))
2778 fatal ("%s: not an import library", soname);
2779 FSEEK (libptr, soldsh.s_scnptr, BEGINNING);
2780 if (FREAD (&soldh, sizeof (soldh), 1, libptr) != 1)
2781 fatal ("%s: can't read loader section", soname);
2782 /*fprintf (stderr, "\tscanning %s\n", soname);*/
2783 symcnt = soldh.l_nsyms;
2784 lsyms = (LDSYM *) alloca (symcnt * sizeof (*lsyms));
2785 symcnt = FREAD (lsyms, sizeof (*lsyms), symcnt, libptr);
2786 ldstrings = alloca (soldh.l_stlen);
2787 FSEEK (libptr, soldsh.s_scnptr+soldh.l_stoff, BEGINNING);
2788 FREAD (ldstrings, soldh.l_stlen, 1, libptr);
2789 for (i = 0; i < symcnt; ++i)
2791 LDSYM *l = lsyms + i;
2792 if (LDR_EXPORT (*l))
2796 expname = l->l_name;
2797 else if (l->l_offset < soldh.l_stlen)
2798 expname = ldstrings + l->l_offset;
2799 switch (is_ctor_dtor (expname))
2803 fprintf (stderr, "\t%s\n", expname);
2804 add_to_list (&constructors, expname);
2808 add_to_list (&destructors, expname);
2811 default: /* not a constructor or destructor */
2818 fprintf (stderr, "%s: type = %04X flags = %04X\n",
2819 ah.ar_name, type, HEADER (libptr).f_flags);
2821 while (ldclose (libptr) == FAILURE);
2822 /* printf (stderr, "closed %s\n", soname); */
2826 #endif /* XCOFF_SCAN_LIBS */
2828 #endif /* OBJECT_FORMAT_COFF */
2832 * OSF/rose specific stuff.
2835 #ifdef OBJECT_FORMAT_ROSE
2837 /* Union of the various load commands */
2839 typedef union load_union
2841 ldc_header_t hdr; /* common header */
2842 load_cmd_map_command_t map; /* map indexing other load cmds */
2843 interpreter_command_t iprtr; /* interpreter pathname */
2844 strings_command_t str; /* load commands strings section */
2845 region_command_t region; /* region load command */
2846 reloc_command_t reloc; /* relocation section */
2847 package_command_t pkg; /* package load command */
2848 symbols_command_t sym; /* symbol sections */
2849 entry_command_t ent; /* program start section */
2850 gen_info_command_t info; /* object information */
2851 func_table_command_t func; /* function constructors/destructors */
2854 /* Structure to point to load command and data section in memory. */
2856 typedef struct load_all
2858 load_union_t *load; /* load command */
2859 char *section; /* pointer to section */
2862 /* Structure to contain information about a file mapped into memory. */
2866 char *start; /* start of map */
2867 char *name; /* filename */
2868 long size; /* size of the file */
2869 long rounded_size; /* size rounded to page boundary */
2870 int fd; /* file descriptor */
2871 int rw; /* != 0 if opened read/write */
2872 int use_mmap; /* != 0 if mmap'ed */
2875 extern int decode_mach_o_hdr ();
2876 extern int encode_mach_o_hdr ();
2878 static void add_func_table PROTO((mo_header_t *, load_all_t *,
2879 symbol_info_t *, int));
2880 static void print_header PROTO((mo_header_t *));
2881 static void print_load_command PROTO((load_union_t *, size_t, int));
2882 static void bad_header PROTO((int));
2883 static struct file_info *read_file PROTO((char *, int, int));
2884 static void end_file PROTO((struct file_info *));
2886 /* OSF/rose specific version to scan the name list of the loaded
2887 program for the symbols g++ uses for static constructors and
2890 The constructor table begins at __CTOR_LIST__ and contains a count
2891 of the number of pointers (or -1 if the constructors are built in a
2892 separate section by the linker), followed by the pointers to the
2893 constructor functions, terminated with a null pointer. The
2894 destructor table has the same format, and begins at __DTOR_LIST__. */
2897 scan_prog_file (prog_name, which_pass)
2899 enum pass which_pass;
2903 load_all_t *load_array;
2904 load_all_t *load_end;
2905 load_all_t *load_cmd;
2906 int symbol_load_cmds;
2912 struct file_info *obj_file;
2914 mo_lcid_t cmd_strings = -1;
2915 symbol_info_t *main_sym = 0;
2916 int rw = (which_pass != PASS_FIRST);
2918 prog_fd = open (prog_name, (rw) ? O_RDWR : O_RDONLY);
2920 fatal_perror ("can't read %s", prog_name);
2922 obj_file = read_file (prog_name, prog_fd, rw);
2923 obj = obj_file->start;
2925 status = decode_mach_o_hdr (obj, MO_SIZEOF_RAW_HDR, MOH_HEADER_VERSION, &hdr);
2926 if (status != MO_HDR_CONV_SUCCESS)
2927 bad_header (status);
2930 /* Do some basic sanity checks. Note we explicitly use the big endian magic number,
2931 since the hardware will automatically swap bytes for us on loading little endian
2934 #ifndef CROSS_COMPILE
2935 if (hdr.moh_magic != MOH_MAGIC_MSB
2936 || hdr.moh_header_version != MOH_HEADER_VERSION
2937 || hdr.moh_byte_order != OUR_BYTE_ORDER
2938 || hdr.moh_data_rep_id != OUR_DATA_REP_ID
2939 || hdr.moh_cpu_type != OUR_CPU_TYPE
2940 || hdr.moh_cpu_subtype != OUR_CPU_SUBTYPE
2941 || hdr.moh_vendor_type != OUR_VENDOR_TYPE)
2943 fatal ("incompatibilities between object file & expected values");
2948 print_header (&hdr);
2950 offset = hdr.moh_first_cmd_off;
2951 load_end = load_array
2952 = (load_all_t *) xcalloc (sizeof (load_all_t), hdr.moh_n_load_cmds + 2);
2954 /* Build array of load commands, calculating the offsets */
2955 for (i = 0; i < hdr.moh_n_load_cmds; i++)
2957 load_union_t *load_hdr; /* load command header */
2959 load_cmd = load_end++;
2960 load_hdr = (load_union_t *) (obj + offset);
2962 /* If modifying the program file, copy the header. */
2965 load_union_t *ptr = (load_union_t *) xmalloc (load_hdr->hdr.ldci_cmd_size);
2966 bcopy ((char *)load_hdr, (char *)ptr, load_hdr->hdr.ldci_cmd_size);
2969 /* null out old command map, because we will rewrite at the end. */
2970 if (ptr->hdr.ldci_cmd_type == LDC_CMD_MAP)
2972 cmd_strings = ptr->map.lcm_ld_cmd_strings;
2973 ptr->hdr.ldci_cmd_type = LDC_UNDEFINED;
2977 load_cmd->load = load_hdr;
2978 if (load_hdr->hdr.ldci_section_off > 0)
2979 load_cmd->section = obj + load_hdr->hdr.ldci_section_off;
2982 print_load_command (load_hdr, offset, i);
2984 offset += load_hdr->hdr.ldci_cmd_size;
2987 /* If the last command is the load command map and is not undefined,
2988 decrement the count of load commands. */
2989 if (rw && load_end[-1].load->hdr.ldci_cmd_type == LDC_UNDEFINED)
2992 hdr.moh_n_load_cmds--;
2995 /* Go through and process each symbol table section. */
2996 symbol_load_cmds = 0;
2997 for (load_cmd = load_array; load_cmd < load_end; load_cmd++)
2999 load_union_t *load_hdr = load_cmd->load;
3001 if (load_hdr->hdr.ldci_cmd_type == LDC_SYMBOLS)
3007 char *kind = "unknown";
3009 switch (load_hdr->sym.symc_kind)
3011 case SYMC_IMPORTS: kind = "imports"; break;
3012 case SYMC_DEFINED_SYMBOLS: kind = "defined"; break;
3013 case SYMC_STABS: kind = "stabs"; break;
3016 fprintf (stderr, "\nProcessing symbol table #%d, offset = 0x%.8lx, kind = %s\n",
3017 symbol_load_cmds, load_hdr->hdr.ldci_section_off, kind);
3020 if (load_hdr->sym.symc_kind != SYMC_DEFINED_SYMBOLS)
3023 str_sect = load_array[load_hdr->sym.symc_strings_section].section;
3024 if (str_sect == (char *) 0)
3025 fatal ("string section missing");
3027 if (load_cmd->section == (char *) 0)
3028 fatal ("section pointer missing");
3030 num_syms = load_hdr->sym.symc_nentries;
3031 for (i = 0; i < num_syms; i++)
3033 symbol_info_t *sym = ((symbol_info_t *) load_cmd->section) + i;
3034 char *name = sym->si_name.symbol_name + str_sect;
3041 char *n = name + strlen (name) - strlen (NAME__MAIN);
3043 if ((n - name) < 0 || strcmp (n, NAME__MAIN))
3053 switch (is_ctor_dtor (name))
3056 add_to_list (&constructors, name);
3060 add_to_list (&destructors, name);
3063 default: /* not a constructor or destructor */
3069 fprintf (stderr, "\ttype = 0x%.4x, sc = 0x%.2x, flags = 0x%.8x, name = %.30s\n",
3070 sym->si_type, sym->si_sc_type, sym->si_flags, name);
3075 if (symbol_load_cmds == 0)
3076 fatal ("no symbol table found");
3078 /* Update the program file now, rewrite header and load commands. At present,
3079 we assume that there is enough space after the last load command to insert
3080 one more. Since the first section written out is page aligned, and the
3081 number of load commands is small, this is ok for the present. */
3085 load_union_t *load_map;
3088 if (cmd_strings == -1)
3089 fatal ("no cmd_strings found");
3091 /* Add __main to initializer list.
3092 If we are building a program instead of a shared library, don't
3093 do anything, since in the current version, you cannot do mallocs
3094 and such in the constructors. */
3096 if (main_sym != (symbol_info_t *) 0
3097 && ((hdr.moh_flags & MOH_EXECABLE_F) == 0))
3098 add_func_table (&hdr, load_array, main_sym, FNTC_INITIALIZATION);
3101 fprintf (stderr, "\nUpdating header and load commands.\n\n");
3103 hdr.moh_n_load_cmds++;
3104 size = sizeof (load_cmd_map_command_t) + (sizeof (mo_offset_t) * (hdr.moh_n_load_cmds - 1));
3106 /* Create new load command map. */
3108 fprintf (stderr, "load command map, %d cmds, new size %ld.\n",
3109 (int)hdr.moh_n_load_cmds, (long)size);
3111 load_map = (load_union_t *) xcalloc (1, size);
3112 load_map->map.ldc_header.ldci_cmd_type = LDC_CMD_MAP;
3113 load_map->map.ldc_header.ldci_cmd_size = size;
3114 load_map->map.lcm_ld_cmd_strings = cmd_strings;
3115 load_map->map.lcm_nentries = hdr.moh_n_load_cmds;
3116 load_array[hdr.moh_n_load_cmds-1].load = load_map;
3118 offset = hdr.moh_first_cmd_off;
3119 for (i = 0; i < hdr.moh_n_load_cmds; i++)
3121 load_map->map.lcm_map[i] = offset;
3122 if (load_array[i].load->hdr.ldci_cmd_type == LDC_CMD_MAP)
3123 hdr.moh_load_map_cmd_off = offset;
3125 offset += load_array[i].load->hdr.ldci_cmd_size;
3128 hdr.moh_sizeofcmds = offset - MO_SIZEOF_RAW_HDR;
3131 print_header (&hdr);
3134 status = encode_mach_o_hdr (&hdr, obj, MO_SIZEOF_RAW_HDR);
3135 if (status != MO_HDR_CONV_SUCCESS)
3136 bad_header (status);
3139 fprintf (stderr, "writing load commands.\n\n");
3141 /* Write load commands */
3142 offset = hdr.moh_first_cmd_off;
3143 for (i = 0; i < hdr.moh_n_load_cmds; i++)
3145 load_union_t *load_hdr = load_array[i].load;
3146 size_t size = load_hdr->hdr.ldci_cmd_size;
3149 print_load_command (load_hdr, offset, i);
3151 bcopy ((char *) load_hdr, (char *) (obj + offset), size);
3156 end_file (obj_file);
3158 if (close (prog_fd))
3159 fatal_perror ("closing %s", prog_name);
3162 fprintf (stderr, "\n");
3166 /* Add a function table to the load commands to call a function
3167 on initiation or termination of the process. */
3170 add_func_table (hdr_p, load_array, sym, type)
3171 mo_header_t *hdr_p; /* pointer to global header */
3172 load_all_t *load_array; /* array of ptrs to load cmds */
3173 symbol_info_t *sym; /* pointer to symbol entry */
3174 int type; /* fntc_type value */
3176 /* Add a new load command. */
3177 int num_cmds = ++hdr_p->moh_n_load_cmds;
3178 int load_index = num_cmds - 1;
3179 size_t size = sizeof (func_table_command_t) + sizeof (mo_addr_t);
3180 load_union_t *ptr = xcalloc (1, size);
3181 load_all_t *load_cmd;
3184 /* Set the unresolved address bit in the header to force the loader to be
3185 used, since kernel exec does not call the initialization functions. */
3186 hdr_p->moh_flags |= MOH_UNRESOLVED_F;
3188 load_cmd = &load_array[load_index];
3189 load_cmd->load = ptr;
3190 load_cmd->section = (char *) 0;
3192 /* Fill in func table load command. */
3193 ptr->func.ldc_header.ldci_cmd_type = LDC_FUNC_TABLE;
3194 ptr->func.ldc_header.ldci_cmd_size = size;
3195 ptr->func.ldc_header.ldci_section_off = 0;
3196 ptr->func.ldc_header.ldci_section_len = 0;
3197 ptr->func.fntc_type = type;
3198 ptr->func.fntc_nentries = 1;
3200 /* copy address, turn it from abs. address to (region,offset) if necessary. */
3201 /* Is the symbol already expressed as (region, offset)? */
3202 if ((sym->si_flags & SI_ABSOLUTE_VALUE_F) == 0)
3204 ptr->func.fntc_entry_loc[i].adr_lcid = sym->si_value.def_val.adr_lcid;
3205 ptr->func.fntc_entry_loc[i].adr_sctoff = sym->si_value.def_val.adr_sctoff;
3208 /* If not, figure out which region it's in. */
3211 mo_vm_addr_t addr = sym->si_value.abs_val;
3214 for (i = 0; i < load_index; i++)
3216 if (load_array[i].load->hdr.ldci_cmd_type == LDC_REGION)
3218 region_command_t *region_ptr = &load_array[i].load->region;
3220 if ((region_ptr->regc_flags & REG_ABS_ADDR_F) != 0
3221 && addr >= region_ptr->regc_addr.vm_addr
3222 && addr <= region_ptr->regc_addr.vm_addr + region_ptr->regc_vm_size)
3224 ptr->func.fntc_entry_loc[0].adr_lcid = i;
3225 ptr->func.fntc_entry_loc[0].adr_sctoff = addr - region_ptr->regc_addr.vm_addr;
3233 fatal ("could not convert 0x%l.8x into a region", addr);
3238 "%s function, region %d, offset = %ld (0x%.8lx)\n",
3239 (type == FNTC_INITIALIZATION) ? "init" : "term",
3240 (int)ptr->func.fntc_entry_loc[i].adr_lcid,
3241 (long)ptr->func.fntc_entry_loc[i].adr_sctoff,
3242 (long)ptr->func.fntc_entry_loc[i].adr_sctoff);
3247 /* Print the global header for an OSF/rose object. */
3250 print_header (hdr_ptr)
3251 mo_header_t *hdr_ptr;
3253 fprintf (stderr, "\nglobal header:\n");
3254 fprintf (stderr, "\tmoh_magic = 0x%.8lx\n", hdr_ptr->moh_magic);
3255 fprintf (stderr, "\tmoh_major_version = %d\n", (int)hdr_ptr->moh_major_version);
3256 fprintf (stderr, "\tmoh_minor_version = %d\n", (int)hdr_ptr->moh_minor_version);
3257 fprintf (stderr, "\tmoh_header_version = %d\n", (int)hdr_ptr->moh_header_version);
3258 fprintf (stderr, "\tmoh_max_page_size = %d\n", (int)hdr_ptr->moh_max_page_size);
3259 fprintf (stderr, "\tmoh_byte_order = %d\n", (int)hdr_ptr->moh_byte_order);
3260 fprintf (stderr, "\tmoh_data_rep_id = %d\n", (int)hdr_ptr->moh_data_rep_id);
3261 fprintf (stderr, "\tmoh_cpu_type = %d\n", (int)hdr_ptr->moh_cpu_type);
3262 fprintf (stderr, "\tmoh_cpu_subtype = %d\n", (int)hdr_ptr->moh_cpu_subtype);
3263 fprintf (stderr, "\tmoh_vendor_type = %d\n", (int)hdr_ptr->moh_vendor_type);
3264 fprintf (stderr, "\tmoh_load_map_cmd_off = %d\n", (int)hdr_ptr->moh_load_map_cmd_off);
3265 fprintf (stderr, "\tmoh_first_cmd_off = %d\n", (int)hdr_ptr->moh_first_cmd_off);
3266 fprintf (stderr, "\tmoh_sizeofcmds = %d\n", (int)hdr_ptr->moh_sizeofcmds);
3267 fprintf (stderr, "\tmon_n_load_cmds = %d\n", (int)hdr_ptr->moh_n_load_cmds);
3268 fprintf (stderr, "\tmoh_flags = 0x%.8lx", (long)hdr_ptr->moh_flags);
3270 if (hdr_ptr->moh_flags & MOH_RELOCATABLE_F)
3271 fprintf (stderr, ", relocatable");
3273 if (hdr_ptr->moh_flags & MOH_LINKABLE_F)
3274 fprintf (stderr, ", linkable");
3276 if (hdr_ptr->moh_flags & MOH_EXECABLE_F)
3277 fprintf (stderr, ", execable");
3279 if (hdr_ptr->moh_flags & MOH_EXECUTABLE_F)
3280 fprintf (stderr, ", executable");
3282 if (hdr_ptr->moh_flags & MOH_UNRESOLVED_F)
3283 fprintf (stderr, ", unresolved");
3285 fprintf (stderr, "\n\n");
3290 /* Print a short summary of a load command. */
3293 print_load_command (load_hdr, offset, number)
3294 load_union_t *load_hdr;
3298 mo_long_t type = load_hdr->hdr.ldci_cmd_type;
3299 char *type_str = (char *) 0;
3303 case LDC_UNDEFINED: type_str = "UNDEFINED"; break;
3304 case LDC_CMD_MAP: type_str = "CMD_MAP"; break;
3305 case LDC_INTERPRETER: type_str = "INTERPRETER"; break;
3306 case LDC_STRINGS: type_str = "STRINGS"; break;
3307 case LDC_REGION: type_str = "REGION"; break;
3308 case LDC_RELOC: type_str = "RELOC"; break;
3309 case LDC_PACKAGE: type_str = "PACKAGE"; break;
3310 case LDC_SYMBOLS: type_str = "SYMBOLS"; break;
3311 case LDC_ENTRY: type_str = "ENTRY"; break;
3312 case LDC_FUNC_TABLE: type_str = "FUNC_TABLE"; break;
3313 case LDC_GEN_INFO: type_str = "GEN_INFO"; break;
3317 "cmd %2d, sz: 0x%.2lx, coff: 0x%.3lx, doff: 0x%.6lx, dlen: 0x%.6lx",
3319 (long) load_hdr->hdr.ldci_cmd_size,
3321 (long) load_hdr->hdr.ldci_section_off,
3322 (long) load_hdr->hdr.ldci_section_len);
3324 if (type_str == (char *) 0)
3325 fprintf (stderr, ", ty: unknown (%ld)\n", (long) type);
3327 else if (type != LDC_REGION)
3328 fprintf (stderr, ", ty: %s\n", type_str);
3333 switch (load_hdr->region.regc_usage_type)
3335 case REG_TEXT_T: region = ", .text"; break;
3336 case REG_DATA_T: region = ", .data"; break;
3337 case REG_BSS_T: region = ", .bss"; break;
3338 case REG_GLUE_T: region = ", .glue"; break;
3339 #if defined (REG_RDATA_T) && defined (REG_SDATA_T) && defined (REG_SBSS_T) /*mips*/
3340 case REG_RDATA_T: region = ", .rdata"; break;
3341 case REG_SDATA_T: region = ", .sdata"; break;
3342 case REG_SBSS_T: region = ", .sbss"; break;
3346 fprintf (stderr, ", ty: %s, vaddr: 0x%.8lx, vlen: 0x%.6lx%s\n",
3348 (long) load_hdr->region.regc_vm_addr,
3349 (long) load_hdr->region.regc_vm_size,
3357 /* Fatal error when {en,de}code_mach_o_header fails. */
3363 char *msg = (char *) 0;
3367 case MO_ERROR_BAD_MAGIC: msg = "bad magic number"; break;
3368 case MO_ERROR_BAD_HDR_VERS: msg = "bad header version"; break;
3369 case MO_ERROR_BAD_RAW_HDR_VERS: msg = "bad raw header version"; break;
3370 case MO_ERROR_BUF2SML: msg = "raw header buffer too small"; break;
3371 case MO_ERROR_OLD_RAW_HDR_FILE: msg = "old raw header file"; break;
3372 case MO_ERROR_UNSUPPORTED_VERS: msg = "unsupported version"; break;
3375 if (msg == (char *) 0)
3376 fatal ("unknown {de,en}code_mach_o_hdr return value %d", status);
3382 /* Read a file into a memory buffer. */
3384 static struct file_info *
3385 read_file (name, fd, rw)
3386 char *name; /* filename */
3387 int fd; /* file descriptor */
3388 int rw; /* read/write */
3390 struct stat stat_pkt;
3391 struct file_info *p = (struct file_info *) xcalloc (sizeof (struct file_info), 1);
3393 static int page_size;
3396 if (fstat (fd, &stat_pkt) < 0)
3397 fatal_perror ("fstat %s", name);
3400 p->size = stat_pkt.st_size;
3401 p->rounded_size = stat_pkt.st_size;
3407 fprintf (stderr, "mmap %s, %s\n", name, (rw) ? "read/write" : "read-only");
3410 page_size = sysconf (_SC_PAGE_SIZE);
3412 p->rounded_size = ((p->size + page_size - 1) / page_size) * page_size;
3413 p->start = mmap ((caddr_t) 0,
3414 (rw) ? p->rounded_size : p->size,
3415 (rw) ? (PROT_READ | PROT_WRITE) : PROT_READ,
3416 MAP_FILE | MAP_VARIABLE | MAP_SHARED,
3420 if (p->start != (char *) 0 && p->start != (char *) -1)
3424 #endif /* USE_MMAP */
3429 fprintf (stderr, "read %s\n", name);
3432 p->start = xmalloc (p->size);
3433 if (lseek (fd, 0L, SEEK_SET) < 0)
3434 fatal_perror ("lseek to 0 on %s", name);
3436 len = read (fd, p->start, p->size);
3438 fatal_perror ("read %s", name);
3441 fatal ("read %ld bytes, expected %ld, from %s", len, p->size, name);
3447 /* Do anything necessary to write a file back from memory. */
3451 struct file_info *ptr; /* file information block */
3459 fprintf (stderr, "msync %s\n", ptr->name);
3461 if (msync (ptr->start, ptr->rounded_size, MS_ASYNC))
3462 fatal_perror ("msync %s", ptr->name);
3466 fprintf (stderr, "munmap %s\n", ptr->name);
3468 if (munmap (ptr->start, ptr->size))
3469 fatal_perror ("munmap %s", ptr->name);
3472 #endif /* USE_MMAP */
3479 fprintf (stderr, "write %s\n", ptr->name);
3481 if (lseek (ptr->fd, 0L, SEEK_SET) < 0)
3482 fatal_perror ("lseek to 0 on %s", ptr->name);
3484 len = write (ptr->fd, ptr->start, ptr->size);
3486 fatal_perror ("write %s", ptr->name);
3488 if (len != ptr->size)
3489 fatal ("wrote %ld bytes, expected %ld, to %s", len, ptr->size, ptr->name);
3498 #endif /* OBJECT_FORMAT_ROSE */