Skip to content

Commit

Permalink
update(build): use elftoolchain/libelf instead of elfutils/libelf
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra committed Nov 28, 2024
1 parent efc198f commit 6b3ab43
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 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()

add_subdirectory(contrib/elftoolchain)

include(libscap)
include(libsinsp)

Expand Down
34 changes: 16 additions & 18 deletions cmake/modules/libbpf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -57,8 +60,3 @@ else()
)
endif()

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

include_directories(${LIBBPF_INCLUDE})
8 changes: 1 addition & 7 deletions userspace/libpman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,17 @@ 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}
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}"
Expand Down
2 changes: 0 additions & 2 deletions userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions userspace/libscap/engine/bpf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 6b3ab43

Please sign in to comment.