Skip to content

Commit c26012d

Browse files
authored
Merge pull request #21 from shaymargolis/add-lots-of-archs
Add lots of archs
2 parents f7645f9 + fede0ca commit c26012d

16 files changed

Lines changed: 189 additions & 43 deletions

File tree

.github/workflows/python-app.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ jobs:
3636
pytest --compiler-arch=mipsbe
3737
pytest --compiler-arch=mipsle
3838
pytest --compiler-arch=armle
39+
pytest --compiler-arch=x86
40+
pytest --compiler-arch=x86_64

shellblocks/compiler_arch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from typing import List
2+
from shellblocks.compiler_arch_option import CompilerArchOption
23

34

45
class CompilerArch():
56
def __init__(self):
67
pass
78

9+
def compiler_arch_option(self) -> [CompilerArchOption]:
10+
raise NotImplementedError()
11+
812
def compile_primitive(self, src_path: str) -> List[str]:
913
raise NotImplementedError()
1014

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from subprocess import check_output, CalledProcessError
2+
from enum import Enum
3+
4+
5+
class CompilerArchOption(Enum):
6+
MIPSBE = "mipsbe"
7+
MIPSLE = "mipsle"
8+
ARMLE = "armle"
9+
X86 = "x86"
10+
X86_64 = "x86_64"
11+
12+
13+
def get_current_platform():
14+
try:
15+
machine_bytes = check_output(["gcc", "-dumpmachine"])
16+
except FileNotFoundError:
17+
return None
18+
except CalledProcessError:
19+
return None
20+
21+
machine = machine_bytes.decode()
22+
23+
if "mips-" in machine:
24+
return CompilerArchOption.MIPSBE
25+
if "mipsel-" in machine:
26+
return CompilerArchOption.MIPSLE
27+
if "arm-" in machine:
28+
return CompilerArchOption.ARMLE
29+
if "x86_64-" in machine:
30+
return CompilerArchOption.X86_64
31+
32+
return None

shellblocks/compiler_archs/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
from enum import Enum
1+
from shellblocks.compiler_arch_option import CompilerArchOption
22
from shellblocks.compiler_archs.mips import CompilerArchMIPSBE, CompilerArchMIPSLE
33
from shellblocks.compiler_archs.arm import CompilerArchARMLE
4+
from shellblocks.compiler_archs.x86 import CompilerArchX86
5+
from shellblocks.compiler_archs.x86_64 import CompilerArchX86_64
46
from shellblocks.compiler_arch import CompilerArch
57

68

7-
class CompilerArchOption(Enum):
8-
MIPSBE = "mipsbe"
9-
MIPSLE = "mipsle"
10-
ARMLE = "armle"
11-
12-
139
def compiler_arch_to_object(arch: CompilerArchOption) -> CompilerArch:
1410
if arch == CompilerArchOption.MIPSBE:
1511
return CompilerArchMIPSBE()
1612
elif arch == CompilerArchOption.MIPSLE:
1713
return CompilerArchMIPSLE()
1814
elif arch == CompilerArchOption.ARMLE:
1915
return CompilerArchARMLE()
16+
elif arch == CompilerArchOption.X86:
17+
return CompilerArchX86()
18+
elif arch == CompilerArchOption.X86_64:
19+
return CompilerArchX86_64()
2020

2121
raise NotImplementedError()
2222

shellblocks/compiler_archs/arm.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import List
22

3+
from shellblocks.compiler_arch_option import CompilerArchOption
34
from shellblocks.compiler_archs.gcc import CompilerArchGCC
45
from shellblocks.utils import sources_location
56

@@ -8,8 +9,6 @@ class CompilerArchARM(CompilerArchGCC):
89
def __init__(self):
910
super().__init__()
1011

11-
self.compiler_path = self.get_compiler_path()
12-
1312
def get_headers(self) -> List[str]:
1413
return ["arch/arm/utils.h"]
1514

@@ -20,3 +19,6 @@ def get_ldscript_path(self):
2019
class CompilerArchARMLE(CompilerArchARM):
2120
def get_compiler_path(self):
2221
return "arm-linux-gnueabi-gcc-10"
22+
23+
def compiler_arch_option(self) -> [CompilerArchOption]:
24+
return [CompilerArchOption.MIPSLE]

shellblocks/compiler_archs/gcc.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from shellblocks.compiler_arch import CompilerArch
2+
from shellblocks.compiler_arch_option import get_current_platform
23

