[metal] Use "/proc/self/exe" as program name & symbol to yoink

for support for opening argv[0]
This commit is contained in:
tkchia 2022-11-02 14:48:26 +00:00
parent 484da042ab
commit 7409100874
5 changed files with 11 additions and 9 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/calls/syscall-sysv.internal.h" #include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/errno.h" #include "libc/errno.h"
@ -76,10 +77,14 @@ static inline void GetProgramExecutableNameImpl(char *p, char *e) {
return; return;
} }
if (IsMetal()) {
if (!memccpy(p, APE_COM_NAME, 0, e - p - 1)) e[-1] = 0;
return;
}
// if argv[0] exists then turn it into an absolute path. we also try // if argv[0] exists then turn it into an absolute path. we also try
// adding a .com suffix since the ape auto-appends it when resolving // adding a .com suffix since the ape auto-appends it when resolving
if (__argc && (((q = __argv[0]) && !sys_faccessat(AT_FDCWD, q, F_OK, 0)) || if (__argc && (((q = __argv[0]) && !sys_faccessat(AT_FDCWD, q, F_OK, 0)) ||
IsMetal() ||
((q = StrCat(u.path, __argv[0], ".com")) && ((q = StrCat(u.path, __argv[0], ".com")) &&
!sys_faccessat(AT_FDCWD, q, F_OK, 0)))) { !sys_faccessat(AT_FDCWD, q, F_OK, 0)))) {
if (*q != '/') { if (*q != '/') {

View file

@ -15,8 +15,6 @@ extern size_t __ape_com_size;
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#define APE_COM_NAME "/ape.com" #define APE_COM_NAME "/proc/self/exe"
#define APE_COM_ALT_NAME "/proc/self/exe"
#define APE_COM_URI "file:/proc/self/exe"
#endif /* COSMOPOLITAN_LIBC_CALLS_METALFILE_INTERNAL_H_ */ #endif /* COSMOPOLITAN_LIBC_CALLS_METALFILE_INTERNAL_H_ */

View file

@ -34,5 +34,5 @@
pop %rsi pop %rsi
pop %rdi pop %rdi
.init.end 101,_init_metalfile .init.end 101,_init_metalfile
APE_COM_URI: APE_COM_NAME:
.endobj APE_COM_URI,globl,hidden .endobj APE_COM_NAME,globl,hidden

View file

@ -38,8 +38,7 @@
int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) { int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
int fd; int fd;
struct MetalFile *state; struct MetalFile *state;
if (dirfd != AT_FDCWD || (strcmp(file, APE_COM_NAME) && if (dirfd != AT_FDCWD || strcmp(file, APE_COM_NAME)) return enoent();
strcmp(file, APE_COM_ALT_NAME))) return enoent();
if (flags != O_RDONLY) return eacces(); if (flags != O_RDONLY) return eacces();
if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size)) if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size))
return eopnotsupp(); return eopnotsupp();

View file

@ -30,7 +30,7 @@
#include "libc/zip.h" #include "libc/zip.h"
#include "libc/zipos/zipos.internal.h" #include "libc/zipos/zipos.internal.h"
STATIC_YOINK(APE_COM_URI); STATIC_YOINK(APE_COM_NAME);
static uint64_t __zipos_get_min_offset(const uint8_t *base, static uint64_t __zipos_get_min_offset(const uint8_t *base,
const uint8_t *cdir) { const uint8_t *cdir) {