Patch to constify/prototype/clean collect2.[ch] & tlink.c, part 1/2

Kaveh R. Ghazi ghazi@caip.rutgers.edu
Wed Sep 1 08:36:00 GMT 1999


	This patch constifies/prototypes and cleans stuff in
collect2.[ch] and tlink.c.  Some notes:

1.  I removed including toplev.h from tlink.c since toplev.o and
tlink.o are never linked together.  The decls tlink.c was expecting
are obtained from collect2.c anyway, so I added the appropriate
prototypes in collect2.h to fix this.

2.  Some constipated variables end up being passed to a non-const
argv.  In order to make this work without casting, I wrote and used a
macro called STRDUPA to provide a string copy via alloca.  Its
basically a copy of the strdupa macro from glibc except that it uses
global vars instead of a non-portable stmt-expr.  Please sanity check
that doing this is okay, especially where I pass the result as a
function arg.  (I seem to recall someone telling me once its bad to
use alloca in a function call parameter???  I can easily fix that one
case to not do so.)

3.  I've seen a lot of alloca/strcpy in the sources.  Should STRDUPA
be put in system.h?


I test bootstrapped this on OSF4 and SunOS4.  Okay to install?

		Thanks,
		--Kaveh


This is part 1/2:

1999-09-01  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* collect2.c (STRDUPA): New macro.
	(c_file, o_file, export_file, import_file, ldout, output_file,
	nm_file_name, ldd_file_name, strip_file_name, c_file_name,
	prefix_list, libexts, is_ctor_dtor, find_a_file, add_prefix,
	prefix_from_env, prefix_from_string, do_wait, fork_execute,
	maybe_unlink, add_to_list, extract_init_priority, write_list,
	dump_list, dump_prefix_list, write_list_with_asm, write_c_file,
	write_c_file_stat, write_c_file_glob, scan_prog_file,
	scan_libraries, is_in_list, resolve_lib_name, use_import_list,
	ignore_library, extract_string, notice, dump_file, target_machine,
	collect_wait, collect_execute, libname, locatelib, aix_std_libs,
	read_file, print_load_command): Constify a char*.
	(fdopen, error, fatal, fatal_perror): Don't prototype.
	(my_strerror): Remove.  All callers use xstrerror instead.
	(xcalloc, xmalloc, xrealloc, xstrdup, putenv): Remove definitions.
	(main): Add prototype.  Constify lots of char* ptrs.  Change calls
	to xcalloc/strcpy/strcat/...  to one call to concat.
	(main, scan_prog_file, scan_libraries): Pass a const char* to
	non-const argv via STRDUPA.
	(mapfile, libselect, libcompare, locatelib): Add prototypes.

	
diff -rup orig/egcs-CVS19990830/gcc/collect2.c egcs-CVS19990830/gcc/collect2.c
--- orig/egcs-CVS19990830/gcc/collect2.c	Tue Aug 31 21:48:37 1999
+++ egcs-CVS19990830/gcc/collect2.c	Tue Aug 31 21:44:25 1999
@@ -29,6 +29,19 @@ Boston, MA 02111-1307, USA.  */
 #include "system.h"
 #include <signal.h>
 
+/* The STRDUPA macro is like strdup, except that it allocates memory
+   using alloca.  Its argument is evaluated only once, but it uses
+   global variables so its not thread safe. */
+static const char	*__strdupa_srcptr;
+static char		*__strdupa_dstptr;
+static size_t		 __strdupa_len;
+#define STRDUPA(STRING) \
+  (__strdupa_srcptr = (STRING), \
+   __strdupa_len = strlen (__strdupa_srcptr) + 1, \
+   __strdupa_dstptr = (char *) alloca (__strdupa_len), \
+   memcpy (__strdupa_dstptr, __strdupa_srcptr, __strdupa_len), \
+   __strdupa_dstptr)
+
 #ifdef vfork /* Autoconf may define this to fork for us. */
 # define VFORK_STRING "fork"
 #else
@@ -194,20 +207,20 @@ int debug;				/* true if -debug */
 
 static int shared_obj;		        /* true if -shared */
 
-static char *c_file;			/* <xxx>.c for constructor/destructor list.  */
-static char *o_file;			/* <xxx>.o for constructor/destructor list.  */
+static const char *c_file;		/* <xxx>.c for constructor/destructor list.  */
+static const char *o_file;		/* <xxx>.o for constructor/destructor list.  */
 #ifdef COLLECT_EXPORT_LIST
-static char *export_file;	        /* <xxx>.x for AIX export list.  */
-static char *import_file;	        /* <xxx>.p for AIX import list.  */
+static const char *export_file;	        /* <xxx>.x for AIX export list.  */
+static const char *import_file;	        /* <xxx>.p for AIX import list.  */
 #endif
-char *ldout;				/* File for ld errors.  */
-static char *output_file;		/* Output file for ld.  */
-static char *nm_file_name;		/* pathname of nm */
+const char *ldout;			/* File for ld errors.  */
+static const char *output_file;		/* Output file for ld.  */
+static const char *nm_file_name;	/* pathname of nm */
 #ifdef LDD_SUFFIX
-static char *ldd_file_name;		/* pathname of ldd (or equivalent) */
+static const char *ldd_file_name;	/* pathname of ldd (or equivalent) */
 #endif
-static char *strip_file_name;		/* pathname of strip */
-char *c_file_name;		        /* pathname of gcc */
+static const char *strip_file_name;		/* pathname of strip */
+const char *c_file_name;	        /* pathname of gcc */
 static char *initname, *fininame;	/* names of init and fini funcs */
 
 static struct head constructors;	/* list of constructors found */
@@ -229,8 +242,6 @@ int pexecute_pid;
 /* Defined in the automatically-generated underscore.c.  */
 extern int prepends_underscore;
 
-extern FILE *fdopen ();
-
 #ifndef GET_ENV_PATH_LIST
 #define GET_ENV_PATH_LIST(VAR,NAME)	do { (VAR) = getenv (NAME); } while (0)
 #endif
@@ -240,7 +251,7 @@ extern FILE *fdopen ();
 
 struct prefix_list
 {
-  char *prefix;               /* String to prepend to the path.  */
+  const char *prefix;         /* String to prepend to the path.  */
   struct prefix_list *next;   /* Next in linked list.  */
 };
 
@@ -248,7 +259,7 @@ struct path_prefix
 {
   struct prefix_list *plist;  /* List of prefixes to try */
   int max_len;                /* Max length of a prefix in PLIST */
-  char *name;                 /* Name of this list (used in config stuff) */
+  const char *name;           /* Name of this list (used in config stuff) */
 };
 
 #ifdef COLLECT_EXPORT_LIST
@@ -258,48 +269,47 @@ static struct path_prefix cmdline_lib_di
 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
 					  &libpath_lib_dirs, NULL};
-static char *libexts[3] = {"a", "so", NULL};  /* possible library extentions */
+static const char *libexts[3] = {"a", "so", NULL};  /* possible library extentions */
 #endif
 
-void error		PVPROTO((const char *, ...)) ATTRIBUTE_PRINTF_1;
-void fatal		PVPROTO((const char *, ...)) 
-  ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
-void fatal_perror	PVPROTO((const char *, ...))
-  ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
-static char *my_strerror	PROTO((int));
 static const char *my_strsignal	PROTO((int));
 static void handler		PROTO((int));
-static int is_ctor_dtor		PROTO((char *));
-static char *find_a_file	PROTO((struct path_prefix *, char *));
-static void add_prefix		PROTO((struct path_prefix *, char *));
-static void prefix_from_env	PROTO((char *, struct path_prefix *));
-static void prefix_from_string	PROTO((char *, struct path_prefix *));
-static void do_wait		PROTO((char *));
-static void fork_execute	PROTO((char *, char **));
-static void maybe_unlink	PROTO((char *));
-static void add_to_list		PROTO((struct head *, char *));
-static int  extract_init_priority PROTO((char *));
+static int is_ctor_dtor		PROTO((const char *));
+static char *find_a_file	PROTO((struct path_prefix *, const char *));
+static void add_prefix		PROTO((struct path_prefix *, const char *));
+static void prefix_from_env	PROTO((const char *, struct path_prefix *));
+static void prefix_from_string	PROTO((const char *, struct path_prefix *));
+static void do_wait		PROTO((const char *));
+static void fork_execute	PROTO((const char *, char **));
+static void maybe_unlink	PROTO((const char *));
+static void add_to_list		PROTO((struct head *, const char *));
+static int extract_init_priority PROTO((const char *));
 static void sort_ids		PROTO((struct head *));
-static void write_list		PROTO((FILE *, char *, struct id *));
+static void write_list		PROTO((FILE *, const char *, struct id *));
 #ifdef COLLECT_EXPORT_LIST
-static void dump_list		PROTO((FILE *, char *, struct id *));
+static void dump_list		PROTO((FILE *, const char *, struct id *));
 #endif
 #if 0
-static void dump_prefix_list	PROTO((FILE *, char *, struct prefix_list *));
+static void dump_prefix_list	PROTO((FILE *, const char *, struct prefix_list *));
+#endif
+static void write_list_with_asm PROTO((FILE *, const char *, struct id *));
+static void write_c_file	PROTO((FILE *, const char *));
+static void write_c_file_stat	PROTO((FILE *, const char *));
+#ifndef LD_INIT_SWITCH
+static void write_c_file_glob	PROTO((FILE *, const char *));
 #endif
-static void write_list_with_asm PROTO((FILE *, char *, struct id *));
-static void write_c_file	PROTO((FILE *, char *));
-static void scan_prog_file	PROTO((char *, enum pass));
+static void scan_prog_file	PROTO((const char *, enum pass));
 #ifdef SCAN_LIBRARIES
-static void scan_libraries	PROTO((char *));
+static void scan_libraries	PROTO((const char *));
 #endif
 #ifdef COLLECT_EXPORT_LIST
