Skip to content

Commit 3ba9191

Browse files
authored
Merge pull request #48 from alexcrichton/update-wasm-bits
Update wasm handling of embedded bitcode
2 parents 9f9da27 + e319eda commit 3ba9191

File tree

9 files changed

+47
-15
lines changed

9 files changed

+47
-15
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// REQUIRES: webassembly-registered-target
2+
3+
// RUN: %clang -c -target wasm32-unknown-unknown %s -fembed-bitcode -o %t.o
4+
// RUN: llvm-readobj -S %t.o | FileCheck --check-prefix=CHECK %s
5+
// CHECK: Name: .llvmbc
6+
// CHECK: Name: .llvmcmd

clang/test/Driver/fembed-bitcode.c

+9
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@
3434
// RUN: | FileCheck --check-prefix=CHECK-HEXAGON %s
3535
// CHECK-HEXAGON: "-target-feature"
3636
// CHECK-HEXAGON: "+reserved-r19"
37+
//
38+
// RUN: %clang -target wasm32-unknown-unknown -fembed-bitcode=all -pthread -c %s -o /dev/null -### 2>&1 \
39+
// RUN: | FileCheck --check-prefix=CHECK-WASM %s
40+
41+
// CHECK-WASM: "-cc1"
42+
// CHECK-WASM: "-target-feature" "+atomics"
43+
44+
// CHECK-WASM: "-cc1"
45+
// CHECK-WASM: "-target-feature" "+atomics"

lld/wasm/Writer.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,21 @@ void Writer::calculateCustomSections() {
111111
for (InputSection *section : file->customSections) {
112112
StringRef name = section->getName();
113113
// These custom sections are known the linker and synthesized rather than
114-
// blindly copied
114+
// blindly copied.
115115
if (name == "linking" || name == "name" || name == "producers" ||
116116
name == "target_features" || name.startswith("reloc."))
117117
continue;
118-
// .. or it is a debug section
118+
// These custom sections are generated by `clang -fembed-bitcode`.
119+
// These are used by the rust toolchain to ship LTO data along with
120+
// compiled object code, but they don't want this included in the linker
121+
// output.
122+
if (name == ".llvmbc" || name == ".llvmcmd")
123+
continue;
124+
// Strip debug section in that option was specified.
119125
if (stripDebug && name.startswith(".debug_"))
120126
continue;
127+
// Otherwise include custom sections by default and concatenate their
128+
// contents.
121129
customSectionMapping[name].push_back(section);
122130
}
123131
}

llvm/include/llvm/MC/MCSymbolWasm.h

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class MCSymbolWasm : public MCSymbol {
1818
bool IsWeak = false;
1919
bool IsHidden = false;
2020
bool IsComdat = false;
21+
mutable bool IsUsedInInitArray = false;
2122
mutable bool IsUsedInGOT = false;
2223
Optional<std::string> ImportModule;
2324
Optional<std::string> ImportName;
@@ -82,6 +83,9 @@ class MCSymbolWasm : public MCSymbol {
8283
void setUsedInGOT() const { IsUsedInGOT = true; }
8384
bool isUsedInGOT() const { return IsUsedInGOT; }
8485

86+
void setUsedInInitArray() const { IsUsedInInitArray = true; }
87+
bool isUsedInInitArray() const { return IsUsedInInitArray; }
88+
8589
const wasm::WasmSignature *getSignature() const { return Signature; }
8690
void setSignature(wasm::WasmSignature *Sig) { Signature = Sig; }
8791

llvm/include/llvm/Object/Wasm.h

-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ class WasmObjectFile : public ObjectFile {
183183
bool isSectionData(DataRefImpl Sec) const override;
184184
bool isSectionBSS(DataRefImpl Sec) const override;
185185
bool isSectionVirtual(DataRefImpl Sec) const override;
186-
bool isSectionBitcode(DataRefImpl Sec) const override;
187186
relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
188187
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
189188

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,14 @@ static const Comdat *getWasmComdat(const GlobalValue *GV) {
16881688
}
16891689

16901690
static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
1691+
// Certain data sections we treat as named custom sections rather than
1692+
// segments within the data section.
1693+
// This could be avoided if all data segements (the wasm sense) were
1694+
// represented as thier own sections (in the llvm sense).
1695+
// TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138
1696+
if (Name == ".llvmcmd" || Name == ".llvmbc")
1697+
return SectionKind::getMetadata();
1698+
16911699
// If we're told we have function data, then use that.
16921700
if (K.isText())
16931701
return SectionKind::getText();

llvm/lib/MC/WasmObjectWriter.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,6 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
434434
uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
435435
MCContext &Ctx = Asm.getContext();
436436

437-
// The .init_array isn't translated as data, so don't do relocations in it.
438-
if (FixupSection.getSectionName().startswith(".init_array"))
439-
return;
440-
441437
if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
442438
assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
443439
"Should not have constructed this");
@@ -483,6 +479,12 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
483479
const MCSymbolRefExpr *RefA = Target.getSymA();
484480
const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
485481

482+
// The .init_array isn't translated as data, so don't do relocations in it.
483+
if (FixupSection.getSectionName().startswith(".init_array")) {
484+
SymA->setUsedInInitArray();
485+
return;
486+
}
487+
486488
if (SymA && SymA->isVariable()) {
487489
const MCExpr *Expr = SymA->getVariableValue();
488490
const auto *Inner = cast<MCSymbolRefExpr>(Expr);
@@ -1113,16 +1115,13 @@ void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) {
11131115
}
11141116

11151117
static bool isInSymtab(const MCSymbolWasm &Sym) {
1116-
if (Sym.isUsedInReloc())
1118+
if (Sym.isUsedInReloc() || Sym.isUsedInInitArray())
11171119
return true;
11181120

11191121
if (Sym.isComdat() && !Sym.isDefined())
11201122
return false;
11211123

1122-
if (Sym.isTemporary() && Sym.getName().empty())
1123-
return false;
1124-
1125-
if (Sym.isTemporary() && Sym.isData() && !Sym.getSize())
1124+
if (Sym.isTemporary())
11261125
return false;
11271126

11281127
if (Sym.isSection())
@@ -1578,7 +1577,7 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
15781577
report_fatal_error("fixups in .init_array should be symbol references");
15791578
const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol());
15801579
if (TargetSym.getIndex() == InvalidIndex)
1581-
report_fatal_error("symbols in .init_array should exist in symbtab");
1580+
report_fatal_error("symbols in .init_array should exist in symtab");
15821581
if (!TargetSym.isFunction())
15831582
report_fatal_error("symbols in .init_array should be for functions");
15841583
InitFuncs.push_back(

llvm/lib/Object/IRObjectFile.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
9494
return Object;
9595
case file_magic::elf_relocatable:
9696
case file_magic::macho_object:
97+
case file_magic::wasm_object:
9798
case file_magic::coff_object: {
9899
Expected<std::unique_ptr<ObjectFile>> ObjFile =
99100
ObjectFile::createObjectFile(Object, Type);

llvm/lib/Object/WasmObjectFile.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,6 @@ bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
14551455

14561456
bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
14571457

1458-
bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
1459-
14601458
relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
14611459
DataRefImpl RelocRef;
14621460
RelocRef.d.a = Ref.d.a;

0 commit comments

Comments
 (0)