Skip to content

Commit 0a9cf2c

Browse files
authored
Merge pull request #49 from alexcrichton/update-wasm-bits-10
Update wasm handling of bitcode
2 parents 3b0e8db + 6386f36 commit 0a9cf2c

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
@@ -30,3 +30,12 @@
3030
// RUN: | FileCheck --check-prefix=CHECK-HEXAGON %s
3131
// CHECK-HEXAGON: "-target-feature"
3232
// CHECK-HEXAGON: "+reserved-r19"
33+
//
34+
// RUN: %clang -target wasm32-unknown-unknown -fembed-bitcode=all -pthread -c %s -o /dev/null -### 2>&1 \
35+
// RUN: | FileCheck --check-prefix=CHECK-WASM %s
36+
37+
// CHECK-WASM: "-cc1"
38+
// CHECK-WASM: "-target-feature" "+atomics"
39+
40+
// CHECK-WASM: "-cc1"
41+
// CHECK-WASM: "-target-feature" "+atomics"

lld/wasm/Writer.cpp

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

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;
@@ -95,6 +96,9 @@ class MCSymbolWasm : public MCSymbol {
9596
void setUsedInGOT() const { IsUsedInGOT = true; }
9697
bool isUsedInGOT() const { return IsUsedInGOT; }
9798

99+
void setUsedInInitArray() const { IsUsedInInitArray = true; }
100+
bool isUsedInInitArray() const { return IsUsedInInitArray; }
101+
98102
const wasm::WasmSignature *getSignature() const { return Signature; }
99103
void setSignature(wasm::WasmSignature *Sig) { Signature = Sig; }
100104

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
@@ -1692,6 +1692,14 @@ static const Comdat *getWasmComdat(const GlobalValue *GV) {
16921692
}
16931693

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

llvm/lib/MC/WasmObjectWriter.cpp

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

439-
// The .init_array isn't translated as data, so don't do relocations in it.
440-
if (FixupSection.getSectionName().startswith(".init_array"))
441-
return;
442-
443439
if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
444440
// To get here the A - B expression must have failed evaluateAsRelocatable.
445441
// This means either A or B must be undefined and in WebAssembly we can't
@@ -456,6 +452,12 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
456452
const MCSymbolRefExpr *RefA = Target.getSymA();
457453
const auto *SymA = cast<MCSymbolWasm>(&RefA->getSymbol());
458454

455+
// The .init_array isn't translated as data, so don't do relocations in it.
456+
if (FixupSection.getSectionName().startswith(".init_array")) {
457+
SymA->setUsedInInitArray();
458+
return;
459+
}
460+
459461
if (SymA->isVariable()) {
460462
const MCExpr *Expr = SymA->getVariableValue();
461463
const auto *Inner = cast<MCSymbolRefExpr>(Expr);
@@ -1084,16 +1086,13 @@ void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) {
10841086
}
10851087

10861088
static bool isInSymtab(const MCSymbolWasm &Sym) {
1087-
if (Sym.isUsedInReloc())
1089+
if (Sym.isUsedInReloc() || Sym.isUsedInInitArray())
10881090
return true;
10891091

10901092
if (Sym.isComdat() && !Sym.isDefined())
10911093
return false;
10921094

1093-
if (Sym.isTemporary() && Sym.getName().empty())
1094-
return false;
1095-
1096-
if (Sym.isTemporary() && Sym.isData() && !Sym.getSize())
1095+
if (Sym.isTemporary())
10971096
return false;
10981097

10991098
if (Sym.isSection())
@@ -1565,7 +1564,7 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
15651564
report_fatal_error("fixups in .init_array should be symbol references");
15661565
const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol());
15671566
if (TargetSym.getIndex() == InvalidIndex)
1568-
report_fatal_error("symbols in .init_array should exist in symbtab");
1567+
report_fatal_error("symbols in .init_array should exist in symtab");
15691568
if (!TargetSym.isFunction())
15701569
report_fatal_error("symbols in .init_array should be for functions");
15711570
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)