]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/common/nrun.c
sim: formally assume unistd.h always exists (via gnulib)
[thirdparty/binutils-gdb.git] / sim / common / nrun.c
index 9213d076b0caca46606bb8041afee337e82ef25b..2ebf8a1271663399e231ebd157ae13ca22903a0c 100644 (file)
@@ -1,5 +1,5 @@
 /* New version of run front end support for simulators.
-   Copyright (C) 1997-2017 Free Software Foundation, Inc.
+   Copyright (C) 1997-2023 Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -15,32 +15,25 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Need to be before general includes, to pick up e.g. _GNU_SOURCE.  */
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+#include "defs.h"
 
 #include <signal.h>
-
+#include <stdlib.h>
 /* For strsignal.  */
-#ifdef HAVE_STRING_H
 #include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-
-#include "sim-main.h"
+/* For chdir.  */
+#include <unistd.h>
 
 #include "bfd.h"
+#include "environ.h"
 
-#ifdef HAVE_ENVIRON
-extern char **environ;
-#endif
+#include "sim-main.h"
+#include "sim-signal.h"
+#include "sim/callback.h"
 
-#ifdef HAVE_UNISTD_H
-/* For chdir.  */
-#include <unistd.h>
+#ifndef HAVE_STRSIGNAL
+/* While libiberty provides a fallback, it doesn't provide a prototype.  */
+extern const char *strsignal (int);
 #endif
 
 static void usage (void);
@@ -66,6 +59,7 @@ main (int argc, char **argv)
 {
   const char *name;
   char **prog_argv = NULL;
+  char **prog_envp = NULL;
   struct bfd *prog_bfd;
   enum sim_stop reason;
   int sigrc = 0;
@@ -94,19 +88,21 @@ main (int argc, char **argv)
       abort ();
     }
 
-  /* We can't set the endianness in the callback structure until
-     sim_config is called, which happens in sim_open.  */
-  default_callback.target_endian
-    = (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
-       ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
+  /* We can't set the endianness in the callback structure until sim_config is
+     called, which happens in sim_open.  If it's still the default, switch it.
+     Don't use CURRENT_TARGET_BYTE_ORDER as all its internal processing already
+     happened in sim_config.  */
+  if (default_callback.target_endian == BFD_ENDIAN_UNKNOWN)
+    default_callback.target_endian = current_target_byte_order;
 
   /* Was there a program to run?  */
   prog_argv = STATE_PROG_ARGV (sd);
+  prog_envp = STATE_PROG_ENVP (sd) ? : environ;
   prog_bfd = STATE_PROG_BFD (sd);
   if (prog_argv == NULL || *prog_argv == NULL)
     usage ();
 
-  name = *prog_argv;
+  name = STATE_PROG_FILE (sd);
 
   /* For simulators that don't open prog during sim_open() */
   if (prog_bfd == NULL)
@@ -134,11 +130,7 @@ main (int argc, char **argv)
     exit (1);
 
   /* Prepare the program for execution.  */
-#ifdef HAVE_ENVIRON
-  sim_create_inferior (sd, prog_bfd, prog_argv, environ);
-#else
-  sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
-#endif
+  sim_create_inferior (sd, prog_bfd, prog_argv, prog_envp);
 
   /* To accommodate relative file paths, chdir to sysroot now.  We
      mustn't do this until BFD has opened the program, else we wouldn't
@@ -234,7 +226,8 @@ main (int argc, char **argv)
 static void
 usage (void)
 {
-  fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
+  fprintf (stderr, "Usage: %s [options] [VAR=VAL|--] program [program args]\n",
+          myname);
   fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
   exit (1);
 }