Keep argv[0], add COSMOPOLITAN_PROGRAM_EXECUTABLE (#980)

* Introduce env.com

Handy tool for debugging environment issues.

* Inject path as COSMOPOLITAN_PROGRAM_EXECUTABLE

`argv[0]` was previously being used as a communication channel between
the loader and the binary, giving the binary its full path for use e.g.
in `GetProgramExecutableName`. But `argv[0]` is not a good channel for
this; much of what made 2a3813c6 so gross is due to that.

This change fixes the issue by preserving `argv[0]` and establishing a
new communication channel: `COSMOPOLITAN_PROGRAM_EXECUTABLE`.

The M1 loader will always set this as the first variable. Linux should
soon follow. On the other side, `GetProgramExecutableName` checks that
variable first. If it sees it, it trusts it as-is.

A lot of the churn in `ape/ape-m1.c` in this change is actually backing
out hacks introduced in 2a3813c6; the best comparison is:

    git diff 2a3813c6^..
This commit is contained in:
Jōshin 2023-12-04 15:45:46 -05:00 committed by GitHub
parent 2a3813c6cf
commit ed8fadea37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 168 additions and 38 deletions

10
examples/env.c Normal file
View file

@ -0,0 +1,10 @@
#include "libc/stdio/stdio.h"
#include "libc/runtime/runtime.h"
int main(int argc, char* argv[]) {
printf("%s\n", argv[0]);
for (char **p = environ; *p; ++p) {
printf(" %s\n", *p);
}
return 0;
}