Skip to content

Commit

Permalink
[WebAssemlby] MC: Don't write COMDAT symbols as global imports
Browse files Browse the repository at this point in the history
This was causing undefined references at link time in lld.

Differential Revision: https://reviews.llvm.org/D41959

llvm-svn: 322309
  • Loading branch information
sbc100 committed Jan 11, 2018
1 parent 2aac3ee commit d423f0d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/MC/MCSymbolWasm.h
Expand Up @@ -19,6 +19,7 @@ class MCSymbolWasm : public MCSymbol {
bool IsFunction = false;
bool IsWeak = false;
bool IsHidden = false;
bool IsComdat = false;
std::string ModuleName;
SmallVector<wasm::ValType, 1> Returns;
SmallVector<wasm::ValType, 4> Params;
Expand Down Expand Up @@ -49,6 +50,9 @@ class MCSymbolWasm : public MCSymbol {
bool isHidden() const { return IsHidden; }
void setHidden(bool isHidden) { IsHidden = isHidden; }

bool isComdat() const { return IsComdat; }
void setComdat(bool isComdat) { IsComdat = isComdat; }

const StringRef getModuleName() const { return ModuleName; }

const SmallVector<wasm::ValType, 1> &getReturns() const {
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/MC/MCContext.cpp
Expand Up @@ -490,8 +490,10 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
const Twine &Group, unsigned UniqueID,
const char *BeginSymName) {
MCSymbolWasm *GroupSym = nullptr;
if (!Group.isTriviallyEmpty() && !Group.str().empty())
if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
GroupSym->setComdat(true);
}

return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/MC/WasmObjectWriter.cpp
Expand Up @@ -1122,7 +1122,8 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
continue;

// If the symbol is not defined in this translation unit, import it.
if (!WS.isDefined(/*SetUsed=*/false) || WS.isVariable()) {
if ((!WS.isDefined(/*SetUsed=*/false) && !WS.isComdat()) ||
WS.isVariable()) {
WasmImport Import;
Import.ModuleName = WS.getModuleName();
Import.FieldName = WS.getName();
Expand Down
38 changes: 36 additions & 2 deletions llvm/test/MC/WebAssembly/comdat.ll
Expand Up @@ -22,7 +22,41 @@ define linkonce_odr i32 @sharedFn() #1 comdat($sharedComdat) {
ret i32 0
}

; CHECK: - Type: EXPORT
; CHECK: Sections:
; CHECK-NEXT: - Type: TYPE
; CHECK-NEXT: Signatures:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: ReturnType: I32
; CHECK-NEXT: ParamTypes:
; CHECK-NEXT: - Type: IMPORT
; CHECK-NEXT: Imports:
; CHECK-NEXT: - Module: env
; CHECK-NEXT: Field: __linear_memory
; CHECK-NEXT: Kind: MEMORY
; CHECK-NEXT: Memory:
; CHECK-NEXT: Initial: 0x00000001
; CHECK-NEXT: - Module: env
; CHECK-NEXT: Field: __indirect_function_table
; CHECK-NEXT: Kind: TABLE
; CHECK-NEXT: Table:
; CHECK-NEXT: ElemType: ANYFUNC
; CHECK-NEXT: Limits:
; CHECK-NEXT: Initial: 0x00000000
; CHECK-NEXT: - Module: env
; CHECK-NEXT: Field: funcImport
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: SigIndex: 0
; CHECK-NEXT: - Type: FUNCTION
; CHECK-NEXT: FunctionTypes: [ 0, 0, 0 ]
; CHECK-NEXT: - Type: GLOBAL
; CHECK-NEXT: Globals:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Type: I32
; CHECK-NEXT: Mutable: false
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 0
; CHECK-NEXT: - Type: EXPORT
; CHECK-NEXT: Exports:
; CHECK-NEXT: - Name: callImport
; CHECK-NEXT: Kind: FUNCTION
Expand All @@ -35,7 +69,7 @@ define linkonce_odr i32 @sharedFn() #1 comdat($sharedComdat) {
; CHECK-NEXT: Index: 3
; CHECK-NEXT: - Name: constantData
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 1
; CHECK-NEXT: Index: 0
; CHECK-NEXT: - Type: CODE
; CHECK-NEXT: Relocations:
; CHECK-NEXT: - Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB
Expand Down

0 comments on commit d423f0d

Please sign in to comment.