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. */
37 #include "gansidecl.h"
43 extern char *sys_errlist
[];
49 /* Obstack allocation and deallocation routines. */
50 #define obstack_chunk_alloc xmalloc
51 #define obstack_chunk_free free
58 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
61 #define WTERMSIG(S) ((S) & 0x7f)
64 #define WIFEXITED(S) (((S) & 0xff) == 0)
67 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
70 extern char *choose_temp_base ();
72 /* On certain systems, we have code that works by scanning the object file
73 directly. But this code uses system-specific header files and library
74 functions, so turn it off in a cross-compiler. Likewise, the names of
75 the utilities are not correct for a cross-compiler; we have to hope that
76 cross-versions are in the proper directories. */
79 #undef SUNOS4_SHARED_LIBRARIES
80 #undef OBJECT_FORMAT_COFF
81 #undef OBJECT_FORMAT_ROSE
83 #undef REAL_LD_FILE_NAME
84 #undef REAL_NM_FILE_NAME
85 #undef REAL_STRIP_FILE_NAME
88 /* If we cannot use a special method, use the ordinary one:
89 run nm to find what symbols are present.
90 In a cross-compiler, this means you need a cross nm,
91 but that is not quite as unpleasant as special headers. */
93 #if !defined (OBJECT_FORMAT_COFF) && !defined (OBJECT_FORMAT_ROSE)
94 #define OBJECT_FORMAT_NONE
97 #ifdef OBJECT_FORMAT_COFF
106 /* Many versions of ldfcn.h define these. */
114 /* Some systems have an ISCOFF macro, but others do not. In some cases
115 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
116 that either do not have an ISCOFF macro in /usr/include or for those
117 where it is wrong. */
120 #define MY_ISCOFF(X) ISCOFF (X)
123 #endif /* OBJECT_FORMAT_COFF */
125 #ifdef OBJECT_FORMAT_ROSE
132 #include <sys/mman.h>
136 #include <mach_o_format.h>
137 #include <mach_o_header.h>
138 #include <mach_o_vals.h>
139 #include <mach_o_types.h>
141 #endif /* OBJECT_FORMAT_ROSE */
143 #ifdef OBJECT_FORMAT_NONE
145 /* Default flags to pass to nm. */
147 #define NM_FLAGS "-p"
150 #endif /* OBJECT_FORMAT_NONE */
152 /* Some systems use __main in a way incompatible with its use in gcc, in these
153 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
154 give the same symbol without quotes for an alternative entry point. You
155 must define both, or neither. */
157 #define NAME__MAIN "__main"
158 #define SYMBOL__MAIN __main
161 #if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES
162 #define SCAN_LIBRARIES
166 int do_collecting
= 1;
168 int do_collecting
= 0;
171 /* Linked lists of constructor and destructor names. */
187 /* Enumeration giving which pass this is for scanning the program file. */
190 PASS_FIRST
, /* without constructors */
191 PASS_OBJ
, /* individual objects */
192 PASS_LIB
, /* looking for shared libraries */
193 PASS_SECOND
/* with constructors linked in */
196 #ifndef NO_SYS_SIGLIST
197 #ifndef SYS_SIGLIST_DECLARED
198 extern char *sys_siglist
[];
201 extern char *version_string
;
203 int vflag
; /* true if -v */
204 static int rflag
; /* true if -r */
205 static int strip_flag
; /* true if -s */
206 #ifdef COLLECT_EXPORT_LIST
207 static int export_flag
; /* true if -bE */
210 int debug
; /* true if -debug */
212 static int shared_obj
; /* true if -shared */
214 static int temp_filename_length
; /* Length of temp_filename */
215 static char *temp_filename
; /* Base of temp filenames */
216 static char *c_file
; /* <xxx>.c for constructor/destructor list. */
217 static char *o_file
; /* <xxx>.o for constructor/destructor list. */
218 #ifdef COLLECT_EXPORT_LIST
219 static char *export_file
; /* <xxx>.x for AIX export list. */
220 static char *import_file
; /* <xxx>.p for AIX import list. */
222 char *ldout
; /* File for ld errors. */
223 static char *output_file
; /* Output file for ld. */
224 static char *nm_file_name
; /* pathname of nm */
226 static char *ldd_file_name
; /* pathname of ldd (or equivalent) */
228 static char *strip_file_name
; /* pathname of strip */
229 char *c_file_name
; /* pathname of gcc */
230 static char *initname
, *fininame
; /* names of init and fini funcs */
232 static struct head constructors
; /* list of constructors found */
233 static struct head destructors
; /* list of destructors found */
234 #ifdef COLLECT_EXPORT_LIST
235 static struct head exports
; /* list of exported symbols */
236 static struct head imports
; /* list of imported symbols */
237 static struct head undefined
; /* list of undefined symbols */
239 static struct head frame_tables
; /* list of frame unwind info tables */
241 struct obstack temporary_obstack
;
242 struct obstack permanent_obstack
;
243 char * temporary_firstobj
;
245 /* Defined in the automatically-generated underscore.c. */
246 extern int prepends_underscore
;
248 extern char *getenv ();
249 extern char *mktemp ();
250 extern FILE *fdopen ();
252 #ifndef GET_ENVIRONMENT
253 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ENV_VALUE = getenv (ENV_NAME)
256 /* Structure to hold all the directories in which to search for files to
261 char *prefix
; /* String to prepend to the path. */
262 struct prefix_list
*next
; /* Next in linked list. */
267 struct prefix_list
*plist
; /* List of prefixes to try */
268 int max_len
; /* Max length of a prefix in PLIST */
269 char *name
; /* Name of this list (used in config stuff) */
272 #ifdef COLLECT_EXPORT_LIST
273 /* Lists to keep libraries to be scanned for global constructors/destructors. */
274 static struct head libs
; /* list of libraries */
275 static struct path_prefix cmdline_lib_dirs
; /* directories specified with -L */
276 static struct path_prefix libpath_lib_dirs
; /* directories in LIBPATH */
277 static struct path_prefix
*libpaths
[3] = {&cmdline_lib_dirs
,
278 &libpath_lib_dirs
, NULL
};
279 static char *libexts
[3] = {"a", "so", NULL
}; /* possible library extentions */
282 void collect_exit
PROTO((int));
283 void collect_execute
PROTO((char *, char **, char *));
284 void dump_file
PROTO((char *));
285 static void handler
PROTO((int));
286 static int is_ctor_dtor
PROTO((char *));
287 static char *find_a_file
PROTO((struct path_prefix
*, char *));
288 static void add_prefix
PROTO((struct path_prefix
*, char *));
289 static void prefix_from_env
PROTO((char *, struct path_prefix
*));
290 static void prefix_from_string
PROTO((char *, struct path_prefix
*));
291 static void do_wait
PROTO((char *));
292 static void fork_execute
PROTO((char *, char **));
293 static void maybe_unlink
PROTO((char *));
294 static void add_to_list
PROTO((struct head
*, char *));
295 static void write_list
PROTO((FILE *, char *, struct id
*));
296 static void dump_list
PROTO((FILE *, char *, struct id
*));
297 static void dump_prefix_list
PROTO((FILE *, char *, struct prefix_list
*));
298 static void write_list_with_asm
PROTO((FILE *, char *, struct id
*));
299 static void write_c_file
PROTO((FILE *, char *));
300 static void scan_prog_file
PROTO((char *, enum pass
));
301 #ifdef SCAN_LIBRARIES
302 static void scan_libraries
PROTO((char *));
304 #ifdef COLLECT_EXPORT_LIST
305 static int is_in_list
PROTO((char *, struct id
*));
306 static void write_export_file
PROTO((FILE *));
307 static void write_import_file
PROTO((FILE *));
308 static char *resolve_lib_name
PROTO((char *));
309 static int use_import_list
PROTO((char *));
310 static int ignore_library
PROTO((char *));
330 while ((fd
= dup (oldfd
)) != newfd
&& fd
>= 0) /* good enough for low fd's */
333 close (fdtmp
[--fdx
]);
349 static char buffer
[30];
353 if (e
> 0 && e
< sys_nerr
)
354 return sys_errlist
[e
];
356 sprintf (buffer
, "Unknown error %d", e
);
361 /* Delete tempfiles and exit function. */
364 collect_exit (status
)
367 if (c_file
!= 0 && c_file
[0])
368 maybe_unlink (c_file
);
370 if (o_file
!= 0 && o_file
[0])
371 maybe_unlink (o_file
);
373 #ifdef COLLECT_EXPORT_LIST
374 if (export_file
!= 0 && export_file
[0])
375 maybe_unlink (export_file
);
377 if (import_file
!= 0 && import_file
[0])
378 maybe_unlink (import_file
);
381 if (ldout
!= 0 && ldout
[0])
384 maybe_unlink (ldout
);
387 if (status
!= 0 && output_file
!= 0 && output_file
[0])
388 maybe_unlink (output_file
);
394 /* Die when sys call fails. */
397 fatal_perror (string
, arg1
, arg2
, arg3
)
398 char *string
, *arg1
, *arg2
, *arg3
;
402 fprintf (stderr
, "collect2: ");
403 fprintf (stderr
, string
, arg1
, arg2
, arg3
);
404 fprintf (stderr
, ": %s\n", my_strerror (e
));
405 collect_exit (FATAL_EXIT_CODE
);
411 fatal (string
, arg1
, arg2
, arg3
)
412 char *string
, *arg1
, *arg2
, *arg3
;
414 fprintf (stderr
, "collect2: ");
415 fprintf (stderr
, string
, arg1
, arg2
, arg3
);
416 fprintf (stderr
, "\n");
417 collect_exit (FATAL_EXIT_CODE
);
420 /* Write error message. */
423 error (string
, arg1
, arg2
, arg3
, arg4
)
424 char *string
, *arg1
, *arg2
, *arg3
, *arg4
;
426 fprintf (stderr
, "collect2: ");
427 fprintf (stderr
, string
, arg1
, arg2
, arg3
, arg4
);
428 fprintf (stderr
, "\n");
431 /* In case obstack is linked in, and abort is defined to fancy_abort,
432 provide a default entry. */
437 fatal ("internal error");
445 if (c_file
!= 0 && c_file
[0])
446 maybe_unlink (c_file
);
448 if (o_file
!= 0 && o_file
[0])
449 maybe_unlink (o_file
);
451 if (ldout
!= 0 && ldout
[0])
452 maybe_unlink (ldout
);
454 #ifdef COLLECT_EXPORT_LIST
455 if (export_file
!= 0 && export_file
[0])
456 maybe_unlink (export_file
);
458 if (import_file
!= 0 && import_file
[0])
459 maybe_unlink (import_file
);
462 signal (signo
, SIG_DFL
);
463 kill (getpid (), signo
);
468 xcalloc (size1
, size2
)
471 char *ptr
= (char *) calloc (size1
, size2
);
475 fatal ("out of memory");
483 char *ptr
= (char *) malloc (size
);
487 fatal ("out of memory");
496 register char *value
= (char *) realloc (ptr
, size
);
498 fatal ("virtual memory exhausted");
506 return access (name
, R_OK
) == 0;
509 /* Make a copy of a string INPUT with size SIZE. */
512 savestring (input
, size
)
516 char *output
= (char *) xmalloc (size
+ 1);
517 bcopy (input
, output
, size
);
522 /* Parse a reasonable subset of shell quoting syntax. */
539 obstack_1grow (&temporary_obstack
, c
);
540 else if (! inside
&& c
== ' ')
542 else if (! inside
&& c
== '\\')
547 obstack_1grow (&temporary_obstack
, c
);
550 obstack_1grow (&temporary_obstack
, '\0');
552 return obstack_finish (&temporary_obstack
);
559 FILE *stream
= fopen (name
, "r");
560 int no_demangle
= !! getenv ("COLLECT_NO_DEMANGLE");
567 while (c
= getc (stream
),
568 c
!= EOF
&& (ISALNUM (c
) || c
== '_' || c
== '$' || c
== '.'))
569 obstack_1grow (&temporary_obstack
, c
);
570 if (obstack_object_size (&temporary_obstack
) > 0)
572 char *word
, *p
, *result
;
573 obstack_1grow (&temporary_obstack
, '\0');
574 word
= obstack_finish (&temporary_obstack
);
577 ++word
, putc ('.', stderr
);
579 if (*p
== '_' && prepends_underscore
)
585 result
= cplus_demangle (p
, DMGL_PARAMS
| DMGL_ANSI
);
590 fputs (result
, stderr
);
592 diff
= strlen (word
) - strlen (result
);
594 --diff
, putc (' ', stderr
);
595 while (diff
< 0 && c
== ' ')
596 ++diff
, c
= getc (stream
);
601 fputs (word
, stderr
);
604 obstack_free (&temporary_obstack
, temporary_firstobj
);
613 /* Decide whether the given symbol is:
614 a constructor (1), a destructor (2), or neither (0). */
620 struct names
{ char *name
; int len
; int ret
; int two_underscores
; };
622 register struct names
*p
;
624 register char *orig_s
= s
;
626 static struct names special
[] = {
627 #ifdef NO_DOLLAR_IN_LABEL
628 #ifdef NO_DOT_IN_LABEL
629 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
630 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
631 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
633 { "GLOBAL_.I.", sizeof ("GLOBAL_.I.")-1, 1, 0 },
634 { "GLOBAL_.D.", sizeof ("GLOBAL_.D.")-1, 2, 0 },
635 { "GLOBAL_.F.", sizeof ("GLOBAL_.F.")-1, 5, 0 },
638 { "GLOBAL_$I$", sizeof ("GLOBAL_$I$")-1, 1, 0 },
639 { "GLOBAL_$D$", sizeof ("GLOBAL_$D$")-1, 2, 0 },
640 { "GLOBAL_$F$", sizeof ("GLOBAL_$F$")-1, 5, 0 },
642 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
643 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
644 #ifdef CFRONT_LOSSAGE /* Do not collect cfront initialization functions.
645 cfront has its own linker procedure to collect them;
646 if collect2 gets them too, they get collected twice
647 when the cfront procedure is run and the compiler used
648 for linking happens to be GCC. */
649 { "sti__", sizeof ("sti__")-1, 1, 1 },
650 { "std__", sizeof ("std__")-1, 2, 1 },
651 #endif /* CFRONT_LOSSAGE */
655 while ((ch
= *s
) == '_')
661 for (p
= &special
[0]; p
->len
> 0; p
++)
664 && (!p
->two_underscores
|| ((s
- orig_s
) >= 2))
665 && strncmp(s
, p
->name
, p
->len
) == 0)
673 /* Routine to add variables to the environment. */
681 #ifndef VMS /* nor about VMS */
683 extern char **environ
;
684 char **old_environ
= environ
;
691 while ((ch
= *p
++) != '\0' && ch
!= '=')
697 /* Search for replacing an existing environment variable, and
698 count the number of total environment variables. */
699 for (envp
= old_environ
; *envp
; envp
++)
702 if (!strncmp (str
, *envp
, name_len
))
709 /* Add a new environment variable */
710 environ
= (char **) xmalloc (sizeof (char *) * (num_envs
+2));
712 bcopy ((char *) old_environ
, (char *) (environ
+ 1),
713 sizeof (char *) * (num_envs
+1));
719 #endif /* HAVE_PUTENV */
721 /* By default, colon separates directories in a path. */
722 #ifndef PATH_SEPARATOR
723 #define PATH_SEPARATOR ':'
726 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
727 and one from the PATH variable. */
729 static struct path_prefix cpath
, path
;
732 /* This is the name of the target machine. We use it to form the name
733 of the files to execute. */
735 static char *target_machine
= TARGET_MACHINE
;
738 /* Search for NAME using prefix list PPREFIX. We only look for executable
741 Return 0 if not found, otherwise return its name, allocated with malloc. */
744 find_a_file (pprefix
, name
)
745 struct path_prefix
*pprefix
;
749 struct prefix_list
*pl
;
750 int len
= pprefix
->max_len
+ strlen (name
) + 1;
753 fprintf (stderr
, "Looking for '%s'\n", name
);
755 #ifdef EXECUTABLE_SUFFIX
756 len
+= strlen (EXECUTABLE_SUFFIX
);
759 temp
= xmalloc (len
);
761 /* Determine the filename to execute (special case for absolute paths). */
765 || (DIR_SEPARATOR
== '\\' && name
[1] == ':'
766 && (name
[2] == DIR_SEPARATOR
|| name
[2] == '/'))
770 if (access (name
, X_OK
) == 0)
775 fprintf (stderr
, " - found: absolute path\n");
781 fprintf (stderr
, " - failed to locate using absolute path\n");
784 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
786 strcpy (temp
, pl
->prefix
);
789 if (access (temp
, X_OK
) == 0)
792 #ifdef EXECUTABLE_SUFFIX
793 /* Some systems have a suffix for executable files.
794 So try appending that. */
795 strcat (temp
, EXECUTABLE_SUFFIX
);
797 if (access (temp
, X_OK
) == 0)
802 if (debug
&& pprefix
->plist
== NULL
)
803 fprintf (stderr
, " - failed: no entries in prefix list\n");
809 /* Add an entry for PREFIX to prefix list PPREFIX. */
812 add_prefix (pprefix
, prefix
)
813 struct path_prefix
*pprefix
;
816 struct prefix_list
*pl
, **prev
;
821 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
826 prev
= &pprefix
->plist
;
828 /* Keep track of the longest prefix */
830 len
= strlen (prefix
);
831 if (len
> pprefix
->max_len
)
832 pprefix
->max_len
= len
;
834 pl
= (struct prefix_list
*) xmalloc (sizeof (struct prefix_list
));
835 pl
->prefix
= savestring (prefix
, len
);
840 pl
->next
= (struct prefix_list
*) 0;
844 /* Take the value of the environment variable ENV, break it into a path, and
845 add of the entries to PPREFIX. */
848 prefix_from_env (env
, pprefix
)
850 struct path_prefix
*pprefix
;
853 GET_ENVIRONMENT (p
, env
);
856 prefix_from_string (p
, pprefix
);
860 prefix_from_string (p
, pprefix
)
862 struct path_prefix
*pprefix
;
865 char *nstore
= (char *) xmalloc (strlen (p
) + 3);
868 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
873 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
875 strncpy (nstore
, startp
, endp
-startp
);
878 strcpy (nstore
, "./");
880 else if (endp
[-1] != '/')
882 nstore
[endp
-startp
] = '/';
883 nstore
[endp
-startp
+1] = 0;
886 nstore
[endp
-startp
] = 0;
889 fprintf (stderr
, " - add prefix: %s\n", nstore
);
891 add_prefix (pprefix
, nstore
);
894 endp
= startp
= endp
+ 1;
908 char *ld_suffix
= "ld";
909 char *full_ld_suffix
= ld_suffix
;
910 char *real_ld_suffix
= "real-ld";
911 char *collect_ld_suffix
= "collect-ld";
912 char *nm_suffix
= "nm";
913 char *full_nm_suffix
= nm_suffix
;
914 char *gnm_suffix
= "gnm";
915 char *full_gnm_suffix
= gnm_suffix
;
917 char *ldd_suffix
= LDD_SUFFIX
;
918 char *full_ldd_suffix
= ldd_suffix
;
920 char *strip_suffix
= "strip";
921 char *full_strip_suffix
= strip_suffix
;
922 char *gstrip_suffix
= "gstrip";
923 char *full_gstrip_suffix
= gstrip_suffix
;
926 #ifdef COLLECT_EXPORT_LIST
934 char **ld1_argv
= (char **) xcalloc (sizeof (char *), argc
+3);
935 char **ld1
= ld1_argv
;
936 char **ld2_argv
= (char **) xcalloc (sizeof (char *), argc
+6);
937 char **ld2
= ld2_argv
;
938 char **object_lst
= (char **) xcalloc (sizeof (char *), argc
);
939 char **object
= object_lst
;
941 int num_c_args
= argc
+7;
947 /* Parse command line early for instances of -debug. This allows
948 the debug flag to be set before functions like find_a_file()
953 for (i
= 1; argv
[i
] != NULL
; i
++)
954 if (! strcmp (argv
[i
], "-debug"))
959 #ifndef DEFAULT_A_OUT_NAME
960 output_file
= "a.out";
962 output_file
= DEFAULT_A_OUT_NAME
;
965 obstack_begin (&temporary_obstack
, 0);
966 obstack_begin (&permanent_obstack
, 0);
967 temporary_firstobj
= (char *) obstack_alloc (&temporary_obstack
, 0);
969 current_demangling_style
= gnu_demangling
;
970 p
= getenv ("COLLECT_GCC_OPTIONS");
973 char *q
= extract_string (&p
);
974 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
977 obstack_free (&temporary_obstack
, temporary_firstobj
);
980 c_ptr
= c_argv
= (char **) xcalloc (sizeof (char *), num_c_args
);
983 fatal ("no arguments");
986 if (signal (SIGQUIT
, SIG_IGN
) != SIG_IGN
)
987 signal (SIGQUIT
, handler
);
989 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
990 signal (SIGINT
, handler
);
992 if (signal (SIGALRM
, SIG_IGN
) != SIG_IGN
)
993 signal (SIGALRM
, handler
);
996 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
997 signal (SIGHUP
, handler
);
999 if (signal (SIGSEGV
, SIG_IGN
) != SIG_IGN
)
1000 signal (SIGSEGV
, handler
);
1002 if (signal (SIGBUS
, SIG_IGN
) != SIG_IGN
)
1003 signal (SIGBUS
, handler
);
1006 /* Extract COMPILER_PATH and PATH into our prefix list. */
1007 prefix_from_env ("COMPILER_PATH", &cpath
);
1008 prefix_from_env ("PATH", &path
);
1010 #ifdef CROSS_COMPILE
1011 /* If we look for a program in the compiler directories, we just use
1012 the short name, since these directories are already system-specific.
1013 But it we look for a program in the system directories, we need to
1014 qualify the program name with the target machine. */
1017 = xcalloc (strlen (ld_suffix
) + strlen (target_machine
) + 2, 1);
1018 strcpy (full_ld_suffix
, target_machine
);
1019 strcat (full_ld_suffix
, "-");
1020 strcat (full_ld_suffix
, ld_suffix
);
1024 = xcalloc (strlen (gld_suffix
) + strlen (target_machine
) + 2, 1);
1025 strcpy (full_gld_suffix
, target_machine
);
1026 strcat (full_gld_suffix
, "-");
1027 strcat (full_gld_suffix
, gld_suffix
);
1031 = xcalloc (strlen (nm_suffix
) + strlen (target_machine
) + 2, 1);
1032 strcpy (full_nm_suffix
, target_machine
);
1033 strcat (full_nm_suffix
, "-");
1034 strcat (full_nm_suffix
, nm_suffix
);
1037 = xcalloc (strlen (gnm_suffix
) + strlen (target_machine
) + 2, 1);
1038 strcpy (full_gnm_suffix
, target_machine
);
1039 strcat (full_gnm_suffix
, "-");
1040 strcat (full_gnm_suffix
, gnm_suffix
);
1044 = xcalloc (strlen (ldd_suffix
) + strlen (target_machine
) + 2, 1);
1045 strcpy (full_ldd_suffix
, target_machine
);
1046 strcat (full_ldd_suffix
, "-");
1047 strcat (full_ldd_suffix
, ldd_suffix
);
1051 = xcalloc (strlen (strip_suffix
) + strlen (target_machine
) + 2, 1);
1052 strcpy (full_strip_suffix
, target_machine
);
1053 strcat (full_strip_suffix
, "-");
1054 strcat (full_strip_suffix
, strip_suffix
);
1057 = xcalloc (strlen (gstrip_suffix
) + strlen (target_machine
) + 2, 1);
1058 strcpy (full_gstrip_suffix
, target_machine
);
1059 strcat (full_gstrip_suffix
, "-");
1060 strcat (full_gstrip_suffix
, gstrip_suffix
);
1061 #endif /* CROSS_COMPILE */
1063 /* Try to discover a valid linker/nm/strip to use. */
1065 /* Maybe we know the right file to use (if not cross). */
1066 #ifdef REAL_LD_FILE_NAME
1067 ld_file_name
= find_a_file (&path
, REAL_LD_FILE_NAME
);
1068 if (ld_file_name
== 0)
1070 /* Search the (target-specific) compiler dirs for ld'. */
1071 ld_file_name
= find_a_file (&cpath
, real_ld_suffix
);
1072 /* Likewise for `collect-ld'. */
1073 if (ld_file_name
== 0)
1074 ld_file_name
= find_a_file (&cpath
, collect_ld_suffix
);
1075 /* Search the compiler directories for `ld'. We have protection against
1076 recursive calls in find_a_file. */
1077 if (ld_file_name
== 0)
1078 ld_file_name
= find_a_file (&cpath
, ld_suffix
);
1079 /* Search the ordinary system bin directories
1080 for `ld' (if native linking) or `TARGET-ld' (if cross). */
1081 if (ld_file_name
== 0)
1082 ld_file_name
= find_a_file (&path
, full_ld_suffix
);
1084 #ifdef REAL_NM_FILE_NAME
1085 nm_file_name
= find_a_file (&path
, REAL_NM_FILE_NAME
);
1086 if (nm_file_name
== 0)
1088 nm_file_name
= find_a_file (&cpath
, gnm_suffix
);
1089 if (nm_file_name
== 0)
1090 nm_file_name
= find_a_file (&path
, full_gnm_suffix
);
1091 if (nm_file_name
== 0)
1092 nm_file_name
= find_a_file (&cpath
, nm_suffix
);
1093 if (nm_file_name
== 0)
1094 nm_file_name
= find_a_file (&path
, full_nm_suffix
);
1097 ldd_file_name
= find_a_file (&cpath
, ldd_suffix
);
1098 if (ldd_file_name
== 0)
1099 ldd_file_name
= find_a_file (&path
, full_ldd_suffix
);
1102 #ifdef REAL_STRIP_FILE_NAME
1103 strip_file_name
= find_a_file (&path
, REAL_STRIP_FILE_NAME
);
1104 if (strip_file_name
== 0)
1106 strip_file_name
= find_a_file (&cpath
, gstrip_suffix
);
1107 if (strip_file_name
== 0)
1108 strip_file_name
= find_a_file (&path
, full_gstrip_suffix
);
1109 if (strip_file_name
== 0)
1110 strip_file_name
= find_a_file (&cpath
, strip_suffix
);
1111 if (strip_file_name
== 0)
1112 strip_file_name
= find_a_file (&path
, full_strip_suffix
);
1114 /* Determine the full path name of the C compiler to use. */
1115 c_file_name
= getenv ("COLLECT_GCC");
1116 if (c_file_name
== 0)
1118 #ifdef CROSS_COMPILE
1119 c_file_name
= xcalloc (sizeof ("gcc-") + strlen (target_machine
) + 1, 1);
1120 strcpy (c_file_name
, target_machine
);
1121 strcat (c_file_name
, "-gcc");
1123 c_file_name
= "gcc";
1127 p
= find_a_file (&cpath
, c_file_name
);
1129 /* Here it should be safe to use the system search path since we should have
1130 already qualified the name of the compiler when it is needed. */
1132 p
= find_a_file (&path
, c_file_name
);
1137 *ld1
++ = *ld2
++ = ld_file_name
;
1139 /* Make temp file names. */
1140 temp_filename
= choose_temp_base ();
1141 temp_filename_length
= strlen (temp_filename
);
1142 c_file
= xcalloc (temp_filename_length
+ sizeof (".c"), 1);
1143 o_file
= xcalloc (temp_filename_length
+ sizeof (".o"), 1);
1144 #ifdef COLLECT_EXPORT_LIST
1145 export_file
= xmalloc (temp_filename_length
+ sizeof (".x"));
1146 import_file
= xmalloc (temp_filename_length
+ sizeof (".p"));
1148 ldout
= xmalloc (temp_filename_length
+ sizeof (".ld"));
1149 sprintf (ldout
, "%s.ld", temp_filename
);
1150 sprintf (c_file
, "%s.c", temp_filename
);
1151 sprintf (o_file
, "%s.o", temp_filename
);
1152 #ifdef COLLECT_EXPORT_LIST
1153 sprintf (export_file
, "%s.x", temp_filename
);
1154 sprintf (import_file
, "%s.p", temp_filename
);
1156 *c_ptr
++ = c_file_name
;
1161 #ifdef COLLECT_EXPORT_LIST
1162 /* Generate a list of directories from LIBPATH. */
1163 prefix_from_env ("LIBPATH", &libpath_lib_dirs
);
1164 /* Add to this list also two standard directories where
1165 AIX loader always searches for libraries. */
1166 add_prefix (&libpath_lib_dirs
, "/lib");
1167 add_prefix (&libpath_lib_dirs
, "/usr/lib");
1170 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1172 AIX support needs to know if -shared has been specified before
1173 parsing commandline arguments. */
1175 p
= getenv ("COLLECT_GCC_OPTIONS");
1178 char *q
= extract_string (&p
);
1179 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
1180 *c_ptr
++ = obstack_copy0 (&permanent_obstack
, q
, strlen (q
));
1181 if (strncmp (q
, "-shared", sizeof ("-shared") - 1) == 0)
1184 obstack_free (&temporary_obstack
, temporary_firstobj
);
1185 *c_ptr
++ = "-fno-exceptions";
1187 /* !!! When GCC calls collect2,
1188 it does not know whether it is calling collect2 or ld.
1189 So collect2 cannot meaningfully understand any options
1190 except those ld understands.
1191 If you propose to make GCC pass some other option,
1192 just imagine what will happen if ld is really ld!!! */
1194 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1195 /* After the first file, put in the c++ rt0. */
1198 while ((arg
= *++argv
) != (char *) 0)
1200 *ld1
++ = *ld2
++ = arg
;
1206 #ifdef COLLECT_EXPORT_LIST
1207 /* We want to disable automatic exports on AIX when user
1208 explicitly puts an export list in command line */
1210 if (arg
[2] == 'E' || strncmp (&arg
[2], "export", 6) == 0)
1216 if (!strcmp (arg
, "-debug"))
1218 /* Already parsed. */
1227 /* place o_file BEFORE this argument! */
1233 #ifdef COLLECT_EXPORT_LIST
1235 /* Resolving full library name. */
1236 char *s
= resolve_lib_name (arg
+2);
1238 /* If we will use an import list for this library,
1239 we should exclude it from ld args. */
1240 if (use_import_list (s
))
1246 /* Saving a full library name. */
1247 add_to_list (&libs
, s
);
1252 #ifdef COLLECT_EXPORT_LIST
1253 /* Saving directories where to search for libraries. */
1255 add_prefix (&cmdline_lib_dirs
, arg
+2);
1261 output_file
= *ld1
++ = *ld2
++ = *++argv
;
1263 output_file
= &arg
[2];
1272 if (arg
[2] == '\0' && do_collecting
)
1274 /* We must strip after the nm run, otherwise C++ linking
1275 will not work. Thus we strip in the second ld run, or
1276 else with strip if there is no second ld run. */
1288 else if ((p
= rindex (arg
, '.')) != (char *) 0
1289 && (strcmp (p
, ".o") == 0 || strcmp (p
, ".a") == 0
1290 || strcmp (p
, ".so") == 0))
1299 /* place o_file BEFORE this argument! */
1307 #ifdef COLLECT_EXPORT_LIST
1308 /* libraries can be specified directly, i.e. without -l flag. */
1311 /* If we will use an import list for this library,
1312 we should exclude it from ld args. */
1313 if (use_import_list (arg
))
1319 /* Saving a full library name. */
1320 add_to_list (&libs
, arg
);
1326 #ifdef COLLECT_EXPORT_LIST
1327 /* This is added only for debugging purposes. */
1330 fprintf (stderr
, "List of libraries:\n");
1331 dump_list (stderr
, "\t", libs
.first
);
1334 /* The AIX linker will discard static constructors in object files if
1335 nothing else in the file is referenced, so look at them first. */
1337 char **export_object_lst
= object_lst
;
1338 while (export_object_lst
< object
)
1339 scan_prog_file (*export_object_lst
++, PASS_OBJ
);
1342 struct id
*list
= libs
.first
;
1343 for (; list
; list
= list
->next
)
1344 scan_prog_file (list
->name
, PASS_FIRST
);
1347 char *buf1
= alloca (strlen (export_file
) + 5);
1348 char *buf2
= alloca (strlen (import_file
) + 5);
1349 sprintf (buf1
, "-bE:%s", export_file
);
1350 sprintf (buf2
, "-bI:%s", import_file
);
1355 exportf
= fopen (export_file
, "w");
1356 if (exportf
== (FILE *) 0)
1357 fatal_perror ("%s", export_file
);
1358 write_export_file (exportf
);
1359 if (fclose (exportf
))
1360 fatal_perror ("closing %s", export_file
);
1361 importf
= fopen (import_file
, "w");
1362 if (importf
== (FILE *) 0)
1363 fatal_perror ("%s", import_file
);
1364 write_import_file (importf
);
1365 if (fclose (importf
))
1366 fatal_perror ("closing %s", import_file
);
1371 *object
= *c_ptr
= *ld1
= (char *) 0;
1375 fprintf (stderr
, "collect2 version %s", version_string
);
1376 #ifdef TARGET_VERSION
1379 fprintf (stderr
, "\n");
1385 fprintf (stderr
, "ld_file_name = %s\n",
1386 (ld_file_name
? ld_file_name
: "not found"));
1387 fprintf (stderr
, "c_file_name = %s\n",
1388 (c_file_name
? c_file_name
: "not found"));
1389 fprintf (stderr
, "nm_file_name = %s\n",
1390 (nm_file_name
? nm_file_name
: "not found"));
1392 fprintf (stderr
, "ldd_file_name = %s\n",
1393 (ldd_file_name
? ldd_file_name
: "not found"));
1395 fprintf (stderr
, "strip_file_name = %s\n",
1396 (strip_file_name
? strip_file_name
: "not found"));
1397 fprintf (stderr
, "c_file = %s\n",
1398 (c_file
? c_file
: "not found"));
1399 fprintf (stderr
, "o_file = %s\n",
1400 (o_file
? o_file
: "not found"));
1402 ptr
= getenv ("COLLECT_GCC_OPTIONS");
1404 fprintf (stderr
, "COLLECT_GCC_OPTIONS = %s\n", ptr
);
1406 ptr
= getenv ("COLLECT_GCC");
1408 fprintf (stderr
, "COLLECT_GCC = %s\n", ptr
);
1410 ptr
= getenv ("COMPILER_PATH");
1412 fprintf (stderr
, "COMPILER_PATH = %s\n", ptr
);
1414 ptr
= getenv ("LIBRARY_PATH");
1416 fprintf (stderr
, "LIBRARY_PATH = %s\n", ptr
);
1418 fprintf (stderr
, "\n");
1421 /* Load the program, searching all libraries and attempting to provide
1422 undefined symbols from repository information. */
1424 /* On AIX we do this later. */
1425 #ifndef COLLECT_EXPORT_LIST
1426 do_tlink (ld1_argv
, object_lst
);
1429 /* If -r or they will be run via some other method, do not build the
1430 constructor or destructor list, just return now. */
1431 if (rflag
|| ! do_collecting
)
1433 #ifdef COLLECT_EXPORT_LIST
1434 /* But make sure we delete the export file we may have created. */
1435 if (export_file
!= 0 && export_file
[0])
1436 maybe_unlink (export_file
);
1437 if (import_file
!= 0 && import_file
[0])
1438 maybe_unlink (import_file
);
1443 /* Examine the namelist with nm and search it for static constructors
1444 and destructors to call.
1445 Write the constructor and destructor tables to a .s file and reload. */
1447 /* On AIX we already done scanning for global constructors/destructors. */
1448 #ifndef COLLECT_EXPORT_LIST
1449 scan_prog_file (output_file
, PASS_FIRST
);
1452 #ifdef SCAN_LIBRARIES
1453 scan_libraries (output_file
);
1458 fprintf (stderr
, "%d constructor(s) found\n", constructors
.number
);
1459 fprintf (stderr
, "%d destructor(s) found\n", destructors
.number
);
1462 if (constructors
.number
== 0 && destructors
.number
== 0
1463 && frame_tables
.number
== 0
1464 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1465 /* If we will be running these functions ourselves, we want to emit
1466 stubs into the shared library so that we do not have to relink
1467 dependent programs when we add static objects. */
1472 #ifdef COLLECT_EXPORT_LIST
1473 /* Doing tlink without additional code generation */
1474 do_tlink (ld1_argv
, object_lst
);
1476 /* Strip now if it was requested on the command line. */
1479 char **strip_argv
= (char **) xcalloc (sizeof (char *), 3);
1480 strip_argv
[0] = strip_file_name
;
1481 strip_argv
[1] = output_file
;
1482 strip_argv
[2] = (char *) 0;
1483 fork_execute ("strip", strip_argv
);
1486 #ifdef COLLECT_EXPORT_LIST
1487 maybe_unlink (export_file
);
1488 maybe_unlink (import_file
);
1493 maybe_unlink(output_file
);
1494 outf
= fopen (c_file
, "w");
1495 if (outf
== (FILE *) 0)
1496 fatal_perror ("%s", c_file
);
1498 write_c_file (outf
, c_file
);
1501 fatal_perror ("closing %s", c_file
);
1503 /* Tell the linker that we have initializer and finalizer functions. */
1504 #ifdef LD_INIT_SWITCH
1505 *ld2
++ = LD_INIT_SWITCH
;
1507 *ld2
++ = LD_FINI_SWITCH
;
1512 #ifdef COLLECT_EXPORT_LIST
1515 add_to_list (&exports
, initname
);
1516 add_to_list (&exports
, fininame
);
1517 add_to_list (&exports
, "_GLOBAL__DI");
1518 add_to_list (&exports
, "_GLOBAL__DD");
1519 exportf
= fopen (export_file
, "w");
1520 if (exportf
== (FILE *) 0)
1521 fatal_perror ("%s", export_file
);
1522 write_export_file (exportf
);
1523 if (fclose (exportf
))
1524 fatal_perror ("closing %s", export_file
);
1530 fprintf (stderr
, "\n========== output_file = %s, c_file = %s\n",
1531 output_file
, c_file
);
1532 write_c_file (stderr
, "stderr");
1533 fprintf (stderr
, "========== end of c_file\n\n");
1534 #ifdef COLLECT_EXPORT_LIST
1535 fprintf (stderr
, "\n========== export_file = %s\n", export_file
);
1536 write_export_file (stderr
);
1537 fprintf (stderr
, "========== end of export_file\n\n");
1541 /* Assemble the constructor and destructor tables.
1542 Link the tables in with the rest of the program. */
1544 fork_execute ("gcc", c_argv
);
1545 #ifdef COLLECT_EXPORT_LIST
1546 /* On AIX we must call tlink because of possible templates resolution */
1547 do_tlink (ld2_argv
, object_lst
);
1549 /* Otherwise, simply call ld because tlink is already done */
1550 fork_execute ("ld", ld2_argv
);
1552 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1553 constructors/destructors in shared libraries. */
1554 scan_prog_file (output_file
, PASS_SECOND
);
1557 maybe_unlink (c_file
);
1558 maybe_unlink (o_file
);
1560 #ifdef COLLECT_EXPORT_LIST
1561 maybe_unlink (export_file
);
1562 maybe_unlink (import_file
);
1569 /* Wait for a process to finish, and exit if a non-zero status is found. */
1580 if (WIFSIGNALED (status
))
1582 int sig
= WTERMSIG (status
);
1583 #ifdef NO_SYS_SIGLIST
1584 error ("%s terminated with signal %d %s",
1587 (status
& 0200) ? ", core dumped" : "");
1589 error ("%s terminated with signal %d [%s]%s",
1593 (status
& 0200) ? ", core dumped" : "");
1596 collect_exit (FATAL_EXIT_CODE
);
1599 if (WIFEXITED (status
))
1600 return WEXITSTATUS (status
);
1609 int ret
= collect_wait (prog
);
1612 error ("%s returned %d exit status", prog
, ret
);
1618 /* Fork and execute a program, and wait for the reply. */
1621 collect_execute (prog
, argv
, redir
)
1634 fprintf (stderr
, "%s", argv
[0]);
1636 fprintf (stderr
, "[cannot find %s]", prog
);
1638 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
1639 fprintf (stderr
, " %s", str
);
1641 fprintf (stderr
, "\n");
1647 /* If we cannot find a program we need, complain error. Do this here
1648 since we might not end up needing something that we could not find. */
1651 fatal ("cannot find `%s'", prog
);
1653 #ifndef __CYGWIN32__
1658 fatal_perror ("fork");
1660 fatal_perror ("vfork");
1664 if (pid
== 0) /* child context */
1669 if (freopen (redir
, "a", stdout
) == NULL
)
1670 fatal_perror ("redirecting stdout: %s", redir
);
1671 if (freopen (redir
, "a", stderr
) == NULL
)
1672 fatal_perror ("redirecting stderr: %s", redir
);
1675 execvp (argv
[0], argv
);
1676 fatal_perror ("executing %s", prog
);
1679 pid
= _spawnvp (_P_NOWAIT
, argv
[0], argv
);
1681 fatal ("spawnvp failed");
1686 fork_execute (prog
, argv
)
1690 collect_execute (prog
, argv
, NULL
);
1694 /* Unlink a file unless we are debugging. */
1703 fprintf (stderr
, "[Leaving %s]\n", file
);
1707 /* Add a name to a linked list. */
1710 add_to_list (head_ptr
, name
)
1711 struct head
*head_ptr
;
1715 = (struct id
*) xcalloc (sizeof (struct id
) + strlen (name
), 1);
1717 static long sequence_number
= 0;
1718 strcpy (newid
->name
, name
);
1720 if (head_ptr
->first
)
1721 head_ptr
->last
->next
= newid
;
1723 head_ptr
->first
= newid
;
1725 /* Check for duplicate symbols. */
1726 for (p
= head_ptr
->first
;
1727 strcmp (name
, p
->name
) != 0;
1732 head_ptr
->last
->next
= 0;
1737 newid
->sequence
= ++sequence_number
;
1738 head_ptr
->last
= newid
;
1742 /* Write: `prefix', the names on list LIST, `suffix'. */
1745 write_list (stream
, prefix
, list
)
1752 fprintf (stream
, "%sx%d,\n", prefix
, list
->sequence
);
1757 #ifdef COLLECT_EXPORT_LIST
1758 /* This function is really used only on AIX, but may be useful. */
1760 is_in_list (prefix
, list
)
1766 if (!strcmp (prefix
, list
->name
)) return 1;
1773 /* Added for debugging purpose. */
1775 dump_list (stream
, prefix
, list
)
1782 fprintf (stream
, "%s%s,\n", prefix
, list
->name
);
1788 dump_prefix_list (stream
, prefix
, list
)
1791 struct prefix_list
*list
;
1795 fprintf (stream
, "%s%s,\n", prefix
, list
->prefix
);
1801 write_list_with_asm (stream
, prefix
, list
)
1808 fprintf (stream
, "%sx%d __asm__ (\"%s\");\n",
1809 prefix
, list
->sequence
, list
->name
);
1814 /* Write out the constructor and destructor tables statically (for a shared
1815 object), along with the functions to execute them. */
1818 write_c_file_stat (stream
, name
)
1822 char *prefix
, *p
, *q
;
1823 int frames
= (frame_tables
.number
> 0);
1825 /* Figure out name of output_file, stripping off .so version. */
1826 p
= rindex (output_file
, '/');
1828 p
= (char *) output_file
;
1842 if (strncmp (q
, ".so", 3) == 0)
1851 /* q points to null at end of the string (or . of the .so version) */
1852 prefix
= xmalloc (q
- p
+ 1);
1853 strncpy (prefix
, p
, q
- p
);
1855 for (q
= prefix
; *q
; q
++)
1859 fprintf (stderr
, "\nwrite_c_file - output name is %s, prefix is %s\n",
1860 output_file
, prefix
);
1862 #define INIT_NAME_FORMAT "_GLOBAL__FI_%s"
1863 initname
= xmalloc (strlen (prefix
) + sizeof (INIT_NAME_FORMAT
) - 2);
1864 sprintf (initname
, INIT_NAME_FORMAT
, prefix
);
1866 #define FINI_NAME_FORMAT "_GLOBAL__FD_%s"
1867 fininame
= xmalloc (strlen (prefix
) + sizeof (FINI_NAME_FORMAT
) - 2);
1868 sprintf (fininame
, FINI_NAME_FORMAT
, prefix
);
1872 /* Write the tables as C code */
1874 fprintf (stream
, "static int count;\n");
1875 fprintf (stream
, "typedef void entry_pt();\n");
1876 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1880 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1882 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1883 write_list (stream
, "\t\t&", frame_tables
.first
);
1884 fprintf (stream
, "\t0\n};\n");
1886 /* This must match what's in frame.h. */
1887 fprintf (stream
, "struct object {\n");
1888 fprintf (stream
, " void *pc_begin;\n");
1889 fprintf (stream
, " void *pc_end;\n");
1890 fprintf (stream
, " void *fde_begin;\n");
1891 fprintf (stream
, " void *fde_array;\n");
1892 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1893 fprintf (stream
, " struct object *next;\n");
1894 fprintf (stream
, "};\n");
1896 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1897 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1899 fprintf (stream
, "static void reg_frame () {\n");
1900 fprintf (stream
, "\tstatic struct object ob;\n");
1901 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1902 fprintf (stream
, "\t}\n");
1904 fprintf (stream
, "static void dereg_frame () {\n");
1905 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1906 fprintf (stream
, "\t}\n");
1909 fprintf (stream
, "void %s() {\n", initname
);
1910 if (constructors
.number
> 0 || frames
)
1912 fprintf (stream
, "\tstatic entry_pt *ctors[] = {\n");
1913 write_list (stream
, "\t\t", constructors
.first
);
1915 fprintf (stream
, "\treg_frame,\n");
1916 fprintf (stream
, "\t};\n");
1917 fprintf (stream
, "\tentry_pt **p;\n");
1918 fprintf (stream
, "\tif (count++ != 0) return;\n");
1919 fprintf (stream
, "\tp = ctors + %d;\n", constructors
.number
+ frames
);
1920 fprintf (stream
, "\twhile (p > ctors) (*--p)();\n");
1923 fprintf (stream
, "\t++count;\n");
1924 fprintf (stream
, "}\n");
1925 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1926 fprintf (stream
, "void %s() {\n", fininame
);
1927 if (destructors
.number
> 0 || frames
)
1929 fprintf (stream
, "\tstatic entry_pt *dtors[] = {\n");
1930 write_list (stream
, "\t\t", destructors
.first
);
1932 fprintf (stream
, "\tdereg_frame,\n");
1933 fprintf (stream
, "\t};\n");
1934 fprintf (stream
, "\tentry_pt **p;\n");
1935 fprintf (stream
, "\tif (--count != 0) return;\n");
1936 fprintf (stream
, "\tp = dtors;\n");
1937 fprintf (stream
, "\twhile (p < dtors + %d) (*p++)();\n",
1938 destructors
.number
+ frames
);
1940 fprintf (stream
, "}\n");
1944 fprintf (stream
, "void _GLOBAL__DI() {\n\t%s();\n}\n", initname
);
1945 fprintf (stream
, "void _GLOBAL__DD() {\n\t%s();\n}\n", fininame
);
1949 /* Write the constructor/destructor tables. */
1951 #ifndef LD_INIT_SWITCH
1953 write_c_file_glob (stream
, name
)
1957 /* Write the tables as C code */
1959 int frames
= (frame_tables
.number
> 0);
1961 fprintf (stream
, "typedef void entry_pt();\n\n");
1963 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1967 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1969 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1970 write_list (stream
, "\t\t&", frame_tables
.first
);
1971 fprintf (stream
, "\t0\n};\n");
1973 /* This must match what's in frame.h. */
1974 fprintf (stream
, "struct object {\n");
1975 fprintf (stream
, " void *pc_begin;\n");
1976 fprintf (stream
, " void *pc_end;\n");
1977 fprintf (stream
, " void *fde_begin;\n");
1978 fprintf (stream
, " void *fde_array;\n");
1979 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1980 fprintf (stream
, " struct object *next;\n");
1981 fprintf (stream
, "};\n");
1983 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1984 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1986 fprintf (stream
, "static void reg_frame () {\n");
1987 fprintf (stream
, "\tstatic struct object ob;\n");
1988 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1989 fprintf (stream
, "\t}\n");
1991 fprintf (stream
, "static void dereg_frame () {\n");
1992 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1993 fprintf (stream
, "\t}\n");
1996 fprintf (stream
, "\nentry_pt * __CTOR_LIST__[] = {\n");
1997 fprintf (stream
, "\t(entry_pt *) %d,\n", constructors
.number
+ frames
);
1998 write_list (stream
, "\t", constructors
.first
);
2000 fprintf (stream
, "\treg_frame,\n");
2001 fprintf (stream
, "\t0\n};\n\n");
2003 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
2005 fprintf (stream
, "\nentry_pt * __DTOR_LIST__[] = {\n");
2006 fprintf (stream
, "\t(entry_pt *) %d,\n", destructors
.number
+ frames
);
2007 write_list (stream
, "\t", destructors
.first
);
2009 fprintf (stream
, "\tdereg_frame,\n");
2010 fprintf (stream
, "\t0\n};\n\n");
2012 fprintf (stream
, "extern entry_pt %s;\n", NAME__MAIN
);
2013 fprintf (stream
, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN
);
2015 #endif /* ! LD_INIT_SWITCH */
2018 write_c_file (stream
, name
)
2022 fprintf (stream
, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
2023 #ifndef LD_INIT_SWITCH
2025 write_c_file_glob (stream
, name
);
2028 write_c_file_stat (stream
, name
);
2029 fprintf (stream
, "#ifdef __cplusplus\n}\n#endif\n");
2032 #ifdef COLLECT_EXPORT_LIST
2034 write_export_file (stream
)
2037 struct id
*list
= exports
.first
;
2038 for (; list
; list
= list
->next
)
2039 fprintf (stream
, "%s\n", list
->name
);
2043 write_import_file (stream
)
2046 struct id
*list
= imports
.first
;
2047 fprintf (stream
, "%s\n", "#! .");
2048 for (; list
; list
= list
->next
)
2049 fprintf (stream
, "%s\n", list
->name
);
2053 #ifdef OBJECT_FORMAT_NONE
2055 /* Generic version to scan the name list of the loaded program for
2056 the symbols g++ uses for static constructors and destructors.
2058 The constructor table begins at __CTOR_LIST__ and contains a count
2059 of the number of pointers (or -1 if the constructors are built in a
2060 separate section by the linker), followed by the pointers to the
2061 constructor functions, terminated with a null pointer. The
2062 destructor table has the same format, and begins at __DTOR_LIST__. */
2065 scan_prog_file (prog_name
, which_pass
)
2067 enum pass which_pass
;
2069 void (*int_handler
) ();
2070 void (*quit_handler
) ();
2078 if (which_pass
== PASS_SECOND
)
2081 /* If we do not have an `nm', complain. */
2082 if (nm_file_name
== 0)
2083 fatal ("cannot find `nm'");
2085 nm_argv
[argc
++] = nm_file_name
;
2086 if (NM_FLAGS
[0] != '\0')
2087 nm_argv
[argc
++] = NM_FLAGS
;
2089 nm_argv
[argc
++] = prog_name
;
2090 nm_argv
[argc
++] = (char *) 0;
2092 if (pipe (pipe_fd
) < 0)
2093 fatal_perror ("pipe");
2095 inf
= fdopen (pipe_fd
[0], "r");
2096 if (inf
== (FILE *) 0)
2097 fatal_perror ("fdopen");
2099 /* Trace if needed. */
2105 for (p_argv
= &nm_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2106 fprintf (stderr
, " %s", str
);
2108 fprintf (stderr
, "\n");
2114 /* Spawn child nm on pipe */
2119 fatal_perror ("fork");
2121 fatal_perror ("vfork");
2125 if (pid
== 0) /* child context */
2128 if (dup2 (pipe_fd
[1], 1) < 0)
2129 fatal_perror ("dup2 (%d, 1)", pipe_fd
[1]);
2131 if (close (pipe_fd
[0]) < 0)
2132 fatal_perror ("close (%d)", pipe_fd
[0]);
2134 if (close (pipe_fd
[1]) < 0)
2135 fatal_perror ("close (%d)", pipe_fd
[1]);
2137 execv (nm_file_name
, nm_argv
);
2138 fatal_perror ("executing %s", nm_file_name
);
2141 /* Parent context from here on. */
2142 int_handler
= (void (*) ())signal (SIGINT
, SIG_IGN
);
2144 quit_handler
= (void (*) ())signal (SIGQUIT
, SIG_IGN
);
2147 if (close (pipe_fd
[1]) < 0)
2148 fatal_perror ("close (%d)", pipe_fd
[1]);
2151 fprintf (stderr
, "\nnm output with constructors/destructors.\n");
2153 /* Read each line of nm output. */
2154 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2159 /* If it contains a constructor or destructor name, add the name
2160 to the appropriate list. */
2162 for (p
= buf
; (ch
= *p
) != '\0' && ch
!= '\n' && ch
!= '_'; p
++)
2163 if (ch
== ' ' && p
[1] == 'U' && p
[2] == ' ')
2170 /* Find the end of the symbol name.
2171 Do not include `|', because Encore nm can tack that on the end. */
2172 for (end
= p
; (ch2
= *end
) != '\0' && !ISSPACE (ch2
) && ch2
!= '|';
2178 switch (is_ctor_dtor (name
))
2181 if (which_pass
!= PASS_LIB
)
2182 add_to_list (&constructors
, name
);
2186 if (which_pass
!= PASS_LIB
)
2187 add_to_list (&destructors
, name
);
2191 if (which_pass
!= PASS_LIB
)
2192 fatal ("init function found in object %s", prog_name
);
2193 #ifndef LD_INIT_SWITCH
2194 add_to_list (&constructors
, name
);
2199 if (which_pass
!= PASS_LIB
)
2200 fatal ("fini function found in object %s", prog_name
);
2201 #ifndef LD_FINI_SWITCH
2202 add_to_list (&destructors
, name
);
2207 if (which_pass
!= PASS_LIB
)
2208 add_to_list (&frame_tables
, name
);
2210 default: /* not a constructor or destructor */
2215 fprintf (stderr
, "\t%s\n", buf
);
2219 fprintf (stderr
, "\n");
2221 if (fclose (inf
) != 0)
2222 fatal_perror ("fclose of pipe");
2224 do_wait (nm_file_name
);
2226 signal (SIGINT
, int_handler
);
2228 signal (SIGQUIT
, quit_handler
);
2232 #if SUNOS4_SHARED_LIBRARIES
2234 /* Routines to scan the SunOS 4 _DYNAMIC structure to find shared libraries
2235 that the output file depends upon and their initialization/finalization
2236 routines, if any. */
2241 #include <sys/mman.h>
2242 #include <sys/param.h>
2244 #include <sys/dir.h>
2246 /* pointers to the object file */
2247 unsigned object
; /* address of memory mapped file */
2248 unsigned objsize
; /* size of memory mapped to file */
2249 char * code
; /* pointer to code segment */
2250 char * data
; /* pointer to data segment */
2251 struct nlist
*symtab
; /* pointer to symbol table */
2252 struct link_dynamic
*ld
;
2253 struct link_dynamic_2
*ld_2
;
2254 struct head libraries
;
2256 /* Map the file indicated by NAME into memory and store its address. */
2264 if ((fp
= open (name
, O_RDONLY
)) == -1)
2265 fatal ("unable to open file '%s'", name
);
2266 if (fstat (fp
, &s
) == -1)
2267 fatal ("unable to stat file '%s'", name
);
2269 objsize
= s
.st_size
;
2270 object
= (unsigned) mmap (0, objsize
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
,
2273 fatal ("unable to mmap file '%s'", name
);
2278 /* Helpers for locatelib. */
2280 static char *libname
;
2286 return (strncmp (libname
, d
->d_name
, strlen (libname
)) == 0);
2289 /* If one file has an additional numeric extension past LIBNAME, then put
2290 that one first in the sort. If both files have additional numeric
2291 extensions, then put the one with the higher number first in the sort.
2293 We must verify that the extension is numeric, because Sun saves the
2294 original versions of patched libraries with a .FCS extension. Files with
2295 invalid extensions must go last in the sort, so that they will not be used. */
2299 struct direct
**d1
, **d2
;
2301 int i1
, i2
= strlen (libname
);
2302 char *e1
= (*d1
)->d_name
+ i2
;
2303 char *e2
= (*d2
)->d_name
+ i2
;
2305 while (*e1
&& *e2
&& *e1
== '.' && *e2
== '.'
2306 && e1
[1] && ISDIGIT (e1
[1]) && e2
[1] && ISDIGIT (e2
[1]))
2310 i1
= strtol (e1
, &e1
, 10);
2311 i2
= strtol (e2
, &e2
, 10);
2318 /* It has a valid numeric extension, prefer this one. */
2319 if (*e1
== '.' && e1
[1] && ISDIGIT (e1
[1]))
2321 /* It has a invalid numeric extension, must prefer the other one. */
2327 /* It has a valid numeric extension, prefer this one. */
2328 if (*e2
== '.' && e2
[1] && ISDIGIT (e2
[1]))
2330 /* It has a invalid numeric extension, must prefer the other one. */
2338 /* Given the name NAME of a dynamic dependency, find its pathname and add
2339 it to the list of libraries. */
2347 char buf
[MAXPATHLEN
];
2355 /* counting elements in array, need 1 extra for null */
2357 ld_rules
= (char *) (ld_2
->ld_rules
+ code
);
2361 for (; *ld_rules
!= 0; ld_rules
++)
2362 if (*ld_rules
== ':')
2364 ld_rules
= (char *) (ld_2
->ld_rules
+ code
);
2365 ldr
= (char *) malloc (strlen (ld_rules
) + 1);
2366 strcpy (ldr
, ld_rules
);
2368 p
= getenv ("LD_LIBRARY_PATH");
2373 for (q
= p
; *q
!= 0; q
++)
2376 q
= (char *) malloc (strlen (p
) + 1);
2379 l
= (char **) malloc ((cnt
+ 3) * sizeof (char *));
2384 for (; *ldr
!= 0; ldr
++)
2394 for (; *q
!= 0; q
++)
2401 /* built in directories are /lib, /usr/lib, and /usr/local/lib */
2404 *pp
++ = "/usr/local/lib";
2408 for (pp
= l
; *pp
!= 0 ; pp
++)
2410 struct direct
**namelist
;
2412 if ((entries
= scandir (*pp
, &namelist
, libselect
, libcompare
)) > 0)
2414 sprintf (buf
, "%s/%s", *pp
, namelist
[entries
- 1]->d_name
);
2415 add_to_list (&libraries
, buf
);
2417 fprintf (stderr
, "%s\n", buf
);
2424 fprintf (stderr
, "not found\n");
2426 fatal ("dynamic dependency %s not found", name
);
2430 /* Scan the _DYNAMIC structure of the output file to find shared libraries
2431 that it depends upon and any constructors or destructors they contain. */
2434 scan_libraries (prog_name
)
2437 struct exec
*header
;
2439 struct link_object
*lo
;
2440 char buff
[MAXPATHLEN
];
2443 mapfile (prog_name
);
2444 header
= (struct exec
*)object
;
2445 if (N_BADMAG (*header
))
2446 fatal ("bad magic number in file '%s'", prog_name
);
2447 if (header
->a_dynamic
== 0)
2450 code
= (char *) (N_TXTOFF (*header
) + (long) header
);
2451 data
= (char *) (N_DATOFF (*header
) + (long) header
);
2452 symtab
= (struct nlist
*) (N_SYMOFF (*header
) + (long) header
);
2454 if (header
->a_magic
== ZMAGIC
&& header
->a_entry
== 0x20)
2457 ld
= (struct link_dynamic
*) (symtab
->n_value
+ code
);
2463 ld
= (struct link_dynamic
*) data
;
2468 fprintf (stderr
, "dynamic dependencies.\n");
2470 ld_2
= (struct link_dynamic_2
*) ((long) ld
->ld_un
.ld_2
+ (long)base
);
2471 for (lo
= (struct link_object
*) ld_2
->ld_need
; lo
;
2472 lo
= (struct link_object
*) lo
->lo_next
)
2475 lo
= (struct link_object
*) ((long) lo
+ code
);
2476 name
= (char *) (code
+ lo
->lo_name
);
2480 fprintf (stderr
, "\t-l%s.%d => ", name
, lo
->lo_major
);
2481 sprintf (buff
, "lib%s.so.%d.%d", name
, lo
->lo_major
, lo
->lo_minor
);
2487 fprintf (stderr
, "\t%s\n", name
);
2488 add_to_list (&libraries
, name
);
2493 fprintf (stderr
, "\n");
2495 /* now iterate through the library list adding their symbols to
2497 for (list
= libraries
.first
; list
; list
= list
->next
)
2498 scan_prog_file (list
->name
, PASS_LIB
);
2501 #else /* SUNOS4_SHARED_LIBRARIES */
2504 /* Use the List Dynamic Dependencies program to find shared libraries that
2505 the output file depends upon and their initialization/finalization
2506 routines, if any. */
2509 scan_libraries (prog_name
)
2512 static struct head libraries
; /* list of shared libraries found */
2514 void (*int_handler
) ();
2515 void (*quit_handler
) ();
2523 /* If we do not have an `ldd', complain. */
2524 if (ldd_file_name
== 0)
2526 error ("cannot find `ldd'");
2530 ldd_argv
[argc
++] = ldd_file_name
;
2531 ldd_argv
[argc
++] = prog_name
;
2532 ldd_argv
[argc
++] = (char *) 0;
2534 if (pipe (pipe_fd
) < 0)
2535 fatal_perror ("pipe");
2537 inf
= fdopen (pipe_fd
[0], "r");
2538 if (inf
== (FILE *) 0)
2539 fatal_perror ("fdopen");
2541 /* Trace if needed. */
2547 for (p_argv
= &ldd_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2548 fprintf (stderr
, " %s", str
);
2550 fprintf (stderr
, "\n");
2556 /* Spawn child ldd on pipe */
2561 fatal_perror ("fork");
2563 fatal_perror ("vfork");
2567 if (pid
== 0) /* child context */
2570 if (dup2 (pipe_fd
[1], 1) < 0)
2571 fatal_perror ("dup2 (%d, 1)", pipe_fd
[1]);
2573 if (close (pipe_fd
[0]) < 0)
2574 fatal_perror ("close (%d)", pipe_fd
[0]);
2576 if (close (pipe_fd
[1]) < 0)
2577 fatal_perror ("close (%d)", pipe_fd
[1]);
2579 execv (ldd_file_name
, ldd_argv
);
2580 fatal_perror ("executing %s", ldd_file_name
);
2583 /* Parent context from here on. */
2584 int_handler
= (void (*) ()) signal (SIGINT
, SIG_IGN
);
2586 quit_handler
= (void (*) ()) signal (SIGQUIT
, SIG_IGN
);
2589 if (close (pipe_fd
[1]) < 0)
2590 fatal_perror ("close (%d)", pipe_fd
[1]);
2593 fprintf (stderr
, "\nldd output with constructors/destructors.\n");
2595 /* Read each line of ldd output. */
2596 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2599 char *name
, *end
, *p
= buf
;
2601 /* Extract names of libraries and add to list. */
2602 PARSE_LDD_OUTPUT (p
);
2607 if (strncmp (name
, "not found", sizeof ("not found") - 1) == 0)
2608 fatal ("dynamic dependency %s not found", buf
);
2610 /* Find the end of the symbol name. */
2612 (ch2
= *end
) != '\0' && ch2
!= '\n' && !ISSPACE (ch2
) && ch2
!= '|';
2617 if (access (name
, R_OK
) == 0)
2618 add_to_list (&libraries
, name
);
2620 fatal ("unable to open dynamic dependency '%s'", buf
);
2623 fprintf (stderr
, "\t%s\n", buf
);
2626 fprintf (stderr
, "\n");
2628 if (fclose (inf
) != 0)
2629 fatal_perror ("fclose of pipe");
2631 do_wait (ldd_file_name
);
2633 signal (SIGINT
, int_handler
);
2635 signal (SIGQUIT
, quit_handler
);
2638 /* now iterate through the library list adding their symbols to
2640 for (list
= libraries
.first
; list
; list
= list
->next
)
2641 scan_prog_file (list
->name
, PASS_LIB
);
2644 #endif /* LDD_SUFFIX */
2645 #endif /* SUNOS4_SHARED_LIBRARIES */
2647 #endif /* OBJECT_FORMAT_NONE */
2651 * COFF specific stuff.
2654 #ifdef OBJECT_FORMAT_COFF
2656 #if defined(EXTENDED_COFF)
2657 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2658 # define GCC_SYMENT SYMR
2659 # define GCC_OK_SYMBOL(X) ((X).st == stProc && (X).sc == scText)
2660 # define GCC_SYMINC(X) (1)
2661 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2662 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2664 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2665 # define GCC_SYMENT SYMENT
2666 # define GCC_OK_SYMBOL(X) \
2667 (((X).n_sclass == C_EXT) && \
2668 ((X).n_scnum > N_UNDEF) && \
2669 (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) || \
2670 ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT)))
2671 # define GCC_UNDEF_SYMBOL(X) \
2672 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2673 # define GCC_SYMINC(X) ((X).n_numaux+1)
2674 # define GCC_SYMZERO(X) 0
2675 # define GCC_CHECK_HDR(X) (1)
2678 extern char *ldgetname ();
2680 /* COFF version to scan the name list of the loaded program for
2681 the symbols g++ uses for static constructors and destructors.
2683 The constructor table begins at __CTOR_LIST__ and contains a count
2684 of the number of pointers (or -1 if the constructors are built in a
2685 separate section by the linker), followed by the pointers to the
2686 constructor functions, terminated with a null pointer. The
2687 destructor table has the same format, and begins at __DTOR_LIST__. */
2690 scan_prog_file (prog_name
, which_pass
)
2692 enum pass which_pass
;
2694 LDFILE
*ldptr
= NULL
;
2695 int sym_index
, sym_count
;
2697 #ifdef COLLECT_EXPORT_LIST
2698 /* Should we generate an import list for given prog_name? */
2699 int import_flag
= (which_pass
== PASS_OBJ
? 0 : use_import_list (prog_name
));
2702 if (which_pass
!= PASS_FIRST
&& which_pass
!= PASS_OBJ
)
2705 #ifdef COLLECT_EXPORT_LIST
2706 /* We do not need scanning for some standard C libraries. */
2707 if (which_pass
== PASS_FIRST
&& ignore_library (prog_name
))
2710 /* On AIX we have a loop, because there is not much difference
2711 between an object and an archive. This trick allows us to
2712 eliminate scan_libraries() function. */
2716 if ((ldptr
= ldopen (prog_name
, ldptr
)) != NULL
)
2719 if (!MY_ISCOFF (HEADER (ldptr
).f_magic
))
2720 fatal ("%s: not a COFF file", prog_name
);
2722 #ifdef COLLECT_EXPORT_LIST
2723 /* Is current archive member a shared object? */
2724 is_shared
= HEADER (ldptr
).f_flags
& F_SHROBJ
;
2726 if (GCC_CHECK_HDR (ldptr
))
2728 sym_count
= GCC_SYMBOLS (ldptr
);
2729 sym_index
= GCC_SYMZERO (ldptr
);
2730 while (sym_index
< sym_count
)
2734 if (ldtbread (ldptr
, sym_index
, &symbol
) <= 0)
2736 sym_index
+= GCC_SYMINC (symbol
);
2738 if (GCC_OK_SYMBOL (symbol
))
2742 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2743 continue; /* should never happen */
2745 #ifdef XCOFF_DEBUGGING_INFO
2746 /* All AIX function names have a duplicate entry
2747 beginning with a dot. */
2752 switch (is_ctor_dtor (name
))
2755 if (! is_shared
) add_to_list (&constructors
, name
);
2756 #ifdef COLLECT_EXPORT_LIST
2757 if (which_pass
== PASS_OBJ
)
2758 add_to_list (&exports
, name
);
2759 /* If this symbol was undefined and we are building
2760 an import list, we should add a symbol to this
2764 && is_in_list (name
, undefined
.first
))
2765 add_to_list (&imports
, name
);
2770 if (! is_shared
) add_to_list (&destructors
, name
);
2771 #ifdef COLLECT_EXPORT_LIST
2772 if (which_pass
== PASS_OBJ
)
2773 add_to_list (&exports
, name
);
2774 /* If this symbol was undefined and we are building
2775 an import list, we should add a symbol to this
2779 && is_in_list (name
, undefined
.first
))
2780 add_to_list (&imports
, name
);
2784 #ifdef COLLECT_EXPORT_LIST
2787 add_to_list (&constructors
, name
);
2792 add_to_list (&destructors
, name
);
2796 default: /* not a constructor or destructor */
2797 #ifdef COLLECT_EXPORT_LIST
2798 /* If we are building a shared object on AIX we need
2799 to explicitly export all global symbols or add
2800 them to import list. */
2803 if (which_pass
== PASS_OBJ
&& (! export_flag
))
2804 add_to_list (&exports
, name
);
2805 else if (! is_shared
&& which_pass
== PASS_FIRST
2807 && is_in_list(name
, undefined
.first
))
2808 add_to_list (&imports
, name
);
2814 #if !defined(EXTENDED_COFF)
2816 fprintf (stderr
, "\tsec=%d class=%d type=%s%o %s\n",
2817 symbol
.n_scnum
, symbol
.n_sclass
,
2818 (symbol
.n_type
? "0" : ""), symbol
.n_type
,
2823 "\tiss = %5d, value = %5d, index = %5d, name = %s\n",
2824 symbol
.iss
, symbol
.value
, symbol
.index
, name
);
2827 #ifdef COLLECT_EXPORT_LIST
2828 /* If we are building a shared object we should collect
2829 information about undefined symbols for later
2830 import list generation. */
2831 else if (shared_obj
&& GCC_UNDEF_SYMBOL (symbol
))
2835 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2836 continue; /* should never happen */
2838 /* All AIX function names have a duplicate entry
2839 beginning with a dot. */
2842 add_to_list (&undefined
, name
);
2850 fatal ("%s: cannot open as COFF file", prog_name
);
2852 #ifdef COLLECT_EXPORT_LIST
2853 /* On AIX loop continues while there are more members in archive. */
2855 while (ldclose (ldptr
) == FAILURE
);
2857 /* Otherwise we simply close ldptr. */
2858 (void) ldclose(ldptr
);
2863 #ifdef COLLECT_EXPORT_LIST
2865 /* This new function is used to decide whether we should
2866 generate import list for an object or to use it directly. */
2868 use_import_list (prog_name
)
2873 /* If we do not build a shared object then import list should not be used. */
2874 if (! shared_obj
) return 0;
2876 /* Currently we check only for libgcc, but this can be changed in future. */
2877 p
= strstr (prog_name
, "libgcc.a");
2878 if (p
!= 0 && (strlen (p
) == sizeof ("libgcc.a") - 1))
2883 /* Given a library name without "lib" prefix, this function
2884 returns a full library name including a path. */
2886 resolve_lib_name (name
)
2892 for (i
= 0; libpaths
[i
]; i
++)
2893 if (libpaths
[i
]->max_len
> l
)
2894 l
= libpaths
[i
]->max_len
;
2896 lib_buf
= xmalloc (l
+ strlen(name
) + 10);
2898 for (i
= 0; libpaths
[i
]; i
++)
2900 struct prefix_list
*list
= libpaths
[i
]->plist
;
2901 for (; list
; list
= list
->next
)
2903 for (j
= 0; libexts
[j
]; j
++)
2905 /* The following lines are needed because path_prefix list
2906 may contain directories both with trailing '/' and
2909 if (list
->prefix
[strlen(list
->prefix
)-1] != '/')
2911 sprintf (lib_buf
, "%s%slib%s.%s",
2912 list
->prefix
, p
, name
, libexts
[j
]);
2913 if (debug
) fprintf (stderr
, "searching for: %s\n", lib_buf
);
2914 if (file_exists (lib_buf
))
2916 if (debug
) fprintf (stderr
, "found: %s\n", lib_buf
);
2923 fprintf (stderr
, "not found\n");
2925 fatal ("Library lib%s not found", name
);
2929 /* Array of standard AIX libraries which should not
2930 be scanned for ctors/dtors. */
2931 static char* aix_std_libs
[] = {
2936 "/usr/lib/libc_r.a",
2937 "/usr/lib/threads/libc.a",
2938 "/usr/ccs/lib/libc.a",
2939 "/usr/ccs/lib/libc_r.a",
2943 /* This function checks the filename and returns 1
2944 if this name matches the location of a standard AIX library. */
2946 ignore_library (name
)
2949 char **p
= &aix_std_libs
[0];
2950 while (*p
++ != NULL
)
2951 if (! strcmp (name
, *p
)) return 1;
2957 #endif /* OBJECT_FORMAT_COFF */
2961 * OSF/rose specific stuff.
2964 #ifdef OBJECT_FORMAT_ROSE
2966 /* Union of the various load commands */
2968 typedef union load_union
2970 ldc_header_t hdr
; /* common header */
2971 load_cmd_map_command_t map
; /* map indexing other load cmds */
2972 interpreter_command_t iprtr
; /* interpreter pathname */
2973 strings_command_t str
; /* load commands strings section */
2974 region_command_t region
; /* region load command */
2975 reloc_command_t reloc
; /* relocation section */
2976 package_command_t pkg
; /* package load command */
2977 symbols_command_t sym
; /* symbol sections */
2978 entry_command_t ent
; /* program start section */
2979 gen_info_command_t info
; /* object information */
2980 func_table_command_t func
; /* function constructors/destructors */
2983 /* Structure to point to load command and data section in memory. */
2985 typedef struct load_all
2987 load_union_t
*load
; /* load command */
2988 char *section
; /* pointer to section */
2991 /* Structure to contain information about a file mapped into memory. */
2995 char *start
; /* start of map */
2996 char *name
; /* filename */
2997 long size
; /* size of the file */
2998 long rounded_size
; /* size rounded to page boundary */
2999 int fd
; /* file descriptor */
3000 int rw
; /* != 0 if opened read/write */
3001 int use_mmap
; /* != 0 if mmap'ed */
3004 extern int decode_mach_o_hdr ();
3005 extern int encode_mach_o_hdr ();
3007 static void add_func_table
PROTO((mo_header_t
*, load_all_t
*,
3008 symbol_info_t
*, int));
3009 static void print_header
PROTO((mo_header_t
*));
3010 static void print_load_command
PROTO((load_union_t
*, size_t, int));
3011 static void bad_header
PROTO((int));
3012 static struct file_info
*read_file
PROTO((char *, int, int));
3013 static void end_file
PROTO((struct file_info
*));
3015 /* OSF/rose specific version to scan the name list of the loaded
3016 program for the symbols g++ uses for static constructors and
3019 The constructor table begins at __CTOR_LIST__ and contains a count
3020 of the number of pointers (or -1 if the constructors are built in a
3021 separate section by the linker), followed by the pointers to the
3022 constructor functions, terminated with a null pointer. The
3023 destructor table has the same format, and begins at __DTOR_LIST__. */
3026 scan_prog_file (prog_name
, which_pass
)
3028 enum pass which_pass
;
3032 load_all_t
*load_array
;
3033 load_all_t
*load_end
;
3034 load_all_t
*load_cmd
;
3035 int symbol_load_cmds
;
3041 struct file_info
*obj_file
;
3043 mo_lcid_t cmd_strings
= -1;
3044 symbol_info_t
*main_sym
= 0;
3045 int rw
= (which_pass
!= PASS_FIRST
);
3047 prog_fd
= open (prog_name
, (rw
) ? O_RDWR
: O_RDONLY
);
3049 fatal_perror ("cannot read %s", prog_name
);
3051 obj_file
= read_file (prog_name
, prog_fd
, rw
);
3052 obj
= obj_file
->start
;
3054 status
= decode_mach_o_hdr (obj
, MO_SIZEOF_RAW_HDR
, MOH_HEADER_VERSION
, &hdr
);
3055 if (status
!= MO_HDR_CONV_SUCCESS
)
3056 bad_header (status
);
3059 /* Do some basic sanity checks. Note we explicitly use the big endian magic number,
3060 since the hardware will automatically swap bytes for us on loading little endian
3063 #ifndef CROSS_COMPILE
3064 if (hdr
.moh_magic
!= MOH_MAGIC_MSB
3065 || hdr
.moh_header_version
!= MOH_HEADER_VERSION
3066 || hdr
.moh_byte_order
!= OUR_BYTE_ORDER
3067 || hdr
.moh_data_rep_id
!= OUR_DATA_REP_ID
3068 || hdr
.moh_cpu_type
!= OUR_CPU_TYPE
3069 || hdr
.moh_cpu_subtype
!= OUR_CPU_SUBTYPE
3070 || hdr
.moh_vendor_type
!= OUR_VENDOR_TYPE
)
3072 fatal ("incompatibilities between object file & expected values");
3077 print_header (&hdr
);
3079 offset
= hdr
.moh_first_cmd_off
;
3080 load_end
= load_array
3081 = (load_all_t
*) xcalloc (sizeof (load_all_t
), hdr
.moh_n_load_cmds
+ 2);
3083 /* Build array of load commands, calculating the offsets */
3084 for (i
= 0; i
< hdr
.moh_n_load_cmds
; i
++)
3086 load_union_t
*load_hdr
; /* load command header */
3088 load_cmd
= load_end
++;
3089 load_hdr
= (load_union_t
*) (obj
+ offset
);
3091 /* If modifying the program file, copy the header. */
3094 load_union_t
*ptr
= (load_union_t
*) xmalloc (load_hdr
->hdr
.ldci_cmd_size
);
3095 bcopy ((char *)load_hdr
, (char *)ptr
, load_hdr
->hdr
.ldci_cmd_size
);
3098 /* null out old command map, because we will rewrite at the end. */
3099 if (ptr
->hdr
.ldci_cmd_type
== LDC_CMD_MAP
)
3101 cmd_strings
= ptr
->map
.lcm_ld_cmd_strings
;
3102 ptr
->hdr
.ldci_cmd_type
= LDC_UNDEFINED
;
3106 load_cmd
->load
= load_hdr
;
3107 if (load_hdr
->hdr
.ldci_section_off
> 0)
3108 load_cmd
->section
= obj
+ load_hdr
->hdr
.ldci_section_off
;
3111 print_load_command (load_hdr
, offset
, i
);
3113 offset
+= load_hdr
->hdr
.ldci_cmd_size
;
3116 /* If the last command is the load command map and is not undefined,
3117 decrement the count of load commands. */
3118 if (rw
&& load_end
[-1].load
->hdr
.ldci_cmd_type
== LDC_UNDEFINED
)
3121 hdr
.moh_n_load_cmds
--;
3124 /* Go through and process each symbol table section. */
3125 symbol_load_cmds
= 0;
3126 for (load_cmd
= load_array
; load_cmd
< load_end
; load_cmd
++)
3128 load_union_t
*load_hdr
= load_cmd
->load
;
3130 if (load_hdr
->hdr
.ldci_cmd_type
== LDC_SYMBOLS
)
3136 char *kind
= "unknown";
3138 switch (load_hdr
->sym
.symc_kind
)
3140 case SYMC_IMPORTS
: kind
= "imports"; break;
3141 case SYMC_DEFINED_SYMBOLS
: kind
= "defined"; break;
3142 case SYMC_STABS
: kind
= "stabs"; break;
3145 fprintf (stderr
, "\nProcessing symbol table #%d, offset = 0x%.8lx, kind = %s\n",
3146 symbol_load_cmds
, load_hdr
->hdr
.ldci_section_off
, kind
);
3149 if (load_hdr
->sym
.symc_kind
!= SYMC_DEFINED_SYMBOLS
)
3152 str_sect
= load_array
[load_hdr
->sym
.symc_strings_section
].section
;
3153 if (str_sect
== (char *) 0)
3154 fatal ("string section missing");
3156 if (load_cmd
->section
== (char *) 0)
3157 fatal ("section pointer missing");
3159 num_syms
= load_hdr
->sym
.symc_nentries
;
3160 for (i
= 0; i
< num_syms
; i
++)
3162 symbol_info_t
*sym
= ((symbol_info_t
*) load_cmd
->section
) + i
;
3163 char *name
= sym
->si_name
.symbol_name
+ str_sect
;
3170 char *n
= name
+ strlen (name
) - strlen (NAME__MAIN
);
3172 if ((n
- name
) < 0 || strcmp (n
, NAME__MAIN
))
3182 switch (is_ctor_dtor (name
))
3185 add_to_list (&constructors
, name
);
3189 add_to_list (&destructors
, name
);
3192 default: /* not a constructor or destructor */
3198 fprintf (stderr
, "\ttype = 0x%.4x, sc = 0x%.2x, flags = 0x%.8x, name = %.30s\n",
3199 sym
->si_type
, sym
->si_sc_type
, sym
->si_flags
, name
);
3204 if (symbol_load_cmds
== 0)
3205 fatal ("no symbol table found");
3207 /* Update the program file now, rewrite header and load commands. At present,
3208 we assume that there is enough space after the last load command to insert
3209 one more. Since the first section written out is page aligned, and the
3210 number of load commands is small, this is ok for the present. */
3214 load_union_t
*load_map
;
3217 if (cmd_strings
== -1)
3218 fatal ("no cmd_strings found");
3220 /* Add __main to initializer list.
3221 If we are building a program instead of a shared library, do not
3222 do anything, since in the current version, you cannot do mallocs
3223 and such in the constructors. */
3225 if (main_sym
!= (symbol_info_t
*) 0
3226 && ((hdr
.moh_flags
& MOH_EXECABLE_F
) == 0))
3227 add_func_table (&hdr
, load_array
, main_sym
, FNTC_INITIALIZATION
);
3230 fprintf (stderr
, "\nUpdating header and load commands.\n\n");
3232 hdr
.moh_n_load_cmds
++;
3233 size
= sizeof (load_cmd_map_command_t
) + (sizeof (mo_offset_t
) * (hdr
.moh_n_load_cmds
- 1));
3235 /* Create new load command map. */
3237 fprintf (stderr
, "load command map, %d cmds, new size %ld.\n",
3238 (int)hdr
.moh_n_load_cmds
, (long)size
);
3240 load_map
= (load_union_t
*) xcalloc (1, size
);
3241 load_map
->map
.ldc_header
.ldci_cmd_type
= LDC_CMD_MAP
;
3242 load_map
->map
.ldc_header
.ldci_cmd_size
= size
;
3243 load_map
->map
.lcm_ld_cmd_strings
= cmd_strings
;
3244 load_map
->map
.lcm_nentries
= hdr
.moh_n_load_cmds
;
3245 load_array
[hdr
.moh_n_load_cmds
-1].load
= load_map
;
3247 offset
= hdr
.moh_first_cmd_off
;
3248 for (i
= 0; i
< hdr
.moh_n_load_cmds
; i
++)
3250 load_map
->map
.lcm_map
[i
] = offset
;
3251 if (load_array
[i
].load
->hdr
.ldci_cmd_type
== LDC_CMD_MAP
)
3252 hdr
.moh_load_map_cmd_off
= offset
;
3254 offset
+= load_array
[i
].load
->hdr
.ldci_cmd_size
;
3257 hdr
.moh_sizeofcmds
= offset
- MO_SIZEOF_RAW_HDR
;
3260 print_header (&hdr
);
3263 status
= encode_mach_o_hdr (&hdr
, obj
, MO_SIZEOF_RAW_HDR
);
3264 if (status
!= MO_HDR_CONV_SUCCESS
)
3265 bad_header (status
);
3268 fprintf (stderr
, "writing load commands.\n\n");
3270 /* Write load commands */
3271 offset
= hdr
.moh_first_cmd_off
;
3272 for (i
= 0; i
< hdr
.moh_n_load_cmds
; i
++)
3274 load_union_t
*load_hdr
= load_array
[i
].load
;
3275 size_t size
= load_hdr
->hdr
.ldci_cmd_size
;
3278 print_load_command (load_hdr
, offset
, i
);
3280 bcopy ((char *) load_hdr
, (char *) (obj
+ offset
), size
);
3285 end_file (obj_file
);
3287 if (close (prog_fd
))
3288 fatal_perror ("closing %s", prog_name
);
3291 fprintf (stderr
, "\n");
3295 /* Add a function table to the load commands to call a function
3296 on initiation or termination of the process. */
3299 add_func_table (hdr_p
, load_array
, sym
, type
)
3300 mo_header_t
*hdr_p
; /* pointer to global header */
3301 load_all_t
*load_array
; /* array of ptrs to load cmds */
3302 symbol_info_t
*sym
; /* pointer to symbol entry */
3303 int type
; /* fntc_type value */
3305 /* Add a new load command. */
3306 int num_cmds
= ++hdr_p
->moh_n_load_cmds
;
3307 int load_index
= num_cmds
- 1;
3308 size_t size
= sizeof (func_table_command_t
) + sizeof (mo_addr_t
);
3309 load_union_t
*ptr
= xcalloc (1, size
);
3310 load_all_t
*load_cmd
;
3313 /* Set the unresolved address bit in the header to force the loader to be
3314 used, since kernel exec does not call the initialization functions. */
3315 hdr_p
->moh_flags
|= MOH_UNRESOLVED_F
;
3317 load_cmd
= &load_array
[load_index
];
3318 load_cmd
->load
= ptr
;
3319 load_cmd
->section
= (char *) 0;
3321 /* Fill in func table load command. */
3322 ptr
->func
.ldc_header
.ldci_cmd_type
= LDC_FUNC_TABLE
;
3323 ptr
->func
.ldc_header
.ldci_cmd_size
= size
;
3324 ptr
->func
.ldc_header
.ldci_section_off
= 0;
3325 ptr
->func
.ldc_header
.ldci_section_len
= 0;
3326 ptr
->func
.fntc_type
= type
;
3327 ptr
->func
.fntc_nentries
= 1;
3329 /* copy address, turn it from abs. address to (region,offset) if necessary. */
3330 /* Is the symbol already expressed as (region, offset)? */
3331 if ((sym
->si_flags
& SI_ABSOLUTE_VALUE_F
) == 0)
3333 ptr
->func
.fntc_entry_loc
[i
].adr_lcid
= sym
->si_value
.def_val
.adr_lcid
;
3334 ptr
->func
.fntc_entry_loc
[i
].adr_sctoff
= sym
->si_value
.def_val
.adr_sctoff
;
3337 /* If not, figure out which region it's in. */
3340 mo_vm_addr_t addr
= sym
->si_value
.abs_val
;
3343 for (i
= 0; i
< load_index
; i
++)
3345 if (load_array
[i
].load
->hdr
.ldci_cmd_type
== LDC_REGION
)
3347 region_command_t
*region_ptr
= &load_array
[i
].load
->region
;
3349 if ((region_ptr
->regc_flags
& REG_ABS_ADDR_F
) != 0
3350 && addr
>= region_ptr
->regc_addr
.vm_addr
3351 && addr
<= region_ptr
->regc_addr
.vm_addr
+ region_ptr
->regc_vm_size
)
3353 ptr
->func
.fntc_entry_loc
[0].adr_lcid
= i
;
3354 ptr
->func
.fntc_entry_loc
[0].adr_sctoff
= addr
- region_ptr
->regc_addr
.vm_addr
;
3362 fatal ("could not convert 0x%l.8x into a region", addr
);
3367 "%s function, region %d, offset = %ld (0x%.8lx)\n",
3368 (type
== FNTC_INITIALIZATION
) ? "init" : "term",
3369 (int)ptr
->func
.fntc_entry_loc
[i
].adr_lcid
,
3370 (long)ptr
->func
.fntc_entry_loc
[i
].adr_sctoff
,
3371 (long)ptr
->func
.fntc_entry_loc
[i
].adr_sctoff
);
3376 /* Print the global header for an OSF/rose object. */
3379 print_header (hdr_ptr
)
3380 mo_header_t
*hdr_ptr
;
3382 fprintf (stderr
, "\nglobal header:\n");
3383 fprintf (stderr
, "\tmoh_magic = 0x%.8lx\n", hdr_ptr
->moh_magic
);
3384 fprintf (stderr
, "\tmoh_major_version = %d\n", (int)hdr_ptr
->moh_major_version
);
3385 fprintf (stderr
, "\tmoh_minor_version = %d\n", (int)hdr_ptr
->moh_minor_version
);
3386 fprintf (stderr
, "\tmoh_header_version = %d\n", (int)hdr_ptr
->moh_header_version
);
3387 fprintf (stderr
, "\tmoh_max_page_size = %d\n", (int)hdr_ptr
->moh_max_page_size
);
3388 fprintf (stderr
, "\tmoh_byte_order = %d\n", (int)hdr_ptr
->moh_byte_order
);
3389 fprintf (stderr
, "\tmoh_data_rep_id = %d\n", (int)hdr_ptr
->moh_data_rep_id
);
3390 fprintf (stderr
, "\tmoh_cpu_type = %d\n", (int)hdr_ptr
->moh_cpu_type
);
3391 fprintf (stderr
, "\tmoh_cpu_subtype = %d\n", (int)hdr_ptr
->moh_cpu_subtype
);
3392 fprintf (stderr
, "\tmoh_vendor_type = %d\n", (int)hdr_ptr
->moh_vendor_type
);
3393 fprintf (stderr
, "\tmoh_load_map_cmd_off = %d\n", (int)hdr_ptr
->moh_load_map_cmd_off
);
3394 fprintf (stderr
, "\tmoh_first_cmd_off = %d\n", (int)hdr_ptr
->moh_first_cmd_off
);
3395 fprintf (stderr
, "\tmoh_sizeofcmds = %d\n", (int)hdr_ptr
->moh_sizeofcmds
);
3396 fprintf (stderr
, "\tmon_n_load_cmds = %d\n", (int)hdr_ptr
->moh_n_load_cmds
);
3397 fprintf (stderr
, "\tmoh_flags = 0x%.8lx", (long)hdr_ptr
->moh_flags
);
3399 if (hdr_ptr
->moh_flags
& MOH_RELOCATABLE_F
)
3400 fprintf (stderr
, ", relocatable");
3402 if (hdr_ptr
->moh_flags
& MOH_LINKABLE_F
)
3403 fprintf (stderr
, ", linkable");
3405 if (hdr_ptr
->moh_flags
& MOH_EXECABLE_F
)
3406 fprintf (stderr
, ", execable");
3408 if (hdr_ptr
->moh_flags
& MOH_EXECUTABLE_F
)
3409 fprintf (stderr
, ", executable");
3411 if (hdr_ptr
->moh_flags
& MOH_UNRESOLVED_F
)
3412 fprintf (stderr
, ", unresolved");
3414 fprintf (stderr
, "\n\n");
3419 /* Print a short summary of a load command. */
3422 print_load_command (load_hdr
, offset
, number
)
3423 load_union_t
*load_hdr
;
3427 mo_long_t type
= load_hdr
->hdr
.ldci_cmd_type
;
3428 char *type_str
= (char *) 0;
3432 case LDC_UNDEFINED
: type_str
= "UNDEFINED"; break;
3433 case LDC_CMD_MAP
: type_str
= "CMD_MAP"; break;
3434 case LDC_INTERPRETER
: type_str
= "INTERPRETER"; break;
3435 case LDC_STRINGS
: type_str
= "STRINGS"; break;
3436 case LDC_REGION
: type_str
= "REGION"; break;
3437 case LDC_RELOC
: type_str
= "RELOC"; break;
3438 case LDC_PACKAGE
: type_str
= "PACKAGE"; break;
3439 case LDC_SYMBOLS
: type_str
= "SYMBOLS"; break;
3440 case LDC_ENTRY
: type_str
= "ENTRY"; break;
3441 case LDC_FUNC_TABLE
: type_str
= "FUNC_TABLE"; break;
3442 case LDC_GEN_INFO
: type_str
= "GEN_INFO"; break;
3446 "cmd %2d, sz: 0x%.2lx, coff: 0x%.3lx, doff: 0x%.6lx, dlen: 0x%.6lx",
3448 (long) load_hdr
->hdr
.ldci_cmd_size
,
3450 (long) load_hdr
->hdr
.ldci_section_off
,
3451 (long) load_hdr
->hdr
.ldci_section_len
);
3453 if (type_str
== (char *) 0)
3454 fprintf (stderr
, ", ty: unknown (%ld)\n", (long) type
);
3456 else if (type
!= LDC_REGION
)
3457 fprintf (stderr
, ", ty: %s\n", type_str
);
3462 switch (load_hdr
->region
.regc_usage_type
)
3464 case REG_TEXT_T
: region
= ", .text"; break;
3465 case REG_DATA_T
: region
= ", .data"; break;
3466 case REG_BSS_T
: region
= ", .bss"; break;
3467 case REG_GLUE_T
: region
= ", .glue"; break;
3468 #if defined (REG_RDATA_T) && defined (REG_SDATA_T) && defined (REG_SBSS_T) /*mips*/
3469 case REG_RDATA_T
: region
= ", .rdata"; break;
3470 case REG_SDATA_T
: region
= ", .sdata"; break;
3471 case REG_SBSS_T
: region
= ", .sbss"; break;
3475 fprintf (stderr
, ", ty: %s, vaddr: 0x%.8lx, vlen: 0x%.6lx%s\n",
3477 (long) load_hdr
->region
.regc_vm_addr
,
3478 (long) load_hdr
->region
.regc_vm_size
,
3486 /* Fatal error when {en,de}code_mach_o_header fails. */
3492 char *msg
= (char *) 0;
3496 case MO_ERROR_BAD_MAGIC
: msg
= "bad magic number"; break;
3497 case MO_ERROR_BAD_HDR_VERS
: msg
= "bad header version"; break;
3498 case MO_ERROR_BAD_RAW_HDR_VERS
: msg
= "bad raw header version"; break;
3499 case MO_ERROR_BUF2SML
: msg
= "raw header buffer too small"; break;
3500 case MO_ERROR_OLD_RAW_HDR_FILE
: msg
= "old raw header file"; break;
3501 case MO_ERROR_UNSUPPORTED_VERS
: msg
= "unsupported version"; break;
3504 if (msg
== (char *) 0)
3505 fatal ("unknown {de,en}code_mach_o_hdr return value %d", status
);
3511 /* Read a file into a memory buffer. */
3513 static struct file_info
*
3514 read_file (name
, fd
, rw
)
3515 char *name
; /* filename */
3516 int fd
; /* file descriptor */
3517 int rw
; /* read/write */
3519 struct stat stat_pkt
;
3520 struct file_info
*p
= (struct file_info
*) xcalloc (sizeof (struct file_info
), 1);
3522 static int page_size
;
3525 if (fstat (fd
, &stat_pkt
) < 0)
3526 fatal_perror ("fstat %s", name
);
3529 p
->size
= stat_pkt
.st_size
;
3530 p
->rounded_size
= stat_pkt
.st_size
;
3536 fprintf (stderr
, "mmap %s, %s\n", name
, (rw
) ? "read/write" : "read-only");
3539 page_size
= sysconf (_SC_PAGE_SIZE
);
3541 p
->rounded_size
= ((p
->size
+ page_size
- 1) / page_size
) * page_size
;
3542 p
->start
= mmap ((caddr_t
) 0,
3543 (rw
) ? p
->rounded_size
: p
->size
,
3544 (rw
) ? (PROT_READ
| PROT_WRITE
) : PROT_READ
,
3545 MAP_FILE
| MAP_VARIABLE
| MAP_SHARED
,
3549 if (p
->start
!= (char *) 0 && p
->start
!= (char *) -1)
3553 #endif /* USE_MMAP */
3558 fprintf (stderr
, "read %s\n", name
);
3561 p
->start
= xmalloc (p
->size
);
3562 if (lseek (fd
, 0L, SEEK_SET
) < 0)
3563 fatal_perror ("lseek to 0 on %s", name
);
3565 len
= read (fd
, p
->start
, p
->size
);
3567 fatal_perror ("read %s", name
);
3570 fatal ("read %ld bytes, expected %ld, from %s", len
, p
->size
, name
);
3576 /* Do anything necessary to write a file back from memory. */
3580 struct file_info
*ptr
; /* file information block */
3588 fprintf (stderr
, "msync %s\n", ptr
->name
);
3590 if (msync (ptr
->start
, ptr
->rounded_size
, MS_ASYNC
))
3591 fatal_perror ("msync %s", ptr
->name
);
3595 fprintf (stderr
, "munmap %s\n", ptr
->name
);
3597 if (munmap (ptr
->start
, ptr
->size
))
3598 fatal_perror ("munmap %s", ptr
->name
);
3601 #endif /* USE_MMAP */
3608 fprintf (stderr
, "write %s\n", ptr
->name
);
3610 if (lseek (ptr
->fd
, 0L, SEEK_SET
) < 0)
3611 fatal_perror ("lseek to 0 on %s", ptr
->name
);
3613 len
= write (ptr
->fd
, ptr
->start
, ptr
->size
);
3615 fatal_perror ("write %s", ptr
->name
);
3617 if (len
!= ptr
->size
)
3618 fatal ("wrote %ld bytes, expected %ld, to %s", len
, ptr
->size
, ptr
->name
);
3627 #endif /* OBJECT_FORMAT_ROSE */