Skip to content

targets: add default malloc wrappers for esp32 and esp32s3 - #6852

Draft
davecheney wants to merge 1 commit into
tinygo-org:devfrom
davecheney:esp32-malloc-wrap
Draft

davecheney wants to merge 1 commit into
tinygo-org:devfrom
davecheney:esp32-malloc-wrap

Conversation

@davecheney

Copy link
Copy Markdown
Contributor

#5283 and #5510 added --wrap=malloc, --wrap=calloc, --wrap=realloc and --wrap=free to the esp32s3 and esp32 linker flags, for espradio. Only espradio defined __wrap_malloc and the other three wrappers. Without espradio, a program with C code that called malloc did not link, and ld.lld reported "undefined symbol: __wrap_malloc". tinyfs/fatfs is one such program.

With this change, weak __wrap_malloc, __wrap_calloc, __wrap_realloc and __wrap_free call the _real functions, which are the TinyGo allocator. The strong wrappers in espradio replace them when espradio is linked.

tinygo-org#5283 and tinygo-org#5510 added --wrap=malloc, --wrap=calloc, --wrap=realloc and --wrap=free to the esp32s3 and esp32 linker flags, for espradio. Only espradio defined __wrap_malloc and the other three wrappers. Without espradio, a program with C code that called malloc did not link, and ld.lld reported "undefined symbol: __wrap_malloc". tinyfs/fatfs is one such program.

With this change, weak __wrap_malloc, __wrap_calloc, __wrap_realloc and __wrap_free call the __real_ functions, which are the TinyGo allocator. The strong wrappers in espradio replace them when espradio is linked.
@deadprogram

Copy link
Copy Markdown
Member

@davecheney I did not test on hardware, all I did was build for xiao-esp32s3 and esp32-coreboard-v2. Here are the comments from the machine:

A cgo program that calls realloc still does not link on esp32 and esp32s3. The baremetal runtime exports malloc, calloc and free in src/runtime/baremetal.go, but not realloc. So the weak __wrap_realloc calls an undefined __real_realloc. ld.lld now reports undefined symbol: realloc, referenced by esp32-malloc-wrap.c instead of by the user's C code. ESP32-C3 has no wraps and fails the same way, so this gap was already there. We should add a realloc export to baremetal.go in this PR or a follow-up.

@davecheney

Copy link
Copy Markdown
Contributor Author

Let me check again, I had this in my queue for a while and a lot has changed.

@davecheney
davecheney marked this pull request as draft December 5, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants