Skip to content

Commit

Permalink
update(build): use elftoolchain/libelf from fork instead of elfutils/…
Browse files Browse the repository at this point in the history
…libelf

Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra committed Nov 29, 2024
1 parent 5094053 commit 26b0bef
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 117 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ if(BUILD_SHARED_LIBS)
message(STATUS "Shared library soversion: ${FALCOSECURITY_SHARED_LIBS_SOVERSION}")
endif()

include(libelf)

include(libscap)
include(libsinsp)

Expand Down
37 changes: 18 additions & 19 deletions cmake/modules/libbpf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,51 @@
# 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}"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}"
COMPONENT "libs-deps"
)
endif()

if(NOT TARGET libbpf)
add_custom_target(libbpf)
endif()

include_directories(${LIBBPF_INCLUDE})
88 changes: 6 additions & 82 deletions cmake/modules/libelf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
11 changes: 2 additions & 9 deletions userspace/libpman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,23 +36,15 @@ target_include_directories(
$<BUILD_INTERFACE:${LIBS_DIR}> # ppm_enum and tables
$<BUILD_INTERFACE:${LIBS_DIR}/userspace> # 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}"
Expand Down
2 changes: 0 additions & 2 deletions userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions userspace/libscap/engine/bpf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion userspace/libscap/engine/bpf/scap_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 26b0bef

Please sign in to comment.