diff options
| author | Sam Clegg <sbc@chromium.org> | 2019-03-26 19:46:15 +0000 |
|---|---|---|
| committer | Sam Clegg <sbc@chromium.org> | 2019-03-26 19:46:15 +0000 |
| commit | 492f752969ff3a37138372e8e973317dc63ef2d3 (patch) | |
| tree | db63368f472fbb77026751650cc85ecc6c114f07 /llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp | |
| parent | 6f4c45e9fc70473e2e176608657c02beb122973b (diff) | |
| download | llvm-492f752969ff3a37138372e8e973317dc63ef2d3.zip llvm-492f752969ff3a37138372e8e973317dc63ef2d3.tar.gz llvm-492f752969ff3a37138372e8e973317dc63ef2d3.tar.bz2 | |
[WebAssembly] Initial implementation of PIC code generation
This change implements lowering of references global symbols in PIC
mode.
This change implements lowering of global references in PIC mode using a
new @GOT reference type. @GOT references can be used with function or
data symbol names combined with the get_global instruction. In this case
the linker will insert the wasm global that stores the address of the
symbol (either in memory for data symbols or in the wasm table for
function symbols).
For now I'm continuing to use the R_WASM_GLOBAL_INDEX_LEB relocation
type for this type of reference which means that this relocation type
can refer to either a global or a function or data symbol. We could
choose to introduce specific relocation types for GOT entries in the
future. See the current dynamic linking proposal:
https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
Differential Revision: https://reviews.llvm.org/D54647
llvm-svn: 357022
Diffstat (limited to 'llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp')
| -rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp index e357842..9ca9779 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -73,16 +73,18 @@ MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( auto *WasmSym = cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name)); const WebAssemblySubtarget &Subtarget = Printer.getSubtarget(); - // Except for the two exceptions (__stack_pointer and __cpp_exception), all - // other external symbols used by CodeGen are functions. It's OK to hardcode - // knowledge of specific symbols here; this method is precisely there for - // fetching the signatures of known Clang-provided symbols. - if (strcmp(Name, "__stack_pointer") == 0) { + // Except for certain known symbols, all symbols used by CodeGen are + // functions. It's OK to hardcode knowledge of specific symbols here; this + // method is precisely there for fetching the signatures of known + // Clang-provided symbols. + if (strcmp(Name, "__stack_pointer") == 0 || + strcmp(Name, "__memory_base") == 0 || strcmp(Name, "__table_base") == 0) { + bool Mutable = strcmp(Name, "__stack_pointer") == 0; WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); WasmSym->setGlobalType(wasm::WasmGlobalType{ uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 : wasm::WASM_TYPE_I32), - true}); + Mutable}); return WasmSym; } @@ -118,19 +120,23 @@ MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( return WasmSym; } -MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(MCSymbol *Sym, - int64_t Offset, - bool IsFunc, bool IsGlob, - bool IsEvent) const { - const MCExpr *Expr = - MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx); +MCOperand WebAssemblyMCInstLower::lowerSymbolOperand( + MCSymbol *Sym, int64_t Offset, bool IsFunc, unsigned TargetFlags) const { + MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; + if (TargetFlags & WebAssemblyII::MO_GOT) + Kind = MCSymbolRefExpr::VK_GOT; + const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx); if (Offset != 0) { - if (IsFunc) + if (TargetFlags & WebAssemblyII::MO_GOT) + report_fatal_error("GOT symbol references do not support offsets"); + unsigned Type = TargetFlags & WebAssemblyII::MO_SYMBOL_MASK; + assert((Type == WebAssemblyII::MO_SYMBOL_FUNCTION) == IsFunc); + if (Type == WebAssemblyII::MO_SYMBOL_FUNCTION || IsFunc) report_fatal_error("Function addresses with offsets not supported"); - if (IsGlob) + if (Type == WebAssemblyII::MO_SYMBOL_GLOBAL) report_fatal_error("Global indexes with offsets not supported"); - if (IsEvent) + if (Type == WebAssemblyII::MO_SYMBOL_EVENT) report_fatal_error("Event indexes with offsets not supported"); Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); @@ -230,11 +236,9 @@ void WebAssemblyMCInstLower::lower(const MachineInstr *MI, break; } case MachineOperand::MO_GlobalAddress: - assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG && - "WebAssembly does not use target flags on GlobalAddresses"); MCOp = lowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), MO.getGlobal()->getValueType()->isFunctionTy(), - false, false); + MO.getTargetFlags()); break; case MachineOperand::MO_ExternalSymbol: // The target flag indicates whether this is a symbol for a @@ -242,18 +246,14 @@ void WebAssemblyMCInstLower::lower(const MachineInstr *MI, assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 && "WebAssembly uses only symbol flags on ExternalSymbols"); MCOp = lowerSymbolOperand( - GetExternalSymbolSymbol(MO), /*Offset=*/0, - (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0, - (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0, - (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_EVENT) != 0); + GetExternalSymbolSymbol(MO), /*Offset=*/0, false, MO.getTargetFlags()); break; case MachineOperand::MO_MCSymbol: // This is currently used only for LSDA symbols (GCC_except_table), // because global addresses or other external symbols are handled above. assert(MO.getTargetFlags() == 0 && "WebAssembly does not use target flags on MCSymbol"); - MCOp = lowerSymbolOperand(MO.getMCSymbol(), /*Offset=*/0, false, false, - false); + MCOp = lowerSymbolOperand(MO.getMCSymbol(), /*Offset=*/0, false, MO.getTargetFlags()); break; } |
