Fix contractenvmetav0 not being emitted (again) #388
Merged
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.
What
Move the link section for contractenvmetav0 into an exported function that is exported in every contract.
Why
In Rust's build system sections only get included into the final build if the object file containing those sections are processed by the linker, but as an optimization step if no code is called in an object file it is discarded. This has the unfortunate effect of causing anything else in those object files, such as link sections, to be discarded. Placing anything that must be included in the build inside an exported function ensures the object files won't be discarded.
wasm-bindgen does a similar thing to this, and so this seems to be a reasonably accepted way to work around this limitation in the build system. (Some more details about their approach here: koute/cargo-web#92 (comment).)
This has an unfortunate side-effect that all contracts will have a function in the resulting WASM named
_
, however this function won't be rendered in the contract specification. The overhead of this is very minimal on file size. Total overhead is still only 12 bytes on the token contract, and the XDR being embedded is also 12 bytes, so I'm not even sure how that happens, it's as if its free!This is the second attempted fix to this problem. The first fix was #386. If this fix doesn't stick, I will suggest we simply tell people to always use
codegen-units = 1
as I don't think it is worth us to expend more time on working around the compiler.Close #383