-
Notifications
You must be signed in to change notification settings - Fork 126
fix(kernel): fix riscv64 static-pie segfault in ELF loader #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2653750
docs: record riscv64 static-pie loader root cause
e2cafba
feat(kernel): add ELF relocation processing for riscv64 static-pie
86403db
feat(kernel): add R_RISCV_64 relocation support for static-pie
2a3a389
fix(kernel): fix riscv64 static-pie segfault in ELF loader
936cfc7
fix(kernel): fix dead_code warning for apply_relocations on non-riscv64
bdf430a
fix(kernel): fix R_RISCV_64 relocation calculation and prebuild.sh er…
e36e94c
ci: retrigger CI run
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # riscv64 static-pie segfault | ||
|
|
||
| ## Reproduction | ||
|
|
||
| - Minimal C program (printf only) | ||
| - Built with `riscv64-linux-musl-gcc -static-pie` | ||
| - ELF Type: DYN (Position-Independent Executable) | ||
| - QEMU on StarryOS riscv64 | ||
| - Result: segfault, RC=139 | ||
|
|
||
| ```c | ||
| #include <stdio.h> | ||
| int main(void) { | ||
| printf("static-pie test OK\n"); | ||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| ## Crash | ||
|
|
||
| ``` | ||
| pc(sepc)=0x00000000000003b0 | ||
| Segmentation fault (core dumped) | ||
| ``` | ||
|
|
||
| ## Root cause | ||
|
|
||
| PC=0x3b0 is the start of `.plt` section, NOT `.text` (0x3f0). | ||
|
|
||
| The binary has unresolved PLT relocations (from `readelf -r`): | ||
|
|
||
| - `.rela.plt`: `R_RISCV_JUMP_SLOT` for `puts` and `__libc_start_main` | ||
| - `.rela.dyn`: `R_RISCV_RELATIVE` entries for data pointers | ||
|
|
||
| The StarryOS ELF loader (`os/StarryOS/kernel/src/mm/loader.rs`) maps segments | ||
| and jumps to entry, but does NOT process `.rela.dyn` or `.rela.plt` relocations. | ||
| The unresolved PLT entries cause a jump to 0x3b0 (PLT stub) which segfaults. | ||
|
|
||
| aarch64/x86_64 static-pie works because their musl CRT handles relocations | ||
| differently, or their PLT stubs happen to work without relocation processing. | ||
|
|
||
| ## Conclusion | ||
|
|
||
| Not a llama.cpp issue. The ELF loader needs relocation processing for | ||
| static-PIE (Type=DYN) binaries. The fix requires: | ||
| 1. Parsing `.rela.dyn` and `.rela.plt` sections | ||
| 2. Applying R_RISCV_RELATIVE and R_RISCV_JUMP_SLOT relocations | ||
| 3. Then jumping to the entry point | ||
|
|
||
| This is a kernel loader enhancement, tracked separately. |
13 changes: 13 additions & 0 deletions
13
apps/starry/static-pie-test/build-riscv64gc-unknown-none-elf.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| env = {AX_IP = "10.0.2.15", AX_GW = "10.0.2.2"} | ||
| features = [ | ||
| "ax-hal/riscv64-qemu-virt", | ||
| "qemu", | ||
| "ax-driver/virtio-blk", | ||
| "ax-driver/virtio-net", | ||
| "ax-driver/virtio-gpu", | ||
| "ax-driver/virtio-input", | ||
| "ax-driver/virtio-socket", | ||
| ] | ||
| log = "Warn" | ||
| plat_dyn = false | ||
| target = "riscv64gc-unknown-none-elf" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| app_dir="${STARRY_APP_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}" | ||
| overlay_dir="${STARRY_OVERLAY_DIR:-}" | ||
|
|
||
| if [[ -z "$overlay_dir" ]]; then | ||
| echo "error: STARRY_OVERLAY_DIR is required" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| install -Dm0755 "$app_dir/static-pie-test.sh" "$overlay_dir/usr/bin/static-pie-test.sh" | ||
|
|
||
| # Compile and copy static-pie test binary | ||
| TOOLCHAIN="/root/project/toolchains/riscv64-linux-musl-cross/bin/riscv64-linux-musl-gcc" | ||
| TEST_SRC="/tmp/opencode/static-pie-test.c" | ||
| TEST_BIN="/tmp/opencode/static-pie-test" | ||
|
|
||
| mkdir -p /tmp/opencode | ||
| cat > "$TEST_SRC" << "CEOF" | ||
| #include <stdio.h> | ||
| int main(void) { | ||
| printf("static-pie test OK\n"); | ||
| return 0; | ||
| } | ||
| CEOF | ||
|
|
||
| if [[ -f "$TOOLCHAIN" ]]; then | ||
| "$TOOLCHAIN" -static -o "$TEST_BIN" "$TEST_SRC" | ||
| install -Dm0755 "$TEST_BIN" "$overlay_dir/usr/bin/static-pie-test" | ||
| echo "Static-pie binary compiled and installed to /usr/bin/static-pie-test" | ||
| else | ||
| echo "Warning: riscv64 toolchain not found, skipping binary" | ||
|
SongShiQ marked this conversation as resolved.
Outdated
|
||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| args = [ | ||
| "-nographic", | ||
| "-m", | ||
| "512M", | ||
| "-cpu", | ||
| "rv64", | ||
| "-device", | ||
| "virtio-blk-pci,drive=disk0", | ||
| "-drive", | ||
| "id=disk0,if=none,format=raw,file=${workspace}/tmp/axbuild/rootfs/rootfs-riscv64-static-pie-test.img", | ||
| "-device", | ||
| "virtio-net-pci,netdev=net0", | ||
| "-netdev", | ||
| "user,id=net0", | ||
| ] | ||
| uefi = false | ||
| to_bin = true | ||
| shell_prefix = "root@starry:" | ||
| shell_init_cmd = "/usr/bin/static-pie-test.sh" | ||
| success_regex = ["(?m)^STATIC_PIE_TEST_PASSED"] | ||
| fail_regex = ["(?i)panic", "(?i)segmentation fault", "(?i)SIGSEGV"] | ||
| timeout = 30 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #!/bin/sh | ||
| echo "BEFORE_BINARY" | ||
| /usr/bin/static-pie-test | ||
| RC=$? | ||
| echo "AFTER_BINARY RC=$RC" | ||
| if [ $RC -eq 0 ]; then | ||
| echo "STATIC_PIE_TEST_PASSED" | ||
| else | ||
| echo "STATIC_PIE_TEST_FAILED: RC=$RC" | ||
| fi |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.