diff options
| author | Dan Gohman <sunfish@mozilla.com> | 2019-11-25 09:50:58 -0800 |
|---|---|---|
| committer | Dan Gohman <sunfish@mozilla.com> | 2020-02-27 07:55:36 -0800 |
| commit | 00072c08c75050ae2c835b7bb0e505475dbcd7b9 (patch) | |
| tree | f6ea954da28498b216233acac4507efe27ff1eea /clang/lib/CodeGen/CodeGenModule.cpp | |
| parent | 197bda587b4bb5e7603ad05fc1106332edc6afbd (diff) | |
[WebAssembly] Mangle the argc/argv `main` as `__wasm_argc_argv`.
WebAssembly enforces a rule that caller and callee signatures must
match. This means that the traditional technique of passing `main`
`argc` and `argv` even when it doesn't need them doesn't work.
Currently the backend renames `main` to `__original_main`, however this
doesn't interact well with LTO'ing libc, and the name isn't intuitive.
This patch allows us to transition to `__main_argc_argv` instead.
This implements the proposal in
https://github.com/WebAssembly/tool-conventions/pull/134
with a flag to disable it when targeting Emscripten, though this is
expected to be temporary, as discussed in the proposal comments.
Differential Revision: https://reviews.llvm.org/D70700
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
| -rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 652efd857064..faff623a2973 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -448,6 +448,10 @@ void CodeGenModule::Release() { CodeGenFunction(*this).EmitCfiCheckStub(); } emitAtAvailableLinkGuard(); + if (Context.getTargetInfo().getTriple().isWasm() && + !Context.getTargetInfo().getTriple().isOSEmscripten()) { + EmitMainVoidAlias(); + } emitLLVMUsed(); if (SanStats) SanStats->finish(); @@ -5600,6 +5604,17 @@ void CodeGenModule::EmitDeferredUnusedCoverageMappings() { } } +void CodeGenModule::EmitMainVoidAlias() { + // In order to transition away from "__original_main" gracefully, emit an + // alias for "main" in the no-argument case so that libc can detect when + // new-style no-argument main is in used. + if (llvm::Function *F = getModule().getFunction("main")) { + if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() && + F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) + addUsedGlobal(llvm::GlobalAlias::create("__main_void", F)); + } +} + /// Turns the given pointer into a constant. static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, const void *Ptr) { |
