[WebAssembly] Create synthetic __wasm_call_ctors function
ClosedPublic

Authored by sbc100 on Jan 9 2018, 5:26 PM.

Details

Summary

This change create a new synthetic function in the final
output binary which calls the static constructors in sequence.

See: https://github.com/WebAssembly/tool-conventions/issues/25

Diff Detail

Repository
rLLD LLVM Linker
sbc100 created this revision.Jan 9 2018, 5:26 PM
sbc100 updated this revision to Diff 129203.Jan 9 2018, 5:44 PM
  • rebase
ruiu added a subscriber: ruiu.Jan 9 2018, 5:49 PM
ruiu added inline comments.
wasm/Driver.cpp
303

Since no one sets Config->CtorFunction, I'd inline it and directly write "__wasm_call_ctors" here.

wasm/InputChunks.h
136

I'd try to make the class hierarchy as shallow as possible. Do you think inheriting InputFunction is better than inheriting InputChunk?

wasm/Writer.cpp
708

Are you going to do the same thing for .dtors in a follow-up patch?

Please write a comment to describe this function creates a linker-synthesized function that calls initialization functions, which in turn be called by the startup routine on process startup.

727

Please add a comment.

sbc100 added inline comments.Jan 9 2018, 5:56 PM
wasm/Driver.cpp
303

I was going to make this configurable via flag, but maybe I'll wait until we really need that before adding it.

wasm/InputChunks.h
136

Right now the CodeSection is built from a vector of InputFunction * so this is needed. I think I tried refactoring and failed, but I can try again.

wasm/Writer.cpp
708

llvm already lowers all the dtors to ctors that call __cxa_atexit.
See: https://reviews.llvm.org/D41211

AndrewScheidecker requested changes to this revision.Jan 9 2018, 6:00 PM
AndrewScheidecker added inline comments.
wasm/Writer.cpp
714

This size needs to account for the variable length function indices. Perhaps just wait until after the encoded function is generated to compute the size?

717

“END” -> “CALL”
Also, instructions don’t use leb128 encoding.

This revision now requires changes to proceed.Jan 9 2018, 6:00 PM
sbc100 updated this revision to Diff 129348.Jan 10 2018, 3:04 PM
sbc100 marked 3 inline comments as done.
  • feedback
wasm/Writer.cpp
714

Good call, I was being very lazy.

This revision is now accepted and ready to land.Jan 10 2018, 4:21 PM
sbc100 updated this revision to Diff 129471.Jan 11 2018, 10:17 AM
  • implement ctor/dtor priority
Closed by commit rLLD322388: [WebAssembly] Create synthetic __wasm_call_ctors function (authored by sbc, committed by ). · Explain Why
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.
ruiu added inline comments.Jan 12 2018, 2:22 PM
wasm/InputChunks.h
142–144

This comment is not specific to this patch, but why does this function returns 'const uint8_t *' instead of ArrayRef<uint8_t>? Usually the latter type is better because it does bound-checking.

sbc100 added inline comments.Jan 12 2018, 2:27 PM
wasm/InputChunks.h
142–144

Good point. I'll see if I can good up a patch.