cosmopolitan/tool/build/lib/argv.c

83 lines
3.6 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/arraylist2.h"
#include "libc/assert.h"
#include "libc/bits/popcnt.h"
2020-06-15 14:18:57 +00:00
#include "libc/bits/safemacros.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
2020-06-15 14:18:57 +00:00
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/machine.h"
#include "tool/build/lib/memory.h"
2020-06-15 14:18:57 +00:00
#define STACKALIGN 16
#define LINUX_AT_EXECFN 31
2020-06-15 14:18:57 +00:00
static size_t GetArgListLen(char **p) {
size_t n;
for (n = 0; *p; ++p) ++n;
return n;
2020-06-15 14:18:57 +00:00
}
static int64_t PushString(struct Machine *m, const char *s) {
size_t n;
int64_t sp;
n = strlen(s) + 1;
sp = Read64(m->sp);
sp -= n;
Write64(m->sp, sp);
VirtualRecv(m, sp, s, n);
return sp;
}
void LoadArgv(struct Machine *m, const char *prog, char **args, char **vars) {
int64_t i, n, sp, *p, *bloc;
size_t narg, nenv, naux, nall;
DCHECK_NOTNULL(prog);
DCHECK_NOTNULL(args);
DCHECK_NOTNULL(vars);
naux = 1;
nenv = GetArgListLen(vars);
narg = GetArgListLen(args);
nall = 1 + 1 + narg + 1 + nenv + 1 + (naux + 1) * 2;
bloc = gc(malloc(sizeof(int64_t) * nall));
p = bloc + nall;
*--p = 0;
*--p = 0;
*--p = PushString(m, prog);
*--p = LINUX_AT_EXECFN;
for (*--p = 0, i = nenv; i--;) *--p = PushString(m, vars[i]);
for (*--p = 0, i = narg; i--;) *--p = PushString(m, args[i]);
*--p = PushString(m, prog);
*--p = 1 + narg;
DCHECK_EQ(bloc, p);
sp = Read64(m->sp);
while ((sp - nall * sizeof(int64_t)) & (STACKALIGN - 1)) --sp;
sp -= nall * sizeof(int64_t);
DCHECK_EQ(0, sp % STACKALIGN);
Write64(m->sp, sp);
Write64(m->di, 0); /* or ape detects freebsd */
VirtualRecv(m, sp, bloc, sizeof(int64_t) * nall);
2020-06-15 14:18:57 +00:00
}