From 5ac781fea44451a2c3e8e04c9f4d579b33ac7923 Mon Sep 17 00:00:00 2001 From: RJ Ascani Date: Mon, 22 Jun 2026 14:07:46 -0700 Subject: [PATCH] Don't link libdl when cross-compiling (fixes baremetal -ldl link failure) The top-level CMakeLists.txt links libdl into executorch_core when EXECUTORCH_USE_DL is on (the default): find_library(DL_LIBRARY_EXISTS NAMES dl) if(DL_LIBRARY_EXISTS): target_link_libraries(executorch_core PRIVATE dl) When cross-compiling for a bare-metal target (arm-none-eabi, esp, zephyr), find_library still resolves the *host* libdl, so -ldl is linked into executorch_core and propagates through the exported target interface, failing the bare-metal executor-runner link ("cannot find -ldl"). dladdr() (the only reason for the link) is compiled out on those targets anyway (it's gated by the ET_USE_LIBDL macro, which the CMake build never defines). Two layers: - Guard the link with `NOT CMAKE_CROSSCOMPILING` in CMakeLists.txt, so any cross-compiled target is covered even without a preset. - Set EXECUTORCH_USE_DL OFF explicitly in the arm_baremetal / esp_baremetal / zephyr presets, documenting the intent for those bare-metal builds. Confirmed against the cortex-m FVP runner: with -ldl removed the arm_executor_runner links and runs on Corstone-300. Co-authored-by: Claude --- CMakeLists.txt | 6 +++++- tools/cmake/preset/arm_baremetal.cmake | 2 ++ tools/cmake/preset/esp_baremetal.cmake | 2 ++ tools/cmake/preset/zephyr.cmake | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index abd032e3e30..4abc4ab52f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -519,7 +519,11 @@ target_link_libraries(executorch_core PRIVATE program_schema) if(ANDROID) target_link_libraries(executorch_core PUBLIC log) endif() -if(EXECUTORCH_USE_DL) +# Skip when cross-compiling: find_library() resolves the host libdl even for a +# bare-metal target (e.g. arm-none-eabi), which then links -ldl into the +# baremetal runner and fails ("cannot find -ldl"). dladdr() isn't used on those +# targets anyway. Per-target presets also set EXECUTORCH_USE_DL OFF explicitly. +if(EXECUTORCH_USE_DL AND NOT CMAKE_CROSSCOMPILING) # Check if dl exists for this toolchain and only then link it. find_library(DL_LIBRARY_EXISTS NAMES dl) # Check if the library was found diff --git a/tools/cmake/preset/arm_baremetal.cmake b/tools/cmake/preset/arm_baremetal.cmake index c12cc95233a..cb39de6945e 100644 --- a/tools/cmake/preset/arm_baremetal.cmake +++ b/tools/cmake/preset/arm_baremetal.cmake @@ -29,6 +29,8 @@ set_overridable_option(EXECUTORCH_BUILD_EXECUTOR_RUNNER OFF) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR OFF) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF) set_overridable_option(EXECUTORCH_BUILD_ARM_BAREMETAL ON) +# Bare-metal has no libdl (see CMakeLists.txt EXECUTORCH_USE_DL block). +set_overridable_option(EXECUTORCH_USE_DL OFF) set_overridable_option(EXECUTORCH_BUILD_KERNELS_QUANTIZED ON) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL ON) set_overridable_option(EXECUTORCH_BUILD_CORTEX_M ON) diff --git a/tools/cmake/preset/esp_baremetal.cmake b/tools/cmake/preset/esp_baremetal.cmake index 3df77586d1d..8935b6f2fe7 100644 --- a/tools/cmake/preset/esp_baremetal.cmake +++ b/tools/cmake/preset/esp_baremetal.cmake @@ -7,6 +7,8 @@ set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}") set_overridable_option(EXECUTORCH_BUILD_EXECUTOR_RUNNER OFF) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR OFF) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF) +# Bare-metal has no libdl (see CMakeLists.txt EXECUTORCH_USE_DL block). +set_overridable_option(EXECUTORCH_USE_DL OFF) set_overridable_option(EXECUTORCH_BUILD_KERNELS_QUANTIZED ON) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL ON) set_overridable_option(EXECUTORCH_ENABLE_LOGGING ON) diff --git a/tools/cmake/preset/zephyr.cmake b/tools/cmake/preset/zephyr.cmake index 651e3e0b3c6..69a39bbad71 100644 --- a/tools/cmake/preset/zephyr.cmake +++ b/tools/cmake/preset/zephyr.cmake @@ -6,6 +6,8 @@ set_overridable_option(EXECUTORCH_BUILD_COREML OFF) set_overridable_option(EXECUTORCH_ENABLE_EVENT_TRACER OFF) +# Bare-metal has no libdl (see CMakeLists.txt EXECUTORCH_USE_DL block). +set_overridable_option(EXECUTORCH_USE_DL OFF) set_overridable_option(EXECUTORCH_BUILD_KERNELS_LLM OFF) set_overridable_option(EXECUTORCH_BUILD_KERNELS_LLM_AOT OFF) set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF)