mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
98b3c27c76
The previous approach of erasing basic blocks one-by-one from duplicate function definitions could crash with a segfault. When a function has complex control flow (branches, switches, PHI nodes), deleting a basic block that is still referenced by instructions in other blocks of the same function causes use-after-free in LLVM. LLVM's C++ Function::deleteBody() avoids this by calling dropAllReferences() on all blocks first, but that API is not exposed in the Go LLVM bindings. Fix this by using a completely different approach: instead of manually deleting the function body, set the duplicate function's linkage to LinkOnceODRLinkage. This tells the LLVM linker that the definition can be merged with another copy, causing it to prefer the already-linked ExternalLinkage definition in the destination module. The result is the same (the runtime's definition wins) but without any unsafe block manipulation.