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

@ -48,6 +48,7 @@ for x; do
done
set -- "$AR" "$ARFLAGS" "$OUT" "$@"
printf %s\\n "$*" >"$OUT.cmd"
OUTDIR="${OUT%/*}"
if [ "$OUTDIR" != "$OUT" ] && [ ! -d "$OUTDIR" ]; then

Binary file not shown.

View file

@ -151,9 +151,11 @@ if [ "${PLAT#*clang}" != "${PLAT}" ]; then
FIRST=0
continue
fi
TRAPV= # clang handles -f{,no-}{trap,wrap}v weird
# removes flags clang whines about
case "$x" in
-gstabs) ;;
-ftrapv) ;;
-ffixed-*) ;;
-fcall-saved*) ;;
-fsignaling-nans) ;;
@ -161,6 +163,7 @@ if [ "${PLAT#*clang}" != "${PLAT}" ]; then
-fno-fp-int-builtin-inexact) ;;
-Wno-unused-but-set-variable) ;;
-Wunsafe-loop-optimizations) ;;
-mdispatch-scheduler) ;;
-ftracer) ;;
-frounding-math) ;;
-fmerge-constants) ;;
@ -189,6 +192,7 @@ if [ "${PLAT#*clang}" != "${PLAT}" ]; then
-fschedule-insns) ;;
-fno-semantic-interposition) ;;
-mno-fentry) ;;
-f*shrink-wrap) ;;
-f*schedule-insns2) ;;
-fvect-cost-model=*) ;;
-fsimd-cost-model=*) ;;
@ -228,6 +232,7 @@ if [ "${PLAT#*clang}" != "${PLAT}" ]; then
;;
esac
done
set -- "$@" -fno-stack-protector
else
# removes flags only clang supports
FIRST=1

View file

@ -17,8 +17,11 @@ CONFIG_CCFLAGS += \
$(FTRACE) \
-Og
CONFIG_COPTS += \
-ftrapv
TARGET_ARCH ?= \
-msse3
-march=k8-sse3
RAGELFLAGS ?= -G2
@ -71,10 +74,7 @@ CONFIG_CPPFLAGS += \
CONFIG_CCFLAGS += \
$(BACKTRACES) \
-O3
#TARGET_ARCH ?= \
-msse3
-O2
RAGELFLAGS = -G2
@ -103,6 +103,12 @@ CONFIG_COPTS += \
$(SECURITY_BLANKETS) \
$(SANITIZER)
CONFIG_COPTS += \
-ftrapv
TARGET_ARCH ?= \
-march=k8-sse3
OVERRIDE_CCFLAGS += \
-fno-pie
@ -135,7 +141,7 @@ CONFIG_CCFLAGS += \
-fno-align-loops
TARGET_ARCH ?= \
-msse3
-march=k8-sse3
endif
@ -172,6 +178,8 @@ endif
ifeq ($(MODE), ansi)
CONFIG_CFLAGS += -std=c11
#CONFIG_CPPFLAGS += -ansi
CONFIG_CXXFLAGS += -std=c++11
TARGET_ARCH ?= -march=k8-sse3
endif

View file

@ -45,7 +45,6 @@
# ASFLAGS assembler flags (don't use -Wa, frontend prefix)
# TARGET_ARCH microarchitecture flags (e.g. -march=native)
SHELL = /bin/sh
DD ?= /bin/dd
CP ?= /bin/cp -f
RM ?= /bin/rm -f
@ -53,15 +52,14 @@ SED ?= /bin/sed
MKDIR ?= /bin/mkdir -p
TAGS ?= /usr/bin/ctags # emacs source builds or something breaks it
ARFLAGS = rcsD
TAGSFLAGS ?= -e -a --if0=no --langmap=c:.c.h.i --line-directives=yes
SILENT ?= 1
ZFLAGS ?= -4 --rsyncable
ZFLAGS ?=
XARGS ?= xargs -P4 -rs8000
NICE ?= build/actuallynice
RAGEL ?= ragel
DOT ?= dot
GZ ?= gzip
CLANG = clang-11
CLANG = clang-10
FC = gfortran #/opt/cross9f/bin/x86_64-linux-musl-gfortran
# see build/compile, etc. which run third_party/gcc/unbundle.sh
@ -108,8 +106,8 @@ FTRACE = \
-pg
SANITIZER = \
-fsanitize=undefined \
-fsanitize=leak \
-fsanitize=undefined \
-fsanitize=implicit-signed-integer-truncation \
-fsanitize=implicit-integer-sign-change
@ -139,6 +137,7 @@ DEFAULT_OFLAGS = \
-gdescribe-dies
DEFAULT_COPTS = \
-mno-red-zone \
-fno-math-errno \
-fno-trapping-math \
-fno-fp-int-builtin-inexact \
@ -149,9 +148,13 @@ DEFAULT_COPTS = \
-fstrict-aliasing \
-fstrict-overflow \
-fno-omit-frame-pointer \
-fno-optimize-sibling-calls \
-fno-semantic-interposition \
-mno-omit-leaf-frame-pointer
MATHEMATICAL = \
-O3 \
-fwrapv
DEFAULT_CPPFLAGS = \
-DIMAGE_BASE_VIRTUAL=$(IMAGE_BASE_VIRTUAL) \
-nostdinc \
@ -176,15 +179,14 @@ DEFAULT_ASFLAGS = \
--noexecstack
DEFAULT_LDFLAGS = \
-h \
-static \
--relax \
-nostdlib \
-m elf_x86_64 \
--gc-sections \
--build-id=none \
--cref -Map=$@.map \
--no-dynamic-linker \
-z max-page-size=0x1000 \
-Ttext-segment=$(IMAGE_BASE_VIRTUAL)
ASONLYFLAGS = \
@ -270,8 +272,8 @@ COMPILE.F.flags = $(cc.flags) $(cpp.flags) $(copt.flags) $(f.flags)
COMPILE.i.flags = $(cc.flags) $(copt.flags) $(c.flags)
COMPILE.ii.flags = $(cc.flags) $(copt.flags) $(cxx.flags)
LINK.flags = $(DEFAULT_LDFLAGS) $(CONFIG_LDFLAGS) $(LDFLAGS)
OBJECTIFY.c.flags = $(OBJECTIFY.S.flags) $(copt.flags) $(c.flags)
OBJECTIFY.cxx.flags = $(OBJECTIFY.S.flags) $(copt.flags) $(cxx.flags)
OBJECTIFY.c.flags = $(OBJECTIFY.S.flags) $(c.flags)
OBJECTIFY.cxx.flags = $(OBJECTIFY.S.flags) $(cxx.flags)
OBJECTIFY.s.flags = $(ASONLYFLAGS) $(s.flags)
OBJECTIFY.S.flags = $(copt.flags) $(cc.flags) $(o.flags) $(cpp.flags) $(S.flags)
OBJECTIFY.f.flags = $(copt.flags) $(cc.flags) $(o.flags) $(copt.flags) $(S.flags) $(f.flags)
@ -339,7 +341,6 @@ OBJECTIFY.ncabi.c = \
-fno-stack-protector \
-fno-instrument-functions \
-fno-optimize-sibling-calls \
-mpreferred-stack-boundary=3 \
-fno-sanitize=all \
-fcall-saved-rcx \
-fcall-saved-rdx \
@ -352,10 +353,26 @@ OBJECTIFY.ncabi.c = \
-c \
-xc
BUILD_SRCS = \
build/definitions.mk \
build/rules.mk \
build/compile \
build/link \
build/lolsan \
build/remote
# Initializer ABI
#
# Doesn't clobber RDI and RSI.
OBJECTIFY.initabi.c = \
$(GCC) \
$(OBJECTIFY.c.flags) \
-mno-fentry \
-fno-stack-protector \
-fno-instrument-functions \
-fno-optimize-sibling-calls \
-fno-sanitize=all \
-fcall-saved-rdi \
-fcall-saved-rsi \
-c
TAGSFLAGS = \
-e \
-a \
--if0=no \
--langmap=c:.c.h.i \
--line-directives=yes \
--exclude=libc/nt/struct/imagefileheader.h \
--exclude=libc/nt/struct/filesegmentelement.h

View file

@ -54,4 +54,17 @@ set -- --regex-c='/^[_[:alpha:]][_[:alnum:]]*[ *][ *]*\([_[:alpha:]][_[:alnum:]]
# extern int32_t (*const SetEvent)(int64_t hEvent) wincall;
set -- --regex-c='/^extern [^(]*(\*const \([^)]*\))(/\1/b' "$@"
exec ${TAGS:-ctags} -e --langmap=c:.c.h "$@"
# ctags doesn't understand forward declarations, e.g.
# struct WorstSoftwareEver;
set -- --regex-c='/^struct.*;$/uehocruehcroue/b' "$@"
exec ${TAGS:-ctags} \
-e \
--langmap=c:.c.h \
--exclude=libc/nt/struct/imagefileheader.h \
--exclude=libc/nt/struct/imageseparatedebugheader.h \
--exclude=libc/nt/struct/importobjectheader.h \
--exclude=libc/nt/struct/nonpageddebuginfo.h \
--exclude=libc/nt/struct/ansistring.h \
--exclude=libc/nt/struct/filesegmentelement.h \
"$@"

