Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
PoroCYon
smol
Commits
afb8693c
Commit
afb8693c
authored
Apr 30, 2019
by
PoroCYon
Browse files
stuff works, EXCEPT 32-BIT STILL CRASHES
parent
734345f3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
afb8693c
...
...
@@ -47,7 +47,7 @@ SMOLFLAGS += -Duse_dnload_loader -Dalign_stack #-Dno_start_arg -Dunsafe_dynamic
NASM
?=
nasm
PYTHON3
?=
python3
SMOLFLAGS
+=
--nasm
=
$(NASM)
$(LDFLAGS)
SMOLFLAGS
+=
--nasm
=
$(NASM)
all
:
$(BINDIR)/hello-crt $(BINDIR)/sdl-crt $(BINDIR)/flag $(BINDIR)/hello-_start
...
...
smol/parse.py
View file @
afb8693c
...
...
@@ -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
:
continue
...
...
@@ -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
smol/util.py
View file @
afb8693c
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
))
subprocess
.
check_call
(
args
)
def
doexecoutp
(
args
):
proc
=
subprocess
.
Popen
(
args
,
stdout
=
subprocess
.
PIPE
)
proc
.
wait
()
return
None
if
proc
.
returncode
!=
0
else
proc
.
stdout
.
read
()
smold
View file @
afb8693c
#!/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
()
print
(
args
)
#
print(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
)
table
.
flush
()
try
:
subprocess
.
check_call
([
args
.
nasm
]
+
as_args
+
\
doexec
([
args
.
nasm
]
+
as_args
+
\
[
'-I'
,
args
.
runtime_dir
+
'/'
,
'-f'
,
'elf{}'
.
format
(
bits
),
\
table
.
name
,
'-o'
,
tableobj
.
name
])
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
,
tableobj
.
name
]
\
+
ld_args
+
args
.
input
)
except
subprocess
.
CalledProcessError
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment