Add x86_64-linux-gnu emulator

I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
This commit is contained in:
Justine Tunney 2020-08-25 04:23:25 -07:00
parent 467504308a
commit f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions

View file

@ -43,23 +43,18 @@ asm(".include \"libc/disclaimer.inc\"");
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
STATIC_YOINK("_init_getopt");
#define BADCH (int)'?'
#define BADARG (int)':'
int opterr, /* if error message should be printed */
optind, /* index into parent argv vector */
optopt, /* character checked for validity */
optreset; /* reset getopt */
char *optarg; /* argument associated with option */
static char *place; /* option letter processing */
#define BADCH (int)'?'
#define BADARG (int)':'
static char EMSG[] = { '\0' };
INITIALIZER(300, _init_getopt, {
opterr = 1;
optind = 1;
place = EMSG;
});
char *getopt_place; /* option letter processing */
char kGetoptEmsg[1];
/*
* getopt --
@ -77,35 +72,35 @@ getopt(int nargc, char * const nargv[], const char *ostr)
if (optind == 0)
optind = 1;
if (optreset || *place == 0) { /* update scanning pointer */
if (optreset || *getopt_place == 0) { /* update scanning pointer */
optreset = 0;
place = nargv[optind];
if (optind >= nargc || *place++ != '-') {
getopt_place = nargv[optind];
if (optind >= nargc || *getopt_place++ != '-') {
/* Argument is absent or is not an option */
place = EMSG;
getopt_place = kGetoptEmsg;
return (-1);
}
optopt = *place++;
if (optopt == '-' && *place == 0) {
optopt = *getopt_place++;
if (optopt == '-' && *getopt_place == 0) {
/* "--" => end of options */
++optind;
place = EMSG;
getopt_place = kGetoptEmsg;
return (-1);
}
if (optopt == 0) {
/* Solitary '-', treat as a '-' option
if the program (eg su) is looking for it. */
place = EMSG;
getopt_place = kGetoptEmsg;
if (strchr(ostr, '-') == NULL)
return (-1);
optopt = '-';
}
} else
optopt = *place++;
optopt = *getopt_place++;
/* See if option letter is one the caller wanted... */
if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
if (*place == 0)
if (*getopt_place == 0)
++optind;
if (opterr && *ostr != ':')
fprintf(stderr,
@ -118,18 +113,18 @@ getopt(int nargc, char * const nargv[], const char *ostr)
if (oli[1] != ':') {
/* don't need argument */
optarg = NULL;
if (*place == 0)
if (*getopt_place == 0)
++optind;
} else {
/* Option-argument is either the rest of this argument or the
entire next argument. */
if (*place)
optarg = place;
if (*getopt_place)
optarg = getopt_place;
else if (nargc > ++optind)
optarg = nargv[optind];
else {
/* option-argument absent */
place = EMSG;
getopt_place = kGetoptEmsg;
if (*ostr == ':')
return (BADARG);
if (opterr)
@ -138,7 +133,7 @@ getopt(int nargc, char * const nargv[], const char *ostr)
program_invocation_name, optopt);
return (BADCH);
}
place = EMSG;
getopt_place = kGetoptEmsg;
++optind;
}
return (optopt); /* return option letter */

View file

@ -50,7 +50,7 @@ THIRD_PARTY_GETOPT_SRCS = $(foreach x,$(THIRD_PARTY_GETOPT_ARTIFACTS),$($(x)_SRC
THIRD_PARTY_GETOPT_HDRS = $(foreach x,$(THIRD_PARTY_GETOPT_ARTIFACTS),$($(x)_HDRS))
THIRD_PARTY_GETOPT_CHECKS = $(foreach x,$(THIRD_PARTY_GETOPT_ARTIFACTS),$($(x)_CHECKS))
THIRD_PARTY_GETOPT_OBJS = $(foreach x,$(THIRD_PARTY_GETOPT_ARTIFACTS),$($(x)_OBJS))
$(THIRD_PARTY_GETOPT_OBJS): $(BUILD_FILES) third_party/getopt/getopt.mk
$(THIRD_PARTY_GETOPT_OBJS): third_party/getopt/getopt.mk
.PHONY: o/$(MODE)/third_party/getopt
o/$(MODE)/third_party/getopt: $(THIRD_PARTY_GETOPT_CHECKS)

30
third_party/getopt/initgetopt.S vendored Normal file
View file

@ -0,0 +1,30 @@
/*-*- 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
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/macros.h"
.source __FILE__
.init.start 201,_init_getopt
push $1
pop %rax
mov %eax,opterr(%rip)
mov %eax,optind(%rip)
lea kGetoptEmsg(%rip),%rax
mov %rax,getopt_place(%rip)
.init.end 201,_init_getopt