Namespace-wrap vendored mbedtls to avoid clashes with statically-linked duckdb - #830
Merged
Conversation
Both ladybug's third_party/mbedtls and a statically-linked duckdb (libduckdb_static.a) bundle mbedtls with plain, unprefixed mbedtls_* symbols. When both end up on one link line (e.g. GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs'), the linker reports ~19 duplicate symbol errors for _mbedtls_cipher_*. Fix by wrapping our vendored copy in namespace lbug_mbedtls: - third_party/ports/mbedtls/do-patch.py (new): port patch script, following the zstd port pattern. It renames .c -> .cpp, strips the now-dead extern "C" guards (the copy is compiled as C++ everywhere), and wraps top-level code segments in balanced namespace pairs. Segmentation tracks preprocessor conditional context and brace depth per line, so sibling conditional branches (MBEDTLS_AES_ALT vs MBEDTLS_SELF_TEST) and mid-declaration conditionals (lone 'static' followed by a conditional attribute block) stay balanced. It also absorbs the previously-missing format_c_to_cpp.py step, making the port runnable again. - ports/mbedtls/Makefile: invoke do-patch.py during build. - third_party/mbedtls: vendored sources regenerated via the script (57 files; only namespace pairs and extern "C" removals). Public headers re-expose names via 'using namespace lbug_mbedtls', so in-tree consumers (src/common/sha256.cpp, extension/httpfs/src/crypto.cpp) are unchanged. Requires LadybugDB/extensions#63 (submodule bump included here). Verified with GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs': the link succeeds, libmbedtls.a exports zero plain _mbedtls_* symbols, and the shell passes a smoke query.
adsharma
force-pushed
the
fix/mbedtls-namespace-wrap
branch
from
August 22, 2026 17:09
3356a5a to
8432982
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs'failed at the final link with duplicate symbol errors: both ourthird_party/mbedtlsand Homebrew'slibduckdb_static.abundle mbedtls with plain, unprefixedmbedtls_*symbols, and both copies get pulled into the shell link.Fix
Wrap our vendored mbedtls in
namespace lbug_mbedtlsso its symbols can never collide with another statically-linked mbedtls copy:.c->.cpp, strips the deadextern "C"guards (the copy is compiled as C++ everywhere, and C linkage is exactly what forces plain symbol names), and wraps top-level code segments in balancednamespace lbug_mbedtls { }pairs. Segmentation tracks preprocessor conditional context and brace depth per line, so sibling conditional branches (MBEDTLS_AES_ALTvsMBEDTLS_SELF_TEST) and mid-declaration conditionals (the lonestatic+ conditional-attribute idiom inbignum.cpp) stay balanced. Also absorbs the previously-missingformat_c_to_cpp.pystep, making the port runnable again.do-patch.pyduring build.extern "C"removals).libduckdb_generated_extension_loader.a+ bundled extension archives when linking duckdb statically).Public headers re-expose names via
using namespace lbug_mbedtls, so in-tree consumers (src/common/sha256.cpp,extension/httpfs/src/crypto.cpp) are unchanged.Verification
GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs'completes: the link succeeds,libmbedtls.aexports zero plain_mbedtls_*symbols (222 namespaced ones), and the shell passes a smoke query.