Skip to content

More reliable $prog.ape #1071

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

Merged
merged 10 commits into from
Jan 7, 2024
Merged

More reliable $prog.ape #1071

merged 10 commits into from
Jan 7, 2024

Conversation

mrdomino
Copy link
Collaborator

@mrdomino mrdomino commented Jan 7, 2024

Now it doesn't matter what argv $prog.ape is invoked with. We just get our executable path the apple way.

https://github.com/opensource-apple/dyld/blob/master/src/dyld.cpp#L4789

PR also includes passing AT_FLAGS from the M1 loader, which lets the GetProgramExecutableName tests always detect whether argv[0] should be tested from auxv.

Also changes literally to match loader.c in behavior.

Unlike loader.c, we need AccessCommand to handle the `.ape` indirection.
So FindCommand stays.
@mrdomino mrdomino marked this pull request as draft January 7, 2024 13:01
@mrdomino
Copy link
Collaborator Author

mrdomino commented Jan 7, 2024

I want to rework AccessCommand a bit now that we no longer need to path search in the indirect case.

No longer need to do a path search in the indirect case, nor check for
the .ape file's presence.
@mrdomino
Copy link
Collaborator Author

mrdomino commented Jan 7, 2024

Done.

@mrdomino mrdomino marked this pull request as ready for review January 7, 2024 13:22
@mrdomino mrdomino requested a review from jart January 7, 2024 13:37
Copy link
Owner

@jart jart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I forgot about Apple's auxv string array. Is there anything else good we can pilfer from there?

@jart jart merged commit aa37a32 into jart:master Jan 7, 2024
@mrdomino
Copy link
Collaborator Author

mrdomino commented Jan 7, 2024

Here's what it gives us. Nothing looks immediately useful to me.

% cc apple.c -o apple
% cat apple.c
#include <stdio.h>

int main(int argc, char *argv[], char *envp[])
{
  char **p;
  for (p = envp; *p; ++p) {
  }
  for (++p; *p; ++p) {
    printf("%s\n", *p);
  }
  return 0;
}
% ./apple
executable_path=./apple



ptr_munge=
main_stack=
executable_file=0x1a0100000d,0x1689fd4
dyld_file=0x1a0100000d,0xfffffff0009a8fa
executable_cdhash=1360c6329d477bc5198ee5997a4f5f5ae8166f9e
executable_boothash=b110e39ca11baa63bdb1da60bdc0c2cc15731989
arm64e_abi=os
th_port=

@mrdomino mrdomino deleted the m1-exepath branch January 7, 2024 15:16
@mrdomino
Copy link
Collaborator Author

mrdomino commented Jan 7, 2024

Notably though, we should grab executable_path out of their auxv before we reset it in crt.S.

@ghaerr
Copy link
Contributor

ghaerr commented Jan 7, 2024

Is there anything else good we can pilfer from there?

Here's the actual Apple exec code that creates the auxv string array. The exec_apple_add_strings (line 5530) function shows each of the keys that might be interesting to us.

The executable_path= string is handled by calls to the exec_save_path function. @mrdomino, you might find all this interesting. I studied it earlier when I was investigating some issues with @jart's original loader and the ELF AUXV array which isn't used by machO binaries.

BTW, Apple's loader source is here, which I found interesting. The link shows the loader code lines which disallow static binaries on M1, obviating the need for two Apple loaders in Cosmopolitan. Note that development and debug kernels allow M1 static binaries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants