diff --git a/.gitignore b/.gitignore index 906a738..6787451 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /bin /obj -/__pycache__ +__pycache__ diff --git a/Makefile b/Makefile index 42a8a94..03bf855 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ OBJDIR := obj BINDIR := bin -SRCDIR := src +SRCDIR := rt +PYDIR := src LDDIR := ld TESTDIR:= test @@ -40,7 +41,7 @@ CXXFLAGS += -m$(BITS) $(shell pkg-config --cflags sdl2) LIBS=-lc -SMOLFLAGS += +SMOLFLAGS += -s ASFLAGS += -DUSE_INTERP -DUSE_DNLOAD_LOADER -DNO_START_ARG -DUNSAFE_DYNAMIC #-DALIGN_STACK #-DUSE_DNLOAD_LOADER #-DUSE_DT_DEBUG #-DUSE_DL_FINI #-DNO_START_ARG #-DUNSAFE_DYNAMIC @@ -73,7 +74,7 @@ $(OBJDIR)/%.start.o: $(OBJDIR)/%.lto.o $(OBJDIR)/crt1.lto.o $(CC) $(LDFLAGS) -r -o "$@" $^ $(OBJDIR)/symbols.%.asm: $(OBJDIR)/%.o - $(PYTHON3) ./smol.py $(SMOLFLAGS) $(LIBS) "$<" "$@" + $(PYTHON3) $(PYDIR)/smol.py $(SMOLFLAGS) $(LIBS) "$<" "$@" $(OBJDIR)/stub.%.o: $(OBJDIR)/symbols.%.asm $(SRCDIR)/header32.asm \ $(SRCDIR)/loader32.asm diff --git a/src/crt1.c b/rt/crt1.c similarity index 100% rename from src/crt1.c rename to rt/crt1.c diff --git a/src/elf.inc b/rt/elf.inc similarity index 100% rename from src/elf.inc rename to rt/elf.inc diff --git a/src/header32.asm b/rt/header32.asm similarity index 100% rename from src/header32.asm rename to rt/header32.asm diff --git a/src/header64.asm b/rt/header64.asm similarity index 100% rename from src/header64.asm rename to rt/header64.asm diff --git a/src/linkscr.inc b/rt/linkscr.inc similarity index 100% rename from src/linkscr.inc rename to rt/linkscr.inc diff --git a/src/loader32.asm b/rt/loader32.asm similarity index 92% rename from src/loader32.asm rename to rt/loader32.asm index 8648595..ae2c3f8 100644 --- a/src/loader32.asm +++ b/rt/loader32.asm @@ -11,7 +11,7 @@ _smol_start: - +;.loopme: jmp short .loopme %ifdef USE_DL_FINI push edx ; _dl_fini %endif @@ -81,28 +81,46 @@ _smol_start: add esi, ebx push ecx +%ifndef USE_HASH16 push ebx push 33 push 5381 pop eax pop ebx +%else + xor eax, eax +%endif xor ecx, ecx .nexthashiter: - xchg eax, ecx - lodsb - or al, al - xchg eax, ecx - jz short .breakhash + ; + xchg eax, ecx + lodsb + or al, al + xchg eax, ecx + jz short .breakhash - push edx - mul ebx - pop edx - add eax, ecx - jmp short .nexthashiter +%ifndef USE_HASH16 + push edx + mul ebx + pop edx +; add eax, ecx +%else + ror ax, 2 +; add ax, cx +%endif + add eax, ecx + jmp short .nexthashiter .breakhash: +%ifndef USE_HASH16 pop ebx +%endif pop ecx +;%ifndef USE_HASH16 +; cmp ecx, eax +;%else +; cmp cx, ax +;%endif cmp ecx, eax je short .hasheq @@ -244,7 +262,7 @@ link.done: %ifdef USE_DL_FINI pop edx ; _dl_fini %endif - ; move esp into eax, *then* increase the stack by 4, as main() + ; move esp into eax, *then* increase the stack by 4, as main() ; expects a return address to be inserted by a call instruction ; (which we don't have, so we're doing a 1-byte fixup instead of a ; 5-byte call) diff --git a/src/loader64.asm b/rt/loader64.asm similarity index 100% rename from src/loader64.asm rename to rt/loader64.asm diff --git a/src/rtld.inc b/rt/rtld.inc similarity index 100% rename from src/rtld.inc rename to rt/rtld.inc diff --git a/smol.py b/src/smol.py similarity index 91% rename from smol.py rename to src/smol.py index fadb6c5..b01471c 100755 --- a/smol.py +++ b/src/smol.py @@ -30,6 +30,9 @@ def main(): parser.add_argument('--readelf', default=shutil.which('readelf'), \ help="which readelf binary to use") + parser.add_argument('-s', '--hash16', default=False, action='store_true', \ + help="Use 16-bit (BSD) hashes instead of 32-bit djb2 hashes. "\ + +"Conflicts with -DUSE_DNLOAD_LOADER") # parser.add_argument('-d', '--dnload', default=False, action='store_true', \ # help="Use dnload's mechanism of importing functions. Slightly larger, but usually better compressable.") # parser.add_argument('--libsep', default=False, action='store_true', \ @@ -69,7 +72,7 @@ def main(): symbols.setdefault(library, []) symbols[library].append((symbol, reloc)) - output(arch, symbols, args.nx, args.output) + output(arch, symbols, args.nx, args.hash16, args.output) if __name__ == '__main__': main() diff --git a/smolemit.py b/src/smolemit.py similarity index 87% rename from smolemit.py rename to src/smolemit.py index 6b76ad5..386ce91 100644 --- a/smolemit.py +++ b/src/smolemit.py @@ -3,11 +3,11 @@ import sys from smolshared import * -def output_x86(libraries, nx, outf): +def output_x86(libraries, nx, h16, outf): outf.write('; vim: set ft=nasm:\n') # be friendly - if nx: - outf.write('%define USE_NX 1\n') + if nx: outf.write('%define USE_NX 1\n') + if h16: outf.write('%define USE_HASH16 1\n') usedrelocs = set({}) for library, symrels in libraries.items(): @@ -56,7 +56,7 @@ dynamic.end: eprintf('Relocation type ' + reloc + ' of symbol ' + sym + ' unsupported!') sys.exit(1) - hash = hash_djb2(sym) + hash = hash_bsd2(sym) if h16 else hash_djb2(sym) if nx: outf.write("\t\t_symbols.{lib}.{name}: dd 0x{hash:x}"\ .format(lib=shorts[library],name=sym,hash=hash).lstrip('\n')) @@ -87,7 +87,14 @@ global {name} # end output_x86 -def output_amd64(libraries, outf): +def output_amd64(libraries, nx, h16, outf): + if h16: + eprintf("--hash16 not supported yet for x86_64 outputs.") + exit(1) + + if nx: outf.write('%define USE_NX 1\n') +# if h16: outf.write('%define USE_HASH16 1\n') + outf.write('; vim: set ft=nasm:\n') outf.write('bits 64\n') @@ -129,7 +136,7 @@ global {name} {name}: """.format(name=sym).lstrip('\n')) - hash = hash_djb2(sym) + hash = hash_bsd2(sym) if h16 else hash_djb2(sym) outf.write('\t\t_symbols.{lib}.{name}: dq 0x{hash:x}\n'\ .format(lib=shorts[library],name=sym,hash=hash)) @@ -152,9 +159,9 @@ global {name} # end output_amd64 -def output(arch, libraries, nx, outf): - if arch == 'i386': output_x86(libraries, nx, outf) - elif arch == 'x86_64': output_amd64(libraries, outf) +def output(arch, libraries, nx, h16, outf): + if arch == 'i386': output_x86(libraries, nx, h16, outf) + elif arch == 'x86_64': output_amd64(libraries, nx, h16, outf) else: eprintf("E: cannot emit for arch '" + str(arch) + "'") sys.exit(1) diff --git a/smolparse.py b/src/smolparse.py similarity index 100% rename from smolparse.py rename to src/smolparse.py diff --git a/smolshared.py b/src/smolshared.py similarity index 67% rename from smolshared.py rename to src/smolshared.py index 76f04d7..6882da1 100644 --- a/smolshared.py +++ b/src/smolshared.py @@ -3,9 +3,16 @@ import sys archmagic = { 'i386': 3, 3: 'i386' , + # arm: 40 'x86_64': 62, 62: 'x86_64', } +def hash_bsd2(s): + h = 0 + for c in s: + h = ((h >> 2) + ((h & 3) << 14) + ord(c)) & 0xFFFF + return h + def hash_djb2(s): h = 5381 for c in s: