Skip to content

Commit fb91079

Browse files
committed
WIP: build on macos/linux
1 parent 1c4805e commit fb91079

24 files changed

+216
-126
lines changed

.devcontainer/devcontainer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "riscy-business",
3+
"image": "ubuntu:22.04",
4+
"runArgs": ["--platform=linux/amd64"]
5+
}

.devcontainer/packages.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
apt update && apt upgrade -y
3+
apt install -y git build-essential curl wget lsb-release software-properties-common gnupg cmake ninja-build ripgrep gdb
4+
wget https://apt.llvm.org/llvm.sh
5+
chmod +x llvm.sh
6+
./llvm.sh 19

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ build*/
1313
*.exe
1414
*.dll
1515
*.pdb
16+
*.trace
1617

1718
__pycache__/
1819

payload/CMakeLists.txt

Lines changed: 21 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

payload/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# payload
2+
3+
## Build (macos)
4+
5+
Install `lld` and `llvm` via homebrew:
6+
7+
```sh
8+
brew install lld llvm
9+
```
10+
11+
Configure the build:
12+
13+
```sh
14+
cmake -B build --toolchain ~/zig-cross/x86_64-windows-gnu.cmake -DCLANG_EXECUTABLE=$(brew --prefix llvm)/bin/clang -DLLD_EXECUTABLE=$(brew --prefix lld)/bin/ld.lld -DOBJCOPY_EXECUTABLE=$(brew --prefix llvm)/bin/llvm-objcopy
15+
```
16+
17+
## Build (Windows)
18+
19+
Install the LLVM toolset in with the Visual Studio Installer and use the following command line to configure:
20+
21+
```sh
22+
cmake -B build -T ClangCL -DCLANG_EXECUTABLE=D:\CodeBlocks\llvm-project-19.1.6.src\install\bin\clang.exe -DLLD_EXECUTABLE=D:\CodeBlocks\llvm-project-19.1.6.src\install\bin\ld.lld.exe -DOBJCOPY_EXECUTABLE=D:\CodeBlocks\llvm-project-19.1.6.src\install\bin\llvm-objcopy.exe
23+
```

payload/cmake.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ msvc-runtime = "static"
1212
[template.riscvm]
1313
type = "executable"
1414
add-function = "add_riscvm_executable"
15-
compile-options = ["/GR-", "/EHs-"]
16-
compile-definitions = ["_HAS_EXCEPTIONS=0"]
15+
msvc.compile-options = ["/GR-", "/EHs-"]
16+
clang.compile-options = ["-fno-exceptions", "-fno-rtti"]
17+
compile-definitions = ["_HAS_EXCEPTIONS=0"] # TODO: MSVC only?
1718

1819
# Windows implementation of the riscvm syscalls (for debugging only)
1920
[target.riscvm-crt0]
@@ -23,9 +24,9 @@ sources = ["crt/riscvm-crt0.cpp"]
2324
[target.payload]
2425
type = "riscvm"
2526
sources = [
26-
"src/main.cpp",
27+
"src/main.c",
2728
"crt/minicrt.c",
28-
"crt/minicrt.cpp",
29+
#"crt/minicrt.cpp",
2930
]
3031
headers = [
3132
"include/phnt.h",

payload/cmake/cmkr.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include_guard()
22

33
# Change these defaults to point to your infrastructure if desired
44
set(CMKR_REPO "https://github.com/build-cpp/cmkr" CACHE STRING "cmkr git repository" FORCE)
5-
set(CMKR_TAG "v0.2.33" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
5+
set(CMKR_TAG "v0.2.44" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
66
set(CMKR_COMMIT_HASH "" CACHE STRING "cmkr git commit hash (optional)" FORCE)
77

88
# To bootstrap/generate a cmkr project: cmake -P cmkr.cmake

payload/cmake/riscvm.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@ set(EMBED_TYPE "post-merge-pre-opt") # post-merge-pre-opt/optimized
1111
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "^MSVC$") # clang-cl
1212
add_compile_options(-flto)
1313
add_link_options(/mllvm:-lto-embed-bitcode=${EMBED_TYPE})
14-
elseif(WIN32) # clang (Windows)
14+
elseif(WIN32 AND CMAKE_HOST_SYSTEM_NAME MATCHES "Windows") # clang (Windows)
1515
add_compile_options(-fuse-ld=lld-link -flto)
1616
add_link_options(-Wl,/mllvm:-lto-embed-bitcode=${EMBED_TYPE})
1717
else() # clang (unix)
1818
add_compile_options(-fuse-ld=lld-link -flto)
19-
add_link_options(-mllvm -lto-embed-bitcode=${EMBED_TYPE})
19+
add_link_options(-Wl,--plugin-opt=-lto-embed-bitcode=${EMBED_TYPE} -static)
2020
endif()
2121

2222
# Some common annoying warnings when including Windows.h
2323
add_compile_options(-Wno-pragma-pack -Wno-microsoft-enum-forward-reference)
2424

2525
# Find the regular LLVM toolchain
26+
# TODO: support zig cross-compilation toolchain
2627
get_filename_component(LLVM_DIR "${CMAKE_CXX_COMPILER}" DIRECTORY)
2728
find_program(CLANG_EXECUTABLE clang PATHS "${LLVM_DIR}" NO_DEFAULT_PATH REQUIRED)
2829
message(STATUS "Found clang: ${CLANG_EXECUTABLE}")
30+
# TODO: can we use the clang executable itself as a fallback?
2931
find_program(LLD_EXECUTABLE ld.lld PATHS "${LLVM_DIR}" NO_DEFAULT_PATH REQUIRED)
3032
message(STATUS "Found lld: ${LLD_EXECUTABLE}")
33+
# TODO: is there a cmake variable for this?
3134
find_program(OBJCOPY_EXECUTABLE llvm-objcopy PATHS "${LLVM_DIR}" NO_DEFAULT_PATH REQUIRED)
3235
message(STATUS "Found llvm-objcopy: ${OBJCOPY_EXECUTABLE}")
3336

payload/crt/minicrt.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
11
#include <assert.h>
2-
#include <phnt.h>
2+
// #include <phnt.h>
33
#include <malloc.h>
4+
#include <windows.h>
5+
6+
NTSYSAPI
7+
void* NTAPI RtlGetCurrentPeb(VOID);
8+
9+
NTSYSAPI
10+
PVOID
11+
NTAPI
12+
RtlAllocateHeap(_In_ HANDLE HeapHandle, _In_opt_ ULONG Flags, _In_ SIZE_T Size);
13+
14+
NTSYSAPI
15+
PVOID
16+
NTAPI
17+
RtlReAllocateHeap(_In_ HANDLE HeapHandle, _In_ ULONG Flags, _Frees_ptr_opt_ PVOID BaseAddress, _In_ SIZE_T Size);
18+
19+
NTSYSAPI
20+
ULONG
21+
NTAPI
22+
RtlFreeHeap(_In_ HANDLE HeapHandle, _In_opt_ ULONG Flags, _Frees_ptr_opt_ _Post_invalid_ PVOID BaseAddress);
23+
24+
static HANDLE GetHeapHandle()
25+
{
26+
HANDLE heap;
27+
memcpy(&heap, (char*)RtlGetCurrentPeb() + 0x30, sizeof(HANDLE));
28+
return heap;
29+
}
430

531
void* __cdecl malloc(size_t size)
632
{
7-
HANDLE heap = RtlGetCurrentPeb()->ProcessHeap;
8-
return RtlAllocateHeap(heap, HEAP_ZERO_MEMORY, size);
33+
return RtlAllocateHeap(GetHeapHandle(), HEAP_ZERO_MEMORY, size);
934
}
1035

1136
#ifndef _DEBUG
1237
void* __cdecl _expand(void* block, size_t size)
1338
{
14-
HANDLE heap = RtlGetCurrentPeb()->ProcessHeap;
15-
return RtlReAllocateHeap(heap, HEAP_ZERO_MEMORY, block, size);
39+
return RtlReAllocateHeap(GetHeapHandle(), HEAP_ZERO_MEMORY, block, size);
1640
}
1741
#endif
1842

1943
void __cdecl free(void* block)
2044
{
21-
HANDLE heap = RtlGetCurrentPeb()->ProcessHeap;
22-
RtlFreeHeap(heap, 0, block);
45+
RtlFreeHeap(GetHeapHandle(), 0, block);
2346
}
2447

2548
void* __cdecl calloc(size_t num, size_t size)

payload/crt/minicrt.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <phnt.h>
1+
#include <windows.h>
22
#include <stdlib.h>
33
#include <exception>
44

@@ -65,6 +65,8 @@ void __cdecl _Throw_Cpp_error(int Code)
6565
DebugBreak();
6666
}
6767

68+
#if 0 // TODO: libcpp
69+
6870
_Prhand _Raise_handler = [](const stdext::exception&)
6971
{
7072
DebugBreak();
@@ -81,5 +83,6 @@ _Lockit::_Lockit(int _Kind) noexcept
8183
_Lockit::~_Lockit() noexcept
8284
{
8385
}
86+
#endif
8487

8588
} // namespace std

0 commit comments

Comments
 (0)