Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set(ZLIB_USE_STATIC_LIBS ON)

option(HTTPD_DATADOG_ENABLE_RUM "Enable RUM product" OFF)
option(HTTPD_DATADOG_ENABLE_COVERAGE "Enable code coverage instrumentation" OFF)
option(HTTPD_DATADOG_PATCH_AWAY_LIBC "Patch away libc dependency" OFF)
option(HTTPD_DATADOG_VERIFY_NEEDED "Verify dynamic library dependencies" OFF)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE ReleaseWithDeb)
Expand Down
4 changes: 2 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"HTTPD_SRC_DIR": "/httpd",
"CMAKE_BUILD_TYPE": "Debug",
"HTTPD_DATADOG_ENABLE_COVERAGE": "1",
"HTTPD_DATADOG_PATCH_AWAY_LIBC": "1"
"HTTPD_DATADOG_VERIFY_NEEDED": "1"
}
},
{
Expand All @@ -22,7 +22,7 @@
"CMAKE_TOOLCHAIN_FILE": "/sysroot/x86_64-none-linux-musl/Toolchain.cmake",
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"HTTPD_SRC_DIR": "/httpd",
"HTTPD_DATADOG_PATCH_AWAY_LIBC": "1"
"HTTPD_DATADOG_VERIFY_NEEDED": "1"
}
}
]
Expand Down
19 changes: 7 additions & 12 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ function(split_debug_info target_name)
endif()
endfunction()

function(patch_away_libc target)
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
return()
endif()

find_program(PATCHELF patchelf)
if (PATCHELF STREQUAL "PATCHELF-NOTFOUND")
message(WARNING "Patchelf not found. Can't build glibc + musl binaries")
else()
add_custom_command(TARGET ${target} POST_BUILD
COMMAND patchelf --remove-needed libc.musl-${CMAKE_SYSTEM_PROCESSOR}.so.1 $<TARGET_FILE:${target}>)
endif()
function(verify_needed target)
find_program(READELF readelf REQUIRED)
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DREADELF=${READELF}
-DLIBRARY=$<TARGET_FILE:${target}>
-P ${CMAKE_SOURCE_DIR}/cmake/verify-needed.cmake)
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved
endfunction()
35 changes: 35 additions & 0 deletions cmake/verify-needed.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Check the dynamic dependencies of LIBRARY, so it loads on glibc and musl:
# - It may only depend on the libraries below: glibc provides them, and musl
# maps them to its own libc.
# - It must depend on libc.so.6. Otherwise, libc is likely linked statically.

cmake_minimum_required(VERSION 3.19)

set(allowed_libraries
libc.so.6
libdl.so.2
libm.so.6
libpthread.so.0)

execute_process(
COMMAND "${READELF}" --dynamic "${LIBRARY}"
OUTPUT_VARIABLE dynamic_section
COMMAND_ERROR_IS_FATAL ANY)

# readelf prints one line per dependency, such as:
# 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]

string(REGEX MATCHALL
"Shared library: \\[[^]]+\\]"
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved
needed_entries
"${dynamic_section}")
foreach(entry IN LISTS needed_entries)
string(REGEX REPLACE "Shared library: \\[([^]]+)\\]" "\\1" library "${entry}")
if(NOT library IN_LIST allowed_libraries)
message(FATAL_ERROR "${LIBRARY} needs a library that is not allowed: ${library}")
endif()
endforeach()

if(NOT dynamic_section MATCHES "Shared library: \\[libc\\.so\\.6\\]")
message(FATAL_ERROR "${LIBRARY} does not need libc.so.6: is libc linked statically?")
endif()
4 changes: 2 additions & 2 deletions mod_datadog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_link_options(mod_datadog PRIVATE "-Wl,-exported_symbols_list" "-Wl,${EXPORTED_SYMBOLS}")
endif()

if(HTTPD_DATADOG_PATCH_AWAY_LIBC)
patch_away_libc(mod_datadog)
if(HTTPD_DATADOG_VERIFY_NEEDED)
verify_needed(mod_datadog)
endif()

install(
Expand Down
Loading