Commit afb8693c authored by PoroCYon's avatar PoroCYon
Browse files


parent 734345f3
......@@ -47,7 +47,7 @@ SMOLFLAGS += -Duse_dnload_loader -Dalign_stack #-Dno_start_arg -Dunsafe_dynamic
NASM ?= nasm
PYTHON3 ?= python3
SMOLFLAGS += --nasm=$(NASM)
all: $(BINDIR)/hello-crt $(BINDIR)/sdl-crt $(BINDIR)/flag $(BINDIR)/hello-_start
......@@ -5,8 +5,8 @@ import subprocess
import struct
import sys
from .elf import ELFMachine
from .util import warn, error
from .elf import ELFMachine
from .util import warn, error, doexecoutp
def decide_arch(files):
......@@ -34,7 +34,7 @@ def decide_arch(files):
def build_reloc_typ_table(reo):
relocs = {}
for s in reo.decode('utf-8').splitlines():
for s in reo.decode('utf-8').splitlines()[3:]:
cols = s.split()
# prolly a 'header' line
......@@ -56,7 +56,7 @@ def get_needed_syms(readelf_bin, inpfiles):
relocs = build_reloc_typ_table(outrel)
syms = set()
for entry in output.decode('utf-8').splitlines():
for entry in output.decode('utf-8').splitlines()[3:]:
cols = entry.split()
if len(cols) < 8:
......@@ -102,3 +102,9 @@ def find_symbol(scanelf_bin, libraries, libnames, symbol):
return None
def ld_is_cc(ldpath):
outf = doexecoutp([ldpath, "--version"])
if outf is None: return False # good enough
outf = outf.decode('utf-8')
return "GCC" in outf or "clang" in outf # also good enough
import sys
import struct
import subprocess
import sys
def eprintf(*args, **kwargs, ):
print(*args, file=sys.stderr, **kwargs)
......@@ -43,3 +44,14 @@ def readstr(blob, off):
return text.decode('utf-8'), off
def doexec(args, verbose=False):
if verbose: log(str(args))
def doexecoutp(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
return None if proc.returncode != 0 else
#!/usr/bin/env python3
import os
from os import path
import sys
import argparse
import os
from os import path
import shutil
import sys
import tempfile
import subprocess
from smol.util import error
from smol.util import error, doexec
from smol.elf import ELFMachine, ELF_DEFAULT_BITS
from smol.parse import decide_arch, find_lib, find_symbol, get_cc_paths, get_needed_syms
from smol.parse import decide_arch, find_lib, find_symbol, get_cc_paths, \
get_needed_syms, ld_is_cc
from smol.emit import output_table
......@@ -23,10 +24,10 @@ def main():
help='directories to search libraries in')
parser.add_argument('-s', '--hash16', default=False, action='store_true', \
parser.add_argument('--hash16', default=False, action='store_true', \
help="Use 16-bit (BSD) hashes instead of 32-bit djb2 hashes. "\
+"Must be used with -Duse_dnload_loader")
parser.add_argument('-n', '--nx', default=False, action='store_true', \
parser.add_argument('--nx', default=False, action='store_true', \
help="Use NX (i.e. don't use RWE pages). Costs the size of one phdr, "\
+"plus some extra bytes on i386.")
......@@ -52,7 +53,7 @@ def main():
args, ld_args = parser.parse_known_args()
for util in ['nasm', 'cc', 'scanelf', 'readelf', 'ld']:
if not getattr(args, util):
......@@ -86,8 +87,12 @@ def main():
if not arch or not bits:
error('Invalid architecture!')
if arch == ELFMachine.i386: ld_args.append("-melf_i386")
elif arch == ELFMachine.i386: ld_args.append("-melf_x86_64")
ldiscc = ld_is_cc(args.ld)
if arch == ELFMachine.i386:
ld_args.append("-m32" if ldiscc else "-melf_i386")
elif arch == ELFMachine.i386:
ld_args.append("-m32" if ldiscc else "-melf_x86_64")
else: pass # shouldn't happen
syms = get_needed_syms(args.readelf, args.input)
......@@ -113,15 +118,15 @@ def main():
as_args = ['-D' + opt.upper() for opt in opts]
with tempfile.NamedTemporaryFile('w', suffix='.asm') as table, \
tempfile.NamedTemporaryFile('w', suffix='.o') as tableobj:
table = open("/tmp/test.asm", 'w')
output_table(arch, symbols, args.nx, args.hash16, table)
subprocess.check_call([args.nasm] + as_args + \
doexec([args.nasm] + as_args + \
['-I', args.runtime_dir + '/', '-f', 'elf{}'.format(bits), \, '-o',])
subprocess.check_call([args.ld, '-T', \
os.path.join(args.linker_dir, 'link.ld'), '--oformat=binary', \
oformat = ('-Wl,' if ldiscc else '') + '--oformat=binary'
doexec([args.ld, '-T', \
os.path.join(args.linker_dir, 'link.ld'), oformat, \
'-o', args.output,] \
+ ld_args + args.input)
except subprocess.CalledProcessError:
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