Skip to content

Commit 068bfbf

Browse files
committed
Support new dylink.0 custom section format
See also: llvm change: https://reviews.llvm.org/D109595 wabt change: WebAssembly/wabt#1707 emscripten change: emscripten-core/emscripten#15019
1 parent 23e452a commit 068bfbf

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

src/wasm-binary.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ namespace UserSections {
403403
extern const char* Name;
404404
extern const char* SourceMapUrl;
405405
extern const char* Dylink;
406+
extern const char* Dylink0;
406407
extern const char* Linking;
407408
extern const char* Producers;
408409
extern const char* TargetFeatures;
@@ -435,7 +436,10 @@ enum Subsection {
435436
NameElem = 8,
436437
NameData = 9,
437438
// see: https://github.com/WebAssembly/gc/issues/193
438-
NameField = 10
439+
NameField = 10,
440+
441+
DylinkMemInfo = 1,
442+
DylinkNeeded = 2,
439443
};
440444

441445
} // namespace UserSections
@@ -1229,6 +1233,7 @@ class WasmBinaryWriter {
12291233
void writeUserSection(const UserSection& section);
12301234
void writeFeaturesSection();
12311235
void writeDylinkSection();
1236+
void writeLegacyDylinkSection();
12321237

12331238
void initializeDebugInfo();
12341239
void writeSourceMapProlog();
@@ -1556,6 +1561,7 @@ class WasmBinaryBuilder {
15561561
void readNames(size_t);
15571562
void readFeatures(size_t);
15581563
void readDylink(size_t);
1564+
void readDylink0(size_t);
15591565

15601566
// Debug information reading helpers
15611567
void setDebugLocations(std::istream* sourceMap_) { sourceMap = sourceMap_; }

src/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,7 @@ class UserSection {
18221822
// The optional "dylink" section is used in dynamic linking.
18231823
class DylinkSection {
18241824
public:
1825+
bool isLegacy = false;
18251826
Index memorySize, memoryAlignment, tableSize, tableAlignment;
18261827
std::vector<Name> neededDynlibs;
18271828
std::vector<char> tail;

src/wasm/wasm-binary.cpp

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ void WasmBinaryWriter::writeFeaturesSection() {
10671067
finishSection(start);
10681068
}
10691069

1070-
void WasmBinaryWriter::writeDylinkSection() {
1070+
void WasmBinaryWriter::writeLegacyDylinkSection() {
10711071
if (!wasm->dylinkSection) {
10721072
return;
10731073
}
@@ -1088,6 +1088,38 @@ void WasmBinaryWriter::writeDylinkSection() {
10881088
finishSection(start);
10891089
}
10901090

1091+
void WasmBinaryWriter::writeDylinkSection() {
1092+
if (!wasm->dylinkSection) {
1093+
return;
1094+
}
1095+
1096+
if (wasm->dylinkSection->isLegacy) {
1097+
writeLegacyDylinkSection();
1098+
return;
1099+
}
1100+
1101+
auto start = startSection(BinaryConsts::User);
1102+
writeInlineString(BinaryConsts::UserSections::Dylink0);
1103+
1104+
auto substart = startSubsection(BinaryConsts::UserSections::Subsection::DylinkMemInfo);
1105+
o << U32LEB(wasm->dylinkSection->memorySize);
1106+
o << U32LEB(wasm->dylinkSection->memoryAlignment);
1107+
o << U32LEB(wasm->dylinkSection->tableSize);
1108+
o << U32LEB(wasm->dylinkSection->tableAlignment);
1109+
finishSubsection(substart);
1110+
1111+
if (wasm->dylinkSection->neededDynlibs.size()) {
1112+
substart = startSubsection(BinaryConsts::UserSections::Subsection::DylinkNeeded);
1113+
o << U32LEB(wasm->dylinkSection->neededDynlibs.size());
1114+
for (auto& neededDynlib : wasm->dylinkSection->neededDynlibs) {
1115+
writeInlineString(neededDynlib.c_str());
1116+
}
1117+
finishSubsection(substart);
1118+
}
1119+
1120+
finishSection(start);
1121+
}
1122+
10911123
void WasmBinaryWriter::writeDebugLocation(const Function::DebugLocation& loc) {
10921124
if (loc == lastDebugLocation) {
10931125
return;
@@ -1435,6 +1467,7 @@ void WasmBinaryBuilder::read() {
14351467
}
14361468

14371469
void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
1470+
BYN_TRACE("== readUserSection\n");
14381471
auto oldPos = pos;
14391472
Name sectionName = getInlineString();
14401473
size_t read = pos - oldPos;
@@ -1452,6 +1485,8 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
14521485
readFeatures(payloadLen);
14531486
} else if (sectionName.equals(BinaryConsts::UserSections::Dylink)) {
14541487
readDylink(payloadLen);
1488+
} else if (sectionName.equals(BinaryConsts::UserSections::Dylink0)) {
1489+
readDylink0(payloadLen);
14551490
} else {
14561491
// an unfamiliar custom section
14571492
if (sectionName.equals(BinaryConsts::UserSections::Linking)) {
@@ -3252,6 +3287,7 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
32523287

32533288
auto sectionPos = pos;
32543289

3290+
wasm.dylinkSection->isLegacy = true;
32553291
wasm.dylinkSection->memorySize = getU32LEB();
32563292
wasm.dylinkSection->memoryAlignment = getU32LEB();
32573293
wasm.dylinkSection->tableSize = getU32LEB();
@@ -3267,7 +3303,49 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
32673303
wasm.dylinkSection->tail = {tail.first, tail.second};
32683304

32693305
if (pos != sectionPos + payloadLen) {
3270-
throwError("bad features section size");
3306+
throwError("bad dylink section size");
3307+
}
3308+
3309+
}
3310+
3311+
void WasmBinaryBuilder::readDylink0(size_t payloadLen) {
3312+
BYN_TRACE("== readDylink0\n");
3313+
auto sectionPos = pos;
3314+
uint32_t lastType = 0;
3315+
3316+
wasm.dylinkSection = make_unique<DylinkSection>();
3317+
while (pos < sectionPos + payloadLen) {
3318+
auto dylinkType = getU32LEB();
3319+
if (lastType && dylinkType <= lastType) {
3320+
std::cerr << "warning: out-of-order dylink.0 subsection: " << dylinkType
3321+
<< std::endl;
3322+
}
3323+
lastType = dylinkType;
3324+
auto subsectionSize = getU32LEB();
3325+
auto subsectionPos = pos;
3326+
if (dylinkType == BinaryConsts::UserSections::Subsection::DylinkMemInfo) {
3327+
wasm.dylinkSection->memorySize = getU32LEB();
3328+
wasm.dylinkSection->memoryAlignment = getU32LEB();
3329+
wasm.dylinkSection->tableSize = getU32LEB();
3330+
wasm.dylinkSection->tableAlignment = getU32LEB();
3331+
} else if (dylinkType ==
3332+
BinaryConsts::UserSections::Subsection::DylinkNeeded) {
3333+
size_t numNeededDynlibs = getU32LEB();
3334+
for (size_t i = 0; i < numNeededDynlibs; ++i) {
3335+
wasm.dylinkSection->neededDynlibs.push_back(getInlineString());
3336+
}
3337+
} else {
3338+
std::cerr << "warning: unknown name subsection with id "
3339+
<< std::to_string(dylinkType) << " at " << pos << std::endl;
3340+
pos = subsectionPos + subsectionSize;
3341+
}
3342+
if (pos != subsectionPos + subsectionSize) {
3343+
throwError("bad dylink.0 subsection position change");
3344+
}
3345+
}
3346+
3347+
if (pos != sectionPos + payloadLen) {
3348+
throwError("bad dylink.0 section size");
32713349
}
32723350
}
32733351

src/wasm/wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace UserSections {
3131
const char* Name = "name";
3232
const char* SourceMapUrl = "sourceMappingURL";
3333
const char* Dylink = "dylink";
34+
const char* Dylink0 = "dylink.0";
3435
const char* Linking = "linking";
3536
const char* Producers = "producers";
3637
const char* TargetFeatures = "target_features";

0 commit comments

Comments
 (0)