1 //===-- CodeGen/AsmPrinter/WasmException.cpp - Wasm Exception Impl --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing WebAssembly exception info into asm
10 // files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "WasmException.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/IR/Mangler.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCStreamer.h"
20 using namespace llvm;
21 
22 void WasmException::endModule() {
23   // These are symbols used to throw/catch C++ exceptions and C longjmps. These
24   // symbols have to be emitted somewhere once in the module. Check if each of
25   // the symbols has already been created, i.e., we have at least one 'throw' or
26   // 'catch' instruction with the symbol in the module, and emit the symbol only
27   // if so.
28   //
29   // But in dynamic linking, it is in general not possible to come up with a
30   // module instantiating order in which tag-defining modules are loaded before
31   // the importing modules. So we make them undefined symbols here, define tags
32   // in the JS side, and feed them to each importing module.
33   if (!Asm->isPositionIndependent()) {
34     for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
35       SmallString<60> NameStr;
36       Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
37       if (Asm->OutContext.lookupSymbol(NameStr)) {
38         MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
39         Asm->OutStreamer->emitLabel(ExceptionSym);
40       }
41     }
42   }
43 }
44 
45 void WasmException::markFunctionEnd() {
46   // Get rid of any dead landing pads.
47   if (!Asm->MF->getLandingPads().empty()) {
48     auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
49     // Wasm does not set BeginLabel and EndLabel information for landing pads,
50     // so we should set the second argument false.
51     NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
52   }
53 }
54 
55 void WasmException::endFunction(const MachineFunction *MF) {
56   bool ShouldEmitExceptionTable = false;
57   for (const LandingPadInfo &Info : MF->getLandingPads()) {
58     if (MF->hasWasmLandingPadIndex(Info.LandingPadBlock)) {
59       ShouldEmitExceptionTable = true;
60       break;
61     }
62   }
63   if (!ShouldEmitExceptionTable)
64     return;
65   MCSymbol *LSDALabel = emitExceptionTable();
66   assert(LSDALabel && ".GCC_exception_table has not been emitted!");
67 
68   // Wasm requires every data section symbol to have a .size set. So we emit an
69   // end marker and set the size as the difference between the start end the end
70   // marker.
71   MCSymbol *LSDAEndLabel = Asm->createTempSymbol("GCC_except_table_end");
72   Asm->OutStreamer->emitLabel(LSDAEndLabel);
73   MCContext &OutContext = Asm->OutStreamer->getContext();
74   const MCExpr *SizeExp = MCBinaryExpr::createSub(
75       MCSymbolRefExpr::create(LSDAEndLabel, OutContext),
76       MCSymbolRefExpr::create(LSDALabel, OutContext), OutContext);
77   Asm->OutStreamer->emitELFSize(LSDALabel, SizeExp);
78 }
79 
80 // Compute the call-site table for wasm EH. Even though we use the same function
81 // name to share the common routines, a call site entry in the table corresponds
82 // to not a call site for possibly-throwing functions but a landing pad. In wasm
83 // EH the VM is responsible for stack unwinding. After an exception occurs and
84 // the stack is unwound, the control flow is transferred to wasm 'catch'
85 // instruction by the VM, after which the personality function is called from
86 // the compiler-generated code. Refer to WasmEHPrepare pass for more
87 // information.
88 void WasmException::computeCallSiteTable(
89     SmallVectorImpl<CallSiteEntry> &CallSites,
90     SmallVectorImpl<CallSiteRange> &CallSiteRanges,
91     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
92     const SmallVectorImpl<unsigned> &FirstActions) {
93   MachineFunction &MF = *Asm->MF;
94   for (unsigned I = 0, N = LandingPads.size(); I < N; ++I) {
95     const LandingPadInfo *Info = LandingPads[I];
96     MachineBasicBlock *LPad = Info->LandingPadBlock;
97     // We don't emit LSDA for single catch (...).
98     if (!MF.hasWasmLandingPadIndex(LPad))
99       continue;
100     // Wasm EH must maintain the EH pads in the order assigned to them by the
101     // WasmEHPrepare pass.
102     unsigned LPadIndex = MF.getWasmLandingPadIndex(LPad);
103     CallSiteEntry Site = {nullptr, nullptr, Info, FirstActions[I]};
104     if (CallSites.size() < LPadIndex + 1)
105       CallSites.resize(LPadIndex + 1);
106     CallSites[LPadIndex] = Site;
107   }
108 }
109