diff --git a/bpftime-verifier/test/simple.cpp b/bpftime-verifier/test/simple.cpp index c864e515..fc1b8a6a 100644 --- a/bpftime-verifier/test/simple.cpp +++ b/bpftime-verifier/test/simple.cpp @@ -8,7 +8,7 @@ using namespace bpftime; using namespace verifier; /* a * b / 2 for 32 bit -clang -O2 -target bpf -m32 -c example/bpf/mul.bpf.c -o prog.o +clang -Xlinker --export-dynamic -O2 -target bpf -m32 -c example/bpf/mul.bpf.c -o prog.o */ static const unsigned char bpf_mul_optimized[] = { 0xb7, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, diff --git a/cmake/libbpf.cmake b/cmake/libbpf.cmake index 48da808c..314caedc 100644 --- a/cmake/libbpf.cmake +++ b/cmake/libbpf.cmake @@ -109,7 +109,7 @@ function(add_ebpf_program_target target_name source_file output_file) string(STRIP ${UNAME_ARCH} UNAME_ARCH_STRIPPED) add_custom_command( OUTPUT ${output_file} - COMMAND clang -O2 -target bpf -c -g -D__TARGET_ARCH_${UNAME_ARCH_STRIPPED} -I${CMAKE_SOURCE_DIR}/third_party/vmlinux/${UNAME_ARCH_STRIPPED} -I${LIBBPF_INCLUDE_DIRS}/uapi -I${LIBBPF_INCLUDE_DIRS} ${source_file} -o ${output_file} + COMMAND clang -Xlinker --export-dynamic -O2 -target bpf -c -g -D__TARGET_ARCH_${UNAME_ARCH_STRIPPED} -I${CMAKE_SOURCE_DIR}/third_party/vmlinux/${UNAME_ARCH_STRIPPED} -I${LIBBPF_INCLUDE_DIRS}/uapi -I${LIBBPF_INCLUDE_DIRS} ${source_file} -o ${output_file} DEPENDS ${source_file} ) add_custom_target(${target_name} diff --git a/example/error-inject/Makefile b/example/error-inject/Makefile index e0a00ba9..b4b3b128 100644 --- a/example/error-inject/Makefile +++ b/example/error-inject/Makefile @@ -108,7 +108,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/goroutine/Makefile b/example/goroutine/Makefile index eb497e8c..43cc8fb0 100644 --- a/example/goroutine/Makefile +++ b/example/goroutine/Makefile @@ -104,7 +104,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/bashreadline/Makefile b/example/libbpf-tools/bashreadline/Makefile index 89469598..db872134 100644 --- a/example/libbpf-tools/bashreadline/Makefile +++ b/example/libbpf-tools/bashreadline/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/funclatency/Makefile b/example/libbpf-tools/funclatency/Makefile index ef6a5d17..6971d75c 100644 --- a/example/libbpf-tools/funclatency/Makefile +++ b/example/libbpf-tools/funclatency/Makefile @@ -108,7 +108,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/gethostlatency/Makefile b/example/libbpf-tools/gethostlatency/Makefile index 8ffb069e..e36b77db 100644 --- a/example/libbpf-tools/gethostlatency/Makefile +++ b/example/libbpf-tools/gethostlatency/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/mountsnoop/Makefile b/example/libbpf-tools/mountsnoop/Makefile index c305acf7..96da6cf0 100644 --- a/example/libbpf-tools/mountsnoop/Makefile +++ b/example/libbpf-tools/mountsnoop/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/opensnoop/Makefile b/example/libbpf-tools/opensnoop/Makefile index b1792bcb..1d2b2a2f 100644 --- a/example/libbpf-tools/opensnoop/Makefile +++ b/example/libbpf-tools/opensnoop/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/sigsnoop/Makefile b/example/libbpf-tools/sigsnoop/Makefile index f56d6f9a..4445cc6b 100644 --- a/example/libbpf-tools/sigsnoop/Makefile +++ b/example/libbpf-tools/sigsnoop/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/statsnoop/Makefile b/example/libbpf-tools/statsnoop/Makefile index e44b3fe2..51dba4bb 100644 --- a/example/libbpf-tools/statsnoop/Makefile +++ b/example/libbpf-tools/statsnoop/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/libbpf-tools/syscount/Makefile b/example/libbpf-tools/syscount/Makefile index f34cced2..1dcced02 100644 --- a/example/libbpf-tools/syscount/Makefile +++ b/example/libbpf-tools/syscount/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/malloc/Makefile b/example/malloc/Makefile index ca06b65b..fd86e284 100644 --- a/example/malloc/Makefile +++ b/example/malloc/Makefile @@ -108,7 +108,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/minimal/Makefile b/example/minimal/Makefile index ba07669f..4eaedb7d 100644 --- a/example/minimal/Makefile +++ b/example/minimal/Makefile @@ -108,7 +108,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/opensnoop/Makefile b/example/opensnoop/Makefile index fdb718fb..589f6429 100644 --- a/example/opensnoop/Makefile +++ b/example/opensnoop/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/patch/redis/Makefile b/example/patch/redis/Makefile index 2175edf2..3ae7aff2 100644 --- a/example/patch/redis/Makefile +++ b/example/patch/redis/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/sslsniff/Makefile b/example/sslsniff/Makefile index 145b1ff3..dcbaeac3 100644 --- a/example/sslsniff/Makefile +++ b/example/sslsniff/Makefile @@ -105,7 +105,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/tailcall_minimal/Makefile b/example/tailcall_minimal/Makefile index f2ab52ec..781c7073 100644 --- a/example/tailcall_minimal/Makefile +++ b/example/tailcall_minimal/Makefile @@ -108,7 +108,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/example/usdt_minimal/Makefile b/example/usdt_minimal/Makefile index 4b21d113..b481ab4a 100644 --- a/example/usdt_minimal/Makefile +++ b/example/usdt_minimal/Makefile @@ -108,7 +108,7 @@ $(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPU # Build BPF code $(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) $(call msg,BPF,$@) - $(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) diff --git a/runtime/test/bpf/Makefile b/runtime/test/bpf/Makefile index 02541287..cd76696b 100644 --- a/runtime/test/bpf/Makefile +++ b/runtime/test/bpf/Makefile @@ -64,7 +64,7 @@ $(BPFTOOL): | $(BPFTOOL_OUTPUT) # Build BPF code %.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) - $(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ + $(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.bpf.o,$@) diff --git a/runtime/test/include/test_bpf_progs.h b/runtime/test/include/test_bpf_progs.h index ee0b6ef8..f388aee0 100644 --- a/runtime/test/include/test_bpf_progs.h +++ b/runtime/test/include/test_bpf_progs.h @@ -66,7 +66,7 @@ int mul_test() { int c = a * b; return c; } -in 64 bit: using clang -target bpf -c mul.bpf.c -o mul.bpf.o to compile +in 64 bit: using clang -Xlinker --export-dynamic -target bpf -c mul.bpf.c -o mul.bpf.o to compile */ const unsigned char bpf_mul_64_bit[] = { 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x63, 0x1a, 0xfc, 0xff, @@ -80,7 +80,7 @@ const unsigned char bpf_mul_64_bit[] = { /* a * b / 2 for 32 bit -clang -O2 -target bpf -m32 -c example/bpf/mul.bpf.c -o prog.o +clang -Xlinker --export-dynamic -O2 -target bpf -m32 -c example/bpf/mul.bpf.c -o prog.o */ const unsigned char bpf_mul_optimized[] = { 0xb7, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, diff --git a/tools/aot/example/Makefile b/tools/aot/example/Makefile index fb6c3fac..d2f7c876 100644 --- a/tools/aot/example/Makefile +++ b/tools/aot/example/Makefile @@ -1,5 +1,5 @@ malloc: main.c - clang -O2 -flto main.c do_count.o -o malloc + clang -Xlinker --export-dynamic -O2 -flto main.c do_count.o -o malloc .PHONY: clean clean: diff --git a/vm/llvm-jit/CMakeLists.txt b/vm/llvm-jit/CMakeLists.txt index 47c71c71..05cad9ca 100644 --- a/vm/llvm-jit/CMakeLists.txt +++ b/vm/llvm-jit/CMakeLists.txt @@ -49,6 +49,7 @@ else() llvm_map_components_to_libnames(LLVM_LIBS Core OrcJIT + mcjit Support nativecodegen ) diff --git a/vm/llvm-jit/src/llvm/compiler.cpp b/vm/llvm-jit/src/llvm/compiler.cpp index f2995228..138caeff 100644 --- a/vm/llvm-jit/src/llvm/compiler.cpp +++ b/vm/llvm-jit/src/llvm/compiler.cpp @@ -3,6 +3,7 @@ * Copyright (c) 2022, eunomia-bpf org * All rights reserved. */ +#include "llvm/IR/Argument.h" #include "llvm_jit_context.hpp" #include "ebpf_inst.h" #include "spdlog/spdlog.h" @@ -154,14 +155,17 @@ Expected llvm_bpf_jit_context::generateModule( // The main function Function *bpf_func = Function::Create( FunctionType::get(Type::getInt64Ty(*context), - { Type::getInt8PtrTy(*context), + { llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(*context)), Type::getInt64Ty(*context) }, false), Function::ExternalLinkage, "bpf_main", jitModule.get()); - + // Get args of uint64_t bpf_main(uint64_t, uint64_t) - Argument *mem = bpf_func->getArg(0); - Argument *mem_len = bpf_func->getArg(1); + llvm::Argument* mem = bpf_func->getArg(0); + llvm::Argument* mem_len = bpf_func->getArg(1); + + + std::vector regs; std::vector allBlocks; diff --git a/vm/llvm-jit/src/llvm/llvm_jit_context.cpp b/vm/llvm-jit/src/llvm/llvm_jit_context.cpp index 9265bed7..7534acd3 100644 --- a/vm/llvm-jit/src/llvm/llvm_jit_context.cpp +++ b/vm/llvm-jit/src/llvm/llvm_jit_context.cpp @@ -3,6 +3,141 @@ * Copyright (c) 2022, eunomia-bpf org * All rights reserved. */ + +#ifdef WIN32 +#pragma warning(disable : 4141 4244 4291 4146 4267 4275 4624 4800) +#endif + +#include +#include +#include +#include + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if LLVM_VERSION_MAJOR >= 16 +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#if LLVM_VERSION_MAJOR < 17 +#include +#endif +#include +#include +#include +#include +#include +#include +#include + +#if LLVM_VERSION_MAJOR >= 17 +#include +#include +#include +#include "llvm/LTO/LTOBackend.h" +#include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Analysis/CGSCCPassManager.h" +#include "llvm/Analysis/ModuleSummaryAnalysis.h" +#include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/Bitcode/BitcodeReader.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/LLVMRemarkStreamer.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/PassManager.h" +#include "llvm/IR/Verifier.h" +#include "llvm/LTO/LTO.h" +#include "llvm/MC/TargetRegistry.h" +#include "llvm/Object/ModuleSymbolTable.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" +#include "llvm/Passes/StandardInstrumentations.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Program.h" +#include "llvm/Support/ThreadPool.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/VirtualFileSystem.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/TargetParser/SubtargetFeature.h" +#include "llvm/Transforms/IPO/WholeProgramDevirt.h" +#include "llvm/Transforms/Scalar/LoopPassManager.h" +#include "llvm/Transforms/Utils/FunctionImportUtils.h" +#include "llvm/Transforms/Utils/SplitModule.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ExecutionEngine/GenericValue.h" +#include "llvm/ExecutionEngine/JITEventListener.h" +#include "llvm/ExecutionEngine/MCJIT.h" +#include "llvm/ExecutionEngine/ObjectCache.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/Mangler.h" +#include "llvm/IR/Module.h" +#include "llvm/MC/MCContext.h" +#include "llvm/Object/Archive.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/DynamicLibrary.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SmallVectorMemoryBuffer.h" +#include "llvm/AsmParser/Parser.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/raw_os_ostream.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Transforms/Scalar/IndVarSimplify.h" +#include "llvm/Transforms/Scalar/LICM.h" +#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h" +#include "llvm/Transforms/Scalar/LoopDataPrefetch.h" +#include "llvm/Transforms/Scalar/LoopDeletion.h" +#include "llvm/Transforms/Scalar/LoopDistribute.h" +#include "llvm/Transforms/Scalar/LoopFuse.h" +#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" +#include "llvm/Transforms/Scalar/LoopInstSimplify.h" +#include "llvm/Transforms/Scalar/LoopLoadElimination.h" +#include "llvm/Transforms/Scalar/LoopPassManager.h" +#include "llvm/Transforms/Scalar/LoopPredication.h" +#include "llvm/Transforms/Scalar/LoopRotation.h" +#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" +#include "llvm/Transforms/Scalar/LoopSink.h" +#include "llvm/Transforms/Scalar/LoopStrengthReduce.h" +#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" +#include "llvm/Transforms/Scalar/LoopUnrollPass.h" +#endif + +// Disappears in LLVM 15 +#if LLVM_VERSION_MAJOR >= 14 +#include +#else +#include +#endif + +#if LLVM_VERSION_MAJOR >= 10 +#include +#include +#endif + #include "llvm_jit_context.hpp" #include "bpftime_vm_compat.hpp" #include "compiler_utils.hpp" @@ -40,6 +175,11 @@ using namespace llvm; using namespace llvm::orc; using namespace bpftime; +using namespace std; + + + + struct spin_lock_guard { pthread_spinlock_t *spin; @@ -57,13 +197,46 @@ static ExitOnError ExitOnErr; static void optimizeModule(llvm::Module &M) { - llvm::legacy::PassManager PM; + // std::cout << "LLVM_VERSION_MAJOR: " << LLVM_VERSION_MAJOR << std::endl; + #if LLVM_VERSION_MAJOR >= 17 + // ===================== + // Create the analysis managers. + // These must be declared in this order so that they are destroyed in the + // correct order due to inter-analysis-manager references. + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; + + // Create the new pass manager builder. + // Take a look at the PassBuilder constructor parameters for more + // customization, e.g. specifying a TargetMachine or various debugging + // options. + PassBuilder PB; + + // Register all the basic analyses with the managers. + PB.registerModuleAnalyses(MAM); + PB.registerCGSCCAnalyses(CGAM); + PB.registerFunctionAnalyses(FAM); + PB.registerLoopAnalyses(LAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + + // Create the pass manager. + // This one corresponds to a typical -O2 optimization pipeline. + ModulePassManager MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O3); - llvm::PassManagerBuilder PMB; - PMB.OptLevel = 3; - PMB.populateModulePassManager(PM); + // Optimize the IR! + MPM.run(M, MAM); + // ===================================== + #else + llvm::legacy::PassManager PM; - PM.run(M); + llvm::PassManagerBuilder PMB; + PMB.OptLevel = 3; + PMB.populateModulePassManager(PM); + + PM.run(M); + #endif } #if defined(__arm__) || defined(_M_ARM) @@ -153,14 +326,21 @@ std::vector llvm_bpf_jit_context::do_aot_compile( std::unique_ptr BOS = std::make_unique( objStream); - legacy::PassManager pass; - if (targetMachine->addPassesToEmitFile( - pass, *BOS, nullptr, CGFT_ObjectFile)) { - SPDLOG_ERROR( - "Unable to emit module for target machine"); - throw std::runtime_error( - "Unable to emit module for target machine"); - } + +legacy::PassManager pass; +// auto FileType = CGFT_ObjectFile; +#if LLVM_VERSION_MAJOR >= 18 +if (targetMachine->addPassesToEmitFile(pass, *BOS, nullptr, CodeGenFileType::ObjectFile)) { +#elif LLVM_VERSION_MAJOR >= 10 +if (targetMachine->addPassesToEmitFile(pass, *BOS, nullptr, CGFT_ObjectFile)) { +#elif LLVM_VERSION_MAJOR >= 8 +if (targetMachine->addPassesToEmitFile(pass, *BOS, nullptr, TargetMachine::CGFT_ObjectFile)) { +#else +if (targetMachine->addPassesToEmitFile(pass, *BOS, TargetMachine::CGFT_ObjectFile, true)) { +#endif + SPDLOG_ERROR("Unable to emit module for target machine"); + throw std::runtime_error("Unable to emit module for target machine"); +} pass.run(module); SPDLOG_INFO("AOT: done, received {} bytes", @@ -184,13 +364,21 @@ std::vector llvm_bpf_jit_context::do_aot_compile(bool print_ir) std::vector extNames, lddwNames; for (uint32_t i = 0; i < std::size(vm->ext_funcs); i++) { if (vm->ext_funcs[i].has_value()) { - extNames.push_back(ext_func_sym(i)); + #if LLVM_VERSION_MAJOR >= 16 + extNames.emplace_back(ext_func_sym(i)); + #else + extNames.push_back(ext_func_sym(i)); + #endif } } const auto tryDefineLddwHelper = [&](const char *name, void *func) { if (func) { - lddwNames.push_back(name); + #if LLVM_VERSION_MAJOR >= 16 + lddwNames.emplace_back(name); + #else + lddwNames.push_back(name); + #endif } }; tryDefineLddwHelper(LDDW_HELPER_MAP_BY_FD, (void *)vm->map_by_fd); @@ -245,8 +433,17 @@ llvm_bpf_jit_context::create_and_initialize_lljit_instance() ext_func_sym(i)); sym.setFlags(JITSymbolFlags::Callable | JITSymbolFlags::Exported); + + + #if LLVM_VERSION_MAJOR < 17 extSymbols.try_emplace(symName, sym); extFuncNames.push_back(ext_func_sym(i)); + #else + auto symbol = ::llvm::orc::ExecutorSymbolDef (::llvm::orc::ExecutorAddr (sym.getAddress()), sym.getFlags()); + extSymbols.try_emplace(symName, symbol); + extFuncNames.emplace_back(ext_func_sym(i)); + #endif + } } #if defined(__arm__) || defined(_M_ARM) @@ -264,11 +461,22 @@ llvm_bpf_jit_context::create_and_initialize_lljit_instance() SPDLOG_DEBUG("Defining LDDW helper {} with addr {:x}", name, (uintptr_t)func); auto sym = JITEvaluatedSymbol::fromPointer(func); + // printf("The type of sym %s\n", typeid(sym).name()); sym.setFlags(JITSymbolFlags::Callable | JITSymbolFlags::Exported); - lddwSyms.try_emplace( - jit->getExecutionSession().intern(name), sym); + + + + #if LLVM_VERSION_MAJOR < 17 + lddwSyms.try_emplace(jit->getExecutionSession().intern(name), sym); definedLddwHelpers.push_back(name); + #else + auto symbol = ::llvm::orc::ExecutorSymbolDef (::llvm::orc::ExecutorAddr (sym.getAddress()), sym.getFlags()); + lddwSyms.try_emplace(jit->getExecutionSession().intern(name), symbol); + definedLddwHelpers.emplace_back(name); + #endif + + } }; tryDefineLddwHelper(LDDW_HELPER_MAP_BY_FD, (void *)vm->map_by_fd); @@ -298,4 +506,4 @@ bpftime::vm::compat::precompiled_ebpf_function llvm_bpf_jit_context::get_entry_a SPDLOG_DEBUG("LLVM-JIT: Entry func is {:x}", (uintptr_t)addr); return addr; } -} +} \ No newline at end of file diff --git a/vm/llvm-jit/src/llvm/llvm_jit_context.hpp b/vm/llvm-jit/src/llvm/llvm_jit_context.hpp index 2745267a..fc188738 100644 --- a/vm/llvm-jit/src/llvm/llvm_jit_context.hpp +++ b/vm/llvm-jit/src/llvm/llvm_jit_context.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/vm/test/include/test_bpf_progs.h b/vm/test/include/test_bpf_progs.h index 7905bbd8..97cd4742 100644 --- a/vm/test/include/test_bpf_progs.h +++ b/vm/test/include/test_bpf_progs.h @@ -62,7 +62,7 @@ int mul_test() { int c = a * b; return c; } -in 64 bit: using clang -target bpf -c mul.bpf.c -o mul.bpf.o to compile +in 64 bit: using clang -Xlinker --export-dynamic -target bpf -c mul.bpf.c -o mul.bpf.o to compile */ const unsigned char bpf_mul_64_bit[] = { 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x63, 0x1a, 0xfc, 0xff, @@ -76,7 +76,7 @@ const unsigned char bpf_mul_64_bit[] = { /* a * b / 2 for 32 bit -clang -O2 -target bpf -m32 -c example/bpf/mul.bpf.c -o prog.o +clang -Xlinker --export-dynamic -O2 -target bpf -m32 -c example/bpf/mul.bpf.c -o prog.o */ const unsigned char bpf_mul_optimized[] = { 0xb7, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, diff --git a/vm/tools/compile_code.py b/vm/tools/compile_code.py index e299b4bc..9c38bd8f 100644 --- a/vm/tools/compile_code.py +++ b/vm/tools/compile_code.py @@ -77,9 +77,9 @@ def write_binary(ops): print(pc, li) def compile_code(src): - cmd = f"clang -g -emit-llvm -c {src} -o - | llc -march=bpf -filetype=obj -o code.o" - # cmd = f"clang -O2 -emit-llvm -c {src} -o - | llc -march=bpf -filetype=obj -o code.o" - # cmd = f"clang -O2 -target bpf -c {src} -o {CODEO} " + cmd = f"clang -Xlinker --export-dynamic -g -emit-llvm -c {src} -o - | llc -march=bpf -filetype=obj -o code.o" + # cmd = f"clang -Xlinker --export-dynamic -O2 -emit-llvm -c {src} -o - | llc -march=bpf -filetype=obj -o code.o" + # cmd = f"clang -Xlinker --export-dynamic -O2 -target bpf -c {src} -o {CODEO} " exec_command(cmd, os.getcwd()) if os.path.exists(CODEO): dump_elf_text(CODEO)