forked from OSchip/llvm-project
[nolibc] Make GetArgsAndEnv libc-independent.
__libc_stack_end is made into a weak symbol if possible. If libc is not linked, read args and environment from /proc. llvm-svn: 182276
This commit is contained in:
parent
312425f32d
commit
d5169edc36
|
@ -315,21 +315,11 @@ bool SetEnv(const char *name, const char *value) {
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifdef __GLIBC__
|
||||
|
||||
extern "C" {
|
||||
extern void *__libc_stack_end;
|
||||
extern void *__libc_stack_end SANITIZER_WEAK_ATTRIBUTE;
|
||||
}
|
||||
|
||||
static void GetArgsAndEnv(char ***argv, char ***envp) {
|
||||
uptr *stack_end = (uptr *)__libc_stack_end;
|
||||
int argc = *stack_end;
|
||||
*argv = (char**)(stack_end + 1);
|
||||
*envp = (char**)(stack_end + argc + 2);
|
||||
}
|
||||
|
||||
#else // __GLIBC__
|
||||
|
||||
#if !SANITIZER_GO
|
||||
static void ReadNullSepFileToArray(const char *path, char ***arr,
|
||||
int arr_size) {
|
||||
char *buff;
|
||||
|
@ -348,15 +338,25 @@ static void ReadNullSepFileToArray(const char *path, char ***arr,
|
|||
}
|
||||
(*arr)[count] = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void GetArgsAndEnv(char ***argv, char ***envp) {
|
||||
static const int kMaxArgv = 2000, kMaxEnvp = 2000;
|
||||
ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
|
||||
ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
|
||||
static void GetArgsAndEnv(char*** argv, char*** envp) {
|
||||
#if !SANITIZER_GO
|
||||
if (&__libc_stack_end) {
|
||||
#endif
|
||||
uptr* stack_end = (uptr*)__libc_stack_end;
|
||||
int argc = *stack_end;
|
||||
*argv = (char**)(stack_end + 1);
|
||||
*envp = (char**)(stack_end + argc + 2);
|
||||
#if !SANITIZER_GO
|
||||
} else {
|
||||
static const int kMaxArgv = 2000, kMaxEnvp = 2000;
|
||||
ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
|
||||
ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // __GLIBC__
|
||||
|
||||
void ReExec() {
|
||||
char **argv, **envp;
|
||||
GetArgsAndEnv(&argv, &envp);
|
||||
|
|
Loading…
Reference in New Issue