Skip to content

Commit

Permalink
[WebAssembly] Make __attribute__((used)) not imply export.
Browse files Browse the repository at this point in the history
Add an WASM_SYMBOL_NO_STRIP flag, so that __attribute__((used)) doesn't
need to imply exporting. When targeting Emscripten, have
WASM_SYMBOL_NO_STRIP imply exporting.

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

llvm-svn: 370415
  • Loading branch information
Dan Gohman committed Aug 29, 2019
1 parent 452e564 commit da84b68
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions llvm/include/llvm/BinaryFormat/Wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
const unsigned WASM_SYMBOL_EXPORTED = 0x20;
const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
const unsigned WASM_SYMBOL_NO_STRIP = 0x80;

#define WASM_RELOC(name, value) name = value,

Expand Down
7 changes: 7 additions & 0 deletions llvm/include/llvm/MC/MCSymbolWasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class MCSymbolWasm : public MCSymbol {
modifyFlags(wasm::WASM_SYMBOL_EXPORTED, wasm::WASM_SYMBOL_EXPORTED);
}

bool isNoStrip() const {
return getFlags() & wasm::WASM_SYMBOL_NO_STRIP;
}
void setNoStrip() const {
modifyFlags(wasm::WASM_SYMBOL_NO_STRIP, wasm::WASM_SYMBOL_NO_STRIP);
}

bool isWeak() const { return IsWeak; }
void setWeak(bool isWeak) { IsWeak = isWeak; }

Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/MC/MCWasmObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class raw_pwrite_stream;

class MCWasmObjectTargetWriter : public MCObjectTargetWriter {
const unsigned Is64Bit : 1;
const unsigned IsEmscripten : 1;

protected:
explicit MCWasmObjectTargetWriter(bool Is64Bit_);
explicit MCWasmObjectTargetWriter(bool Is64Bit_, bool IsEmscripten);

public:
virtual ~MCWasmObjectTargetWriter();
Expand All @@ -38,6 +39,7 @@ class MCWasmObjectTargetWriter : public MCObjectTargetWriter {
/// \name Accessors
/// @{
bool is64Bit() const { return Is64Bit; }
bool isEmscripten() const { return IsEmscripten; }
/// @}
};

Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/MC/MCWasmObjectTargetWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

using namespace llvm;

MCWasmObjectTargetWriter::MCWasmObjectTargetWriter(bool Is64Bit)
: Is64Bit(Is64Bit) {}
MCWasmObjectTargetWriter::MCWasmObjectTargetWriter(bool Is64Bit,
bool IsEmscripten)
: Is64Bit(Is64Bit), IsEmscripten(IsEmscripten) {}

// Pin the vtable to this object file
MCWasmObjectTargetWriter::~MCWasmObjectTargetWriter() = default;
2 changes: 1 addition & 1 deletion llvm/lib/MC/MCWasmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
break;

case MCSA_NoDeadStrip:
Symbol->setExported();
Symbol->setNoStrip();
break;

default:
Expand Down
9 changes: 7 additions & 2 deletions llvm/lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class WasmObjectWriter : public MCObjectWriter {

// TargetObjectWriter wrappers.
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
bool isEmscripten() const { return TargetObjectWriter->isEmscripten(); }

void startSection(SectionBookkeeping &Section, unsigned SectionId);
void startCustomSection(SectionBookkeeping &Section, StringRef Name);
Expand Down Expand Up @@ -1443,8 +1444,12 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
if (WS.isUndefined())
Flags |= wasm::WASM_SYMBOL_UNDEFINED;
if (WS.isExported())
Flags |= wasm::WASM_SYMBOL_EXPORTED;
if (WS.isNoStrip()) {
Flags |= wasm::WASM_SYMBOL_NO_STRIP;
if (isEmscripten()) {
Flags |= wasm::WASM_SYMBOL_EXPORTED;
}
}
if (WS.getName() != WS.getImportName())
Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME;

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjectYAML/WasmYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
BCaseMask(UNDEFINED, UNDEFINED);
BCaseMask(EXPORTED, EXPORTED);
BCaseMask(EXPLICIT_NAME, EXPLICIT_NAME);
BCaseMask(NO_STRIP, NO_STRIP);
#undef BCaseMask
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ namespace {

class WebAssemblyAsmBackend final : public MCAsmBackend {
bool Is64Bit;
bool IsEmscripten;

public:
explicit WebAssemblyAsmBackend(bool Is64Bit)
: MCAsmBackend(support::little), Is64Bit(Is64Bit) {}
explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsEmscripten)
: MCAsmBackend(support::little), Is64Bit(Is64Bit),
IsEmscripten(IsEmscripten) {}

unsigned getNumFixupKinds() const override {
return WebAssembly::NumTargetFixupKinds;
Expand Down Expand Up @@ -123,11 +125,11 @@ void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,

std::unique_ptr<MCObjectTargetWriter>
WebAssemblyAsmBackend::createObjectTargetWriter() const {
return createWebAssemblyWasmObjectWriter(Is64Bit);
return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten);
}

} // end anonymous namespace

MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
return new WebAssemblyAsmBackend(TT.isArch64Bit());
return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten());
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII);
MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT);

std::unique_ptr<MCObjectTargetWriter>
createWebAssemblyWasmObjectWriter(bool Is64Bit);
createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten);

namespace WebAssembly {
enum OperandType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ using namespace llvm;
namespace {
class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
public:
explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
explicit WebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten);

private:
unsigned getRelocType(const MCValue &Target,
const MCFixup &Fixup) const override;
};
} // end anonymous namespace

WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
: MCWasmObjectTargetWriter(Is64Bit) {}
WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit,
bool IsEmscripten)
: MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {}

static const MCSection *getFixupSection(const MCExpr *Expr) {
if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
Expand Down Expand Up @@ -116,6 +117,6 @@ unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
}

std::unique_ptr<MCObjectTargetWriter>
llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) {
return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
}
4 changes: 2 additions & 2 deletions llvm/test/MC/WebAssembly/no-dead-strip.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ entry:
; CHECK-NEXT: Symbol {
; CHECK-NEXT: Name: foo
; CHECK-NEXT: Type: FUNCTION (0x0)
; CHECK-NEXT: Flags [ (0x20)
; CHECK-NEXT: EXPORTED (0x20)
; CHECK-NEXT: Flags [ (0x80)
; CHECK-NEXT: NO_STRIP (0x80)
; CHECK-NEXT: ]
; CHECK-NEXT: ElementIndex: 0x0
; CHECK-NEXT: }
Expand Down
1 change: 1 addition & 0 deletions llvm/tools/llvm-readobj/WasmDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static const EnumEntry<unsigned> WasmSymbolFlags[] = {
ENUM_ENTRY(UNDEFINED),
ENUM_ENTRY(EXPORTED),
ENUM_ENTRY(EXPLICIT_NAME),
ENUM_ENTRY(NO_STRIP),
#undef ENUM_ENTRY
};

Expand Down

0 comments on commit da84b68

Please sign in to comment.