Commit 8a4b7ce4 authored by PoroCYon's avatar PoroCYon Committed by PoroCYon
Browse files

src->rt; smol*.py -> src; add optional 16-bit hash version

parent c75be623
/bin /bin
/obj /obj
/__pycache__ __pycache__
OBJDIR := obj OBJDIR := obj
BINDIR := bin BINDIR := bin
SRCDIR := src SRCDIR := rt
PYDIR := src
LDDIR := ld LDDIR := ld
TESTDIR:= test TESTDIR:= test
...@@ -40,7 +41,7 @@ CXXFLAGS += -m$(BITS) $(shell pkg-config --cflags sdl2) ...@@ -40,7 +41,7 @@ CXXFLAGS += -m$(BITS) $(shell pkg-config --cflags sdl2)
LIBS=-lc LIBS=-lc
SMOLFLAGS += SMOLFLAGS += -s
ASFLAGS += -DUSE_INTERP -DUSE_DNLOAD_LOADER -DNO_START_ARG -DUNSAFE_DYNAMIC #-DALIGN_STACK 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 #-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 ...@@ -73,7 +74,7 @@ $(OBJDIR)/%.start.o: $(OBJDIR)/%.lto.o $(OBJDIR)/crt1.lto.o
$(CC) $(LDFLAGS) -r -o "$@" $^ $(CC) $(LDFLAGS) -r -o "$@" $^
$(OBJDIR)/symbols.%.asm: $(OBJDIR)/%.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 \ $(OBJDIR)/stub.%.o: $(OBJDIR)/symbols.%.asm $(SRCDIR)/header32.asm \
$(SRCDIR)/loader32.asm $(SRCDIR)/loader32.asm
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
_smol_start: _smol_start:
;.loopme: jmp short .loopme
%ifdef USE_DL_FINI %ifdef USE_DL_FINI
push edx ; _dl_fini push edx ; _dl_fini
%endif %endif
...@@ -81,28 +81,46 @@ _smol_start: ...@@ -81,28 +81,46 @@ _smol_start:
add esi, ebx add esi, ebx
push ecx push ecx
%ifndef USE_HASH16
push ebx push ebx
push 33 push 33
push 5381 push 5381
pop eax pop eax
pop ebx pop ebx
%else
xor eax, eax
%endif
xor ecx, ecx xor ecx, ecx
.nexthashiter: .nexthashiter:
xchg eax, ecx ;
lodsb xchg eax, ecx
or al, al lodsb
xchg eax, ecx or al, al
jz short .breakhash xchg eax, ecx
jz short .breakhash
push edx
mul ebx %ifndef USE_HASH16
pop edx push edx
add eax, ecx mul ebx
jmp short .nexthashiter pop edx
; add eax, ecx
%else
ror ax, 2
; add ax, cx
%endif
add eax, ecx
jmp short .nexthashiter
.breakhash: .breakhash:
%ifndef USE_HASH16
pop ebx pop ebx
%endif
pop ecx pop ecx
;%ifndef USE_HASH16
; cmp ecx, eax
;%else
; cmp cx, ax
;%endif
cmp ecx, eax cmp ecx, eax
je short .hasheq je short .hasheq
...@@ -244,7 +262,7 @@ link.done: ...@@ -244,7 +262,7 @@ link.done:
%ifdef USE_DL_FINI %ifdef USE_DL_FINI
pop edx ; _dl_fini pop edx ; _dl_fini
%endif %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 ; 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 ; (which we don't have, so we're doing a 1-byte fixup instead of a
; 5-byte call) ; 5-byte call)
......
...@@ -30,6 +30,9 @@ def main(): ...@@ -30,6 +30,9 @@ def main():
parser.add_argument('--readelf', default=shutil.which('readelf'), \ parser.add_argument('--readelf', default=shutil.which('readelf'), \
help="which readelf binary to use") 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', \ # parser.add_argument('-d', '--dnload', default=False, action='store_true', \
# help="Use dnload's mechanism of importing functions. Slightly larger, but usually better compressable.") # help="Use dnload's mechanism of importing functions. Slightly larger, but usually better compressable.")
# parser.add_argument('--libsep', default=False, action='store_true', \ # parser.add_argument('--libsep', default=False, action='store_true', \
...@@ -69,7 +72,7 @@ def main(): ...@@ -69,7 +72,7 @@ def main():
symbols.setdefault(library, []) symbols.setdefault(library, [])
symbols[library].append((symbol, reloc)) symbols[library].append((symbol, reloc))
output(arch, symbols, args.nx, args.output) output(arch, symbols, args.nx, args.hash16, args.output)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
......
...@@ -3,11 +3,11 @@ import sys ...@@ -3,11 +3,11 @@ import sys
from smolshared import * 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 outf.write('; vim: set ft=nasm:\n') # be friendly
if nx: if nx: outf.write('%define USE_NX 1\n')
outf.write('%define USE_NX 1\n') if h16: outf.write('%define USE_HASH16 1\n')
usedrelocs = set({}) usedrelocs = set({})
for library, symrels in libraries.items(): for library, symrels in libraries.items():
...@@ -56,7 +56,7 @@ dynamic.end: ...@@ -56,7 +56,7 @@ dynamic.end:
eprintf('Relocation type ' + reloc + ' of symbol ' + sym + ' unsupported!') eprintf('Relocation type ' + reloc + ' of symbol ' + sym + ' unsupported!')
sys.exit(1) sys.exit(1)
hash = hash_djb2(sym) hash = hash_bsd2(sym) if h16 else hash_djb2(sym)
if nx: if nx:
outf.write("\t\t_symbols.{lib}.{name}: dd 0x{hash:x}"\ outf.write("\t\t_symbols.{lib}.{name}: dd 0x{hash:x}"\
.format(lib=shorts[library],name=sym,hash=hash).lstrip('\n')) .format(lib=shorts[library],name=sym,hash=hash).lstrip('\n'))
...@@ -87,7 +87,14 @@ global {name} ...@@ -87,7 +87,14 @@ global {name}
# end output_x86 # 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('; vim: set ft=nasm:\n')
outf.write('bits 64\n') outf.write('bits 64\n')
...@@ -129,7 +136,7 @@ global {name} ...@@ -129,7 +136,7 @@ global {name}
{name}: {name}:
""".format(name=sym).lstrip('\n')) """.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'\ outf.write('\t\t_symbols.{lib}.{name}: dq 0x{hash:x}\n'\
.format(lib=shorts[library],name=sym,hash=hash)) .format(lib=shorts[library],name=sym,hash=hash))
...@@ -152,9 +159,9 @@ global {name} ...@@ -152,9 +159,9 @@ global {name}
# end output_amd64 # end output_amd64
def output(arch, libraries, nx, outf): def output(arch, libraries, nx, h16, outf):
if arch == 'i386': output_x86(libraries, nx, outf) if arch == 'i386': output_x86(libraries, nx, h16, outf)
elif arch == 'x86_64': output_amd64(libraries, outf) elif arch == 'x86_64': output_amd64(libraries, nx, h16, outf)
else: else:
eprintf("E: cannot emit for arch '" + str(arch) + "'") eprintf("E: cannot emit for arch '" + str(arch) + "'")
sys.exit(1) sys.exit(1)
......
...@@ -3,9 +3,16 @@ import sys ...@@ -3,9 +3,16 @@ import sys
archmagic = { archmagic = {
'i386': 3, 3: 'i386' , 'i386': 3, 3: 'i386' ,
# arm: 40
'x86_64': 62, 62: 'x86_64', '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): def hash_djb2(s):
h = 5381 h = 5381
for c in s: for c in s:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment