mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
Mint APE Loader v1.7
This change reduces the memory requirements of your APE Loader by 10x, in terms of virtual memory size, thanks to the help of alloca(). We're also now creating argument blocks with the same layout across systems.
This commit is contained in:
parent
d967a94c9a
commit
1d8937d528
31 changed files with 367 additions and 656 deletions
240
ape/ape-m1.c
240
ape/ape-m1.c
|
@ -87,11 +87,9 @@ struct Syslib {
|
||||||
#define AT_RANDOM 25
|
#define AT_RANDOM 25
|
||||||
#define AT_EXECFN 31
|
#define AT_EXECFN 31
|
||||||
|
|
||||||
#define STACK_SIZE (8ul * 1024 * 1024)
|
#define AUXV_WORDS 29
|
||||||
#define STACK_ALIGN (sizeof(long) * 2)
|
|
||||||
#define AUXV_BYTES (sizeof(long) * 2 * 15)
|
|
||||||
|
|
||||||
// from the xnu codebase
|
/* from the xnu codebase */
|
||||||
#define _COMM_PAGE_START_ADDRESS 0x0000000FFFFFC000ul
|
#define _COMM_PAGE_START_ADDRESS 0x0000000FFFFFC000ul
|
||||||
#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C)
|
#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C)
|
||||||
#define _COMM_PAGE_APRR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110)
|
#define _COMM_PAGE_APRR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110)
|
||||||
|
@ -149,7 +147,7 @@ union ElfEhdrBuf {
|
||||||
|
|
||||||
union ElfPhdrBuf {
|
union ElfPhdrBuf {
|
||||||
struct ElfPhdr phdr;
|
struct ElfPhdr phdr;
|
||||||
char buf[4096];
|
char buf[1024];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PathSearcher {
|
struct PathSearcher {
|
||||||
|
@ -160,15 +158,7 @@ struct PathSearcher {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ApeLoader {
|
struct ApeLoader {
|
||||||
union ElfEhdrBuf ehdr;
|
|
||||||
struct PathSearcher ps;
|
struct PathSearcher ps;
|
||||||
// this memory shall be discarded by the guest
|
|
||||||
//////////////////////////////////////////////
|
|
||||||
// this memory shall be known to guest program
|
|
||||||
union {
|
|
||||||
char argblock[ARG_MAX];
|
|
||||||
long numblock[ARG_MAX / sizeof(long)];
|
|
||||||
};
|
|
||||||
union ElfPhdrBuf phdr;
|
union ElfPhdrBuf phdr;
|
||||||
struct Syslib lib;
|
struct Syslib lib;
|
||||||
char rando[16];
|
char rando[16];
|
||||||
|
@ -561,19 +551,15 @@ __attribute__((__noreturn__)) static void Spawn(const char *exe, int fd,
|
||||||
unsigned long wipe;
|
unsigned long wipe;
|
||||||
prot1 = prot;
|
prot1 = prot;
|
||||||
prot2 = prot;
|
prot2 = prot;
|
||||||
/*
|
/* when we ask the system to map the interval [vaddr,vaddr+filesz)
|
||||||
* when we ask the system to map the interval [vaddr,vaddr+filesz)
|
it might schlep extra file content into memory on both the left
|
||||||
* it might schlep extra file content into memory on both the left
|
and the righthand side. that's because elf doesn't require that
|
||||||
* and the righthand side. that's because elf doesn't require that
|
either side of the interval be aligned on the system page size.
|
||||||
* either side of the interval be aligned on the system page size.
|
normally we can get away with ignoring these junk bytes. but if
|
||||||
*
|
the segment defines bss memory (i.e. memsz > filesz) then we'll
|
||||||
* normally we can get away with ignoring these junk bytes. but if
|
need to clear the extra bytes in the page, if they exist. since
|
||||||
* the segment defines bss memory (i.e. memsz > filesz) then we'll
|
we can't do that if we're mapping a read-only page, we can just
|
||||||
* need to clear the extra bytes in the page, if they exist.
|
map it with write permissions and call mprotect on it afterward */
|
||||||
*
|
|
||||||
* since we can't do that if we're mapping a read-only page, we'll
|
|
||||||
* actually map it with write permissions and protect it afterward
|
|
||||||
*/
|
|
||||||
a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */
|
a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */
|
||||||
b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */
|
b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */
|
||||||
c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */
|
c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */
|
||||||
|
@ -648,16 +634,17 @@ __attribute__((__noreturn__)) static void Spawn(const char *exe, int fd,
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *TryElf(struct ApeLoader *M, const char *exe, int fd,
|
static const char *TryElf(struct ApeLoader *M, union ElfEhdrBuf *ebuf,
|
||||||
long *sp, long *bp, char *execfn) {
|
const char *exe, int fd, long *sp, long *auxv,
|
||||||
|
char *execfn) {
|
||||||
long i, rc;
|
long i, rc;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
struct ElfEhdr *e;
|
struct ElfEhdr *e;
|
||||||
struct ElfPhdr *p;
|
struct ElfPhdr *p;
|
||||||
|
|
||||||
/* validate elf header */
|
/* validate elf header */
|
||||||
e = &M->ehdr.ehdr;
|
e = &ebuf->ehdr;
|
||||||
if (READ32(M->ehdr.buf) != READ32("\177ELF")) {
|
if (READ32(ebuf->buf) != READ32("\177ELF")) {
|
||||||
return "didn't embed ELF magic";
|
return "didn't embed ELF magic";
|
||||||
}
|
}
|
||||||
if (e->e_ident[EI_CLASS] == ELFCLASS32) {
|
if (e->e_ident[EI_CLASS] == ELFCLASS32) {
|
||||||
|
@ -678,7 +665,7 @@ static const char *TryElf(struct ApeLoader *M, const char *exe, int fd,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read program headers */
|
/* read program headers */
|
||||||
rc = pread(fd, M->phdr.buf, size, M->ehdr.ehdr.e_phoff);
|
rc = pread(fd, M->phdr.buf, size, ebuf->ehdr.e_phoff);
|
||||||
if (rc < 0) return "failed to read ELF program headers";
|
if (rc < 0) return "failed to read ELF program headers";
|
||||||
if (rc != size) return "truncated read of ELF program headers";
|
if (rc != size) return "truncated read of ELF program headers";
|
||||||
|
|
||||||
|
@ -735,26 +722,35 @@ static const char *TryElf(struct ApeLoader *M, const char *exe, int fd,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* simulate linux auxiliary values */
|
/* simulate linux auxiliary values */
|
||||||
long auxv[][2] = {
|
auxv[0] = AT_PHDR;
|
||||||
{AT_PHDR, (long)&M->phdr.phdr}, //
|
auxv[1] = (long)&M->phdr.phdr;
|
||||||
{AT_PHENT, M->ehdr.ehdr.e_phentsize}, //
|
auxv[2] = AT_PHENT;
|
||||||
{AT_PHNUM, M->ehdr.ehdr.e_phnum}, //
|
auxv[3] = ebuf->ehdr.e_phentsize;
|
||||||
{AT_ENTRY, M->ehdr.ehdr.e_entry}, //
|
auxv[4] = AT_PHNUM;
|
||||||
{AT_PAGESZ, pagesz}, //
|
auxv[5] = ebuf->ehdr.e_phnum;
|
||||||
{AT_UID, getuid()}, //
|
auxv[6] = AT_ENTRY;
|
||||||
{AT_EUID, geteuid()}, //
|
auxv[7] = ebuf->ehdr.e_entry;
|
||||||
{AT_GID, getgid()}, //
|
auxv[8] = AT_PAGESZ;
|
||||||
{AT_EGID, getegid()}, //
|
auxv[9] = pagesz;
|
||||||
{AT_HWCAP, 0xffb3ffffu}, //
|
auxv[10] = AT_UID;
|
||||||
{AT_HWCAP2, 0x181}, //
|
auxv[11] = getuid();
|
||||||
{AT_SECURE, issetugid()}, //
|
auxv[12] = AT_EUID;
|
||||||
{AT_RANDOM, (long)M->rando}, //
|
auxv[13] = geteuid();
|
||||||
{AT_EXECFN, (long)execfn}, //
|
auxv[14] = AT_GID;
|
||||||
{0, 0}, //
|
auxv[15] = getgid();
|
||||||
};
|
auxv[16] = AT_EGID;
|
||||||
_Static_assert(sizeof(auxv) == AUXV_BYTES,
|
auxv[17] = getegid();
|
||||||
"Please update the AUXV_BYTES constant");
|
auxv[18] = AT_HWCAP;
|
||||||
MemMove(bp, auxv, sizeof(auxv));
|
auxv[19] = 0xffb3ffffu;
|
||||||
|
auxv[20] = AT_HWCAP2;
|
||||||
|
auxv[21] = 0x181;
|
||||||
|
auxv[22] = AT_SECURE;
|
||||||
|
auxv[23] = issetugid();
|
||||||
|
auxv[24] = AT_RANDOM;
|
||||||
|
auxv[25] = (long)M->rando;
|
||||||
|
auxv[26] = AT_EXECFN;
|
||||||
|
auxv[27] = (long)execfn;
|
||||||
|
auxv[28] = 0;
|
||||||
|
|
||||||
/* we're now ready to load */
|
/* we're now ready to load */
|
||||||
Spawn(exe, fd, sp, e, p, &M->lib);
|
Spawn(exe, fd, sp, e, p, &M->lib);
|
||||||
|
@ -788,31 +784,17 @@ static long sys_mmap(void *addr, size_t size, int prot, int flags, int fd,
|
||||||
int main(int argc, char **argv, char **envp) {
|
int main(int argc, char **argv, char **envp) {
|
||||||
long z;
|
long z;
|
||||||
void *map;
|
void *map;
|
||||||
long *sp, *bp, *ip;
|
|
||||||
int c, i, n, fd, rc;
|
int c, i, n, fd, rc;
|
||||||
struct ApeLoader *M;
|
struct ApeLoader *M;
|
||||||
unsigned char rando[24];
|
long *sp, *sp2, *auxv;
|
||||||
|
union ElfEhdrBuf *ebuf;
|
||||||
char *p, *pe, *tp, *exe, *prog, *execfn;
|
char *p, *pe, *tp, *exe, *prog, *execfn;
|
||||||
|
|
||||||
// generate some hard random data
|
/* allocate loader memory in program's arg block */
|
||||||
if (getentropy(rando, sizeof(rando))) {
|
n = sizeof(struct ApeLoader);
|
||||||
Pexit(argv[0], -1, "getentropy");
|
M = __builtin_alloca(n);
|
||||||
}
|
|
||||||
|
|
||||||
// make the stack look like a linux one
|
/* expose apple libs */
|
||||||
map = mmap((void *)(0x7f0000000000 | (long)rando[23] << 32), STACK_SIZE,
|
|
||||||
PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
|
||||||
if (map == MAP_FAILED) {
|
|
||||||
Pexit(argv[0], -1, "stack mmap");
|
|
||||||
}
|
|
||||||
|
|
||||||
// put argument block at top of allocated stack
|
|
||||||
z = (long)map;
|
|
||||||
z += STACK_SIZE - sizeof(struct ApeLoader);
|
|
||||||
z &= -_Alignof(struct ApeLoader);
|
|
||||||
M = (struct ApeLoader *)z;
|
|
||||||
|
|
||||||
// expose screwy apple libs
|
|
||||||
M->lib.magic = SYSLIB_MAGIC;
|
M->lib.magic = SYSLIB_MAGIC;
|
||||||
M->lib.version = SYSLIB_VERSION;
|
M->lib.version = SYSLIB_VERSION;
|
||||||
M->lib.fork = sys_fork;
|
M->lib.fork = sys_fork;
|
||||||
|
@ -833,44 +815,24 @@ int main(int argc, char **argv, char **envp) {
|
||||||
M->lib.dispatch_semaphore_wait = dispatch_semaphore_wait;
|
M->lib.dispatch_semaphore_wait = dispatch_semaphore_wait;
|
||||||
M->lib.dispatch_walltime = dispatch_walltime;
|
M->lib.dispatch_walltime = dispatch_walltime;
|
||||||
|
|
||||||
// copy system provided argument block
|
/* getenv("_") is close enough to at_execfn */
|
||||||
bp = M->numblock;
|
|
||||||
tp = M->argblock + sizeof(M->argblock);
|
|
||||||
*bp++ = argc;
|
|
||||||
for (i = 0; i < argc; ++i) {
|
|
||||||
tp -= (n = StrLen(argv[i]) + 1);
|
|
||||||
MemMove(tp, argv[i], n);
|
|
||||||
*bp++ = (long)tp;
|
|
||||||
}
|
|
||||||
*bp++ = 0;
|
|
||||||
for (i = 0; envp[i]; ++i) {
|
|
||||||
tp -= (n = StrLen(envp[i]) + 1);
|
|
||||||
MemMove(tp, envp[i], n);
|
|
||||||
*bp++ = (long)tp;
|
|
||||||
}
|
|
||||||
*bp++ = 0;
|
|
||||||
|
|
||||||
// get arguments that point into our block
|
|
||||||
sp = M->numblock;
|
|
||||||
argc = *sp;
|
|
||||||
argv = (char **)(sp + 1);
|
|
||||||
envp = (char **)(sp + 1 + argc + 1);
|
|
||||||
|
|
||||||
// xnu stores getauxval(at_execfn) in getenv("_")
|
|
||||||
execfn = argc > 0 ? argv[0] : 0;
|
execfn = argc > 0 ? argv[0] : 0;
|
||||||
for (i = 0; envp[i]; ++i) {
|
for (i = 0; envp[i]; ++i) {
|
||||||
if (envp[i][0] == '_' && envp[i][1] == '=') {
|
if (envp[i][0] == '_' && envp[i][1] == '=') {
|
||||||
execfn = envp[i] + 2;
|
execfn = envp[i] + 2;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// interpret command line arguments
|
/* sneak the system five abi back out of args */
|
||||||
|
sp = (long *)(argv - 1);
|
||||||
|
auxv = (long *)(envp + i + 1);
|
||||||
|
|
||||||
|
/* interpret command line arguments */
|
||||||
if (argc >= 3 && !StrCmp(argv[1], "-")) {
|
if (argc >= 3 && !StrCmp(argv[1], "-")) {
|
||||||
// if the first argument is a hyphen then we give the user the
|
/* if the first argument is a hyphen then we give the user the
|
||||||
// power to change argv[0] or omit it entirely. most operating
|
power to change argv[0] or omit it entirely. most operating
|
||||||
// systems don't permit the omission of argv[0] but we do, b/c
|
systems don't permit the omission of argv[0] but we do, b/c
|
||||||
// it's specified by ANSI X3.159-1988.
|
it's specified by ANSI X3.159-1988. */
|
||||||
prog = (char *)sp[3];
|
prog = (char *)sp[3];
|
||||||
argc = sp[3] = sp[0] - 3;
|
argc = sp[3] = sp[0] - 3;
|
||||||
argv = (char **)((sp += 3) + 1);
|
argv = (char **)((sp += 3) + 1);
|
||||||
|
@ -887,19 +849,41 @@ int main(int argc, char **argv, char **envp) {
|
||||||
argv = (char **)((sp += 1) + 1);
|
argv = (char **)((sp += 1) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// search for executable
|
/* 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 = (auxv - sp + AUXV_WORDS + 1) * sizeof(long);
|
||||||
|
sp2 = __builtin_alloca(n);
|
||||||
|
if ((long)sp2 & 15) ++sp2;
|
||||||
|
for (; n > 0; n -= pagesz) {
|
||||||
|
((char *)sp2)[n - 1] = 0;
|
||||||
|
}
|
||||||
|
MemMove(sp2, sp, (auxv - sp) * sizeof(long));
|
||||||
|
argv = (char **)(sp2 + 1);
|
||||||
|
envp = (char **)(sp2 + 1 + argc + 1);
|
||||||
|
auxv = sp2 + (auxv - sp);
|
||||||
|
sp = sp2;
|
||||||
|
|
||||||
|
/* allocate ephemeral memory for reading file */
|
||||||
|
n = sizeof(union ElfEhdrBuf);
|
||||||
|
ebuf = __builtin_alloca(n);
|
||||||
|
for (; n > 0; n -= pagesz) {
|
||||||
|
((char *)ebuf)[n - 1] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* search for executable */
|
||||||
if (!(exe = Commandv(&M->ps, prog, GetEnv(envp, "PATH")))) {
|
if (!(exe = Commandv(&M->ps, prog, GetEnv(envp, "PATH")))) {
|
||||||
Pexit(prog, 0, "not found (maybe chmod +x)");
|
Pexit(prog, 0, "not found (maybe chmod +x)");
|
||||||
} else if ((fd = openat(AT_FDCWD, exe, O_RDONLY)) < 0) {
|
} else if ((fd = openat(AT_FDCWD, exe, O_RDONLY)) < 0) {
|
||||||
Pexit(exe, -1, "open");
|
Pexit(exe, -1, "open");
|
||||||
} else if ((rc = read(fd, M->ehdr.buf, sizeof(M->ehdr.buf))) < 0) {
|
} else if ((rc = read(fd, ebuf->buf, sizeof(ebuf->buf))) < 0) {
|
||||||
Pexit(exe, -1, "read");
|
Pexit(exe, -1, "read");
|
||||||
} else if ((unsigned long)rc < sizeof(M->ehdr.ehdr)) {
|
} else if ((unsigned long)rc < sizeof(ebuf->ehdr)) {
|
||||||
Pexit(exe, 0, "too small");
|
Pexit(exe, 0, "too small");
|
||||||
}
|
}
|
||||||
pe = M->ehdr.buf + rc;
|
pe = ebuf->buf + rc;
|
||||||
|
|
||||||
// resolve argv[0] to reflect path search
|
/* resolve argv[0] to reflect path search */
|
||||||
if ((argc > 0 && *prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) ||
|
if ((argc > 0 && *prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) ||
|
||||||
!StrCmp(BaseName(prog), argv[0])) {
|
!StrCmp(BaseName(prog), argv[0])) {
|
||||||
tp -= (n = StrLen(exe) + 1);
|
tp -= (n = StrLen(exe) + 1);
|
||||||
|
@ -907,28 +891,20 @@ int main(int argc, char **argv, char **envp) {
|
||||||
argv[0] = tp;
|
argv[0] = tp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// squeeze and align the argument block
|
/* generate some hard random data */
|
||||||
ip = (long *)(((long)tp - AUXV_BYTES) & -sizeof(long));
|
if (getentropy(M->rando, sizeof(M->rando))) {
|
||||||
ip -= (n = bp - sp);
|
Pexit(argv[0], -1, "getentropy");
|
||||||
ip = (long *)((long)ip & -STACK_ALIGN);
|
}
|
||||||
MemMove(ip, sp, n * sizeof(long));
|
|
||||||
bp = ip + n;
|
|
||||||
sp = ip;
|
|
||||||
|
|
||||||
// relocate the guest's random numbers
|
/* ape intended behavior
|
||||||
MemMove(M->rando, rando, sizeof(M->rando));
|
1. if ape, will scan shell script for elf printf statements
|
||||||
Bzero(rando, sizeof(rando));
|
2. shell script may have multiple lines producing elf headers
|
||||||
|
3. all elf printf lines must exist in the first 8192 bytes of file
|
||||||
// ape intended behavior
|
4. elf program headers may appear anywhere in the binary */
|
||||||
// 1. if file is an elf executable, it'll be used as-is
|
if (READ64(ebuf->buf) == READ64("MZqFpD='") ||
|
||||||
// 2. if ape, will scan shell script for elf printf statements
|
READ64(ebuf->buf) == READ64("jartsr='") ||
|
||||||
// 3. shell script may have multiple lines producing elf headers
|
READ64(ebuf->buf) == READ64("APEDBG='")) {
|
||||||
// 4. all elf printf lines must exist in the first 8192 bytes of file
|
for (p = ebuf->buf; p < pe; ++p) {
|
||||||
// 5. elf program headers may appear anywhere in the binary
|
|
||||||
if (READ64(M->ehdr.buf) == READ64("MZqFpD='") ||
|
|
||||||
READ64(M->ehdr.buf) == READ64("jartsr='") ||
|
|
||||||
READ64(M->ehdr.buf) == READ64("APEDBG='")) {
|
|
||||||
for (p = M->ehdr.buf; p < pe; ++p) {
|
|
||||||
if (READ64(p) != READ64("printf '")) {
|
if (READ64(p) != READ64("printf '")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -946,15 +922,15 @@ int main(int argc, char **argv, char **envp) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
M->ehdr.buf[i++] = c;
|
ebuf->buf[i++] = c;
|
||||||
if (i >= sizeof(M->ehdr.buf)) {
|
if (i >= sizeof(ebuf->buf)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i >= sizeof(M->ehdr.ehdr)) {
|
if (i >= sizeof(ebuf->ehdr)) {
|
||||||
TryElf(M, exe, fd, sp, bp, execfn);
|
TryElf(M, ebuf, exe, fd, sp, auxv, execfn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Pexit(exe, 0, TryElf(M, exe, fd, sp, bp, execfn));
|
Pexit(exe, 0, TryElf(M, ebuf, exe, fd, sp, auxv, execfn));
|
||||||
}
|
}
|
||||||
|
|
|
@ -610,7 +610,7 @@ apesh: .ascii "\n@\n#'\"\n" // sixth edition shebang
|
||||||
// extract the loader into a temp folder, and use it to
|
// extract the loader into a temp folder, and use it to
|
||||||
// load the APE without modifying it.
|
// load the APE without modifying it.
|
||||||
.ascii "[ x\"$1\" != x--assimilate ] && {\n"
|
.ascii "[ x\"$1\" != x--assimilate ] && {\n"
|
||||||
.ascii "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.6\"\n"
|
.ascii "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.7\"\n"
|
||||||
.ascii "[ -x \"$t\" ] || {\n"
|
.ascii "[ -x \"$t\" ] || {\n"
|
||||||
.ascii "mkdir -p \"${t%/*}\" &&\n"
|
.ascii "mkdir -p \"${t%/*}\" &&\n"
|
||||||
.ascii "dd if=\"$o\" of=\"$t.$$\" skip="
|
.ascii "dd if=\"$o\" of=\"$t.$$\" skip="
|
||||||
|
@ -818,7 +818,7 @@ ape.ident:
|
||||||
.long 1
|
.long 1
|
||||||
1: .asciz "APE"
|
1: .asciz "APE"
|
||||||
2: .balign 4
|
2: .balign 4
|
||||||
3: .long 106000000
|
3: .long 107000000
|
||||||
4: .size ape.ident,.-ape.ident
|
4: .size ape.ident,.-ape.ident
|
||||||
.type ape.ident,@object
|
.type ape.ident,@object
|
||||||
.previous
|
.previous
|
||||||
|
|
114
ape/loader.c
114
ape/loader.c
|
@ -158,7 +158,7 @@
|
||||||
{ \
|
{ \
|
||||||
char ibuf[19] = {0}; \
|
char ibuf[19] = {0}; \
|
||||||
Utox(ibuf, VAR); \
|
Utox(ibuf, VAR); \
|
||||||
Print(os, 2, #VAR " ", ibuf, "\n", 0l); \
|
Print(os, 2, ibuf, " " #VAR, "\n", 0l); \
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ElfEhdr {
|
struct ElfEhdr {
|
||||||
|
@ -196,7 +196,7 @@ union ElfEhdrBuf {
|
||||||
|
|
||||||
union ElfPhdrBuf {
|
union ElfPhdrBuf {
|
||||||
struct ElfPhdr phdr;
|
struct ElfPhdr phdr;
|
||||||
char buf[4096];
|
char buf[1024];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PathSearcher {
|
struct PathSearcher {
|
||||||
|
@ -208,7 +208,6 @@ struct PathSearcher {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ApeLoader {
|
struct ApeLoader {
|
||||||
union ElfEhdrBuf ehdr;
|
|
||||||
union ElfPhdrBuf phdr;
|
union ElfPhdrBuf phdr;
|
||||||
struct PathSearcher ps;
|
struct PathSearcher ps;
|
||||||
char path[1024];
|
char path[1024];
|
||||||
|
@ -622,7 +621,6 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd,
|
||||||
found_code = 0;
|
found_code = 0;
|
||||||
found_entry = 0;
|
found_entry = 0;
|
||||||
virtmin = virtmax = 0;
|
virtmin = virtmax = 0;
|
||||||
if (!pagesz) pagesz = 4096;
|
|
||||||
if (pagesz & (pagesz - 1)) {
|
if (pagesz & (pagesz - 1)) {
|
||||||
Pexit(os, exe, 0, "AT_PAGESZ isn't two power");
|
Pexit(os, exe, 0, "AT_PAGESZ isn't two power");
|
||||||
}
|
}
|
||||||
|
@ -715,19 +713,15 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd,
|
||||||
unsigned long wipe;
|
unsigned long wipe;
|
||||||
prot1 = prot;
|
prot1 = prot;
|
||||||
prot2 = prot;
|
prot2 = prot;
|
||||||
/*
|
/* when we ask the system to map the interval [vaddr,vaddr+filesz)
|
||||||
* when we ask the system to map the interval [vaddr,vaddr+filesz)
|
it might schlep extra file content into memory on both the left
|
||||||
* it might schlep extra file content into memory on both the left
|
and the righthand side. that's because elf doesn't require that
|
||||||
* and the righthand side. that's because elf doesn't require that
|
either side of the interval be aligned on the system page size.
|
||||||
* either side of the interval be aligned on the system page size.
|
normally we can get away with ignoring these junk bytes. but if
|
||||||
*
|
the segment defines bss memory (i.e. memsz > filesz) then we'll
|
||||||
* normally we can get away with ignoring these junk bytes. but if
|
need to clear the extra bytes in the page, if they exist. since
|
||||||
* the segment defines bss memory (i.e. memsz > filesz) then we'll
|
we can't do that if we're mapping a read-only page, we can just
|
||||||
* need to clear the extra bytes in the page, if they exist.
|
map it with write permissions and call mprotect on it afterward */
|
||||||
*
|
|
||||||
* since we can't do that if we're mapping a read-only page, we'll
|
|
||||||
* actually map it with write permissions and protect it afterward
|
|
||||||
*/
|
|
||||||
a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */
|
a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */
|
||||||
b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */
|
b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */
|
||||||
c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */
|
c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */
|
||||||
|
@ -768,8 +762,9 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd,
|
||||||
Launch(IsFreebsd() ? sp : 0, dynbase + e->e_entry, sp, os);
|
Launch(IsFreebsd() ? sp : 0, dynbase + e->e_entry, sp, os);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *TryElf(struct ApeLoader *M, const char *exe, int fd,
|
static const char *TryElf(struct ApeLoader *M, union ElfEhdrBuf *ebuf,
|
||||||
long *sp, long *auxv, unsigned long pagesz, int os) {
|
const char *exe, int fd, long *sp, long *auxv,
|
||||||
|
unsigned long pagesz, int os) {
|
||||||
long i, rc;
|
long i, rc;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
struct ElfEhdr *e;
|
struct ElfEhdr *e;
|
||||||
|
@ -782,8 +777,8 @@ static const char *TryElf(struct ApeLoader *M, const char *exe, int fd,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* validate elf header */
|
/* validate elf header */
|
||||||
e = &M->ehdr.ehdr;
|
e = &ebuf->ehdr;
|
||||||
if (READ32(M->ehdr.buf) != READ32("\177ELF")) {
|
if (READ32(ebuf->buf) != READ32("\177ELF")) {
|
||||||
return "didn't embed ELF magic";
|
return "didn't embed ELF magic";
|
||||||
}
|
}
|
||||||
if (e->e_ident[EI_CLASS] == ELFCLASS32) {
|
if (e->e_ident[EI_CLASS] == ELFCLASS32) {
|
||||||
|
@ -891,7 +886,7 @@ static __attribute__((__noreturn__)) void ShowUsage(int os, int fd, int rc) {
|
||||||
Print(os, fd,
|
Print(os, fd,
|
||||||
"NAME\n"
|
"NAME\n"
|
||||||
"\n"
|
"\n"
|
||||||
" actually portable executable loader version 1.6\n"
|
" actually portable executable loader version 1.7\n"
|
||||||
" copyright 2023 justine alexandra roberts tunney\n"
|
" copyright 2023 justine alexandra roberts tunney\n"
|
||||||
" https://justine.lol/ape.html\n"
|
" https://justine.lol/ape.html\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
@ -909,14 +904,14 @@ static __attribute__((__noreturn__)) void ShowUsage(int os, int fd, int rc) {
|
||||||
Exit(rc, os);
|
Exit(rc, os);
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((__noreturn__)) //
|
__attribute__((__noreturn__)) void ApeLoader(long di, long *sp, char dl) {
|
||||||
void ApeLoader(long di, long *sp, char dl) {
|
int rc, n;
|
||||||
int rc;
|
unsigned i;
|
||||||
unsigned i, n;
|
|
||||||
int c, fd, os, argc;
|
int c, fd, os, argc;
|
||||||
struct ApeLoader *M;
|
struct ApeLoader *M;
|
||||||
unsigned long pagesz;
|
unsigned long pagesz;
|
||||||
long *auxv, *ap, *ew;
|
union ElfEhdrBuf *ebuf;
|
||||||
|
long *auxv, *ap, *endp, *sp2;
|
||||||
char *p, *pe, *exe, *ape, *prog, **argv, **envp;
|
char *p, *pe, *exe, *ape, *prog, **argv, **envp;
|
||||||
|
|
||||||
(void)Utox;
|
(void)Utox;
|
||||||
|
@ -935,7 +930,7 @@ void ApeLoader(long di, long *sp, char dl) {
|
||||||
argc = *sp;
|
argc = *sp;
|
||||||
argv = (char **)(sp + 1);
|
argv = (char **)(sp + 1);
|
||||||
envp = (char **)(sp + 1 + argc + 1);
|
envp = (char **)(sp + 1 + argc + 1);
|
||||||
auxv = (long *)(sp + 1 + argc + 1);
|
auxv = sp + 1 + argc + 1;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (!*auxv++) {
|
if (!*auxv++) {
|
||||||
break;
|
break;
|
||||||
|
@ -960,15 +955,12 @@ void ApeLoader(long di, long *sp, char dl) {
|
||||||
os = NETBSD;
|
os = NETBSD;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ew = ap + 1;
|
if (!pagesz) {
|
||||||
|
pagesz = 4096;
|
||||||
|
}
|
||||||
|
endp = ap + 1;
|
||||||
|
|
||||||
/* allocate loader memory */
|
/* the default operating system */
|
||||||
n = sizeof(*M) / sizeof(long);
|
|
||||||
MemMove(sp - n, sp, (char *)ew - (char *)sp);
|
|
||||||
sp -= n, argv -= n, envp -= n, auxv -= n;
|
|
||||||
M = (struct ApeLoader *)(ew - n);
|
|
||||||
|
|
||||||
/* default operating system */
|
|
||||||
if (!os) {
|
if (!os) {
|
||||||
os = LINUX;
|
os = LINUX;
|
||||||
}
|
}
|
||||||
|
@ -1005,17 +997,43 @@ void ApeLoader(long di, long *sp, char dl) {
|
||||||
argv = (char **)((sp += 1) + 1);
|
argv = (char **)((sp += 1) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* allocate loader memory in program's arg block */
|
||||||
|
n = sizeof(struct ApeLoader);
|
||||||
|
M = __builtin_alloca(n);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
sp2 = __builtin_alloca(n);
|
||||||
|
if ((long)sp2 & 15) ++sp2;
|
||||||
|
for (; n > 0; n -= pagesz) {
|
||||||
|
((char *)sp2)[n - 1] = 0;
|
||||||
|
}
|
||||||
|
MemMove(sp2, sp, (endp - sp) * sizeof(long));
|
||||||
|
argv = (char **)(sp2 + 1);
|
||||||
|
envp = (char **)(sp2 + 1 + argc + 1);
|
||||||
|
auxv = (char **)(sp2 + (auxv - sp));
|
||||||
|
sp = sp2;
|
||||||
|
|
||||||
|
/* allocate ephemeral memory for reading file */
|
||||||
|
n = sizeof(union ElfEhdrBuf);
|
||||||
|
ebuf = __builtin_alloca(n);
|
||||||
|
for (; n > 0; n -= pagesz) {
|
||||||
|
((char *)ebuf)[n - 1] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* resolve path of executable and read its first page */
|
/* resolve path of executable and read its first page */
|
||||||
if (!(exe = Commandv(&M->ps, os, prog, GetEnv(envp, "PATH")))) {
|
if (!(exe = Commandv(&M->ps, os, prog, GetEnv(envp, "PATH")))) {
|
||||||
Pexit(os, prog, 0, "not found (maybe chmod +x or ./ needed)");
|
Pexit(os, prog, 0, "not found (maybe chmod +x or ./ needed)");
|
||||||
} else if ((fd = Open(exe, O_RDONLY, 0, os)) < 0) {
|
} else if ((fd = Open(exe, O_RDONLY, 0, os)) < 0) {
|
||||||
Pexit(os, exe, fd, "open");
|
Pexit(os, exe, fd, "open");
|
||||||
} else if ((rc = Pread(fd, M->ehdr.buf, sizeof(M->ehdr.buf), 0, os)) < 0) {
|
} else if ((rc = Pread(fd, ebuf->buf, sizeof(ebuf->buf), 0, os)) < 0) {
|
||||||
Pexit(os, exe, rc, "read");
|
Pexit(os, exe, rc, "read");
|
||||||
} else if ((unsigned long)rc < sizeof(M->ehdr.ehdr)) {
|
} else if ((unsigned long)rc < sizeof(ebuf->ehdr)) {
|
||||||
Pexit(os, exe, 0, "too small");
|
Pexit(os, exe, 0, "too small");
|
||||||
}
|
}
|
||||||
pe = M->ehdr.buf + rc;
|
pe = ebuf->buf + rc;
|
||||||
|
|
||||||
/* change argv[0] to resolved path if it's ambiguous */
|
/* change argv[0] to resolved path if it's ambiguous */
|
||||||
if ((argc > 0 && *prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) ||
|
if ((argc > 0 && *prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) ||
|
||||||
|
@ -1028,10 +1046,10 @@ void ApeLoader(long di, long *sp, char dl) {
|
||||||
2. shell script may have multiple lines producing elf headers
|
2. shell script may have multiple lines producing elf headers
|
||||||
3. all elf printf lines must exist in the first 8192 bytes of file
|
3. all elf printf lines must exist in the first 8192 bytes of file
|
||||||
4. elf program headers may appear anywhere in the binary */
|
4. elf program headers may appear anywhere in the binary */
|
||||||
if (READ64(M->ehdr.buf) == READ64("MZqFpD='") ||
|
if (READ64(ebuf->buf) == READ64("MZqFpD='") ||
|
||||||
READ64(M->ehdr.buf) == READ64("jartsr='") ||
|
READ64(ebuf->buf) == READ64("jartsr='") ||
|
||||||
READ64(M->ehdr.buf) == READ64("APEDBG='")) {
|
READ64(ebuf->buf) == READ64("APEDBG='")) {
|
||||||
for (p = M->ehdr.buf; p < pe; ++p) {
|
for (p = ebuf->buf; p < pe; ++p) {
|
||||||
if (READ64(p) != READ64("printf '")) {
|
if (READ64(p) != READ64("printf '")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -1049,15 +1067,15 @@ void ApeLoader(long di, long *sp, char dl) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
M->ehdr.buf[i++] = c;
|
ebuf->buf[i++] = c;
|
||||||
if (i >= sizeof(M->ehdr.buf)) {
|
if (i >= sizeof(ebuf->buf)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i >= sizeof(M->ehdr.ehdr)) {
|
if (i >= sizeof(ebuf->ehdr)) {
|
||||||
TryElf(M, exe, fd, sp, auxv, pagesz, os);
|
TryElf(M, ebuf, exe, fd, sp, auxv, pagesz, os);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Pexit(os, exe, 0, TryElf(M, exe, fd, sp, auxv, pagesz, os));
|
Pexit(os, exe, 0, TryElf(M, ebuf, exe, fd, sp, auxv, pagesz, os));
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,6 @@ XnuEntrypoint:
|
||||||
mov $_HOSTXNU,%dl // xnu's not unix!
|
mov $_HOSTXNU,%dl // xnu's not unix!
|
||||||
ElfEntrypoint:
|
ElfEntrypoint:
|
||||||
mov %rsp,%rsi // save real stack
|
mov %rsp,%rsi // save real stack
|
||||||
sub $1024*1024,%rsp // room for allocs
|
|
||||||
call ApeLoader
|
call ApeLoader
|
||||||
.endfn ElfEntrypoint,globl
|
.endfn ElfEntrypoint,globl
|
||||||
.endfn XnuEntrypoint,globl
|
.endfn XnuEntrypoint,globl
|
||||||
|
@ -65,7 +64,7 @@ ape.ident:
|
||||||
.long 1
|
.long 1
|
||||||
1: .asciz "APE"
|
1: .asciz "APE"
|
||||||
2: .balign 4
|
2: .balign 4
|
||||||
3: .long 106000000
|
3: .long 107000000
|
||||||
4: .size ape.ident,.-ape.ident
|
4: .size ape.ident,.-ape.ident
|
||||||
.type ape.ident,@object
|
.type ape.ident,@object
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@ echo "Author: Justine Tunney <jtunney@gmail.com>" >&2
|
||||||
|
|
||||||
if [ -f o/depend ] && make -j8 o//ape; then
|
if [ -f o/depend ] && make -j8 o//ape; then
|
||||||
echo "successfully recompiled ape loader" >&2
|
echo "successfully recompiled ape loader" >&2
|
||||||
|
elif [ -x o//ape/ape.elf ] && [ -x o//ape/ape.macho ]; then
|
||||||
|
echo "using ape loader you compiled earlier" >&2
|
||||||
elif [ -d build/bootstrap ]; then
|
elif [ -d build/bootstrap ]; then
|
||||||
# if make isn't being used then it's unlikely the user changed the sources
|
# if make isn't being used then it's unlikely the user changed the sources
|
||||||
# in that case the prebuilt binaries should be completely up-to-date
|
# in that case the prebuilt binaries should be completely up-to-date
|
||||||
|
|
|
@ -48,6 +48,7 @@ for x in .ape \
|
||||||
.ape-1.4 \
|
.ape-1.4 \
|
||||||
.ape-1.5 \
|
.ape-1.5 \
|
||||||
.ape-1.6 \
|
.ape-1.6 \
|
||||||
|
.ape-1.7 \
|
||||||
.ape-blink-0.9.2 \
|
.ape-blink-0.9.2 \
|
||||||
.ape-blink-1.0.0; do
|
.ape-blink-1.0.0; do
|
||||||
rm -f \
|
rm -f \
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -10,53 +10,75 @@
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/struct/sigaction.h"
|
#include "libc/calls/struct/sigaction.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
#include "libc/log/check.h"
|
|
||||||
#include "libc/log/color.internal.h"
|
|
||||||
#include "libc/log/log.h"
|
|
||||||
#include "libc/mem/mem.h"
|
|
||||||
#include "libc/nt/thread.h"
|
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/sysv/consts/limits.h"
|
||||||
#include "libc/sysv/consts/exit.h"
|
|
||||||
#include "libc/sysv/consts/fileno.h"
|
|
||||||
#include "libc/sysv/consts/sig.h"
|
#include "libc/sysv/consts/sig.h"
|
||||||
|
|
||||||
volatile bool gotctrlc;
|
void SignalHandler(int sig) {
|
||||||
|
// we don't need to do anything in our signal handler since the signal
|
||||||
void GotCtrlC(int sig) {
|
// delivery itself causes a visible state change, saying what happened
|
||||||
gotctrlc = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
ssize_t rc;
|
|
||||||
size_t got, wrote;
|
printf("echoing stdin until ctrl+c is pressed\n");
|
||||||
unsigned char buf[512];
|
|
||||||
struct sigaction saint = {.sa_handler = GotCtrlC};
|
// you need to set your signal handler using sigaction() rather than
|
||||||
fprintf(stderr, "This echos stdio until Ctrl+C is pressed.\n");
|
// signal(), since the latter uses .sa_flags=SA_RESTART, which means
|
||||||
CHECK_NE(-1, sigaction(SIGINT, &saint, NULL));
|
// read will restart itself after signals, rather than raising EINTR
|
||||||
|
sigaction(SIGINT, &(struct sigaction){.sa_handler = SignalHandler}, 0);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
rc = read(0, buf, 512);
|
|
||||||
if (rc != -1) {
|
// posix guarantees atomic i/o if you use pipe_buf sized buffers
|
||||||
got = rc;
|
// that way we don't need to worry about things like looping and
|
||||||
} else {
|
// we can also be assured that multiple actors wont have tearing
|
||||||
CHECK_EQ(EINTR, errno);
|
char buf[PIPE_BUF];
|
||||||
break;
|
|
||||||
|
// read data from standard input
|
||||||
|
//
|
||||||
|
// since this is a blocking operation and we're not performing a
|
||||||
|
// cpu-bound operation it is almost with absolute certainty that
|
||||||
|
// when the ctrl-c signal gets delivered, it'll happen in read()
|
||||||
|
//
|
||||||
|
// it's possible to be more precise if we were building library
|
||||||
|
// code. for example, you can block signals using sigprocmask()
|
||||||
|
// and then use pselect() to do the waiting.
|
||||||
|
int got = read(0, buf, sizeof(buf));
|
||||||
|
|
||||||
|
// check if the read operation failed
|
||||||
|
// negative one is the *only* return value to indicate errors
|
||||||
|
if (got == -1) {
|
||||||
|
if (errno == EINTR) {
|
||||||
|
// a signal handler was invoked during the read operation
|
||||||
|
// since we have only one such signal handler it's sigint
|
||||||
|
// if we didn't have any signal handlers in our app, then
|
||||||
|
// we wouldn't need to check this errno. using SA_RESTART
|
||||||
|
// is another great way to avoid having to worry about it
|
||||||
|
// however EINTR is very useful, when we choose to use it
|
||||||
|
// the \r character is needed so when the line is printed
|
||||||
|
// it'll overwrite the ^C that got echo'd with the ctrl-c
|
||||||
|
printf("\rgot ctrl+c\n");
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
// log it in the unlikely event something else went wrong
|
||||||
|
perror("<stdin>");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!got) break;
|
|
||||||
rc = write(1, buf, got);
|
// check if the user typed ctrl-d which closes the input handle
|
||||||
if (rc != -1) {
|
if (!got) {
|
||||||
wrote = rc;
|
printf("got eof\n");
|
||||||
} else {
|
exit(0);
|
||||||
CHECK_EQ(EINTR, errno);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
CHECK_EQ(got, wrote);
|
|
||||||
|
// relay read data to standard output
|
||||||
|
//
|
||||||
|
// it's usually safe to ignore the return code of write. the
|
||||||
|
// operating system will send SIGPIPE if there's any problem
|
||||||
|
// which kills the process by default
|
||||||
|
write(1, buf, got);
|
||||||
}
|
}
|
||||||
if (gotctrlc) {
|
|
||||||
fprintf(stderr, "Got Ctrl+C\n");
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "Got EOF\n");
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
69
examples/stackexplorer.c
Normal file
69
examples/stackexplorer.c
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#if 0
|
||||||
|
/*─────────────────────────────────────────────────────────────────╗
|
||||||
|
│ To the extent possible under law, Justine Tunney has waived │
|
||||||
|
│ all copyright and related or neighboring rights to this file, │
|
||||||
|
│ as it is written in the following disclaimers: │
|
||||||
|
│ • http://unlicense.org/ │
|
||||||
|
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||||
|
╚─────────────────────────────────────────────────────────────────*/
|
||||||
|
#endif
|
||||||
|
#include "libc/mem/alg.h"
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
|
#include "libc/stdio/stdio.h"
|
||||||
|
#include "libc/x/xasprintf.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fileoverview Process Initialization Stack Explorer
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct Thing {
|
||||||
|
intptr_t i;
|
||||||
|
char *s;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Things {
|
||||||
|
size_t n;
|
||||||
|
struct Thing *p;
|
||||||
|
} things;
|
||||||
|
|
||||||
|
void Append(intptr_t i, char *s) {
|
||||||
|
things.p = realloc(things.p, ++things.n * sizeof(*things.p));
|
||||||
|
things.p[things.n - 1].i = i;
|
||||||
|
things.p[things.n - 1].s = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Compare(const void *a, const void *b) {
|
||||||
|
struct Thing *x = (struct Thing *)a;
|
||||||
|
struct Thing *y = (struct Thing *)b;
|
||||||
|
if (x->i < y->i) return +1;
|
||||||
|
if (x->i > y->i) return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
Append((uintptr_t)__builtin_frame_address(0), "__builtin_frame_address(0)");
|
||||||
|
Append((uintptr_t)__oldstack, "__oldstack");
|
||||||
|
for (int i = 0;; ++i) {
|
||||||
|
Append((uintptr_t)&argv[i], xasprintf("&argv[%d] = %`'s", i, argv[i]));
|
||||||
|
if (!argv[i]) break;
|
||||||
|
Append((uintptr_t)argv[i], xasprintf("argv[%d] = %`'s", i, argv[i]));
|
||||||
|
}
|
||||||
|
for (int i = 0;; ++i) {
|
||||||
|
Append((uintptr_t)&environ[i],
|
||||||
|
xasprintf("&environ[%d] = %`'s", i, environ[i]));
|
||||||
|
if (!environ[i]) break;
|
||||||
|
Append((uintptr_t)environ[i],
|
||||||
|
xasprintf("environ[%d] = %`'s", i, environ[i]));
|
||||||
|
}
|
||||||
|
for (int i = 0;; i += 2) {
|
||||||
|
Append((uintptr_t)&__auxv[i], xasprintf("&auxv[%d] = %ld", i, __auxv[i]));
|
||||||
|
if (!__auxv[i]) break;
|
||||||
|
Append((uintptr_t)&__auxv[i + 1],
|
||||||
|
xasprintf("&auxv[%d] = %#lx", i + 1, __auxv[i + 1]));
|
||||||
|
}
|
||||||
|
qsort(things.p, things.n, sizeof(*things.p), Compare);
|
||||||
|
for (int i = 0; i < things.n; ++i) {
|
||||||
|
printf("%012lx %s\n", things.p[i].i, things.p[i].s);
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,8 +7,8 @@
|
||||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||||
╚─────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────*/
|
||||||
#endif
|
#endif
|
||||||
#include "libc/calls/calls.h"
|
|
||||||
#include "libc/calls/struct/stat.h"
|
#include "libc/calls/struct/stat.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
#include "libc/fmt/fmt.h"
|
#include "libc/fmt/fmt.h"
|
||||||
|
@ -90,6 +90,10 @@ void PrintFileMetadata(const char *pathname, struct stat *st) {
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
size_t i;
|
size_t i;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
if (argc <= 1) {
|
||||||
|
PrintFileMetadata(".", &st);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
for (i = 1; i < argc; ++i) {
|
for (i = 1; i < argc; ++i) {
|
||||||
if (!strcmp(argv[i], "-n")) {
|
if (!strcmp(argv[i], "-n")) {
|
||||||
numeric = true;
|
numeric = true;
|
||||||
|
|
|
@ -40,7 +40,9 @@ dontinline void ShowIt(const char *path) {
|
||||||
"total file nodes in filesystem");
|
"total file nodes in filesystem");
|
||||||
printf("f_ffree = %,zu (%s)\n", sf.f_ffree,
|
printf("f_ffree = %,zu (%s)\n", sf.f_ffree,
|
||||||
"free file nodes in filesystem");
|
"free file nodes in filesystem");
|
||||||
printf("f_fsid = %#lx (%s)\n", sf.f_fsid, "filesystem id");
|
printf("f_fsid = %#lx (%s)\n",
|
||||||
|
sf.f_fsid.__val[0] | (uint64_t)sf.f_fsid.__val[1] << 32,
|
||||||
|
"filesystem id");
|
||||||
printf("f_owner = %#lx (%s)\n", sf.f_owner, "user that created mount");
|
printf("f_owner = %#lx (%s)\n", sf.f_owner, "user that created mount");
|
||||||
printf("f_namelen = %,zu (%s)\n", sf.f_namelen,
|
printf("f_namelen = %,zu (%s)\n", sf.f_namelen,
|
||||||
"maximum length of filenames");
|
"maximum length of filenames");
|
||||||
|
|
|
@ -180,6 +180,7 @@ textwindows bool __sig_handle(int sigops, int sig, int si_code,
|
||||||
case (intptr_t)SIG_DFL:
|
case (intptr_t)SIG_DFL:
|
||||||
if (__sig_is_fatal(sig)) {
|
if (__sig_is_fatal(sig)) {
|
||||||
STRACE("terminating on %G", sig);
|
STRACE("terminating on %G", sig);
|
||||||
|
_restorewintty();
|
||||||
ExitProcess(sig);
|
ExitProcess(sig);
|
||||||
}
|
}
|
||||||
// fallthrough
|
// fallthrough
|
||||||
|
|
|
@ -85,8 +85,8 @@ int sys_execve(const char *prog, char *const argv[], char *const envp[]) {
|
||||||
(CanExecute((ape = "/usr/bin/ape")) ||
|
(CanExecute((ape = "/usr/bin/ape")) ||
|
||||||
CanExecute((ape = Join(firstnonnull(getenv("TMPDIR"),
|
CanExecute((ape = Join(firstnonnull(getenv("TMPDIR"),
|
||||||
firstnonnull(getenv("HOME"), ".")),
|
firstnonnull(getenv("HOME"), ".")),
|
||||||
".ape-1.6", buf))) ||
|
".ape-1.7", buf))) ||
|
||||||
CanExecute((ape = Join(firstnonnull(getenv("HOME"), "."), ".ape-1.6",
|
CanExecute((ape = Join(firstnonnull(getenv("HOME"), "."), ".ape-1.7",
|
||||||
buf))))) {
|
buf))))) {
|
||||||
shargs[0] = ape;
|
shargs[0] = ape;
|
||||||
shargs[1] = "-";
|
shargs[1] = "-";
|
||||||
|
|
|
@ -43,8 +43,8 @@ int getrlimit(int resource, struct rlimit *rlim) {
|
||||||
} else if (!IsWindows()) {
|
} else if (!IsWindows()) {
|
||||||
rc = sys_getrlimit(resource, rlim);
|
rc = sys_getrlimit(resource, rlim);
|
||||||
} else if (resource == RLIMIT_STACK) {
|
} else if (resource == RLIMIT_STACK) {
|
||||||
rlim->rlim_cur = (uintptr_t)ape_stack_memsz;
|
rlim->rlim_cur = GetStaticStackSize();
|
||||||
rlim->rlim_max = (uintptr_t)ape_stack_memsz;
|
rlim->rlim_max = GetStaticStackSize();
|
||||||
rc = 0;
|
rc = 0;
|
||||||
} else if (resource == RLIMIT_AS) {
|
} else if (resource == RLIMIT_AS) {
|
||||||
rlim->rlim_cur = __virtualmax;
|
rlim->rlim_cur = __virtualmax;
|
||||||
|
|
|
@ -26,10 +26,10 @@
|
||||||
#include "libc/intrin/asan.internal.h"
|
#include "libc/intrin/asan.internal.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
|
#include "libc/runtime/zipos.internal.h"
|
||||||
#include "libc/sock/internal.h"
|
#include "libc/sock/internal.h"
|
||||||
#include "libc/sock/sock.h"
|
#include "libc/sock/sock.h"
|
||||||
#include "libc/sysv/errfuns.h"
|
#include "libc/sysv/errfuns.h"
|
||||||
#include "libc/runtime/zipos.internal.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads data from file descriptor.
|
* Reads data from file descriptor.
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include "libc/intrin/asan.internal.h"
|
#include "libc/intrin/asan.internal.h"
|
||||||
#include "libc/intrin/describeflags.internal.h"
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
|
#include "libc/macros.internal.h"
|
||||||
#include "libc/sysv/consts/rlimit.h"
|
#include "libc/sysv/consts/rlimit.h"
|
||||||
#include "libc/sysv/errfuns.h"
|
#include "libc/sysv/errfuns.h"
|
||||||
|
|
||||||
|
@ -31,6 +32,12 @@
|
||||||
*
|
*
|
||||||
* The following resources are recommended:
|
* The following resources are recommended:
|
||||||
*
|
*
|
||||||
|
* - `RLIMIT_STACK` controls how much stack memory is available to the
|
||||||
|
* main thread. This setting is inherited across fork() and execve()
|
||||||
|
* Please note it's only safe for Cosmopolitan programs, to set this
|
||||||
|
* value to at least `PTHREAD_STACK_MIN * 2`. On Windows this cannot
|
||||||
|
* be used to extend the stack, which is currently hard-coded.
|
||||||
|
*
|
||||||
* - `RLIMIT_AS` limits the size of the virtual address space. This will
|
* - `RLIMIT_AS` limits the size of the virtual address space. This will
|
||||||
* work on all platforms except WSL. It is emulated on XNU and Windows
|
* work on all platforms except WSL. It is emulated on XNU and Windows
|
||||||
* which means it won't propagate across execve() currently.
|
* which means it won't propagate across execve() currently.
|
||||||
|
|
|
@ -278,7 +278,7 @@ void statfs2cosmo(struct statfs *f, const union statfs_meta *m) {
|
||||||
f_files = m->netbsd.f_files;
|
f_files = m->netbsd.f_files;
|
||||||
f_ffree = m->netbsd.f_ffree;
|
f_ffree = m->netbsd.f_ffree;
|
||||||
f_fsid = m->netbsd.f_fsid;
|
f_fsid = m->netbsd.f_fsid;
|
||||||
f_namelen = f->f_namelen;
|
f_namelen = 511;
|
||||||
f_frsize = m->netbsd.f_bsize;
|
f_frsize = m->netbsd.f_bsize;
|
||||||
f_flags = m->netbsd.f_flags;
|
f_flags = m->netbsd.f_flags;
|
||||||
f_owner = m->netbsd.f_owner;
|
f_owner = m->netbsd.f_owner;
|
||||||
|
|
|
@ -48,13 +48,10 @@ static const char *GetFrameName(int x) {
|
||||||
} else if (IsMemtrackFrame(x)) {
|
} else if (IsMemtrackFrame(x)) {
|
||||||
return "memtrack";
|
return "memtrack";
|
||||||
} else if (IsOldStackFrame(x)) {
|
} else if (IsOldStackFrame(x)) {
|
||||||
return "oldstack";
|
return "system stack";
|
||||||
} else if (IsWindows() &&
|
} else if (((GetStaticStackAddr(0) + GetStackSize()) >> 16) <= x &&
|
||||||
(((GetStaticStackAddr(0) + GetStackSize()) >> 16) <= x &&
|
x <= ((GetStaticStackAddr(0) + GetStackSize() - 1) >> 16)) {
|
||||||
x <= ((GetStaticStackAddr(0) + GetStackSize() +
|
return "static stack";
|
||||||
sizeof(struct WinArgs) - 1) >>
|
|
||||||
16))) {
|
|
||||||
return "mainstack";
|
|
||||||
} else if ((int)((intptr_t)__executable_start >> 16) <= x &&
|
} else if ((int)((intptr_t)__executable_start >> 16) <= x &&
|
||||||
x <= (int)(((intptr_t)_end - 1) >> 16)) {
|
x <= (int)(((intptr_t)_end - 1) >> 16)) {
|
||||||
return "image";
|
return "image";
|
||||||
|
|
|
@ -110,9 +110,9 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1) {
|
||||||
|
|
||||||
// get page size
|
// get page size
|
||||||
unsigned long pagesz = 4096;
|
unsigned long pagesz = 4096;
|
||||||
for (int i = 0; auxv[i]; auxv += 2) {
|
for (int i = 0; auxv[i]; i += 2) {
|
||||||
if (auxv[0] == AT_PAGESZ) {
|
if (auxv[i] == AT_PAGESZ) {
|
||||||
pagesz = auxv[1];
|
pagesz = auxv[i + 1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,6 +83,7 @@ extern char kTmpPath[];
|
||||||
extern const char kNtSystemDirectory[];
|
extern const char kNtSystemDirectory[];
|
||||||
extern const char kNtWindowsDirectory[];
|
extern const char kNtWindowsDirectory[];
|
||||||
extern size_t __virtualmax;
|
extern size_t __virtualmax;
|
||||||
|
extern size_t __stackmax;
|
||||||
extern bool __isworker;
|
extern bool __isworker;
|
||||||
/* utilities */
|
/* utilities */
|
||||||
void _intsort(int *, size_t);
|
void _intsort(int *, size_t);
|
||||||
|
|
|
@ -48,6 +48,7 @@
|
||||||
#include "libc/nt/thunk/msabi.h"
|
#include "libc/nt/thunk/msabi.h"
|
||||||
#include "libc/runtime/internal.h"
|
#include "libc/runtime/internal.h"
|
||||||
#include "libc/runtime/memtrack.internal.h"
|
#include "libc/runtime/memtrack.internal.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/runtime/stack.h"
|
#include "libc/runtime/stack.h"
|
||||||
#include "libc/runtime/winargs.internal.h"
|
#include "libc/runtime/winargs.internal.h"
|
||||||
#include "libc/sock/internal.h"
|
#include "libc/sock/internal.h"
|
||||||
|
@ -119,9 +120,9 @@ __msabi static textwindows void DeduplicateStdioHandles(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
__msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
__msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
||||||
|
size_t stacksize;
|
||||||
struct WinArgs *wa;
|
struct WinArgs *wa;
|
||||||
size_t allocsize, stacksize;
|
uintptr_t stackaddr;
|
||||||
uintptr_t stackaddr, allocaddr;
|
|
||||||
__oldstack = (intptr_t)__builtin_frame_address(0);
|
__oldstack = (intptr_t)__builtin_frame_address(0);
|
||||||
if (NtGetPeb()->OSMajorVersion >= 10 &&
|
if (NtGetPeb()->OSMajorVersion >= 10 &&
|
||||||
(intptr_t)v_ntsubsystem == kNtImageSubsystemWindowsCui) {
|
(intptr_t)v_ntsubsystem == kNtImageSubsystemWindowsCui) {
|
||||||
|
@ -138,26 +139,23 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
||||||
_mmi.n = ARRAYLEN(_mmi.s);
|
_mmi.n = ARRAYLEN(_mmi.s);
|
||||||
stackaddr = GetStaticStackAddr(0);
|
stackaddr = GetStaticStackAddr(0);
|
||||||
stacksize = GetStaticStackSize();
|
stacksize = GetStaticStackSize();
|
||||||
allocaddr = stackaddr;
|
|
||||||
allocsize = stacksize + sizeof(struct WinArgs);
|
|
||||||
__imp_MapViewOfFileEx((_mmi.p[0].h = __imp_CreateFileMappingW(
|
__imp_MapViewOfFileEx((_mmi.p[0].h = __imp_CreateFileMappingW(
|
||||||
-1, &kNtIsInheritable, kNtPageExecuteReadwrite,
|
-1, &kNtIsInheritable, kNtPageExecuteReadwrite,
|
||||||
allocsize >> 32, allocsize, NULL)),
|
stacksize >> 32, stacksize, NULL)),
|
||||||
kNtFileMapWrite | kNtFileMapExecute, 0, 0, allocsize,
|
kNtFileMapWrite | kNtFileMapExecute, 0, 0, stacksize,
|
||||||
(void *)allocaddr);
|
(void *)stackaddr);
|
||||||
int prot = (intptr_t)ape_stack_prot;
|
int prot = (intptr_t)ape_stack_prot;
|
||||||
if (~prot & PROT_EXEC) {
|
if (~prot & PROT_EXEC) {
|
||||||
uint32_t oldprot;
|
uint32_t old;
|
||||||
__imp_VirtualProtect((void *)allocaddr, allocsize, kNtPageReadwrite,
|
__imp_VirtualProtect((void *)stackaddr, stacksize, kNtPageReadwrite, &old);
|
||||||
&oldprot);
|
|
||||||
}
|
}
|
||||||
_mmi.p[0].x = allocaddr >> 16;
|
_mmi.p[0].x = stackaddr >> 16;
|
||||||
_mmi.p[0].y = (allocaddr >> 16) + ((allocsize - 1) >> 16);
|
_mmi.p[0].y = (stackaddr >> 16) + ((stacksize - 1) >> 16);
|
||||||
_mmi.p[0].prot = prot;
|
_mmi.p[0].prot = prot;
|
||||||
_mmi.p[0].flags = 0x00000026; // stack+anonymous
|
_mmi.p[0].flags = 0x00000026; // stack+anonymous
|
||||||
_mmi.p[0].size = allocsize;
|
_mmi.p[0].size = stacksize;
|
||||||
_mmi.i = 1;
|
_mmi.i = 1;
|
||||||
wa = (struct WinArgs *)(allocaddr + stacksize);
|
wa = (struct WinArgs *)(stackaddr + (stacksize - sizeof(struct WinArgs)));
|
||||||
int count = GetDosArgv(cmdline, wa->argblock, ARRAYLEN(wa->argblock),
|
int count = GetDosArgv(cmdline, wa->argblock, ARRAYLEN(wa->argblock),
|
||||||
wa->argv, ARRAYLEN(wa->argv));
|
wa->argv, ARRAYLEN(wa->argv));
|
||||||
for (int i = 0; wa->argv[0][i]; ++i) {
|
for (int i = 0; wa->argv[0][i]; ++i) {
|
||||||
|
@ -169,7 +167,7 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
||||||
GetDosEnviron(env16, wa->envblock, ARRAYLEN(wa->envblock) - 8, wa->envp,
|
GetDosEnviron(env16, wa->envblock, ARRAYLEN(wa->envblock) - 8, wa->envp,
|
||||||
ARRAYLEN(wa->envp) - 1);
|
ARRAYLEN(wa->envp) - 1);
|
||||||
__imp_FreeEnvironmentStringsW(env16);
|
__imp_FreeEnvironmentStringsW(env16);
|
||||||
_jmpstack((char *)(stackaddr + stacksize - (intptr_t)ape_stack_align), cosmo,
|
_jmpstack((char *)(stackaddr + (stacksize - sizeof(struct WinArgs))), cosmo,
|
||||||
count, wa->argv, wa->envp, wa->auxv);
|
count, wa->argv, wa->envp, wa->auxv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/calls/calls.h"
|
|
||||||
#include "libc/calls/struct/statfs.h"
|
#include "libc/calls/struct/statfs.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
|
#include "libc/calls/struct/stat.h"
|
||||||
#include "libc/sysv/consts/o.h"
|
#include "libc/sysv/consts/o.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
|
|
|
@ -1390,7 +1390,7 @@ static char *GenerateElfNotes(char *p) {
|
||||||
char *save;
|
char *save;
|
||||||
save = p = ALIGN(p, 4);
|
save = p = ALIGN(p, 4);
|
||||||
noteoff = p - prologue;
|
noteoff = p - prologue;
|
||||||
p = GenerateElfNote(p, "APE", 1, 106000000);
|
p = GenerateElfNote(p, "APE", 1, 107000000);
|
||||||
if (support_vector & _HOSTOPENBSD) {
|
if (support_vector & _HOSTOPENBSD) {
|
||||||
p = GenerateElfNote(p, "OpenBSD", 1, 0);
|
p = GenerateElfNote(p, "OpenBSD", 1, 0);
|
||||||
}
|
}
|
||||||
|
@ -1946,7 +1946,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
// otherwise try to use the ad-hoc self-extracted loader, securely
|
// otherwise try to use the ad-hoc self-extracted loader, securely
|
||||||
if (loaders.n) {
|
if (loaders.n) {
|
||||||
p = stpcpy(p, "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.6\"\n"
|
p = stpcpy(p, "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.7\"\n"
|
||||||
"[ x\"$1\" != x--assimilate ] && "
|
"[ x\"$1\" != x--assimilate ] && "
|
||||||
"[ -x \"$t\" ] && "
|
"[ -x \"$t\" ] && "
|
||||||
"exec \"$t\" \"$o\" \"$@\"\n");
|
"exec \"$t\" \"$o\" \"$@\"\n");
|
||||||
|
|
|
@ -1,123 +0,0 @@
|
||||||
/*-*- mode:asm; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
|
||||||
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
|
|
||||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
||||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
||||||
│ │
|
|
||||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
||||||
│ any purpose with or without fee is hereby granted, provided that the │
|
|
||||||
│ above copyright notice and this permission notice appear in all copies. │
|
|
||||||
│ │
|
|
||||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
||||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
||||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
||||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
||||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
||||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
||||||
#include "libc/macros.internal.h"
|
|
||||||
|
|
||||||
.macro .e local:req linux:req
|
|
||||||
.globl \local
|
|
||||||
.long \local-kLinuxErrnos
|
|
||||||
.byte \linux
|
|
||||||
.endm
|
|
||||||
|
|
||||||
// Lookup table translating errnos between systems.
|
|
||||||
//
|
|
||||||
// @see libc/sysv/systemfive.S
|
|
||||||
.rodata
|
|
||||||
.balign 4
|
|
||||||
kLinuxErrnos:
|
|
||||||
.e EPERM,1
|
|
||||||
.e ENOENT,2
|
|
||||||
.e ESRCH,3
|
|
||||||
.e EINTR,4
|
|
||||||
.e EIO,5
|
|
||||||
.e ENXIO,6
|
|
||||||
.e E2BIG,7
|
|
||||||
.e ENOEXEC,8
|
|
||||||
.e EBADF,9
|
|
||||||
.e ECHILD,10
|
|
||||||
.e EAGAIN,11
|
|
||||||
.e ENOMEM,12
|
|
||||||
.e EACCES,13
|
|
||||||
.e EFAULT,14
|
|
||||||
.e ENOTBLK,15
|
|
||||||
.e EBUSY,16
|
|
||||||
.e EEXIST,17
|
|
||||||
.e EXDEV,18
|
|
||||||
.e ENODEV,19
|
|
||||||
.e ENOTDIR,20
|
|
||||||
.e EISDIR,21
|
|
||||||
.e EINVAL,22
|
|
||||||
.e ENFILE,23
|
|
||||||
.e EMFILE,24
|
|
||||||
.e ENOTTY,25
|
|
||||||
.e ETXTBSY,26
|
|
||||||
.e EFBIG,27
|
|
||||||
.e ENOSPC,28
|
|
||||||
.e ESPIPE,29
|
|
||||||
.e EROFS,30
|
|
||||||
.e EMLINK,31
|
|
||||||
.e EPIPE,32
|
|
||||||
.e EDOM,33
|
|
||||||
.e ERANGE,34
|
|
||||||
.e EDEADLK,35
|
|
||||||
.e ENAMETOOLONG,36
|
|
||||||
.e ENOLCK,37
|
|
||||||
.e ENOSYS,38
|
|
||||||
.e ENOTEMPTY,39
|
|
||||||
.e ELOOP,40
|
|
||||||
.e ENOMSG,42
|
|
||||||
.e EIDRM,43
|
|
||||||
.e EUSERS,87
|
|
||||||
.e ENOTSOCK,88
|
|
||||||
.e EDESTADDRREQ,89
|
|
||||||
.e EMSGSIZE,90
|
|
||||||
.e EPROTOTYPE,91
|
|
||||||
.e ENOPROTOOPT,92
|
|
||||||
.e EPROTONOSUPPORT,93
|
|
||||||
.e ESOCKTNOSUPPORT,94
|
|
||||||
.e EOPNOTSUPP,95
|
|
||||||
.e EPFNOSUPPORT,96
|
|
||||||
.e EAFNOSUPPORT,97
|
|
||||||
.e EADDRINUSE,98
|
|
||||||
.e EADDRNOTAVAIL,99
|
|
||||||
.e EL2NSYNC,45
|
|
||||||
.e EL3HLT,46
|
|
||||||
.e EL3RST,47
|
|
||||||
.e ELNRNG,48
|
|
||||||
.e ETIME,62
|
|
||||||
.e ENONET,64
|
|
||||||
.e EREMOTE,66
|
|
||||||
.e EPROTO,71
|
|
||||||
.e EBADMSG,74
|
|
||||||
.e EOVERFLOW,75
|
|
||||||
.e EILSEQ,84
|
|
||||||
.e ERESTART,85
|
|
||||||
.e ENETDOWN,100
|
|
||||||
.e ENETUNREACH,101
|
|
||||||
.e ENETRESET,102
|
|
||||||
.e ECONNABORTED,103
|
|
||||||
.e ECONNRESET,104
|
|
||||||
.e ENOBUFS,105
|
|
||||||
.e EISCONN,106
|
|
||||||
.e ENOTCONN,107
|
|
||||||
.e ESHUTDOWN,108
|
|
||||||
.e ETOOMANYREFS,109
|
|
||||||
.e ETIMEDOUT,110
|
|
||||||
.e ECONNREFUSED,111
|
|
||||||
.e EHOSTDOWN,112
|
|
||||||
.e EHOSTUNREACH,113
|
|
||||||
.e EALREADY,114
|
|
||||||
.e EINPROGRESS,115
|
|
||||||
.e ESTALE,116
|
|
||||||
.e EDQUOT,122
|
|
||||||
.e ECANCELED,125
|
|
||||||
.e EOWNERDEAD,130
|
|
||||||
.e ENOTRECOVERABLE,131
|
|
||||||
.long 0
|
|
||||||
.byte 0
|
|
||||||
.endobj kLinuxErrnos,globl
|
|
|
@ -1,56 +0,0 @@
|
||||||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
||||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
||||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
||||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
||||||
│ │
|
|
||||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
||||||
│ any purpose with or without fee is hereby granted, provided that the │
|
|
||||||
│ above copyright notice and this permission notice appear in all copies. │
|
|
||||||
│ │
|
|
||||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
||||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
||||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
||||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
||||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
||||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
||||||
#include "tool/build/lib/lines.h"
|
|
||||||
#include "libc/limits.h"
|
|
||||||
#include "libc/mem/mem.h"
|
|
||||||
#include "libc/str/str.h"
|
|
||||||
|
|
||||||
struct Lines *NewLines(void) {
|
|
||||||
return calloc(1, sizeof(struct Lines));
|
|
||||||
}
|
|
||||||
|
|
||||||
void FreeLines(struct Lines *lines) {
|
|
||||||
size_t i;
|
|
||||||
for (i = 0; i < lines->n; ++i) {
|
|
||||||
free(lines->p[i]);
|
|
||||||
}
|
|
||||||
free(lines);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AppendLine(struct Lines *lines, const char *s, size_t n) {
|
|
||||||
lines->p = realloc(lines->p, ++lines->n * sizeof(*lines->p));
|
|
||||||
lines->p[lines->n - 1] = strndup(s, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AppendLines(struct Lines *lines, const char *s) {
|
|
||||||
const char *p;
|
|
||||||
for (;;) {
|
|
||||||
p = strchr(s, '\n');
|
|
||||||
if (p) {
|
|
||||||
AppendLine(lines, s, p - s);
|
|
||||||
s = p + 1;
|
|
||||||
} else {
|
|
||||||
if (*s) {
|
|
||||||
// gcc11 whines about SIZE_MAX > PTRDIFF_MAX
|
|
||||||
AppendLine(lines, s, PTRDIFF_MAX);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
#ifndef COSMOPOLITAN_TOOL_BUILD_LIB_LINES_H_
|
|
||||||
#define COSMOPOLITAN_TOOL_BUILD_LIB_LINES_H_
|
|
||||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
||||||
COSMOPOLITAN_C_START_
|
|
||||||
|
|
||||||
struct Lines {
|
|
||||||
size_t n;
|
|
||||||
char **p;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Lines *NewLines(void);
|
|
||||||
void FreeLines(struct Lines *);
|
|
||||||
void AppendLine(struct Lines *, const char *, size_t);
|
|
||||||
void AppendLines(struct Lines *, const char *);
|
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
|
||||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
||||||
#endif /* COSMOPOLITAN_TOOL_BUILD_LIB_LINES_H_ */
|
|
|
@ -81,13 +81,13 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (!(prog = commandv(argv[optind], pathbuf, sizeof(pathbuf)))) {
|
if (!(prog = commandv(argv[optind], pathbuf, sizeof(pathbuf)))) {
|
||||||
kprintf("%s: command not found\n", argv[optind]);
|
kprintf("%s: command not found\n", argv[optind]);
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outputpath) {
|
if (outputpath) {
|
||||||
if ((outfd = creat(outputpath, 0644)) == -1) {
|
if ((outfd = creat(outputpath, 0644)) == -1) {
|
||||||
perror(outputpath);
|
perror(outputpath);
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,12 +97,12 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (tcgetattr(1, &tio)) {
|
if (tcgetattr(1, &tio)) {
|
||||||
perror("tcgetattr");
|
perror("tcgetattr");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (openpty(&mfd, &sfd, 0, &tio, &wsz)) {
|
if (openpty(&mfd, &sfd, 0, &tio, &wsz)) {
|
||||||
perror("openpty");
|
perror("openpty");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ignore.sa_flags = 0;
|
ignore.sa_flags = 0;
|
||||||
|
@ -116,7 +116,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if ((pid = fork()) == -1) {
|
if ((pid = fork()) == -1) {
|
||||||
perror("fork");
|
perror("fork");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pid) {
|
if (!pid) {
|
||||||
|
@ -148,13 +148,13 @@ int main(int argc, char *argv[]) {
|
||||||
rc = 0;
|
rc = 0;
|
||||||
} else {
|
} else {
|
||||||
perror("read");
|
perror("read");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(got = rc)) {
|
if (!(got = rc)) {
|
||||||
if (waitpid(pid, &ws, 0) == -1) {
|
if (waitpid(pid, &ws, 0) == -1) {
|
||||||
perror("waitpid");
|
perror("waitpid");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -164,7 +164,7 @@ int main(int argc, char *argv[]) {
|
||||||
wrote = rc;
|
wrote = rc;
|
||||||
} else {
|
} else {
|
||||||
perror("write");
|
perror("write");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (outputpath) {
|
if (outputpath) {
|
||||||
|
@ -174,7 +174,7 @@ int main(int argc, char *argv[]) {
|
||||||
wrote = rc;
|
wrote = rc;
|
||||||
} else {
|
} else {
|
||||||
perror("write");
|
perror("write");
|
||||||
return __COUNTER__ + 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,6 +187,7 @@ int main(int argc, char *argv[]) {
|
||||||
if (WIFEXITED(ws)) {
|
if (WIFEXITED(ws)) {
|
||||||
return WEXITSTATUS(ws);
|
return WEXITSTATUS(ws);
|
||||||
} else {
|
} else {
|
||||||
return 128 + WTERMSIG(ws);
|
raise(WTERMSIG(ws));
|
||||||
|
exit(128 + WTERMSIG(ws));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,191 +0,0 @@
|
||||||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
||||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
||||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
||||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
||||||
│ │
|
|
||||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
||||||
│ any purpose with or without fee is hereby granted, provided that the │
|
|
||||||
│ above copyright notice and this permission notice appear in all copies. │
|
|
||||||
│ │
|
|
||||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
||||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
||||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
||||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
||||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
||||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
||||||
#include "libc/calls/calls.h"
|
|
||||||
#include "libc/log/check.h"
|
|
||||||
#include "libc/macros.internal.h"
|
|
||||||
#include "libc/stdio/stdio.h"
|
|
||||||
#include "libc/str/str.h"
|
|
||||||
#include "libc/sysv/consts/o.h"
|
|
||||||
#include "third_party/xed/x86.h"
|
|
||||||
|
|
||||||
const char kPrefixes[][8] = {
|
|
||||||
{},
|
|
||||||
{0x66},
|
|
||||||
{0x67},
|
|
||||||
{0x40},
|
|
||||||
{0x6F},
|
|
||||||
{0x66, 0x6F},
|
|
||||||
{0x66, 0x40},
|
|
||||||
{0x66, 0x6F},
|
|
||||||
{0x67, 0x40},
|
|
||||||
{0x67, 0x6F},
|
|
||||||
{0x66, 0x67},
|
|
||||||
{0x66, 0x67, 0x40},
|
|
||||||
{0x66, 0x67, 0x6F},
|
|
||||||
|
|
||||||
{0x0F},
|
|
||||||
{0x0F, 0x66},
|
|
||||||
{0x0F, 0x67},
|
|
||||||
{0x0F, 0x40},
|
|
||||||
{0x0F, 0x6F},
|
|
||||||
{0x0F, 0x66, 0x6F},
|
|
||||||
{0x0F, 0x66, 0x40},
|
|
||||||
{0x0F, 0x66, 0x6F},
|
|
||||||
{0x0F, 0x67, 0x40},
|
|
||||||
{0x0F, 0x67, 0x6F},
|
|
||||||
{0x0F, 0x66, 0x67},
|
|
||||||
{0x0F, 0x66, 0x67, 0x40},
|
|
||||||
{0x0F, 0x66, 0x67, 0x6F},
|
|
||||||
|
|
||||||
{0xF3, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x0F},
|
|
||||||
{0xF3, 0x67, 0x0F},
|
|
||||||
{0xF3, 0x40, 0x0F},
|
|
||||||
{0xF3, 0x6F, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x6F, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x40, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x6F, 0x0F},
|
|
||||||
{0xF3, 0x67, 0x40, 0x0F},
|
|
||||||
{0xF3, 0x67, 0x6F, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x67, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x67, 0x40, 0x0F},
|
|
||||||
{0xF3, 0x66, 0x67, 0x6F, 0x0F},
|
|
||||||
|
|
||||||
{0xF2, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x0F},
|
|
||||||
{0xF2, 0x67, 0x0F},
|
|
||||||
{0xF2, 0x40, 0x0F},
|
|
||||||
{0xF2, 0x6F, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x6F, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x40, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x6F, 0x0F},
|
|
||||||
{0xF2, 0x67, 0x40, 0x0F},
|
|
||||||
{0xF2, 0x67, 0x6F, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x67, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x67, 0x40, 0x0F},
|
|
||||||
{0xF2, 0x66, 0x67, 0x6F, 0x0F},
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint8_t kModrmPicks[] = {
|
|
||||||
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x0, 0x8, 0x10,
|
|
||||||
0x18, 0x20, 0x28, 0x30, 0x38, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
|
|
||||||
0x46, 0x47, 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78, 0x80,
|
|
||||||
0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x80, 0x88, 0x90, 0x98,
|
|
||||||
0xa0, 0xa8, 0xb0, 0xb8, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
|
|
||||||
0xc7, 0xc0, 0xc8, 0xd0, 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint8_t kOpMap0[] = {
|
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
||||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
||||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
|
|
||||||
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
|
||||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
|
|
||||||
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
|
||||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
|
|
||||||
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
|
|
||||||
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
|
|
||||||
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
|
|
||||||
150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
|
|
||||||
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
|
|
||||||
180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
|
|
||||||
195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
|
|
||||||
210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
|
|
||||||
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
|
|
||||||
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254,
|
|
||||||
255,
|
|
||||||
};
|
|
||||||
|
|
||||||
void WriteOp(int fd, struct XedDecodedInst *xedd) {
|
|
||||||
int i;
|
|
||||||
char buf[128], *p;
|
|
||||||
p = stpcpy(buf, ".byte ");
|
|
||||||
for (i = 0; i < xedd->length; ++i) {
|
|
||||||
if (i) *p++ = ',';
|
|
||||||
*p++ = '0';
|
|
||||||
*p++ = 'x';
|
|
||||||
*p++ = "0123456789abcdef"[(xedd->bytes[i] & 0xf0) >> 4];
|
|
||||||
*p++ = "0123456789abcdef"[xedd->bytes[i] & 0x0f];
|
|
||||||
}
|
|
||||||
*p++ = '\n';
|
|
||||||
CHECK_NE(-1, write(fd, buf, p - buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
int i, j, k, l, m, n, p, o, fd;
|
|
||||||
uint8_t op[16];
|
|
||||||
struct XedDecodedInst xedd[1];
|
|
||||||
fd = open("/tmp/ops.s", O_CREAT | O_TRUNC | O_RDWR, 0644);
|
|
||||||
for (o = 0; o < ARRAYLEN(kOpMap0); ++o) {
|
|
||||||
for (p = 0; p < ARRAYLEN(kPrefixes); ++p) {
|
|
||||||
memset(op, 0x55, 16);
|
|
||||||
n = strlen(kPrefixes[p]);
|
|
||||||
memcpy(op, kPrefixes[p], n);
|
|
||||||
op[n] = kOpMap0[o];
|
|
||||||
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
|
|
||||||
if (!xed_instruction_length_decode(xedd, op, XED_MAX_INSTRUCTION_BYTES)) {
|
|
||||||
if (xedd->op.has_modrm && xedd->op.has_sib) {
|
|
||||||
for (i = 0; i < ARRAYLEN(kModrmPicks); ++i) {
|
|
||||||
for (j = 0; j < ARRAYLEN(kModrmPicks); ++j) {
|
|
||||||
memset(op, 0x55, 16);
|
|
||||||
n = strlen(kPrefixes[p]);
|
|
||||||
memcpy(op, kPrefixes[p], n);
|
|
||||||
op[n] = kOpMap0[o];
|
|
||||||
op[xedd->op.pos_modrm] = kModrmPicks[i];
|
|
||||||
op[xedd->op.pos_sib] = kModrmPicks[j];
|
|
||||||
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
|
|
||||||
if (!xed_instruction_length_decode(xedd, op,
|
|
||||||
XED_MAX_INSTRUCTION_BYTES)) {
|
|
||||||
WriteOp(fd, xedd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (xedd->op.has_modrm) {
|
|
||||||
for (i = 0; i < ARRAYLEN(kModrmPicks); ++i) {
|
|
||||||
memset(op, 0x55, 16);
|
|
||||||
n = strlen(kPrefixes[p]);
|
|
||||||
memcpy(op, kPrefixes[p], n);
|
|
||||||
op[n] = kOpMap0[o];
|
|
||||||
op[xedd->op.pos_modrm] = kModrmPicks[i];
|
|
||||||
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
|
|
||||||
if (!xed_instruction_length_decode(xedd, op,
|
|
||||||
XED_MAX_INSTRUCTION_BYTES)) {
|
|
||||||
WriteOp(fd, xedd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
WriteOp(fd, xedd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
system("as -o /tmp/ops.o /tmp/ops.s");
|
|
||||||
system("objdump -wd /tmp/ops.o |"
|
|
||||||
" grep -v data16 |"
|
|
||||||
" grep -v addr32 |"
|
|
||||||
" grep -v '(bad)' |"
|
|
||||||
" sed 's/^[ :[:xdigit:]]*//' |"
|
|
||||||
" sed 's/^[ :[:xdigit:]]*//' |"
|
|
||||||
" sed 's/[[:space:]]#.*$//' |"
|
|
||||||
" grep -v 'rex\\.' |"
|
|
||||||
" sort -u");
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue