sim: split program path out of argv vector
authorMike Frysinger <vapier@gentoo.org>
Mon, 15 Nov 2021 07:32:06 +0000 (02:32 -0500)
committerMike Frysinger <vapier@gentoo.org>
Mon, 15 Nov 2021 07:53:29 +0000 (02:53 -0500)
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.

31 files changed:
sim/aarch64/interp.c
sim/arm/wrapper.c
sim/avr/interp.c
sim/bfin/interp.c
sim/bpf/sim-if.c
sim/common/nrun.c
sim/common/sim-base.h
sim/common/sim-options.c
sim/common/sim-utils.c
sim/cr16/interp.c
sim/cris/sim-if.c
sim/d10v/interp.c
sim/example-synacor/interp.c
sim/frv/sim-if.c
sim/ft32/interp.c
sim/h8300/compile.c
sim/iq2000/sim-if.c
sim/lm32/sim-if.c
sim/m32r/sim-if.c
sim/m68hc11/interp.c
sim/mcore/interp.c
sim/microblaze/interp.c
sim/mips/interp.c
sim/mn10300/interp.c
sim/moxie/interp.c
sim/msp430/msp430-sim.c
sim/or1k/sim-if.c
sim/pru/interp.c
sim/riscv/interp.c
sim/sh/interp.c
sim/v850/interp.c

index 18c2fc0eacbd6d8387cd0d76e7fc2624002265da..999b949ed88b0eb8c59fefd98be1771c7230fd3a 100644 (file)
@@ -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)
     {
index e697d55a6b5e283761247873a9a8ee882cda4ac1..12b974a95f820e5ffbf285c83c44de0d578a2823 100644 (file)
@@ -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;
index df7177ef23f5b37c5da9d1b12a2cbebeb1e3bb19..8ec07c98f8d21187fd755bb58d4601c0e6f41b88 100644 (file)
@@ -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;
index fab4df7aaf8b9c05f8c2504f2ef8e118f48e376d..088b55937878942c44a5d205a9194f5ace466494 100644 (file)
@@ -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;
index aba191df7ec49992566da07f70fc5339cb0f3100..a8d944115671db42a936f6faefb3b580e03952d9 100644 (file)
@@ -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)
index 3fd78346f957473a09e5e901fe9138a9a0928cba..f1fb7d12ebaa678763f0943468495316fb377069 100644 (file)
@@ -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)
index 674b2d4230847941063033f0e5268dba2d7d64ae..ff54f1d1e4c4f61694ed5ed88d3a2305afed15da 100644 (file)
@@ -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;
index e82ac33fe764a0d9a1d6488b93e583726910e00d..7e5695d8cc78b1e13672e49da6c044ca425fad65 100644 (file)
@@ -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 == '?')
index 88fd20e8876762c137252242805c94e08923ed7d..0e8cd5afc2f49a8e7b7e1361638b3478130e20e6 100644 (file)
@@ -97,6 +97,7 @@ sim_state_free (SIM_DESC sd)
   SIM_STATE_FREE (sd);
 #endif
 
+  free (STATE_PROG_FILE (sd));
   free (sd);
 }
 
index e2aef013d16016b681ea574cc8ea8bbd29a4be2e..7bbcf3cde7c907ba7ab116fca81fc0dac1059bbe 100644 (file)
@@ -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;
index 2d7e9221505ab82e012bd510d8ccb37e34428f7e..500941a27d7b21342debe3390be9d38abe3614c3 100644 (file)
@@ -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));
index 228ca3ea6e499b22b8bab6e00b18e8840f9d6b00..33baea79d79426629d83ef031ad5de922bb6d663 100644 (file)
@@ -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;
index 57b1bee7cd9d167b31b4e49406c50a93478e6b77..d1a82e509d183786f3f6e37a825235f346166a9f 100644 (file)
@@ -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;
index 6d22aaddc090584f273a85364745ef97ca6dee37..d0361537c61af43a656c119a4f3695b6c6b83500 100644 (file)
@@ -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;
index 7da6ecd2937a76485de1fd2327ff236fd5ad3832..a1cc9abe91179c26f75ac972ca99fa44c851a057 100644 (file)
@@ -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;
index 1f3316e20ad977f566e1b0592e4f557c1c8173ce..e729c520e4efad607f7613a9473a07ed3bb45769 100644 (file)
@@ -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;
index 8cfc1e0ca3e6888e16c0ad29c648a0a18ea67ffb..21ed8217bb07d1887ec96cff8fa17afac8884204 100644 (file)
@@ -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;
index 2f8b3449917aace54b1e41a41eb08c23a46588f9..ce9ab5ac96763ef6871666d63215080ed65afb27 100644 (file)
@@ -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;
index e05b1630ace7250063d7f33157d1baab31b9ffbb..b2f7b4636e0de8130dab35d851fae2038da1b4d8 100644 (file)
@@ -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;
index 7d28f40d71dd811a73e1bc4f5356d0fda614d073..977c207bfa01a98b5567949333f085ce5c292426 100644 (file)
@@ -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;
index 138dcccea4d3f642dcb22eeaf328a67eb6025e90..e8a452022192091b9cda8f95d637e5717e7026d9 100644 (file)
@@ -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;
index d2bd9e907e26014fe51205fa770e2f39a85bf15b..d80959617445af17ace0ff5c8adebef223837c65 100644 (file)
@@ -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;
index 4ab908d4f7a002fee64d86a2dd1a3f7518e1b3c1..30d417bab569434fbc1441f44086bac3f97d343f 100644 (file)
@@ -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;
index f16a756c818dfb42c3114e14b8f48c33d4401fd2..551d17630082f5c1754191cb40c1ba0229c2829c 100644 (file)
@@ -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;
index 447f52e6e40212ec7447709a8ade663ced9893e1..3aa6a3b10e0ac9b69c6f8b3f94d4b21bffb6d8df 100644 (file)
@@ -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;
index 6f1c14f759eec5f36957b4a8a76a79b2e43b728e..119d2b95ba340b4ad07394d16a6f03e2babc2e23 100644 (file)
@@ -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;
index 3f47ce5e41b62d8b48dff11869d623f53d663998..2957587afdf9ad9eb418faae5d2e1a6a66a901de 100644 (file)
@@ -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;
index fc8bbe5c4c858ce5bd7c8c1e4adb51f9b890c564..ca640f440bcd26fce0123f1f6e29520ca73dda49 100644 (file)
@@ -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;
index 202412ab8ca0b71b6cf22b39386e8a63a9a589fa..f94a8428d6903ff4992dd0fd87874e4909a9749d 100644 (file)
@@ -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;
index 559b39a63226592ccd75b4294594dfc0dfea2957..1b2b09f951005d08ded3f238ee95eb829885bf16 100644 (file)
@@ -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;
index 92c80a814c69b7d72e8eb8e0b4929b0ae41705e9..0d313658055193eb535a91dc9035dadc70025679 100644 (file)
@@ -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;