-static int is_in_list		PROTO((char *, struct id *));
+static int is_in_list		PROTO((const char *, struct id *));
 static void write_aix_file	PROTO((FILE *, struct id *));
-static char *resolve_lib_name	PROTO((char *));
-static int use_import_list	PROTO((char *));
-static int ignore_library	PROTO((char *));
+static char *resolve_lib_name	PROTO((const char *));
+static int use_import_list	PROTO((const char *));
+static int ignore_library	PROTO((const char *));
 #endif
+static char *extract_string	PROTO((const char **));
 
 #ifdef NO_DUP2
 int
@@ -323,26 +333,6 @@ dup2 (oldfd, newfd)
 }
 #endif
 
-static char *
-my_strerror (e)
-     int e;
-{
-
-#ifdef HAVE_STRERROR
-  return strerror (e);
-
-#else
-
-  if (!e)
-    return "";
-
-  if (e > 0 && e < sys_nerr)
-    return sys_errlist[e];
-
-  return "errno = ?";
-#endif
-}
-
 static const char *
 my_strsignal (s)
      int s;
@@ -401,17 +391,17 @@ collect_exit (status)
 
 /* Notify user of a non-error.  */
 void
-notice VPROTO((char *msgid, ...))
+notice VPROTO((const char *msgid, ...))
 {
 #ifndef ANSI_PROTOTYPES
-  char *msgid;
+  const char *msgid;
 #endif
   va_list ap;
 
   VA_START (ap, msgid);
 
 #ifndef ANSI_PROTOTYPES
-  msgid = va_arg (ap, char *);
+  msgid = va_arg (ap, const char *);
 #endif
 
   vfprintf (stderr, _(msgid), ap);
@@ -437,7 +427,7 @@ fatal_perror VPROTO((const char * msgid,
 
   fprintf (stderr, "collect2: ");
   vfprintf (stderr, _(msgid), ap);
-  fprintf (stderr, ": %s\n", my_strerror (e));
+  fprintf (stderr, ": %s\n", xstrerror (e));
   va_end (ap);
 
   collect_exit (FATAL_EXIT_CODE);
@@ -524,67 +514,20 @@ handler (signo)
 }
 
 
-PTR
-xcalloc (size1, size2)
-  size_t size1, size2;
-{
-  PTR ptr = (PTR) calloc (size1, size2);
-  if (!ptr)
-    fatal ("out of memory");
-  return ptr;
-}
-
-PTR
-xmalloc (size)
-  size_t size;
-{
-  PTR ptr = (PTR) malloc (size);
-  if (!ptr)
-    fatal ("out of memory");
-  return ptr;
-}
-
-PTR
-xrealloc (old, size)
-  PTR old;
-  size_t size;
-{
-  register PTR ptr;
-  if (old)
-    ptr = (PTR) realloc (old, size);
-  else
-    ptr = (PTR) malloc (size);
-  if (ptr == 0)
-    fatal ("virtual memory exhausted");
-  return ptr;
-}
-
 int
 file_exists (name)
-     char *name;
+     const char *name;
 {
   return access (name, R_OK) == 0;
 }
 
-/* Make a copy of a string INPUT with size SIZE.  */
-
-char *
-xstrdup (input)
-  const char *input;
-{
-  register size_t len = strlen (input) + 1;
-  register char *output = xmalloc (len);
-  memcpy (output, input, len);
-  return output;
-}
-
 /* Parse a reasonable subset of shell quoting syntax.  */
 
 static char *
 extract_string (pp)
-     char **pp;
+     const char **pp;
 {
-  char *p = *pp;
+  const char *p = *pp;
   int backquote = 0;
   int inside = 0;
 
@@ -613,7 +556,7 @@ extract_string (pp)
 
 void
 dump_file (name)
-     char *name;
+     const char *name;
 {
   FILE *stream = fopen (name, "r");
   int no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
@@ -628,7 +571,8 @@ dump_file (name)
 	obstack_1grow (&temporary_obstack, c);
       if (obstack_object_size (&temporary_obstack) > 0)
 	{
-	  char *word, *p, *result;
+	  const char *word, *p;
+	  char *result;
 	  obstack_1grow (&temporary_obstack, '\0');
 	  word = obstack_finish (&temporary_obstack);
 
@@ -674,13 +618,13 @@ dump_file (name)
 
 static int
 is_ctor_dtor (s)
-     char *s;
+     const char *s;
 {
-  struct names { char *name; int len; int ret; int two_underscores; };
+  struct names { const char *name; int len; int ret; int two_underscores; };
 
   register struct names *p;
   register int ch;
-  register char *orig_s = s;
+  register const char *orig_s = s;
 
   static struct names special[] = {
 #ifdef NO_DOLLAR_IN_LABEL
@@ -729,54 +673,6 @@ is_ctor_dtor (s)
   return 0;
 }
 
-/* Routine to add variables to the environment.  */
-
-#ifndef HAVE_PUTENV
-
-int
-putenv (str)
-     char *str;
-{
-#ifndef VMS			/* nor about VMS */
-
-  extern char **environ;
-  char **old_environ = environ;
-  char **envp;
-  int num_envs = 0;
-  int name_len = 1;
-  char *p = str;
-  int ch;
-
-  while ((ch = *p++) != '\0' && ch != '=')
-    name_len++;
-
-  if (!ch)
-    abort ();
-
-  /* Search for replacing an existing environment variable, and
-     count the number of total environment variables.  */
-  for (envp = old_environ; *envp; envp++)
-    {
-      num_envs++;
-      if (!strncmp (str, *envp, name_len))
-	{
-	  *envp = str;
-	  return 0;
-	}
-    }
-
-  /* Add a new environment variable */
-  environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
-  *environ = str;
-  bcopy ((char *) old_environ, (char *) (environ + 1),
-	 sizeof (char *) * (num_envs+1));
-
-  return 0;
-#endif	/* VMS */
-}
-
-#endif	/* HAVE_PUTENV */
-
 /* By default, colon separates directories in a path.  */
 #ifndef PATH_SEPARATOR
 #define PATH_SEPARATOR ':'
@@ -791,7 +687,7 @@ static struct path_prefix cpath, path;
 /* This is the name of the target machine.  We use it to form the name
    of the files to execute.  */
 
-static char *target_machine = TARGET_MACHINE;
+static const char *const target_machine = TARGET_MACHINE;
 #endif
 
 /* Search for NAME using prefix list PPREFIX.  We only look for executable
@@ -802,7 +698,7 @@ static char *target_machine = TARGET_MAC
 static char *
 find_a_file (pprefix, name)
      struct path_prefix *pprefix;
-     char *name;
+     const char *name;
 {
   char *temp;
   struct prefix_list *pl;
@@ -885,7 +781,7 @@ find_a_file (pprefix, name)
 static void
 add_prefix (pprefix, prefix)
      struct path_prefix *pprefix;
-     char *prefix;
+     const char *prefix;
 {
   struct prefix_list *pl, **prev;
   int len;
@@ -920,10 +816,10 @@ add_prefix (pprefix, prefix)
 
 static void
 prefix_from_env (env, pprefix)
-     char *env;
+     const char *env;
      struct path_prefix *pprefix;
 {
-  char *p;
+  const char *p;
   GET_ENV_PATH_LIST (p, env);
 
   if (p)
@@ -932,10 +828,10 @@ prefix_from_env (env, pprefix)
 
 static void
 prefix_from_string (p, pprefix)
-     char *p;
+     const char *p;
      struct path_prefix *pprefix;
 {
-  char *startp, *endp;
+  const char *startp, *endp;
   char *nstore = (char *) xmalloc (strlen (p) + 3);
 
   if (debug)
@@ -974,43 +870,44 @@ prefix_from_string (p, pprefix)
 
 /* Main program.  */
 
+int main 		PROTO ((int, char *[]));
 int
 main (argc, argv)
      int argc;
      char *argv[];
 {
-  char *ld_suffix	= "ld";
-  char *full_ld_suffix	= ld_suffix;
-  char *real_ld_suffix	= "real-ld";
-  char *collect_ld_suffix = "collect-ld";
-  char *nm_suffix	= "nm";
-  char *full_nm_suffix	= nm_suffix;
-  char *gnm_suffix	= "gnm";
-  char *full_gnm_suffix	= gnm_suffix;
+  const char *ld_suffix	= "ld";
+  const char *full_ld_suffix	= ld_suffix;
+  const char *real_ld_suffix	= "real-ld";
+  const char *collect_ld_suffix = "collect-ld";
+  const char *nm_suffix	= "nm";
+  const char *full_nm_suffix	= nm_suffix;
+  const char *gnm_suffix	= "gnm";
+  const char *full_gnm_suffix	= gnm_suffix;
 #ifdef LDD_SUFFIX
-  char *ldd_suffix	= LDD_SUFFIX;
-  char *full_ldd_suffix	= ldd_suffix;
+  const char *ldd_suffix	= LDD_SUFFIX;
+  const char *full_ldd_suffix	= ldd_suffix;
 #endif
-  char *strip_suffix	= "strip";
-  char *full_strip_suffix = strip_suffix;
-  char *gstrip_suffix	= "gstrip";
-  char *full_gstrip_suffix = gstrip_suffix;
-  char *arg;
+  const char *strip_suffix	= "strip";
+  const char *full_strip_suffix = strip_suffix;
+  const char *gstrip_suffix	= "gstrip";
+  const char *full_gstrip_suffix = gstrip_suffix;
+  const char *arg;
   FILE *outf;
 #ifdef COLLECT_EXPORT_LIST
   FILE *exportf;
   FILE *importf;
 #endif
-  char *ld_file_name;
-  char *p;
+  const char *ld_file_name;
+  const char *p;
   char **c_argv;
-  char **c_ptr;
+  const char **c_ptr;
   char **ld1_argv;
-  char **ld1;
+  const char **ld1;
   char **ld2_argv;
-  char **ld2;
+  const char **ld2;
   char **object_lst;
-  char **object;
+  const char **object;
   int first_file;
   int num_c_args	= argc+9;
 
@@ -1028,9 +925,9 @@ main (argc, argv)
   /* Do not invoke xcalloc before this point, since locale needs to be
      set first, in case a diagnostic is issued.  */
 
-  ld1 = ld1_argv = (char **) xcalloc (sizeof (char *), argc+3);
-  ld2 = ld2_argv = (char **) xcalloc (sizeof (char *), argc+6);
-  object = object_lst = (char **) xcalloc (sizeof (char *), argc);
+  ld1 = (const char **)(ld1_argv = (char **) xcalloc(sizeof (char *), argc+3));
+  ld2 = (const char **)(ld2_argv = (char **) xcalloc(sizeof (char *), argc+6));
+  object = (const char **)(object_lst = (char **) xcalloc(sizeof (char *), argc));
 
 #ifdef DEBUG
   debug = 1;
@@ -1062,14 +959,15 @@ main (argc, argv)
   p = getenv ("COLLECT_GCC_OPTIONS");
   while (p && *p)
     {
-      char *q = extract_string (&p);
+      const char *q = extract_string (&p);
       if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
 	num_c_args++;
     }
   obstack_free (&temporary_obstack, temporary_firstobj);
   ++num_c_args;
 
-  c_ptr = c_argv = (char **) xcalloc (sizeof (char *), num_c_args);
+  c_ptr = (const char **)
+    (c_argv = (char **) xcalloc (sizeof (char *), num_c_args));
 
   if (argc < 2)
     fatal ("no arguments");
@@ -1105,51 +1003,23 @@ main (argc, argv)
      But it we look for a program in the system directories, we need to
      qualify the program name with the target machine.  */
 
-  full_ld_suffix
-    = xcalloc (strlen (ld_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_ld_suffix, target_machine);
-  strcat (full_ld_suffix, "-");
-  strcat (full_ld_suffix, ld_suffix);
+  full_ld_suffix = concat(target_machine, "-", ld_suffix, NULL);
 
 #if 0
-  full_gld_suffix
-    = xcalloc (strlen (gld_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_gld_suffix, target_machine);
-  strcat (full_gld_suffix, "-");
-  strcat (full_gld_suffix, gld_suffix);
-#endif
-
-  full_nm_suffix
-    = xcalloc (strlen (nm_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_nm_suffix, target_machine);
-  strcat (full_nm_suffix, "-");
-  strcat (full_nm_suffix, nm_suffix);
-
-  full_gnm_suffix
-    = xcalloc (strlen (gnm_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_gnm_suffix, target_machine);
-  strcat (full_gnm_suffix, "-");
-  strcat (full_gnm_suffix, gnm_suffix);
+  full_gld_suffix = concat (target_machine, "-", gld_suffix, NULL);
+#endif
+
+  full_nm_suffix = concat (target_machine, "-", nm_suffix, NULL);
 
+  full_gnm_suffix = concat (target_machine, "-", gnm_suffix, NULL);
+  
 #ifdef LDD_SUFFIX
-  full_ldd_suffix
-    = xcalloc (strlen (ldd_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_ldd_suffix, target_machine);
-  strcat (full_ldd_suffix, "-");
-  strcat (full_ldd_suffix, ldd_suffix);
+  full_ldd_suffix = concat (target_machine, "-", ldd_suffix, NULL);
 #endif
 
-  full_strip_suffix
-    = xcalloc (strlen (strip_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_strip_suffix, target_machine);
-  strcat (full_strip_suffix, "-");
-  strcat (full_strip_suffix, strip_suffix);
+  full_strip_suffix = concat (target_machine, "-", strip_suffix, NULL);
   
-  full_gstrip_suffix
-    = xcalloc (strlen (gstrip_suffix) + strlen (target_machine) + 2, 1);
-  strcpy (full_gstrip_suffix, target_machine);
-  strcat (full_gstrip_suffix, "-");
-  strcat (full_gstrip_suffix, gstrip_suffix);
+  full_gstrip_suffix = concat (target_machine, "-", gstrip_suffix, NULL);
 #endif /* CROSS_COMPILE */
 
   /* Try to discover a valid linker/nm/strip to use.  */
@@ -1214,9 +1084,7 @@ main (argc, argv)
   if (c_file_name == 0)
     {
 #ifdef CROSS_COMPILE
-      c_file_name = xcalloc (sizeof ("gcc-") + strlen (target_machine) + 1, 1);
-      strcpy (c_file_name, target_machine);
-      strcat (c_file_name, "-gcc");
+      c_file_name = concat (target_machine, "-gcc", NULL);
 #else
       c_file_name = "gcc";
 #endif
@@ -1266,7 +1134,7 @@ main (argc, argv)
   p = getenv ("COLLECT_GCC_OPTIONS");
   while (p && *p)
     {
-      char *q = extract_string (&p);
+      const char *q = extract_string (&p);
       if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
 	*c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
       if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
@@ -1328,7 +1196,7 @@ main (argc, argv)
 #ifdef COLLECT_EXPORT_LIST
 	      {
 	        /* Resolving full library name.  */
-		char *s = resolve_lib_name (arg+2);
+		const char *s = resolve_lib_name (arg+2);
 
 		/* If we will use an import list for this library,
 		   we should exclude it from ld args.  */
@@ -1476,7 +1344,7 @@ main (argc, argv)
 #endif
 
   *c_ptr++ = c_file;
-  *object = *c_ptr = *ld1 = (char *) 0;
+  *c_ptr = *ld1 = *object = (char *) 0;
 
   if (vflag)
     {
@@ -1489,7 +1357,7 @@ main (argc, argv)
 
   if (debug)
     {
-      char *ptr;
+      const char *ptr;
       fprintf (stderr, "ld_file_name        = %s\n",
 	       (ld_file_name ? ld_file_name : "not found"));
       fprintf (stderr, "c_file_name         = %s\n",
@@ -1592,8 +1460,8 @@ main (argc, argv)
       if (strip_flag)
 	{
 	  char **strip_argv = (char **) xcalloc (sizeof (char *), 3);
-	  strip_argv[0] = strip_file_name;
-	  strip_argv[1] = output_file;
+	  strip_argv[0] = STRDUPA (strip_file_name);
+	  strip_argv[1] = STRDUPA (output_file);
 	  strip_argv[2] = (char *) 0;
 	  fork_execute ("strip", strip_argv);
 	}
@@ -1703,7 +1571,7 @@ main (argc, argv)
 
 int
 collect_wait (prog)
-     char *prog;
+     const char *prog;
 {
   int status;
 
@@ -1730,7 +1598,7 @@ collect_wait (prog)
 
 static void
 do_wait (prog)
-     char *prog;
+     const char *prog;
 {
   int ret = collect_wait (prog);
   if (ret != 0)
@@ -1745,9 +1613,9 @@ do_wait (prog)
 
 void
 collect_execute (prog, argv, redir)
-     char *prog;
+     const char *prog;
      char **argv;
-     char *redir;
+     const char *redir;
 {
   char *errmsg_fmt;
   char *errmsg_arg;
@@ -1758,7 +1626,7 @@ collect_execute (prog, argv, redir)
   if (vflag || debug)
     {
       char **p_argv;
-      char *str;
+      const char *str;
 
       if (argv[0])
 	fprintf (stderr, "%s", argv[0]);
@@ -1819,7 +1687,7 @@ collect_execute (prog, argv, redir)
 
 static void
 fork_execute (prog, argv)
-     char *prog;
+     const char *prog;
      char **argv;
 {
   collect_execute (prog, argv, NULL);
@@ -1830,7 +1698,7 @@ fork_execute (prog, argv)
 
 static void
 maybe_unlink (file)
-     char *file;
+     const char *file;
 {
   if (!debug)
     unlink (file);
@@ -1846,7 +1714,7 @@ static long sequence_number = 0;
 static void
 add_to_list (head_ptr, name)
      struct head *head_ptr;
-     char *name;
+     const char *name;
 {
   struct id *newid
     = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
@@ -1880,7 +1748,7 @@ add_to_list (head_ptr, name)
 
 static int
 extract_init_priority (name)
-     char *name;
+     const char *name;
 {
   int pos = 0, pri;
 
@@ -1941,7 +1809,7 @@ sort_ids (head_ptr)
 static void
 write_list (stream, prefix, list)
      FILE *stream;
-     char *prefix;
+     const char *prefix;
      struct id *list;
 {
   while (list)
@@ -1955,7 +1823,7 @@ write_list (stream, prefix, list)
 /* This function is really used only on AIX, but may be useful.  */
 static int
 is_in_list (prefix, list)
-     char *prefix;
+     const char *prefix;
      struct id *list;
 {
   while (list)
@@ -1972,7 +1840,7 @@ is_in_list (prefix, list)
 static void
 dump_list (stream, prefix, list)
      FILE *stream;
-     char *prefix;
+     const char *prefix;
      struct id *list;
 {
   while (list)
@@ -1987,7 +1855,7 @@ dump_list (stream, prefix, list)
 static void
 dump_prefix_list (stream, prefix, list)
      FILE *stream;
-     char *prefix;
+     const char *prefix;
      struct prefix_list *list;
 {
   while (list)
@@ -2001,7 +1869,7 @@ dump_prefix_list (stream, prefix, list)
 static void
 write_list_with_asm (stream, prefix, list)
      FILE *stream;
-     char *prefix;
+     const char *prefix;
      struct id *list;
 {
   while (list)
@@ -2018,15 +1886,16 @@ write_list_with_asm (stream, prefix, lis
 static void
 write_c_file_stat (stream, name)
      FILE *stream;
-     char *name;
+     const char *name ATTRIBUTE_UNUSED;
 {
-  char *prefix, *p, *q;
+  const char *p, *q;
+  char *prefix, *r;
   int frames = (frame_tables.number > 0);
 
   /* Figure out name of output_file, stripping off .so version.  */
   p = rindex (output_file, '/');
   if (p == 0)
-    p = (char *) output_file;
+    p = output_file;
   else
     p++;
   q = p;
@@ -2053,9 +1922,9 @@ write_c_file_stat (stream, name)
   prefix = xmalloc (q - p + 1);
   strncpy (prefix, p, q - p);
   prefix[q - p] = 0;
-  for (q = prefix; *q; q++)
-    if (!ISALNUM ((unsigned char)*q))
-      *q = '_';
+  for (r = prefix; *r; r++)
+    if (!ISALNUM ((unsigned char)*r))
+      *r = '_';
   if (debug)
     notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
 	    output_file, prefix);
@@ -2153,7 +2022,7 @@ write_c_file_stat (stream, name)
 static void
 write_c_file_glob (stream, name)
      FILE *stream;
-     char *name;
+     const char *name ATTRIBUTE_UNUSED;
 {
   /* Write the tables as C code  */
 
@@ -2218,7 +2087,7 @@ write_c_file_glob (stream, name)
 static void
 write_c_file (stream, name)
      FILE *stream;
-     char *name;
+     const char *name;
 {
   fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
 #ifndef LD_INIT_SWITCH
@@ -2257,7 +2126,7 @@ write_aix_file (stream, list)
 
 static void
 scan_prog_file (prog_name, which_pass)
-     char *prog_name;
+     const char *prog_name;
      enum pass which_pass;
 {
   void (*int_handler) ();
@@ -2276,11 +2145,11 @@ scan_prog_file (prog_name, which_pass)
   if (nm_file_name == 0)
     fatal ("cannot find `nm'");
 
-  nm_argv[argc++] = nm_file_name;
+  nm_argv[argc++] = STRDUPA(nm_file_name);
   if (NM_FLAGS[0] != '\0')
-    nm_argv[argc++] = NM_FLAGS;
+    nm_argv[argc++] = STRDUPA (NM_FLAGS);
 
-  nm_argv[argc++] = prog_name;
+  nm_argv[argc++] = STRDUPA (prog_name);
   nm_argv[argc++] = (char *) 0;
 
   if (pipe (pipe_fd) < 0)
@@ -2294,7 +2163,7 @@ scan_prog_file (prog_name, which_pass)
   if (vflag)
     {
       char **p_argv;
-      char *str;
+      const char *str;
 
       for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
 	fprintf (stderr, " %s", str);
@@ -2444,9 +2313,11 @@ struct head libraries;
 
 /* Map the file indicated by NAME into memory and store its address.  */
 
+static void mapfile			PROTO ((const char *));
+
 static void
 mapfile (name)
-     char *name;
+     const char *name;
 {
   int fp;
   struct stat s;
@@ -2458,7 +2329,7 @@ mapfile (name)
   objsize = s.st_size;
   object = (unsigned) mmap (0, objsize, PROT_READ|PROT_WRITE, MAP_PRIVATE,
 			    fp, 0);
-  if (object == -1)
+  if (object == (unsigned)-1)
     fatal ("unable to mmap file '%s'", name);
 
   close (fp);
@@ -2466,7 +2337,9 @@ mapfile (name)
 
 /* Helpers for locatelib.  */
 
-static char *libname;
+static const char *libname;
+
+static int libselect			PROTO ((struct direct *));
 
 static int
 libselect (d)
@@ -2482,6 +2355,7 @@ libselect (d)
    We must verify that the extension is numeric, because Sun saves the
    original versions of patched libraries with a .FCS extension.  Files with
    invalid extensions must go last in the sort, so that they will not be used.  */
+static int libcompare		PROTO ((struct direct **, struct direct **));
 
 static int
 libcompare (d1, d2)
@@ -2526,16 +2400,17 @@ libcompare (d1, d2)
 
 /* Given the name NAME of a dynamic dependency, find its pathname and add
    it to the list of libraries.  */
+static void locatelib			PROTO ((const char *));
 
 static void
 locatelib (name)
-     char *name;
+     const char *name;
 {
-  static char **l;
+  static const char **l;
   static int cnt;
   char buf[MAXPATHLEN];
   char *p, *q;
-  char **pp;
+  const char **pp;
 
   if (l == 0)
     {
@@ -2565,7 +2440,7 @@ locatelib (name)
 	  q = (char *) xmalloc (strlen (p) + 1);
 	  strcpy (q, p);
 	}
-      l = (char **) xmalloc ((cnt + 3) * sizeof (char *));
+      l = (const char **) xmalloc ((cnt + 3) * sizeof (char *));
       pp = l;
       if (ldr)
 	{
@@ -2621,7 +2496,7 @@ locatelib (name)
 
 static void 
 scan_libraries (prog_name)
-     char *prog_name;
+     const char *prog_name;
 {
   struct exec *header;
   char *base;
@@ -2696,7 +2571,7 @@ scan_libraries (prog_name)
 
 static void 
 scan_libraries (prog_name)
-     char *prog_name;
+     const char *prog_name;
 {
   static struct head libraries;		/* list of shared libraries found */
   struct id *list;
@@ -2716,8 +2591,8 @@ scan_libraries (prog_name)
       return;
     }
 
-  ldd_argv[argc++] = ldd_file_name;
-  ldd_argv[argc++] = prog_name;
+  ldd_argv[argc++] = STRDUPA (ldd_file_name);
+  ldd_argv[argc++] = STRDUPA (prog_name);
   ldd_argv[argc++] = (char *) 0;
 
   if (pipe (pipe_fd) < 0)
@@ -2731,7 +2606,7 @@ scan_libraries (prog_name)
   if (vflag)
     {
       char **p_argv;
-      char *str;
+      const char *str;
 
       for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
 	fprintf (stderr, " %s", str);
@@ -2874,7 +2749,7 @@ extern char *ldgetname ();
 
 static void
 scan_prog_file (prog_name, which_pass)
-     char *prog_name;
+     const char *prog_name;
      enum pass which_pass;
 {
   LDFILE *ldptr = NULL;
@@ -2899,7 +2774,7 @@ scan_prog_file (prog_name, which_pass)
   do
     {
 #endif
-      if ((ldptr = ldopen (prog_name, ldptr)) != NULL)
+      if ((ldptr = ldopen (STRDUPA(prog_name), ldptr)) != NULL)
 	{
 	  if (! MY_ISCOFF (HEADER (ldptr).f_magic))
 	    fatal ("%s: not a COFF file", prog_name);
@@ -3068,7 +2943,7 @@ scan_prog_file (prog_name, which_pass)
    generate import list for an object or to use it directly.  */
 static int
 use_import_list (prog_name)
-     char *prog_name;
+     const char *prog_name;
 {
   char *p;
 
@@ -3086,7 +2961,7 @@ use_import_list (prog_name)
    returns a full library name including a path.  */
 static char *
 resolve_lib_name (name)
-     char *name;
+     const char *name;
 {
   char *lib_buf;
   int i, j, l = 0;
@@ -3107,7 +2982,7 @@ resolve_lib_name (name)
               /* The following lines are needed because path_prefix list
                  may contain directories both with trailing '/' and
                  without it.  */
-              char *p = "";
+              const char *p = "";
               if (list->prefix[strlen(list->prefix)-1] != '/')
                 p = "/";
        	      sprintf (lib_buf, "%s%slib%s.%s",
@@ -3130,7 +3005,7 @@ if (debug) fprintf (stderr, "found: %s\n
 
 /* Array of standard AIX libraries which should not
    be scanned for ctors/dtors.  */
-static char* aix_std_libs[] = {
+static const char *aix_std_libs[] = {
   "/unix",
   "/lib/libc.a",
   "/lib/libc_r.a",
@@ -3146,9 +3021,9 @@ static char* aix_std_libs[] = {
    if this name matches the location of a standard AIX library. */
 static int
 ignore_library (name)
-     char *name;
+     const char *name;
 {
-  char **p = &aix_std_libs[0];
+  const char **p = &aix_std_libs[0];
   while (*p++ != NULL)
     if (! strcmp (name, *p)) return 1;
   return 0;
@@ -3211,7 +3086,7 @@ static void add_func_table	PROTO((mo_hea
 static void print_header	PROTO((mo_header_t *));
 static void print_load_command	PROTO((load_union_t *, size_t, int));
 static void bad_header		PROTO((int));
-static struct file_info	*read_file  PROTO((char *, int, int));
+static struct file_info	*read_file  PROTO((const char *, int, int));
 static void end_file		PROTO((struct file_info *));
 
 /* OSF/rose specific version to scan the name list of the loaded
@@ -3226,7 +3101,7 @@ static void end_file		PROTO((struct file
 
 static void
 scan_prog_file (prog_name, which_pass)
-     char *prog_name;
+     const char *prog_name;
      enum pass which_pass;
 {
   char *obj;
@@ -3335,7 +3210,7 @@ scan_prog_file (prog_name, which_pass)
 
 	  if (debug)
 	    {
-	      char *kind = "unknown";
+	      const char *kind = "unknown";
 
 	      switch (load_hdr->sym.symc_kind)
 		{
@@ -3626,7 +3501,7 @@ print_load_command (load_hdr, offset, nu
      int number;
 {
   mo_long_t type = load_hdr->hdr.ldci_cmd_type;
-  char *type_str = (char *) 0;
+  const char *type_str = (char *) 0;
 
   switch (type)
     {
@@ -3659,7 +3534,7 @@ print_load_command (load_hdr, offset, nu
 
   else
     {
-      char *region = "";
+      const char *region = "";
       switch (load_hdr->region.regc_usage_type)
 	{
 	case REG_TEXT_T:	region = ", .text";	break;
@@ -3708,7 +3583,7 @@ bad_header (status)
 
 static struct file_info *
 read_file (name, fd, rw)
-     char *name;		/* filename */
+     const char *name;		/* filename */
      int fd;			/* file descriptor */
      int rw;			/* read/write */
 {


More information about the Gcc-patches mailing list