6
build/rollup Executable file
View file

@ -0,0 +1,6 @@
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
for x; do
printf '#include "%s"\n' "$x"
done

View file

@ -37,6 +37,7 @@ o/%.greg.o: %.greg.c; @ACTION=OBJECTIFY.greg build/compile $(OBJECTIFY.greg.c) $
o/%.zip.o: o/%; @build/zipobj $(OUTPUT_OPTION) $<
o/$(MODE)/%.a:; @$(ARCHIVE) $@ $^
o/$(MODE)/%: o/$(MODE)/%.dbg; @ACTION=OBJCOPY TARGET=$@ build/do $(OBJCOPY) -SO binary $< $@
o/$(MODE)/%.o: %.s; @TARGET=$@ build/assemble $(OBJECTIFY.s) $(OUTPUT_OPTION) $<
o/$(MODE)/%.o: o/$(MODE)/%.s; @TARGET=$@ build/assemble $(OBJECTIFY.s) $(OUTPUT_OPTION) $<
o/$(MODE)/%.s: %.S; @ACTION=PREPROCESS build/compile $(PREPROCESS) $(OUTPUT_OPTION) $<
@ -75,15 +76,15 @@ o/$(MODE)/%.runs: o/$(MODE)/%; @ACTION=CHECK.runs TARGET=$< build/runcom $< $(TE
o/$(MODE)/%.pkg:; @build/package $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) $(filter %.o,$^)
o/$(MODE)/%.zip.o: %; @build/zipobj $(OUTPUT_OPTION) $<
o/$(MODE)/%-gcc.asm: %.c; @ACTION=COMPILE.c build/compile $(COMPILE.c) $(OUTPUT_OPTION) $<
o/$(MODE)/%-gcc.asm: %.c; @ACTION=OBJECTIFY.c build/compile $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $<
o/$(MODE)/%-clang.asm: CC = $(CLANG)
o/$(MODE)/%-clang.asm: %.c; @ACTION=COMPILE.c build/compile $(COMPILE.c) $(OUTPUT_OPTION) $< || echo / need $(CLANG) >$@
o/$(MODE)/%-gcc.asm: %.f; @ACTION=COMPILE.f build/compile $(COMPILE.f) $(OUTPUT_OPTION) $<
o/$(MODE)/%-clang.asm: %.c; @ACTION=OBJECTIFY.c build/compile $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $< || echo / need $(CLANG) >$@
o/$(MODE)/%-gcc.asm: %.f; @ACTION=OBJECTIFY.f build/compile $(OBJECTIFY.f) -S -g0 $(OUTPUT_OPTION) $<
o/$(MODE)/%-clang.asm: CC = $(CLANG)
o/$(MODE)/%-clang.asm: %.f; @ACTION=COMPILE.f build/compile $(COMPILE.f) $(OUTPUT_OPTION) $< || echo / need $(CLANG) >$@
o/$(MODE)/%-gcc.asm: %.F; @ACTION=COMPILE.F build/compile $(COMPILE.F) $(OUTPUT_OPTION) $<
o/$(MODE)/%-clang.asm: %.f; @ACTION=OBJECTIFY.f build/compile $(OBJECTIFY.f) -S -g0 $(OUTPUT_OPTION) $< || echo / need $(CLANG) >$@
o/$(MODE)/%-gcc.asm: %.F; @ACTION=OBJECTIFY.F build/compile $(OBJECTIFY.F) -S -g0 $(OUTPUT_OPTION) $<
o/$(MODE)/%-clang.asm: CC = $(CLANG)
o/$(MODE)/%-clang.asm: %.F; @ACTION=COMPILE.F build/compile $(COMPILE.F) $(OUTPUT_OPTION) $< || echo / need $(CLANG) >$@
o/$(MODE)/%-clang.asm: %.F; @ACTION=OBJECTIFY.F build/compile $(OBJECTIFY.F) -S -g0 $(OUTPUT_OPTION) $< || echo / need $(CLANG) >$@
# ragel state machine compiler
.PRECIOUS: build/bootstrap/%.c.gz