From 0f8c06fb12ea8695463b64563ee245c83c3f39b2 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 30 Mar 2020 17:37:01 -0700 Subject: [PATCH] [WebAssembly] Emit .llvmcmd and .llvmbc as custom sections Fixes: https://bugs.llvm.org/show_bug.cgi?id=45362 Differential Revision: https://reviews.llvm.org/D77115 --- include/llvm/MC/MCSymbolWasm.h | 4 ++++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 8 ++++++++ lib/MC/WasmObjectWriter.cpp | 19 +++++++++---------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/llvm/MC/MCSymbolWasm.h b/include/llvm/MC/MCSymbolWasm.h index da9525922fa..ffd8a7aad31 100644 --- a/include/llvm/MC/MCSymbolWasm.h +++ b/include/llvm/MC/MCSymbolWasm.h @@ -18,6 +18,7 @@ class MCSymbolWasm : public MCSymbol { bool IsWeak = false; bool IsHidden = false; bool IsComdat = false; + mutable bool IsUsedInInitArray = false; mutable bool IsUsedInGOT = false; Optional ImportModule; Optional ImportName; @@ -96,6 +97,9 @@ public: void setUsedInGOT() const { IsUsedInGOT = true; } bool isUsedInGOT() const { return IsUsedInGOT; } + void setUsedInInitArray() const { IsUsedInInitArray = true; } + bool isUsedInInitArray() const { return IsUsedInInitArray; } + const wasm::WasmSignature *getSignature() const { return Signature; } void setSignature(wasm::WasmSignature *Sig) { Signature = Sig; } diff --git a/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 17946696a26..e7750ccf3ad 100644 --- a/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1755,6 +1755,14 @@ MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( StringRef Name = GO->getSection(); + // Certain data sections we treat as named custom sections rather than + // segments within the data section. + // This could be avoided if all data segements (the wasm sense) were + // represented as thier own sections (in the llvm sense). + // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138 + if (Name == ".llvmcmd" || Name == ".llvmbc") + Kind = SectionKind::getMetadata(); + StringRef Group = ""; if (const Comdat *C = getWasmComdat(GO)) { Group = C->getName(); diff --git a/lib/MC/WasmObjectWriter.cpp b/lib/MC/WasmObjectWriter.cpp index 321f93d7609..4df809224ed 100644 --- a/lib/MC/WasmObjectWriter.cpp +++ b/lib/MC/WasmObjectWriter.cpp @@ -436,10 +436,6 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm, uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); MCContext &Ctx = Asm.getContext(); - // The .init_array isn't translated as data, so don't do relocations in it. - if (FixupSection.getSectionName().startswith(".init_array")) - return; - if (const MCSymbolRefExpr *RefB = Target.getSymB()) { // To get here the A - B expression must have failed evaluateAsRelocatable. // This means either A or B must be undefined and in WebAssembly we can't @@ -456,6 +452,12 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm, const MCSymbolRefExpr *RefA = Target.getSymA(); const auto *SymA = cast(&RefA->getSymbol()); + // The .init_array isn't translated as data, so don't do relocations in it. + if (FixupSection.getSectionName().startswith(".init_array")) { + SymA->setUsedInInitArray(); + return; + } + if (SymA->isVariable()) { const MCExpr *Expr = SymA->getVariableValue(); const auto *Inner = cast(Expr); @@ -1084,16 +1086,13 @@ void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) { } static bool isInSymtab(const MCSymbolWasm &Sym) { - if (Sym.isUsedInReloc()) + if (Sym.isUsedInReloc() || Sym.isUsedInInitArray()) return true; if (Sym.isComdat() && !Sym.isDefined()) return false; - if (Sym.isTemporary() && Sym.getName().empty()) - return false; - - if (Sym.isTemporary() && Sym.isData() && !Sym.getSize()) + if (Sym.isTemporary()) return false; if (Sym.isSection()) @@ -1565,7 +1564,7 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm, report_fatal_error("fixups in .init_array should be symbol references"); const auto &TargetSym = cast(SymRef->getSymbol()); if (TargetSym.getIndex() == InvalidIndex) - report_fatal_error("symbols in .init_array should exist in symbtab"); + report_fatal_error("symbols in .init_array should exist in symtab"); if (!TargetSym.isFunction()) report_fatal_error("symbols in .init_array should be for functions"); InitFuncs.push_back(