34

45
class CompilerArchGCC(CompilerArch):
56
def __init__(self):
67
super().__init__()
78

8-
self.compiler_path = self.get_compiler_path()
9+
if get_current_platform() in self.compiler_arch_option():
10+
self.compiler_path = "gcc"
11+
else:
12+
self.compiler_path = self.get_compiler_path()
913

1014
def get_compiler_path(self):
1115
raise NotImplementedError()
@@ -15,8 +19,14 @@ def get_ldscript_path(self):
1519

1620
def get_gcc_flags(self):
1721
return [
22+
# Dont try to link with any std library
23+
# or assume hosted assumptions: Allow any entrypoint
24+
# and function definition
1825
"-nostdlib",
1926
"-ffreestanding",
27+
# Require GCC to keep the compilation order
28+
# as declared in the source files
29+
"-fno-toplevel-reorder",
2030
]
2131

2232
def compile_primitive(self, src_path: str) -> [str]:

shellblocks/compiler_archs/mips.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from typing import List
22

33
from shellblocks.compiler_archs.gcc import CompilerArchGCC
4+
from shellblocks.compiler_arch_option import CompilerArchOption
45
from shellblocks.utils import sources_location
56

67

78
class CompilerArchMIPS(CompilerArchGCC):
89
def __init__(self):
910
super().__init__()
1011

11-
self.compiler_path = self.get_compiler_path()
12-
1312
def get_gcc_flags(self):
1413
return super().get_gcc_flags() + [
1514
"-mno-shared",
@@ -26,6 +25,9 @@ class CompilerArchMIPSBE(CompilerArchMIPS):
2625
def get_compiler_path(self):
2726
return "mips-linux-gnu-gcc-9"
2827

28+
def compiler_arch_option(self) -> [CompilerArchOption]:
29+
return [CompilerArchOption.MIPSBE]
30+
2931

3032
class CompilerArchMIPSLE(CompilerArchMIPS):
3133
def get_gcc_flags(self):
@@ -35,3 +37,6 @@ def get_gcc_flags(self):
3537

3638
def get_compiler_path(self):
3739
return "mips-linux-gnu-gcc-9"
40+
41+
def compiler_arch_option(self) -> [CompilerArchOption]:
42+
return [CompilerArchOption.MIPSLE]

shellblocks/compiler_archs/x86.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from shellblocks.compiler_arch_option import CompilerArchOption
2+
from shellblocks.compiler_archs.x86_64 import CompilerArchX86_64
3+
4+
5+
class CompilerArchX86(CompilerArchX86_64):
6+
def __init__(self):
7+
super().__init__()
8+
9+
def get_gcc_flags(self):
10+
return super().get_gcc_flags() + [
11+
"-m32",
12+
]
13+
14+
def compiler_arch_option(self) -> [CompilerArchOption]:
15+
return [CompilerArchOption.X86, CompilerArchOption.X86_64]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
from shellblocks.compiler_arch_option import CompilerArchOption
4+
from shellblocks.compiler_archs.gcc import CompilerArchGCC
5+
from shellblocks.utils import sources_location
6+
7+
8+
class CompilerArchX86_64(CompilerArchGCC):
9+
def __init__(self):
10+
super().__init__()
11+
12+
def get_headers(self) -> List[str]:
13+
return ["arch/x86/utils.h"]
14+
15+
def get_ldscript_path(self):
16+
return (sources_location / "shellcode_ldscript.ld").as_posix()
17+
18+
def get_compiler_path(self):
19+
return "x86_64-linux-gnu-gcc-8"
20+
21+
def compiler_arch_option(self) -> [CompilerArchOption]:
22+
return [CompilerArchOption.X86_64]

shellblocks/src/arch/mips/utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"nop\n\t" \
1010
"bal " GET_REL_LABEL(LABEL) "\n\t" \
1111
"nop\n\t" \
12-
GET_REL_LABEL(LABEL) ": move $v0, $ra\n\t" \
13-
"move %0, $v0\n\t" \
12+
GET_REL_LABEL(LABEL) ": move %0, $ra\n\t" \
1413
"addiu %0, (" #LABEL " - " GET_REL_LABEL(LABEL) ")\n\t" \
1514
: "=r" (OUTPUT) : : "$ra" \
1615
)

0 commit comments

Comments
 (0)