From 26b0bef54df70268a8a908a28776ada3dc589e01 Mon Sep 17 00:00:00 2001 From: Luca Guerra Date: Thu, 28 Nov 2024 16:10:45 +0000 Subject: [PATCH] update(build): use elftoolchain/libelf from fork instead of elfutils/libelf Signed-off-by: Luca Guerra --- CMakeLists.txt | 2 + cmake/modules/libbpf.cmake | 37 +++++---- cmake/modules/libelf.cmake | 88 ++------------------- userspace/libpman/CMakeLists.txt | 11 +-- userspace/libscap/CMakeLists.txt | 2 - userspace/libscap/engine/bpf/CMakeLists.txt | 9 ++- userspace/libscap/engine/bpf/scap_bpf.c | 2 +- 7 files changed, 34 insertions(+), 117 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03af22f2b6..971fb017ed 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() +include(libelf) + include(libscap) include(libsinsp) diff --git a/cmake/modules/libbpf.cmake b/cmake/modules/libbpf.cmake index e68b862600..921365f040 100644 --- a/cmake/modules/libbpf.cmake +++ b/cmake/modules/libbpf.cmake @@ -13,42 +13,47 @@ # 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) + include(FetchContent) + FetchContent_MakeAvailable(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(lbpf STATIC IMPORTED) + set_target_properties(lbpf PROPERTIES IMPORTED_LOCATION ${LIBBPF_LIB}) + file(MAKE_DIRECTORY ${LIBBPF_INCLUDE}) # necessary to make target_include_directories() work + target_include_directories(lbpf INTERFACE ${LIBBPF_INCLUDE}) + target_link_libraries(lbpf INTERFACE elf ${ZLIB_LIB}) + message(STATUS "Using bundled libbpf: include'${LIBBPF_INCLUDE}', lib: ${LIBBPF_LIB}") install( FILES "${LIBBPF_LIB}" @@ -56,9 +61,3 @@ else() COMPONENT "libs-deps" ) endif() - -if(NOT TARGET libbpf) - add_custom_target(libbpf) -endif() - -include_directories(${LIBBPF_INCLUDE}) diff --git a/cmake/modules/libelf.cmake b/cmake/modules/libelf.cmake index 3964df6724..38dd841a08 100644 --- a/cmake/modules/libelf.cmake +++ b/cmake/modules/libelf.cmake @@ -13,86 +13,10 @@ # the License. # -option(USE_BUNDLED_LIBELF "Enable building of the bundled libelf" ${USE_BUNDLED_DEPS}) -option(USE_SHARED_LIBELF "When not using bundled libelf, link it dynamically" ON) +include(FetchContent) -if(LIBELF_INCLUDE) - # we already have LIBELF -elseif(NOT USE_BUNDLED_LIBELF) - find_path(LIBELF_INCLUDE elf.h PATH_SUFFIXES elf) - if(BUILD_SHARED_LIBS OR USE_SHARED_LIBELF) - set(LIBELF_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) - else() - set(LIBELF_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) - endif() - # Zig workaround: since it won't look up in /usr/lib/..., add an HINT - if(CMAKE_C_COMPILER MATCHES "zig") - find_library( - LIBELF_LIB - NAMES libelf${LIBELF_LIB_SUFFIX} - HINTS /usr/lib/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/ - ) - else() - find_library(LIBELF_LIB NAMES libelf${LIBELF_LIB_SUFFIX}) - endif() - if(LIBELF_LIB) - # Zig workaround: avoid include whole /usr/include because it would include also system - # glibc headers breaking the build since we are targeting the build against our boostrapped - # zig. - if(CMAKE_C_COMPILER MATCHES "zig") - message(STATUS "Enabling zig workaround for libelf") - configure_file(${LIBELF_INCLUDE}/libelf.h libelf/libelf.h COPYONLY) - configure_file(${LIBELF_INCLUDE}/elf.h libelf/elf.h COPYONLY) - configure_file(${LIBELF_INCLUDE}/gelf.h libelf/gelf.h COPYONLY) - set(LIBELF_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/libelf) - endif() - message(STATUS "Found LIBELF: include: ${LIBELF_INCLUDE}, lib: ${LIBELF_LIB}") - else() - message(FATAL_ERROR "Couldn't find system libelf") - endif() -else() - if(BUILD_SHARED_LIBS) - set(LIBELF_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) - else() - set(LIBELF_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) - endif() - set(LIBELF_CFLAGS "-I${ZLIB_INCLUDE}") - if(ENABLE_PIC) - set(LIBELF_CFLAGS "${LIBELF_CFLAGS} -fPIC") - endif() - get_filename_component(ZLIB_SRC ${ZLIB_LIB} DIRECTORY) - set(LIBELF_SRC "${PROJECT_BINARY_DIR}/libelf-prefix/src") - set(LIBELF_INCLUDE "${LIBELF_SRC}/libelf/libelf") - set(LIBELF_LIB "${LIBELF_SRC}/libelf/libelf/libelf${LIBELF_LIB_SUFFIX}") - ExternalProject_Add( - libelf - PREFIX "${PROJECT_BINARY_DIR}/libelf-prefix" - DEPENDS zlib - URL "https://sourceware.org/elfutils/ftp/0.189/elfutils-0.189.tar.bz2" - URL_HASH "SHA256=39bd8f1a338e2b7cd4abc3ff11a0eddc6e690f69578a57478d8179b4148708c8" - CONFIGURE_COMMAND - ./configure LDFLAGS=-L${ZLIB_SRC} "CFLAGS=${LIBELF_CFLAGS}" - --enable-deterministic-archives --disable-debuginfod --disable-libdebuginfod - --without-zstd - BUILD_IN_SOURCE 1 - BUILD_COMMAND make -C lib libeu.a - COMMAND make -C libelf libelf${LIBELF_LIB_SUFFIX} - INSTALL_COMMAND "" - UPDATE_COMMAND "" - BUILD_BYPRODUCTS ${LIBELF_LIB} - ) - message(STATUS "Using bundled libelf: include'${LIBELF_INCLUDE}', lib: ${LIBELF_LIB}") - install( - FILES "${LIBELF_LIB}" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}" - COMPONENT "libs-deps" - ) -endif() - -# We add a custom target, in this way we can always depend on `libelf` without distinguishing -# between "bundled" and "not-bundled" case -if(NOT TARGET libelf) - add_custom_target(libelf) -endif() - -include_directories(${LIBELF_INCLUDE}) +FetchContent_Declare( + libelf + URL https://github.com/LucaGuerra/elftoolchain/releases/download/libelf-r4053-0/libelf-r4053-0.tar.gz + URL_HASH SHA256=1861e6332d97f9cdc15a0c03b7b478b87abb47408ab41c50bebd9a384937e44e +) diff --git a/userspace/libpman/CMakeLists.txt b/userspace/libpman/CMakeLists.txt index e1799eb302..b5d982b0df 100644 --- a/userspace/libpman/CMakeLists.txt +++ b/userspace/libpman/CMakeLists.txt @@ -15,6 +15,7 @@ add_compile_options(${FALCOSECURITY_LIBS_USERSPACE_COMPILE_FLAGS}) add_link_options(${FALCOSECURITY_LIBS_USERSPACE_LINK_FLAGS}) +include(libbpf) add_library( pman @@ -35,23 +36,15 @@ 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} -) +target_link_libraries(pman PUBLIC scap_event_schema scap_platform lbpf ${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 f7057f83c4..20ac14cdfb 100644 --- a/userspace/libscap/CMakeLists.txt +++ b/userspace/libscap/CMakeLists.txt @@ -171,14 +171,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 7e0e7ac22f..ecdb2430ae 100644 --- a/userspace/libscap/engine/bpf/CMakeLists.txt +++ b/userspace/libscap/engine/bpf/CMakeLists.txt @@ -13,11 +13,12 @@ # 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) +include(FetchContent) +FetchContent_MakeAvailable(libelf) target_link_libraries( - scap_engine_bpf PRIVATE scap_event_schema scap_platform scap_engine_util scap_error - ${LIBELF_LIB} ${ZLIB_LIB} + scap_engine_bpf PRIVATE scap_event_schema scap_platform scap_engine_util scap_error 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 3f7ea6e586..f57ad1b362 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;