Skip to content
/ clana Public

Solana BPF programs using standard C compiler + sbpf-linker. Zero dependencies, direct syscalls via function pointers.

License

Notifications You must be signed in to change notification settings

Rhovian/clana

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solana BPF Programs with C + sbpf-linker

A minimal template for building Solana BPF programs using standard C and sbpf-linker.

Features

  • ✅ Standard C (C11) with no custom toolchains
  • ✅ Zero external dependencies (no Solana SDK needed)
  • ✅ LLVM bitcode generation via Clang
  • ✅ Direct syscall invocation via function pointers
  • ✅ Automated build pipeline with build.sh
  • ✅ Rust-based integration tests with solana-program-test

Prerequisites

# Install LLVM/Clang (macOS)
brew install llvm

# Install sbpf-linker
cargo install sbpf-linker

# Install Rust for testing
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Building

./build.sh

This generates:

  1. entrypoint.bc - LLVM bitcode from C source
  2. build/program.so - Final Solana program

Testing

cargo test -- --nocapture

Tests will:

  • Build the program
  • Load the .so file into solana-program-test
  • Execute and verify the entrypoint

How It Works

1. Direct Syscalls via Function Pointers

Instead of linking against the Solana SDK, we call syscalls directly:

void (*sol_log_)(const uint8_t *, uint64_t) = (void *)0x207559bd;
sol_log_((const uint8_t *)message, sizeof(message));

The constant 0x207559bd is the syscall hash that Solana VM resolves at runtime.

2. Inline String Data

To prevent sbpf-linker from stripping .rodata, we inline string data:

const char message[] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'};

3. LLVM Bitcode Pipeline

sbpf-linker is an LTO compiler that requires LLVM IR:

clang -target bpfel -O2 -fno-builtin -emit-llvm -c -o entrypoint.bc entrypoint.c
sbpf-linker --cpu v3 --export entrypoint -o program.so entrypoint.bc

Project Structure

.
├── build.sh              # Automated build pipeline
├── Cargo.toml            # Rust test dependencies
├── src/
│   └── entrypoint.c      # Program entrypoint with inline syscalls
└── tests/
    └── integration_test.rs  # Rust integration tests

Deployment

# Build first
./build.sh

# Deploy to devnet
solana program deploy build/program.so --url devnet

# Or deploy to local validator
solana-test-validator &
solana program deploy build/program.so

License

MIT

About

Solana BPF programs using standard C compiler + sbpf-linker. Zero dependencies, direct syscalls via function pointers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published