From e8f20a28b1192d746475f045d77ac84411f164df Mon Sep 17 00:00:00 2001
From: Mike Frysinger <vapier@gentoo.org>
Date: Mon, 15 Nov 2021 02:32:06 -0500
Subject: [PATCH] sim: split program path out of argv vector

We use the program argv to both find the program to run (argv[0]) and
to hold the arguments to the program.  Most of the time this is fine,
but if we want to let programs specify argv[0] independently (which is
possible in standard *NIX programs), this double duty doesn't work.

So let's split the path to the program to run out into a separate
field by itself.  This simplifies the various sim_open funcs too.

By itself, this code is more of a logical cleanup than something that
is super useful.  But it will open up customization of argv[0] in a
follow up commit.  Split the changes to make it easier to review.
---
 sim/aarch64/interp.c         |  5 +----
 sim/arm/wrapper.c            |  5 +----
 sim/avr/interp.c             |  5 +----
 sim/bfin/interp.c            |  5 +----
 sim/bpf/sim-if.c             |  5 +----
 sim/common/nrun.c            |  2 +-
 sim/common/sim-base.h        |  5 +++++
 sim/common/sim-options.c     |  5 ++++-
 sim/common/sim-utils.c       |  1 +
 sim/cr16/interp.c            |  5 +----
 sim/cris/sim-if.c            | 10 +++-------
 sim/d10v/interp.c            |  5 +----
 sim/example-synacor/interp.c |  5 +----
 sim/frv/sim-if.c             |  6 +-----
 sim/ft32/interp.c            |  5 +----
 sim/h8300/compile.c          |  5 +----
 sim/iq2000/sim-if.c          |  6 +-----
 sim/lm32/sim-if.c            |  5 +----
 sim/m32r/sim-if.c            |  6 +-----
 sim/m68hc11/interp.c         |  5 +----
 sim/mcore/interp.c           |  5 +----
 sim/microblaze/interp.c      |  5 +----
 sim/mips/interp.c            |  6 +-----
 sim/mn10300/interp.c         |  6 +-----
 sim/moxie/interp.c           |  5 +----
 sim/msp430/msp430-sim.c      |  5 +----
 sim/or1k/sim-if.c            |  5 +----
 sim/pru/interp.c             |  5 +----
 sim/riscv/interp.c           |  5 +----
 sim/sh/interp.c              |  5 +----
 sim/v850/interp.c            |  6 +-----
 31 files changed, 40 insertions(+), 119 deletions(-)

