diff --git a/CMakeLists.txt b/CMakeLists.txt index 03af22f2b6c..42235d987e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,6 +121,8 @@ if(BUILD_SHARED_LIBS) message(STATUS "Shared library soversion: ${FALCOSECURITY_SHARED_LIBS_SOVERSION}") endif() +add_subdirectory(contrib/elftoolchain) + include(libscap) include(libsinsp) diff --git a/cmake/modules/libbpf.cmake b/cmake/modules/libbpf.cmake index e68b862600d..b8696061043 100644 --- a/cmake/modules/libbpf.cmake +++ b/cmake/modules/libbpf.cmake @@ -13,42 +13,45 @@ # the License. # -option(USE_BUNDLED_LIBBPF "Enable building of the bundled libbpf" ${USE_BUNDLED_DEPS}) - if(LIBBPF_INCLUDE) # we already have libbpf -elseif(NOT USE_BUNDLED_LIBBPF) - find_path(LIBBPF_INCLUDE bpf/libbpf.h) - find_library(LIBBPF_LIB NAMES bpf) - if(LIBBPF_INCLUDE AND LIBBPF_LIB) - message(STATUS "Found libbpf: include: ${LIBBPF_INCLUDE}, lib: ${LIBBPF_LIB}") - else() - message(FATAL_ERROR "Couldn't find system libbpf") - endif() else() include(zlib) - include(libelf) set(LIBBPF_SRC "${PROJECT_BINARY_DIR}/libbpf-prefix/src") set(LIBBPF_BUILD_DIR "${LIBBPF_SRC}/libbpf-build") set(LIBBPF_INCLUDE "${LIBBPF_BUILD_DIR}/root/usr/include") set(LIBBPF_LIB "${LIBBPF_BUILD_DIR}/root/usr/lib64/libbpf.a") + + get_target_property(LIBELF_INCLUDE_DIR elf INCLUDE_DIRECTORIES) + + foreach(dir ${LIBELF_INCLUDE_DIR}) + string(APPEND LIBELF_COMPILER_STRING "-I${dir} ") + endforeach() + ExternalProject_Add( libbpf PREFIX "${PROJECT_BINARY_DIR}/libbpf-prefix" - DEPENDS zlib libelf + DEPENDS zlib elf URL "https://github.com/libbpf/libbpf/archive/refs/tags/v1.3.0.tar.gz" URL_HASH "SHA256=11db86acd627e468bc48b7258c1130aba41a12c4d364f78e184fd2f5a913d861" CONFIGURE_COMMAND mkdir -p build root BUILD_COMMAND make BUILD_STATIC_ONLY=y OBJDIR=${LIBBPF_BUILD_DIR}/build DESTDIR=${LIBBPF_BUILD_DIR}/root NO_PKG_CONFIG=1 - "EXTRA_CFLAGS=-fPIC -I${LIBELF_INCLUDE} -I${ZLIB_INCLUDE}" "LDFLAGS=-Wl,-Bstatic" + "EXTRA_CFLAGS=-fPIC ${LIBELF_COMPILER_STRING} -I${ZLIB_INCLUDE}" "LDFLAGS=-Wl,-Bstatic" "EXTRA_LDFLAGS=-L${LIBELF_SRC}/libelf/libelf -L${ZLIB_SRC}" -C ${LIBBPF_SRC}/libbpf/src install install_uapi_headers INSTALL_COMMAND "" UPDATE_COMMAND "" BUILD_BYPRODUCTS ${LIBBPF_LIB} ) + + add_library(bpf STATIC IMPORTED) + set_target_properties(bpf PROPERTIES IMPORTED_LOCATION ${LIBBPF_LIB}) + file(MAKE_DIRECTORY ${LIBBPF_INCLUDE}) # necessary to make target_include_directories() work + target_include_directories(bpf INTERFACE ${LIBBPF_INCLUDE}) + target_link_libraries(bpf INTERFACE elf ${ZLIB_LIB}) + message(STATUS "Using bundled libbpf: include'${LIBBPF_INCLUDE}', lib: ${LIBBPF_LIB}") install( FILES "${LIBBPF_LIB}" @@ -57,8 +60,3 @@ else() ) endif() -if(NOT TARGET libbpf) - add_custom_target(libbpf) -endif() - -include_directories(${LIBBPF_INCLUDE}) diff --git a/userspace/libpman/CMakeLists.txt b/userspace/libpman/CMakeLists.txt index e1799eb3021..a2396d39b03 100644 --- a/userspace/libpman/CMakeLists.txt +++ b/userspace/libpman/CMakeLists.txt @@ -35,23 +35,17 @@ target_include_directories( $ # ppm_enum and tables $ # scap-stats struct ${ZLIB_INCLUDE} - ${LIBBPF_INCLUDE} ${MODERN_BPF_SKEL_DIR} - ${LIBELF_INCLUDE} ) target_link_libraries( - pman PUBLIC scap_event_schema scap_platform ${LIBBPF_LIB} ${LIBELF_LIB} ${ZLIB_LIB} + pman PUBLIC scap_event_schema scap_platform bpf ${ZLIB_LIB} ) if(TARGET ProbeSkeleton) add_dependencies(pman ProbeSkeleton) endif() -if(USE_BUNDLED_LIBBPF) - add_dependencies(pman libbpf) -endif() - install( TARGETS pman ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" diff --git a/userspace/libscap/CMakeLists.txt b/userspace/libscap/CMakeLists.txt index 466a24864a7..e4f826f3215 100644 --- a/userspace/libscap/CMakeLists.txt +++ b/userspace/libscap/CMakeLists.txt @@ -170,14 +170,12 @@ if(HAS_ENGINE_KMOD) endif() if(HAS_ENGINE_BPF) - include(libelf) add_subdirectory(engine/bpf) target_link_libraries(scap PUBLIC scap_engine_bpf) target_include_directories(scap_engine_bpf PRIVATE ${PROJECT_BINARY_DIR}/driver/src) endif() if(HAS_ENGINE_MODERN_BPF) - include(libelf) add_subdirectory(engine/modern_bpf) target_link_libraries(scap PUBLIC scap_engine_modern_bpf) target_include_directories(scap_engine_modern_bpf PRIVATE ${PROJECT_BINARY_DIR}/driver/src) diff --git a/userspace/libscap/engine/bpf/CMakeLists.txt b/userspace/libscap/engine/bpf/CMakeLists.txt index 7e0e7ac22fa..33ad99fe4f4 100644 --- a/userspace/libscap/engine/bpf/CMakeLists.txt +++ b/userspace/libscap/engine/bpf/CMakeLists.txt @@ -13,11 +13,10 @@ # the License. # add_library(scap_engine_bpf scap_bpf.c attached_prog.c) -add_dependencies(scap_engine_bpf libelf zlib) +add_dependencies(scap_engine_bpf zlib) target_link_libraries( scap_engine_bpf PRIVATE scap_event_schema scap_platform scap_engine_util scap_error - ${LIBELF_LIB} ${ZLIB_LIB} + elf ${ZLIB_LIB} ) -target_include_directories(scap_engine_bpf PRIVATE ${LIBELF_INCLUDE}) set_scap_target_properties(scap_engine_bpf) diff --git a/userspace/libscap/engine/bpf/scap_bpf.c b/userspace/libscap/engine/bpf/scap_bpf.c index 3f7ea6e586a..f57ad1b3620 100644 --- a/userspace/libscap/engine/bpf/scap_bpf.c +++ b/userspace/libscap/engine/bpf/scap_bpf.c @@ -706,7 +706,7 @@ static int32_t load_bpf_file(struct bpf_engine *handle) { handle->m_filepath); } - handle->elf = elf_begin(handle->program_fd, ELF_C_READ_MMAP_PRIVATE, NULL); + handle->elf = elf_begin(handle->program_fd, ELF_C_READ, NULL); if(!handle->elf) { scap_errprintf(handle->m_lasterr, 0, "can't read ELF format"); goto end;