From 54f7a83a62c27d6da9ee66da1022572d6ea45d84 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 15 Nov 2021 23:00:04 -0500 Subject: [PATCH] sim: keep track of program environment strings We've been passing the environment strings to sim_create_inferior, but most ports don't do anything with them. A few will use ad-hoc logic to stuff the stack for user-mode programs, but that's it. Let's formalize this across the board by storing the strings in the normal sim state. This will allow (in future commits) supporting more functionality in the run interface, and to unify some of the libgloss syscalls. --- sim/aarch64/interp.c | 6 ++++++ sim/bfin/interp.c | 6 ++++++ sim/bpf/sim-if.c | 8 +++++++- sim/common/sim-base.h | 4 ++++ sim/common/sim-utils.c | 1 + sim/cris/sim-if.c | 10 ++++++++-- sim/example-synacor/interp.c | 6 ++++++ sim/frv/sim-if.c | 8 +++++++- sim/ft32/interp.c | 7 +++++++ sim/iq2000/sim-if.c | 8 +++++++- sim/lm32/sim-if.c | 8 +++++++- sim/m32r/sim-if.c | 8 +++++++- sim/pru/interp.c | 6 ++++++ sim/riscv/interp.c | 6 ++++++ 14 files changed, 85 insertions(+), 7 deletions(-) diff --git a/sim/aarch64/interp.c b/sim/aarch64/interp.c index 999b949ed88..4d7f4217456 100644 --- a/sim/aarch64/interp.c +++ b/sim/aarch64/interp.c @@ -144,6 +144,12 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + if (trace_load_symbols (sd)) { STATE_PROG_SYMS_COUNT (sd) = diff --git a/sim/bfin/interp.c b/sim/bfin/interp.c index 088b5593787..9d0e737cc33 100644 --- a/sim/bfin/interp.c +++ b/sim/bfin/interp.c @@ -1170,6 +1170,12 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + switch (STATE_ENVIRONMENT (sd)) { case USER_ENVIRONMENT: diff --git a/sim/bpf/sim-if.c b/sim/bpf/sim-if.c index a8d94411567..7503a302ca3 100644 --- a/sim/bpf/sim-if.c +++ b/sim/bpf/sim-if.c @@ -190,7 +190,7 @@ sim_open (SIM_OPEN_KIND kind, SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, - char *const *argv, char *const *envp) + char *const *argv, char *const *env) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; @@ -213,5 +213,11 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/common/sim-base.h b/sim/common/sim-base.h index c34f6f66a91..50def1e02b7 100644 --- a/sim/common/sim-base.h +++ b/sim/common/sim-base.h @@ -165,6 +165,10 @@ struct sim_state { char *prog_argv0; #define STATE_PROG_ARGV0(sd) ((sd)->prog_argv0) + /* The program's environment. */ + char **prog_envp; +#define STATE_PROG_ENVP(sd) ((sd)->prog_envp) + /* The program's bfd. */ struct bfd *prog_bfd; #define STATE_PROG_BFD(sd) ((sd)->prog_bfd) diff --git a/sim/common/sim-utils.c b/sim/common/sim-utils.c index 6f147b2d703..ff6234b1c98 100644 --- a/sim/common/sim-utils.c +++ b/sim/common/sim-utils.c @@ -99,6 +99,7 @@ sim_state_free (SIM_DESC sd) free (STATE_PROG_FILE (sd)); free (STATE_PROG_ARGV0 (sd)); + freeargv (STATE_PROG_ENVP (sd)); free (sd); } diff --git a/sim/cris/sim-if.c b/sim/cris/sim-if.c index 500941a27d7..d29ccc59f35 100644 --- a/sim/cris/sim-if.c +++ b/sim/cris/sim-if.c @@ -951,8 +951,8 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, - char * const *argv ATTRIBUTE_UNUSED, - char * const *envp ATTRIBUTE_UNUSED) + char * const *argv, + char * const *env) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; @@ -977,6 +977,12 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/example-synacor/interp.c b/sim/example-synacor/interp.c index d1a82e509d1..93aea7b0a86 100644 --- a/sim/example-synacor/interp.c +++ b/sim/example-synacor/interp.c @@ -174,5 +174,11 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/frv/sim-if.c b/sim/frv/sim-if.c index d0361537c61..7b0913dad01 100644 --- a/sim/frv/sim-if.c +++ b/sim/frv/sim-if.c @@ -175,7 +175,7 @@ frv_sim_close (SIM_DESC sd, int quitting) SIM_RC sim_create_inferior (SIM_DESC sd, bfd *abfd, char * const *argv, - char * const *envp) + char * const *env) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; @@ -196,5 +196,11 @@ sim_create_inferior (SIM_DESC sd, bfd *abfd, char * const *argv, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/ft32/interp.c b/sim/ft32/interp.c index a1cc9abe911..2838395dd02 100644 --- a/sim/ft32/interp.c +++ b/sim/ft32/interp.c @@ -900,6 +900,13 @@ sim_create_inferior (SIM_DESC sd, freeargv (STATE_PROG_ARGV (sd)); STATE_PROG_ARGV (sd) = dupargv (argv); } + + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + cpu->state.regs[FT32_HARD_SP] = addr; cpu->state.num_i = 0; cpu->state.cycles = 0; diff --git a/sim/iq2000/sim-if.c b/sim/iq2000/sim-if.c index 21ed8217bb0..05b348efe90 100644 --- a/sim/iq2000/sim-if.c +++ b/sim/iq2000/sim-if.c @@ -131,7 +131,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, - char * const *envp) + char * const *env) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; @@ -152,5 +152,11 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/lm32/sim-if.c b/sim/lm32/sim-if.c index ce9ab5ac967..e7bffbdf3f6 100644 --- a/sim/lm32/sim-if.c +++ b/sim/lm32/sim-if.c @@ -192,7 +192,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, - char * const *envp) + char * const *env) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; @@ -213,5 +213,11 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/m32r/sim-if.c b/sim/m32r/sim-if.c index b2f7b4636e0..0cb49a1e4e1 100644 --- a/sim/m32r/sim-if.c +++ b/sim/m32r/sim-if.c @@ -145,7 +145,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, - char * const *envp) + char * const *env) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; @@ -174,6 +174,12 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/pru/interp.c b/sim/pru/interp.c index ca640f440bc..f243df9426d 100644 --- a/sim/pru/interp.c +++ b/sim/pru/interp.c @@ -848,5 +848,11 @@ sim_create_inferior (SIM_DESC sd, struct bfd *prog_bfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + return SIM_RC_OK; } diff --git a/sim/riscv/interp.c b/sim/riscv/interp.c index f94a8428d69..7ecdd55141c 100644 --- a/sim/riscv/interp.c +++ b/sim/riscv/interp.c @@ -153,6 +153,12 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, STATE_PROG_ARGV (sd) = dupargv (argv); } + if (STATE_PROG_ENVP (sd) != env) + { + freeargv (STATE_PROG_ENVP (sd)); + STATE_PROG_ENVP (sd) = dupargv (env); + } + initialize_env (sd, (void *)argv, (void *)env); return SIM_RC_OK; -- 2.30.2