From a19d935d6b9f9af4b14cdf137dc45ae2bea72b6f Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 13 Nov 2026 17:12:32 +0000 Subject: [PATCH 1/5] audio: webrtc: use non-PIC longcalls flags for external LLEXT libs Compile external WebRTC static libraries (AECm, NS, RNNoise, libfvad) with -fno-pic -fno-pie -mlongcalls -mtext-section-literals -DNDEBUG when building relocatable Xtensa LLEXTs. Without these flags, GCC emits R_XTENSA_PLT internal relocations that the Zephyr LLEXT loader cannot resolve against static archive symbols. Signed-off-by: Liam Girdwood --- src/audio/webrtc_aec/webrtc_aec.cmake | 12 +++++++++++- src/audio/webrtc_ns/webrtc_ns.cmake | 12 +++++++++++- src/audio/webrtc_ns2/webrtc_ns2.cmake | 12 +++++++++++- src/audio/webrtc_vad/webrtc_vad.cmake | 12 +++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/audio/webrtc_aec/webrtc_aec.cmake b/src/audio/webrtc_aec/webrtc_aec.cmake index 13e56d89af22..b7648f891e16 100644 --- a/src/audio/webrtc_aec/webrtc_aec.cmake +++ b/src/audio/webrtc_aec/webrtc_aec.cmake @@ -33,6 +33,16 @@ set(WEBRTC_AECM_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-install" set(_aecm_src "${SOF_WEBRTC_APM_SRC_DIR}/webrtc") +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_aecm_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_aecm_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_aecm_arch_cflags "-fPIC") +endif() + if(CONFIG_WEBRTC_AEC_BACKEND_FLOAT) set(_aec_backend_srcs " $SRC/modules/audio_processing/aec/echo_cancellation.c @@ -58,7 +68,7 @@ OBJ=${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-obj rm -rf \"$OBJ\" \"$INST/lib/libwebrtc_aecm.a\" mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" \"$INST/include/webrtc\" -CFLAGS=\"-O2 -fPIC -DNDEBUG -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC -I$SRC/common_audio/signal_processing/include -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -Dmalloc=webrtc_malloc -Dcalloc=webrtc_calloc -Drealloc=webrtc_realloc -Dfree=webrtc_free\" +CFLAGS=\"-O2 ${_aecm_arch_cflags} -DNDEBUG -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC -I$SRC/common_audio/signal_processing/include -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -Dmalloc=webrtc_malloc -Dcalloc=webrtc_calloc -Drealloc=webrtc_realloc -Dfree=webrtc_free\" # AEC backend core SRCS=\"${_aec_backend_srcs} diff --git a/src/audio/webrtc_ns/webrtc_ns.cmake b/src/audio/webrtc_ns/webrtc_ns.cmake index fb39b40e1793..771001fc8427 100644 --- a/src/audio/webrtc_ns/webrtc_ns.cmake +++ b/src/audio/webrtc_ns/webrtc_ns.cmake @@ -37,6 +37,16 @@ set(WEBRTC_NS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-install" set(_ns_src "${SOF_WEBRTC_APM_SRC_DIR}/webrtc") +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_ns_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_ns_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_ns_arch_cflags "-fPIC") +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-build.sh" "#!/bin/sh set -e @@ -46,7 +56,7 @@ OBJ=${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-obj mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" -CFLAGS=\"-O2 -fPIC -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC/modules/audio_processing/ns/include -Dmalloc=webrtc_ns_malloc -Dcalloc=webrtc_ns_calloc -Dfree=webrtc_ns_free\" +CFLAGS=\"-O2 ${_ns_arch_cflags} -DNDEBUG -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC/modules/audio_processing/ns/include -Dmalloc=webrtc_ns_malloc -Dcalloc=webrtc_ns_calloc -Dfree=webrtc_ns_free\" SRCS=\" $SRC/modules/audio_processing/ns/noise_suppression.c diff --git a/src/audio/webrtc_ns2/webrtc_ns2.cmake b/src/audio/webrtc_ns2/webrtc_ns2.cmake index b9c78c55e6ca..d6bd7dda5790 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.cmake +++ b/src/audio/webrtc_ns2/webrtc_ns2.cmake @@ -34,6 +34,16 @@ set(WEBRTC_NS2_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-install" set(_rnn_src "${SOF_RNNOISE_SRC_DIR}/src") set(_rnn_inc "${SOF_RNNOISE_SRC_DIR}/include") +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_rnn_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_rnn_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_rnn_arch_cflags "-fPIC") +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-build.sh" "#!/bin/sh set -e @@ -44,7 +54,7 @@ OBJ=${CMAKE_CURRENT_BINARY_DIR}/rnnoise-obj mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" -CFLAGS=\"-O2 -fPIC -I$SRC -I$INC -DOUTSIDE_SPEEX -DRANDOM_PREFIX=rnn -DFLOAT_APPROX\" +CFLAGS=\"-O2 ${_rnn_arch_cflags} -DNDEBUG -I$SRC -I$INC -DOUTSIDE_SPEEX -DRANDOM_PREFIX=rnn -DFLOAT_APPROX\" # Core RNNoise sources (excludes dump_features.c and train-only files). SRCS=\" diff --git a/src/audio/webrtc_vad/webrtc_vad.cmake b/src/audio/webrtc_vad/webrtc_vad.cmake index d0124c7ded5f..49afd97c3cb4 100644 --- a/src/audio/webrtc_vad/webrtc_vad.cmake +++ b/src/audio/webrtc_vad/webrtc_vad.cmake @@ -39,6 +39,16 @@ set(LIBFVAD_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad-install" # Rather than invoking its CMakeLists (which would need a CMake toolchain file), # we compile the objects directly and archive them. This mirrors the libshine # approach in ffmpeg.cmake. +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_fvad_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_fvad_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_fvad_arch_cflags "-fPIC") +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/fvad-build.sh" "#!/bin/sh set -e @@ -66,7 +76,7 @@ SRCS=\" for f in \$SRCS; do bn=\$(basename \"\$f\" .c) - ${CMAKE_C_COMPILER} -O2 -fPIC \ + ${CMAKE_C_COMPILER} -O2 ${_fvad_arch_cflags} -DNDEBUG \ -I$SRC/include \ -I$SRC/src \ -c \"\$f\" -o \"$OBJ/\${bn}.o\" From 87765f6a185adbef0d0bee565af0c48093d6628b Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 20 Nov 2026 18:50:48 +0000 Subject: [PATCH 2/5] audio: webrtc: handle Zephyr LL_EXTENSION_BUILD macro in module shims Zephyr sets LL_EXTENSION_BUILD rather than LLEXT_MODULE when compiling relocatable LLEXT sources; check both macros in the WebRTC shim files and in webrtc_vad-fvad.c's webrtc_vad_set_active_mod() declaration so active_vad_mod, malloc/free wrappers, and non-userspace BSS placement match the build mode. Also remove the unused include and k_panic() call from webrtc_aec-shims.c. Signed-off-by: Liam Girdwood --- src/audio/webrtc_ns/webrtc_ns-shims.c | 4 ++-- src/audio/webrtc_ns2/webrtc_ns2-shims.c | 6 +++--- src/audio/webrtc_vad/webrtc_vad-fvad.c | 2 +- src/audio/webrtc_vad/webrtc_vad-shims.c | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/audio/webrtc_ns/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c index 4f5b91362079..ecbe56b7858e 100644 --- a/src/audio/webrtc_ns/webrtc_ns-shims.c +++ b/src/audio/webrtc_ns/webrtc_ns-shims.c @@ -13,7 +13,7 @@ #include #include #include -#if !defined(LLEXT_MODULE) +#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD) #include static APP_SYSUSER_BSS struct processing_module *active_ns_mod; #else @@ -60,7 +60,7 @@ void *webrtc_ns_calloc(size_t nmemb, size_t size) return p; } -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) /* ============================ Memory ============================ */ void *malloc(size_t size) diff --git a/src/audio/webrtc_ns2/webrtc_ns2-shims.c b/src/audio/webrtc_ns2/webrtc_ns2-shims.c index e6064d31e0e6..944c214c583a 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2-shims.c +++ b/src/audio/webrtc_ns2/webrtc_ns2-shims.c @@ -9,7 +9,7 @@ #include #include #include -#if !defined(LLEXT_MODULE) +#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD) #include static APP_SYSUSER_BSS struct processing_module *active_ns2_mod; #else @@ -56,7 +56,7 @@ void *webrtc_ns2_calloc(size_t nmemb, size_t size) return p; } -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) /* ============================ Memory ============================ */ void *malloc(size_t size) @@ -285,4 +285,4 @@ double exp(double x) { return (double)expf((float)x); } -#endif /* LLEXT_MODULE */ +#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ diff --git a/src/audio/webrtc_vad/webrtc_vad-fvad.c b/src/audio/webrtc_vad/webrtc_vad-fvad.c index bfb9b3f7cdbc..ae7ea47075fd 100644 --- a/src/audio/webrtc_vad/webrtc_vad-fvad.c +++ b/src/audio/webrtc_vad/webrtc_vad-fvad.c @@ -29,7 +29,7 @@ LOG_MODULE_DECLARE(webrtc_vad, CONFIG_SOF_LOG_LEVEL); -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) void webrtc_vad_set_active_mod(struct processing_module *mod); #else static inline void webrtc_vad_set_active_mod(struct processing_module *mod) { (void)mod; } diff --git a/src/audio/webrtc_vad/webrtc_vad-shims.c b/src/audio/webrtc_vad/webrtc_vad-shims.c index b8ac6860a2c5..52346cca6de3 100644 --- a/src/audio/webrtc_vad/webrtc_vad-shims.c +++ b/src/audio/webrtc_vad/webrtc_vad-shims.c @@ -17,7 +17,7 @@ #include #include #include -#if !defined(LLEXT_MODULE) +#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD) #include static APP_SYSUSER_BSS struct processing_module *active_vad_mod; #else @@ -29,7 +29,7 @@ void webrtc_vad_set_active_mod(struct processing_module *mod) active_vad_mod = mod; } -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) /* ============================ Memory ============================ */ void *malloc(size_t size) @@ -110,4 +110,4 @@ int memcmp(const void *s1, const void *s2, size_t n) } return 0; } -#endif /* LLEXT_MODULE */ +#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ From 3f5fdd2d2c3f2f238076a3d1414eb618687e66b1 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 13 Nov 2026 17:12:32 +0000 Subject: [PATCH 3/5] audio: webrtc_ns, webrtc_ns2: add weak soft-float math shims Provide __attribute__((weak)) soft-float logf, log10f, expf, cosf, sinf, and atanf shims compiled unconditionally so both relocatable LLEXT and monolithic builds (e.g. CAVS 2.5 where CONFIG_LIBRARY_MANAGER=n) link without an external libm or duplicate symbol collisions. Implement log2f and exp2f via range reduction into [sqrt(2)/2, sqrt(2)] and [-0.5, 0.5] so inputs across the full positive domain stay accurate and continuous. Signed-off-by: Liam Girdwood --- src/audio/webrtc_ns/webrtc_ns-shims.c | 214 ++++++++++++++++++++++-- src/audio/webrtc_ns2/webrtc_ns2-shims.c | 73 ++++---- 2 files changed, 241 insertions(+), 46 deletions(-) diff --git a/src/audio/webrtc_ns/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c index ecbe56b7858e..c19045f8fbc5 100644 --- a/src/audio/webrtc_ns/webrtc_ns-shims.c +++ b/src/audio/webrtc_ns/webrtc_ns-shims.c @@ -124,42 +124,51 @@ void *memmove(void *dest, const void *src, size_t n) */ -float fabsf(float x) +__attribute__((weak)) float fabsf(float x) { return (x < 0.0f) ? -x : x; } -double fabs(double x) +__attribute__((weak)) double fabs(double x) { return (x < 0.0) ? -x : x; } static inline float ns_exp2f(float x) { - if (x > 127.0f) return 3.40282347e+38f; - if (x < -126.0f) return 0.0f; - int n = (int)x; - if (x < 0.0f && (float)n != x) n--; - float f = x - (float)n; - float p = 1.0f + f * (0.6931472f + f * (0.2402265f + f * 0.0555041f)); union { float f; uint32_t i; } u; - if (n < -126) return 0.0f; - if (n > 127) n = 127; + float f, p; + int n; + + if (x >= 127.0f) + return 3.40282347e+38f; + if (x < -126.0f) + return 0.0f; + + n = (int)(x >= 0.0f ? x + 0.5f : x - 0.5f); + if (n > 127) + return 3.40282347e+38f; + if (n < -126) + return 0.0f; + + f = (x - (float)n) * 0.69314718f; + p = 1.0f + f * (1.0f + f * (0.5f + f * (0.16666667f + + f * (0.04166667f + f * (0.00833333f + f * 0.00138889f))))); u.i = (uint32_t)(n + 127) << 23; return p * u.f; } -float expf(float x) +__attribute__((weak)) float expf(float x) { return ns_exp2f(x * 1.4426950408889634f); } -double exp(double x) +__attribute__((weak)) double exp(double x) { return (double)expf((float)x); } -double tanh(double x) +__attribute__((weak)) double tanh(double x) { if (x > 10.0) return 1.0; if (x < -10.0) return -1.0; @@ -167,10 +176,179 @@ double tanh(double x) return (e2 - 1.0) / (e2 + 1.0); } -float atanf(float x) +__attribute__((weak)) float atanf(float x) +{ + float ax = fabsf(x); + float base = 0.0f; + float u, u2, res; + + if (ax > 2.41421356f) { + base = 1.5707963267948966f; + u = -1.0f / ax; + } else if (ax > 0.41421356f) { + base = 0.7853981633974483f; + u = (ax - 1.0f) / (ax + 1.0f); + } else { + u = ax; + } + + u2 = u * u; + res = base + u * (1.0f + u2 * (-0.33333333f + u2 * (0.20000000f + + u2 * (-0.14285714f + u2 * 0.11111111f)))); + return (x < 0.0f) ? -res : res; +} + +static float ns_log2f(float x) +{ + union { float f; uint32_t i; } u = { .f = x }; + int e, e_bias = 127; + float m, r, s, poly; + + if (x <= 0.0f) + return -127.0f; + + if (u.i < 0x00800000U) { + x *= 16777216.0f; + u.f = x; + e_bias = 151; + } + + e = (int)((u.i >> 23) & 0xff) - e_bias; + u.i = (u.i & 0x007fffff) | 0x3f800000; + m = u.f; + if (m > 1.4142135623730951f) { + m *= 0.5f; + e++; + } + + r = (m - 1.0f) / (m + 1.0f); + s = r * r; + poly = 1.0f + s * (0.33333333f + s * (0.20000000f + + s * (0.14285714f + s * 0.11111111f))); + + return (float)e + (2.8853900817779268f * r) * poly; +} + +__attribute__((weak)) float logf(float x) +{ + return ns_log2f(x) * 0.6931471805599453f; +} + +__attribute__((weak)) double log(double x) +{ + return (double)logf((float)x); +} + +__attribute__((weak)) float sqrtf(float x) +{ + if (x <= 0.0f) + return 0.0f; + return __builtin_sqrtf(x); +} + +__attribute__((weak)) double sqrt(double x) +{ + return (double)sqrtf((float)x); +} + +__attribute__((weak)) float powf(float x, float y) +{ + if (x <= 0.0f) + return (x == 0.0f && y > 0.0f) ? 0.0f : 1.0f; + if (y == 0.0f) + return 1.0f; + if (y == 1.0f) + return x; + if (y == 2.0f) + return x * x; + if (y == 0.5f) + return sqrtf(x); + return ns_exp2f(y * ns_log2f(x)); +} + +static inline float ns_floorf(float x) +{ + int i = (int)x; + + if (x < 0.0f && (float)i != x) + i--; + return (float)i; +} + +__attribute__((weak)) float floorf(float x) +{ + return ns_floorf(x); +} + +__attribute__((weak)) double floor(double x) +{ + return (double)ns_floorf((float)x); +} + +static inline float ns_ceilf(float x) +{ + int i = (int)x; + + if (x > 0.0f && (float)i != x) + i++; + return (float)i; +} + +static inline float ns_roundf(float x) +{ + return (x >= 0.0f) ? ns_floorf(x + 0.5f) : ns_ceilf(x - 0.5f); +} + +static inline void ns_sincosf_poly(float z, float *s_out, float *c_out) +{ + float z2 = z * z; + + *s_out = z * (1.0f + z2 * (-0.166666667f + z2 * (0.008333333f - z2 * 0.0001984127f))); + *c_out = 1.0f + z2 * (-0.5f + z2 * (0.041666667f + z2 * (-0.0013888889f + z2 * 0.0000248016f))); +} + +__attribute__((weak)) float sinf(float x) +{ + const float inv_half_pi = 0.636619772367581343f; + const float half_pi_hi = 1.5707963267948966f; + int k = (int)ns_roundf(x * inv_half_pi); + float z = x - (float)k * half_pi_hi; + float s, c; + + ns_sincosf_poly(z, &s, &c); + + switch ((k % 4 + 4) % 4) { + case 0: return s; + case 1: return c; + case 2: return -s; + case 3: default: return -c; + } +} + +__attribute__((weak)) double sin(double x) +{ + return (double)sinf((float)x); +} + +__attribute__((weak)) float cosf(float x) +{ + const float inv_half_pi = 0.636619772367581343f; + const float half_pi_hi = 1.5707963267948966f; + int k = (int)ns_roundf(x * inv_half_pi); + float z = x - (float)k * half_pi_hi; + float s, c; + + ns_sincosf_poly(z, &s, &c); + + switch ((k % 4 + 4) % 4) { + case 0: return c; + case 1: return -s; + case 2: return -c; + case 3: default: return s; + } +} + +__attribute__((weak)) double cos(double x) { - if (x == 1.0f) - return 0.7853981633974483f; - float x2 = x * x; - return x * (1.0f + 0.28f * x2) / (1.0f + 0.613f * x2); + return (double)cosf((float)x); } diff --git a/src/audio/webrtc_ns2/webrtc_ns2-shims.c b/src/audio/webrtc_ns2/webrtc_ns2-shims.c index 944c214c583a..9fe0ad0a5558 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2-shims.c +++ b/src/audio/webrtc_ns2/webrtc_ns2-shims.c @@ -111,6 +111,7 @@ void *memmove(void *dest, const void *src, size_t n) } return dest; } +#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ /* ============================ Math ============================== */ @@ -129,31 +130,45 @@ static inline float ns2_ldexp2(int n) static float ns2_log2f(float x) { union { float f; uint32_t i; } u = { .f = x }; - int e; - float m, z; + int e, e_bias = 127; + float m, r, s, poly; if (x <= 0.0f) return -127.0f; - e = (int)((u.i >> 23) & 0xff) - 127; + if (u.i < 0x00800000U) { + x *= 16777216.0f; + u.f = x; + e_bias = 151; + } + + e = (int)((u.i >> 23) & 0xff) - e_bias; u.i = (u.i & 0x007fffff) | 0x3f800000; m = u.f; - z = m - 1.0f; + if (m > 1.4142135623730951f) { + m *= 0.5f; + e++; + } + + r = (m - 1.0f) / (m + 1.0f); + s = r * r; + poly = 1.0f + s * (0.33333333f + s * (0.20000000f + + s * (0.14285714f + s * 0.11111111f))); - return (float)e + z * (1.44269504f - z * (0.72134752f - z * 0.48089834f)); + return (float)e + (2.8853900817779268f * r) * poly; } -float log10f(float x) +__attribute__((weak)) float log10f(float x) { return ns2_log2f(x) * 0.3010299956639812f; } -double log10(double x) +__attribute__((weak)) double log10(double x) { return (double)log10f((float)x); } -float floorf(float x) +__attribute__((weak)) float floorf(float x) { int i = (int)x; @@ -162,12 +177,12 @@ float floorf(float x) return (float)i; } -double floor(double x) +__attribute__((weak)) double floor(double x) { return (double)floorf((float)x); } -float ceilf(float x) +__attribute__((weak)) float ceilf(float x) { int i = (int)x; @@ -181,24 +196,24 @@ static inline float ns2_roundf(float x) return (x >= 0.0f) ? floorf(x + 0.5f) : ceilf(x - 0.5f); } -float fabsf(float x) +__attribute__((weak)) float fabsf(float x) { return (x < 0.0f) ? -x : x; } -double fabs(double x) +__attribute__((weak)) double fabs(double x) { return (x < 0.0) ? -x : x; } -float sqrtf(float x) +__attribute__((weak)) float sqrtf(float x) { if (x <= 0.0f) return 0.0f; return __builtin_sqrtf(x); } -double sqrt(double x) +__attribute__((weak)) double sqrt(double x) { return (double)sqrtf((float)x); } @@ -211,7 +226,7 @@ static inline void ns2_sincosf_poly(float z, float *s_out, float *c_out) *c_out = 1.0f + z2 * (-0.5f + z2 * (0.041666667f + z2 * (-0.0013888889f + z2 * 0.0000248016f))); } -float sinf(float x) +__attribute__((weak)) float sinf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -229,12 +244,12 @@ float sinf(float x) } } -double sin(double x) +__attribute__((weak)) double sin(double x) { return (double)sinf((float)x); } -float cosf(float x) +__attribute__((weak)) float cosf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -252,37 +267,39 @@ float cosf(float x) } } -double cos(double x) +__attribute__((weak)) double cos(double x) { return (double)cosf((float)x); } -float exp2f(float x) +__attribute__((weak)) float exp2f(float x) { int n; float f, p; - if (x > 127.0f) + if (x >= 127.0f) return 3.40282347e+38f; if (x < -126.0f) return 0.0f; - n = (int)x; - if (x < 0.0f && (float)n != x) - n--; - f = x - (float)n; + n = (int)(x >= 0.0f ? x + 0.5f : x - 0.5f); + if (n > 127) + return 3.40282347e+38f; + if (n < -126) + return 0.0f; - p = 1.0f + f * (0.6931472f + f * (0.2402265f + f * 0.0555041f)); + f = (x - (float)n) * 0.69314718f; + p = 1.0f + f * (1.0f + f * (0.5f + f * (0.16666667f + + f * (0.04166667f + f * (0.00833333f + f * 0.00138889f))))); return p * ns2_ldexp2(n); } -float expf(float x) +__attribute__((weak)) float expf(float x) { return exp2f(x * 1.4426950408889634f); } -double exp(double x) +__attribute__((weak)) double exp(double x) { return (double)expf((float)x); } -#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ From 7b7e949df32e232300b094a6b92553a875c08e8e Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sun, 15 Nov 2026 15:29:28 +0000 Subject: [PATCH 4/5] lib: notifier: expose notifier_event as validated Zephyr syscall When CONFIG_USERSPACE is enabled, webrtc_vad runs in a user thread and invokes notifier_event() on VAD state changes. Instead of exporting raw notifier_event() directly to LLEXT/userspace (where host_notify state is not partitioned for user access), expose notifier_event() via a Zephyr __syscall wrapper. In z_vrfy_notifier_event(), restrict userspace delivery to NOTIFIER_ID_VAD on the local core only, verify the exact int payload size, and copy the VAD state into kernel stack storage via k_usermode_from_copy() before invoking z_impl_notifier_event(). Signed-off-by: Liam Girdwood --- src/include/sof/lib/notifier.h | 13 ++++++++++-- src/lib/notifier.c | 5 +++-- zephyr/CMakeLists.txt | 3 +++ zephyr/syscall/notifier.c | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 zephyr/syscall/notifier.c diff --git a/src/include/sof/lib/notifier.h b/src/include/sof/lib/notifier.h index e5413a512cb4..cf93ab359d55 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -88,8 +88,17 @@ void notifier_notify_remote(void); /* data_size is required to manage cache coherency for notifications * across cores. */ -void notifier_event(const void *caller_id, enum notify_id event_type, uint32_t core_mask, - void *caller_data, uint32_t data_size); +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#include + +__syscall void notifier_event(const void *caller_id, enum notify_id event_type, + uint32_t core_mask, void *caller_data, uint32_t data_size); +#include +#else +void z_impl_notifier_event(const void *caller_id, enum notify_id event_type, + uint32_t core_mask, void *caller_data, uint32_t data_size); +#define notifier_event z_impl_notifier_event +#endif void init_system_notify(struct sof *sof); diff --git a/src/lib/notifier.c b/src/lib/notifier.c index cbc9ff5cd7f8..acff1bf46a09 100644 --- a/src/lib/notifier.c +++ b/src/lib/notifier.c @@ -145,8 +145,8 @@ void notifier_notify_remote(void) } -void notifier_event(const void *caller, enum notify_id type, uint32_t core_mask, - void *data, uint32_t data_size) +void z_impl_notifier_event(const void *caller, enum notify_id type, uint32_t core_mask, + void *data, uint32_t data_size) { struct notify_data *notify_data; struct idc_msg notify_msg = { IDC_MSG_NOTIFY, IDC_MSG_NOTIFY_EXT }; @@ -177,6 +177,7 @@ void notifier_event(const void *caller, enum notify_id type, uint32_t core_mask, } } } +EXPORT_SYMBOL(z_impl_notifier_event); __cold void init_system_notify(struct sof *sof) { diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index ed9b620aba4a..4465db074b40 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -641,6 +641,9 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE syscall/dai.c) zephyr_syscall_header(${SOF_SRC_PATH}/include/user/debug_stream_slot.h) +zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/lib/notifier.h) +zephyr_library_sources_ifdef(CONFIG_USERSPACE syscall/notifier.c) + zephyr_syscall_header(${SOF_SRC_PATH}/include/ipc4/notification.h) zephyr_library_link_libraries(SOF) diff --git a/zephyr/syscall/notifier.c b/zephyr/syscall/notifier.c new file mode 100644 index 000000000000..d071dad4c73e --- /dev/null +++ b/zephyr/syscall/notifier.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. + +#include +#include +#include +#include + +/** + * \brief Userspace verification wrapper for notifier_event(). + * + * Validates the notification type and verifies read access to the caller + * payload buffer before delegating dispatch to supervisor context so user + * threads never dereference kernel-only host_notify state. + */ +static inline void z_vrfy_notifier_event(const void *caller_id, + enum notify_id event_type, + uint32_t core_mask, + void *caller_data, + uint32_t data_size) +{ + int vad_state; + + /* + * Restrict userspace notifications to explicit, fixed-payload events + * dispatched synchronously on the local core so privileged kernel + * events and cross-core pointers to user memory cannot escape. + */ + K_OOPS(K_SYSCALL_VERIFY(event_type == NOTIFIER_ID_VAD)); + K_OOPS(K_SYSCALL_VERIFY((core_mask & ~NOTIFIER_TARGET_CORE_LOCAL) == 0)); + K_OOPS(K_SYSCALL_VERIFY(caller_data != NULL && data_size == sizeof(vad_state))); + K_OOPS(k_usermode_from_copy(&vad_state, caller_data, sizeof(vad_state))); + + z_impl_notifier_event(caller_id, event_type, core_mask, + &vad_state, sizeof(vad_state)); +} +#include From 8ba8bd424e6f4554cb80120a1f26e73a87a4dfe1 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sun, 22 Nov 2026 19:01:33 +0000 Subject: [PATCH 5/5] audio: webrtc: build all WebRTC modules as standalone LLEXTs Drop 'LIB openmodules' from all six WebRTC module LLEXT builds (webrtc_aec, webrtc_agc, webrtc_hpf, webrtc_ns, webrtc_ns2, webrtc_vad) so each builds and signs as an individual .llext with its .bin symlink instead of being bundled inside sof--openmodules.ri. Each standalone module keeps sequential module_size VMA advancement so multiple WebRTC stages (e.g. HPF -> AEC -> NS/NS2 -> AGC -> VAD) can load and run concurrently in the same capture pipeline (total 0xbc000 on PTL relocatable). Signed-off-by: Liam Girdwood --- src/audio/webrtc_aec/llext/CMakeLists.txt | 2 -- src/audio/webrtc_agc/llext/CMakeLists.txt | 2 -- src/audio/webrtc_hpf/llext/CMakeLists.txt | 2 -- src/audio/webrtc_ns/llext/CMakeLists.txt | 2 -- src/audio/webrtc_ns2/llext/CMakeLists.txt | 2 -- src/audio/webrtc_vad/llext/CMakeLists.txt | 2 -- 6 files changed, 12 deletions(-) diff --git a/src/audio/webrtc_aec/llext/CMakeLists.txt b/src/audio/webrtc_aec/llext/CMakeLists.txt index 68632b2498f3..795a80f28ec4 100644 --- a/src/audio/webrtc_aec/llext/CMakeLists.txt +++ b/src/audio/webrtc_aec/llext/CMakeLists.txt @@ -5,7 +5,6 @@ if(CONFIG_COMP_WEBRTC_AEC_STUB) sof_llext_build("webrtc_aec" SOURCES ../webrtc_aec.c ../webrtc_aec-stub.c - LIB openmodules ) else() include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_aec.cmake) @@ -25,7 +24,6 @@ else() "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/common_audio/signal_processing/include" LIBS_PATH "${WEBRTC_AECM_INSTALL_DIR}/lib" LIBS webrtc_aecm - LIB openmodules ) add_dependencies(webrtc_aec_llext_lib webrtc_aecm_ext) diff --git a/src/audio/webrtc_agc/llext/CMakeLists.txt b/src/audio/webrtc_agc/llext/CMakeLists.txt index 17c94f8e7776..efa84076445d 100644 --- a/src/audio/webrtc_agc/llext/CMakeLists.txt +++ b/src/audio/webrtc_agc/llext/CMakeLists.txt @@ -5,7 +5,6 @@ if(CONFIG_COMP_WEBRTC_AGC_STUB) sof_llext_build("webrtc_agc" SOURCES ../webrtc_agc.c ../webrtc_agc-stub.c - LIB openmodules ) else() if(NOT DEFINED SOF_WEBRTC_APM_SRC_DIR) @@ -34,6 +33,5 @@ else() "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/modules/audio_processing/agc/legacy" "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/common_audio/signal_processing/include" CFLAGS -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= - LIB openmodules ) endif() diff --git a/src/audio/webrtc_hpf/llext/CMakeLists.txt b/src/audio/webrtc_hpf/llext/CMakeLists.txt index 86ab8dcdb1f8..0882d93c94d7 100644 --- a/src/audio/webrtc_hpf/llext/CMakeLists.txt +++ b/src/audio/webrtc_hpf/llext/CMakeLists.txt @@ -5,12 +5,10 @@ if(CONFIG_COMP_WEBRTC_HPF_STUB) sof_llext_build("webrtc_hpf" SOURCES ../webrtc_hpf.c ../webrtc_hpf-stub.c - LIB openmodules ) else() sof_llext_build("webrtc_hpf" SOURCES ../webrtc_hpf.c ../webrtc_hpf-filter.c - LIB openmodules ) endif() diff --git a/src/audio/webrtc_ns/llext/CMakeLists.txt b/src/audio/webrtc_ns/llext/CMakeLists.txt index 19b7e9aa3508..070841352923 100644 --- a/src/audio/webrtc_ns/llext/CMakeLists.txt +++ b/src/audio/webrtc_ns/llext/CMakeLists.txt @@ -6,7 +6,6 @@ if(CONFIG_COMP_WEBRTC_NS_STUB) sof_llext_build("webrtc_ns" SOURCES ../webrtc_ns.c ../webrtc_ns-stub.c - LIB openmodules ) else() # Real NS: cross-build webrtc-apm NS subset and link it. @@ -20,7 +19,6 @@ else() "${SOF_WEBRTC_APM_SRC_DIR}" LIBS_PATH "${WEBRTC_NS_INSTALL_DIR}/lib" LIBS webrtc_ns_core - LIB openmodules ) add_dependencies(webrtc_ns_llext_lib webrtc_ns_ext) diff --git a/src/audio/webrtc_ns2/llext/CMakeLists.txt b/src/audio/webrtc_ns2/llext/CMakeLists.txt index 9df3309b1797..2f709a01d8cd 100644 --- a/src/audio/webrtc_ns2/llext/CMakeLists.txt +++ b/src/audio/webrtc_ns2/llext/CMakeLists.txt @@ -5,7 +5,6 @@ if(CONFIG_COMP_WEBRTC_NS2_STUB) sof_llext_build("webrtc_ns2" SOURCES ../webrtc_ns2.c ../webrtc_ns2-stub.c - LIB openmodules ) else() include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_ns2.cmake) @@ -17,7 +16,6 @@ else() INCLUDES "${WEBRTC_NS2_INSTALL_DIR}/include" LIBS_PATH "${WEBRTC_NS2_INSTALL_DIR}/lib" LIBS rnnoise - LIB openmodules ) add_dependencies(webrtc_ns2_llext_lib rnnoise_ext) diff --git a/src/audio/webrtc_vad/llext/CMakeLists.txt b/src/audio/webrtc_vad/llext/CMakeLists.txt index 7d12eb109978..7bf517baa76c 100644 --- a/src/audio/webrtc_vad/llext/CMakeLists.txt +++ b/src/audio/webrtc_vad/llext/CMakeLists.txt @@ -6,7 +6,6 @@ if(CONFIG_COMP_WEBRTC_VAD_STUB) sof_llext_build("webrtc_vad" SOURCES ../webrtc_vad.c ../webrtc_vad-stub.c - LIB openmodules ) else() # Real VAD: cross-build libfvad and link it. @@ -19,7 +18,6 @@ else() INCLUDES "${LIBFVAD_INSTALL_DIR}/include" LIBS_PATH "${LIBFVAD_INSTALL_DIR}/lib" LIBS fvad - LIB openmodules ) # The archive must be built before the module compiles/links against it.