Skip to content

Commit

Permalink
Enable BPF shared objects (#2012)
Browse files Browse the repository at this point in the history
* Switch to BPF ELF shared objects (.so)
  • Loading branch information
jackcmay authored Dec 5, 2018
1 parent 209040e commit 0cb5ae4
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 129 deletions.
289 changes: 172 additions & 117 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion programs/native/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ elf = "0.0.10"
env_logger = "0.6.0"
libc = "0.2.44"
log = "0.4.2"
solana_rbpf = "=0.1.4"
solana_rbpf = "=0.1.5"
serde = "1.0.27"
solana-sdk = { path = "../../../sdk", version = "0.11.0" }

Expand Down
2 changes: 1 addition & 1 deletion programs/native/bpf_loader/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::path::PathBuf;
use test::Bencher;

/// BPF program file extension
const PLATFORM_FILE_EXTENSION_BPF: &str = "o";
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
/// Create a BPF program file name
fn create_bpf_path(name: &str) -> PathBuf {
let mut pathbuf = {
Expand Down
20 changes: 20 additions & 0 deletions sdk/bpf/bpf.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
PHDRS
{
text PT_LOAD ;
rodata PT_LOAD ;
dynamic PT_DYNAMIC ;
}

SECTIONS
{
. = SIZEOF_HEADERS;
.text : { *(.text) } :text
.rodata : { *(.rodata) } :rodata
.dynamic : { *(.dynamic) } :dynamic
.dynsym : { *(.dynsym) } :dynamic
.dynstr : { *(.dynstr) } :dynamic
.gnu.hash : { *(.gnu.hash) } :dynamic
.rel.dyn : { *(.rel.dyn) } :dynamic
.hash : { *(.hash) } :dynamic

}
32 changes: 24 additions & 8 deletions sdk/bpf/bpf.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ifdef LLVM_DIR
CC := $(LLVM_DIR)/bin/clang
CXX := $(LLVM_DIR)/bin/clang++
LLC := $(LLVM_DIR)/bin/llc
LLD := $(LLVM_DIR)/bin/ld.lld
OBJ_DUMP := $(LLVM_DIR)/bin/llvm-objdump
endif

Expand All @@ -48,18 +49,28 @@ CXX_FLAGS := \

BPF_C_FLAGS := \
$(C_FLAGS) \
-S \
-emit-llvm \
-target bpf \
-fPIC \

BPF_CXX_FLAGS := \
$(CXX_FLAGS) \
-S \
-emit-llvm \
-target bpf \
-fPIC \

BPF_LLC_FLAGS := \
-march=bpf \
-filetype=obj \

BPF_LLD_FLAGS := \
-z notext \
-shared \
--Bdynamic \
$(LOCAL_PATH)bpf.ld \

OBJ_DUMP_FLAGS := \
-color \
-source \
Expand Down Expand Up @@ -139,22 +150,27 @@ help:
@echo ' - make dump_foo'
@echo ''

.PRECIOUS: $(OUT_DIR)/%.bc
$(OUT_DIR)/%.bc: $(SRC_DIR)/%.c
.PRECIOUS: $(OUT_DIR)/%.ll
$(OUT_DIR)/%.ll: $(SRC_DIR)/%.c
@echo "[cc] $@ ($<)"
$(_@)mkdir -p $(OUT_DIR)
$(_@)$(CC) $(BPF_C_FLAGS) -o $@ -c $< -MD -MF $(@:.bc=.d)
$(_@)$(CC) $(BPF_C_FLAGS) -o $@ -c $< -MD -MF $(@:.ll=.d)

$(OUT_DIR)/%.bc: $(SRC_DIR)/%.cc
$(OUT_DIR)/%.ll: $(SRC_DIR)/%.cc
@echo "[cxx] $@ ($<)"
$(_@)mkdir -p $(OUT_DIR)
$(_@)$(CXX) $(BPF_CXX_FLAGS) -o $@ -c $< -MD -MF $(@:.bc=.d)
$(_@)$(CXX) $(BPF_CXX_FLAGS) -o $@ -c $< -MD -MF $(@:.ll=.d)

.PRECIOUS: $(OUT_DIR)/%.o
$(OUT_DIR)/%.o: $(OUT_DIR)/%.bc
$(OUT_DIR)/%.o: $(OUT_DIR)/%.ll
@echo "[llc] $@ ($<)"
$(_@)$(LLC) $(BPF_LLC_FLAGS) -o $@ $<

.PRECIOUS: $(OUT_DIR)/%.so
$(OUT_DIR)/%.so: $(OUT_DIR)/%.o
@echo "[lld] $@ ($<)"
$(_@)$(LLD) $(BPF_LLD_FLAGS) -o $@ $<

$(OUT_DIR)/test_%: $(TEST_DIR)/%.c
@echo "[test cc] $@ ($<)"
$(_@)mkdir -p $(OUT_DIR)
Expand Down Expand Up @@ -182,11 +198,11 @@ all: $(PROGRAM_NAMES)
test: $(TEST_NAMES)
$(foreach test, $(TEST_NAMES), $(OUT_DIR)/$(test)$(\n))

$(PROGRAM_NAMES): %: $(addprefix $(OUT_DIR)/, %.o) ;
$(PROGRAM_NAMES): %: $(addprefix $(OUT_DIR)/, %.so) ;
$(TEST_NAMES): %: $(addprefix $(OUT_DIR)/, %) ;

dump_%: %
$(_@)$(OBJ_DUMP) $(OBJ_DUMP_FLAGS) $(addprefix $(OUT_DIR)/, $(addsuffix .o, $<))
$(_@)$(OBJ_DUMP) $(OBJ_DUMP_FLAGS) $(addprefix $(OUT_DIR)/, $(addsuffix .so, $<))

clean:
rm -rf $(OUT_DIR)
2 changes: 1 addition & 1 deletion sdk/bpf/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if [[ ! -r criterion-$machine-$version.md ]]; then
fi

# Install LLVM
version=v0.0.4
version=v0.0.5
if [[ ! -f llvm-native-$machine-$version.md ]]; then
(
filename=solana-llvm-$machine.tar.bz2
Expand Down
2 changes: 1 addition & 1 deletion tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::path::PathBuf;

/// BPF program file extension
#[cfg(feature = "bpf_c")]
const PLATFORM_FILE_EXTENSION_BPF: &str = "o";
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
/// Create a BPF program file name
#[cfg(feature = "bpf_c")]
fn create_bpf_path(name: &str) -> PathBuf {
Expand Down

0 comments on commit 0cb5ae4

Please sign in to comment.