Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loader.c uses AT_FLAGS to communicate literally #1168

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions ape/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ EXTERN_C __attribute__((__noreturn__)) void ApeLoader(long di, long *sp,
char arg0, literally;
unsigned long pagesz;
union ElfEhdrBuf *ebuf;
long *auxv, *ap, *endp, *sp2;
long *auxv, *flags, *ap, *endp, *sp2;
char *p, *pe, *exe, *prog, **argv, **envp;

(void)Printf;
Expand Down Expand Up @@ -993,13 +993,15 @@ EXTERN_C __attribute__((__noreturn__)) void ApeLoader(long di, long *sp,
/* detect netbsd and find end of words */
pagesz = 0;
arg0 = 0;
flags = 0;
for (ap = auxv; ap[0]; ap += 2) {
if (ap[0] == AT_PAGESZ) {
pagesz = ap[1];
} else if (SupportsNetbsd() && !os && ap[0] == AT_EXECFN_NETBSD) {
os = NETBSD;
} else if (SupportsLinux() && ap[0] == AT_FLAGS) {
// TODO(mrdomino): maybe set/insert this when we are called as "ape -".
} else if (!flags && ap[0] == AT_FLAGS) {
// TODO(mrdomino): does anyone use auxv 8 for anything else?
flags = ap;
arg0 = !!(ap[1] & AT_FLAGS_PRESERVE_ARGV0);
}
}
Expand Down Expand Up @@ -1047,8 +1049,9 @@ EXTERN_C __attribute__((__noreturn__)) void ApeLoader(long di, long *sp,

/* create new bottom of stack for spawned program
system v abi aligns this on a 16-byte boundary
grows down the alloc by poking the guard pages */
n = (endp - sp + 1) * sizeof(long);
grows down the alloc by poking the guard pages
uses AT_FLAGS to say that argv[0] is preserved */
n = (endp - sp + 2 * !flags + 1) * sizeof(long);
sp2 = (long *)__builtin_alloca(n);
if ((long)sp2 & 15)
++sp2;
Expand All @@ -1059,6 +1062,15 @@ EXTERN_C __attribute__((__noreturn__)) void ApeLoader(long di, long *sp,
argv = (char **)(sp2 + 1);
envp = (char **)(sp2 + 1 + argc + 1);
auxv = sp2 + (auxv - sp);
if (flags) {
flags = flags - sp + sp2;
flags[1] |= literally * AT_FLAGS_PRESERVE_ARGV0;
} else {
flags = (endp - 1) - sp + sp2;
flags[0] = AT_FLAGS;
flags[1] = literally * AT_FLAGS_PRESERVE_ARGV0;
flags[2] = 0; /* end-of-auxv sentinel */
}
sp = sp2;

/* allocate ephemeral memory for reading file */
Expand Down
1 change: 1 addition & 0 deletions test/libc/calls/getprogramexecutablename_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void SetUpOnce(void) {
skiptests = IsOpenbsd() || (IsXnu() && !IsXnuSilicon());
}
} else {
// TODO(mrdomino): test skiparg0 in child
skiparg0 = !(getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0);
}
fprintf(stderr, loaded ? "loaded\n" : "not loaded\n");
Expand Down