diff --git a/sim/aarch64/interp.c b/sim/aarch64/interp.c
index 18c2fc0eacb..999b949ed88 100644
--- a/sim/aarch64/interp.c
+++ b/sim/aarch64/interp.c
@@ -339,10 +339,7 @@ sim_open (SIM_OPEN_KIND                  kind,
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK
       || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
       || sim_parse_args (sd, argv) != SIM_RC_OK
-      || sim_analyze_program (sd,
-			      (STATE_PROG_ARGV (sd) != NULL
-			       ? *STATE_PROG_ARGV (sd)
-			       : NULL), abfd) != SIM_RC_OK
+      || sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK
       || sim_config (sd) != SIM_RC_OK
       || sim_post_argv_init (sd) != SIM_RC_OK)
     {
diff --git a/sim/arm/wrapper.c b/sim/arm/wrapper.c
index e697d55a6b5..12b974a95f8 100644
--- a/sim/arm/wrapper.c
+++ b/sim/arm/wrapper.c
@@ -825,10 +825,7 @@ sim_open (SIM_OPEN_KIND kind,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/avr/interp.c b/sim/avr/interp.c
index df7177ef23f..8ec07c98f8d 100644
--- a/sim/avr/interp.c
+++ b/sim/avr/interp.c
@@ -1710,10 +1710,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/bfin/interp.c b/sim/bfin/interp.c
index fab4df7aaf8..088b5593787 100644
--- a/sim/bfin/interp.c
+++ b/sim/bfin/interp.c
@@ -740,10 +740,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/bpf/sim-if.c b/sim/bpf/sim-if.c
index aba191df7ec..a8d94411567 100644
--- a/sim/bpf/sim-if.c
+++ b/sim/bpf/sim-if.c
@@ -147,10 +147,7 @@ sim_open (SIM_OPEN_KIND kind,
   if (sim_parse_args (sd, argv) != SIM_RC_OK)
     goto error;
 
-  if (sim_analyze_program (sd,
-                           (STATE_PROG_ARGV (sd) != NULL
-                            ? *STATE_PROG_ARGV (sd)
-                            : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     goto error;
 
   if (sim_config (sd) != SIM_RC_OK)
diff --git a/sim/common/nrun.c b/sim/common/nrun.c
index 3fd78346f95..f1fb7d12eba 100644
--- a/sim/common/nrun.c
+++ b/sim/common/nrun.c
@@ -103,7 +103,7 @@ main (int argc, char **argv)
   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)
diff --git a/sim/common/sim-base.h b/sim/common/sim-base.h
index 674b2d42308..ff54f1d1e4c 100644
--- a/sim/common/sim-base.h
+++ b/sim/common/sim-base.h
@@ -151,6 +151,11 @@ struct sim_state {
   const char *model_name;
 #define STATE_MODEL_NAME(sd) ((sd)->model_name)
 
+  /* In standalone simulator, this is the program to run.  Not to be confused
+     with argv which are the strings passed to the program itself.  */
+  char *prog_file;
+#define STATE_PROG_FILE(sd) ((sd)->prog_file)
+
   /* In standalone simulator, this is the program's arguments passed
      on the command line.  */
   char **prog_argv;
diff --git a/sim/common/sim-options.c b/sim/common/sim-options.c
index e82ac33fe76..7e5695d8cc7 100644
--- a/sim/common/sim-options.c
+++ b/sim/common/sim-options.c
@@ -604,7 +604,10 @@ sim_parse_args (SIM_DESC sd, char * const *argv)
       if (optc == -1)
 	{
 	  if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
-	    STATE_PROG_ARGV (sd) = dupargv (argv + optind);
+	    {
+	      STATE_PROG_FILE (sd) = xstrdup (argv[optind]);
+	      STATE_PROG_ARGV (sd) = dupargv (argv + optind);
+	    }
 	  break;
 	}
       if (optc == '?')
diff --git a/sim/common/sim-utils.c b/sim/common/sim-utils.c
index 88fd20e8876..0e8cd5afc2f 100644
--- a/sim/common/sim-utils.c
+++ b/sim/common/sim-utils.c
@@ -97,6 +97,7 @@ sim_state_free (SIM_DESC sd)
   SIM_STATE_FREE (sd);
 #endif
 
+  free (STATE_PROG_FILE (sd));
   free (sd);
 }
 
diff --git a/sim/cr16/interp.c b/sim/cr16/interp.c
index e2aef013d16..7bbcf3cde7c 100644
--- a/sim/cr16/interp.c
+++ b/sim/cr16/interp.c
@@ -423,10 +423,7 @@ sim_open (SIM_OPEN_KIND kind, struct host_callback_struct *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/cris/sim-if.c b/sim/cris/sim-if.c
index 2d7e9221505..500941a27d7 100644
--- a/sim/cris/sim-if.c
+++ b/sim/cris/sim-if.c
@@ -690,11 +690,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     }
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       /* When there's an error, sim_analyze_program has already output
 	 a message.  Let's just clarify it, as "not an object file"
@@ -717,9 +713,9 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 
   if (abfd != NULL && bfd_get_arch (abfd) == bfd_arch_unknown)
     {
-      if (STATE_PROG_ARGV (sd) != NULL)
+      if (STATE_PROG_FILE (sd) != NULL)
 	sim_io_eprintf (sd, "%s: `%s' is not a CRIS program\n",
-			STATE_MY_NAME (sd), *STATE_PROG_ARGV (sd));
+			STATE_MY_NAME (sd), STATE_PROG_FILE (sd));
       else
 	sim_io_eprintf (sd, "%s: program to be run is not a CRIS program\n",
 			STATE_MY_NAME (sd));
diff --git a/sim/d10v/interp.c b/sim/d10v/interp.c
index 228ca3ea6e4..33baea79d79 100644
--- a/sim/d10v/interp.c
+++ b/sim/d10v/interp.c
@@ -780,10 +780,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/example-synacor/interp.c b/sim/example-synacor/interp.c
index 57b1bee7cd9..d1a82e509d1 100644
--- a/sim/example-synacor/interp.c
+++ b/sim/example-synacor/interp.c
@@ -111,10 +111,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/frv/sim-if.c b/sim/frv/sim-if.c
index 6d22aaddc09..d0361537c61 100644
--- a/sim/frv/sim-if.c
+++ b/sim/frv/sim-if.c
@@ -92,11 +92,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, bfd *abfd,
     sim_do_commandf (sd, "memory region 0,0x%x", FRV_DEFAULT_MEM_SIZE);
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/ft32/interp.c b/sim/ft32/interp.c
index 7da6ecd2937..a1cc9abe911 100644
--- a/sim/ft32/interp.c
+++ b/sim/ft32/interp.c
@@ -842,10 +842,7 @@ sim_open (SIM_OPEN_KIND kind,
     }
 
   /* Check for/establish the reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c
index 1f3316e20ad..e729c520e4e 100644
--- a/sim/h8300/compile.c
+++ b/sim/h8300/compile.c
@@ -4755,10 +4755,7 @@ sim_open (SIM_OPEN_KIND kind,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/iq2000/sim-if.c b/sim/iq2000/sim-if.c
index 8cfc1e0ca3e..21ed8217bb0 100644
--- a/sim/iq2000/sim-if.c
+++ b/sim/iq2000/sim-if.c
@@ -93,11 +93,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
   sim_do_commandf (sd, "memory region 0x%x,0x%x", IQ2000_DATA_VALUE, IQ2000_DATA_MEM_SIZE); 
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/lm32/sim-if.c b/sim/lm32/sim-if.c
index 2f8b3449917..ce9ab5ac967 100644
--- a/sim/lm32/sim-if.c
+++ b/sim/lm32/sim-if.c
@@ -133,10 +133,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 #endif
 
   /* check for/establish the reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/m32r/sim-if.c b/sim/m32r/sim-if.c
index e05b1630ace..b2f7b4636e0 100644
--- a/sim/m32r/sim-if.c
+++ b/sim/m32r/sim-if.c
@@ -98,11 +98,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     sim_do_commandf (sd, "memory region 0,0x%x", M32R_DEFAULT_MEM_SIZE);
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/m68hc11/interp.c b/sim/m68hc11/interp.c
index 7d28f40d71d..977c207bfa0 100644
--- a/sim/m68hc11/interp.c
+++ b/sim/m68hc11/interp.c
@@ -436,10 +436,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/mcore/interp.c b/sim/mcore/interp.c
index 138dcccea4d..e8a45202219 100644
--- a/sim/mcore/interp.c
+++ b/sim/mcore/interp.c
@@ -1371,10 +1371,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/microblaze/interp.c b/sim/microblaze/interp.c
index d2bd9e907e2..d8095961744 100644
--- a/sim/microblaze/interp.c
+++ b/sim/microblaze/interp.c
@@ -429,10 +429,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index 4ab908d4f7a..30d417bab56 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -633,11 +633,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* check for/establish the a reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_module_uninstall (sd);
       return 0;
diff --git a/sim/mn10300/interp.c b/sim/mn10300/interp.c
index f16a756c818..551d1763008 100644
--- a/sim/mn10300/interp.c
+++ b/sim/mn10300/interp.c
@@ -268,11 +268,7 @@ sim_open (SIM_OPEN_KIND kind,
   
 
   /* check for/establish the a reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_module_uninstall (sd);
       return 0;
diff --git a/sim/moxie/interp.c b/sim/moxie/interp.c
index 447f52e6e40..3aa6a3b10e0 100644
--- a/sim/moxie/interp.c
+++ b/sim/moxie/interp.c
@@ -1226,10 +1226,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
   sim_do_command(sd," memory region 0xE0000000,0x10000") ; 
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/msp430/msp430-sim.c b/sim/msp430/msp430-sim.c
index 6f1c14f759e..119d2b95ba3 100644
--- a/sim/msp430/msp430-sim.c
+++ b/sim/msp430/msp430-sim.c
@@ -152,10 +152,7 @@ sim_open (SIM_OPEN_KIND kind,
     sim_do_commandf (sd, "memory-region 0x90000,0x70000"); /* HIGH ROM.  */
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_state_free (sd);
       return 0;
diff --git a/sim/or1k/sim-if.c b/sim/or1k/sim-if.c
index 3f47ce5e41b..2957587afdf 100644
--- a/sim/or1k/sim-if.c
+++ b/sim/or1k/sim-if.c
@@ -201,10 +201,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     }
 
   /* Check for/establish the reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/pru/interp.c b/sim/pru/interp.c
index fc8bbe5c4c8..ca640f440bc 100644
--- a/sim/pru/interp.c
+++ b/sim/pru/interp.c
@@ -772,10 +772,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/riscv/interp.c b/sim/riscv/interp.c
index 202412ab8ca..f94a8428d69 100644
--- a/sim/riscv/interp.c
+++ b/sim/riscv/interp.c
@@ -94,10 +94,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/sh/interp.c b/sim/sh/interp.c
index 559b39a6322..1b2b09f9510 100644
--- a/sim/sh/interp.c
+++ b/sim/sh/interp.c
@@ -2367,10 +2367,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/v850/interp.c b/sim/v850/interp.c
index 92c80a814c6..0d313658055 100644
--- a/sim/v850/interp.c
+++ b/sim/v850/interp.c
@@ -239,11 +239,7 @@ sim_open (SIM_OPEN_KIND    kind,
     }
 
   /* check for/establish the a reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_module_uninstall (sd);
       return 0;