Skip to content

Commit 569fda5

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 569fda5

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-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: 81 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,40 @@ 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 =
1105+
startSubsection(BinaryConsts::UserSections::Subsection::DylinkMemInfo);
1106+
o << U32LEB(wasm->dylinkSection->memorySize);
1107+
o << U32LEB(wasm->dylinkSection->memoryAlignment);
1108+
o << U32LEB(wasm->dylinkSection->tableSize);
1109+
o << U32LEB(wasm->dylinkSection->tableAlignment);
1110+
finishSubsection(substart);
1111+
1112+
if (wasm->dylinkSection->neededDynlibs.size()) {
1113+
substart =
1114+
startSubsection(BinaryConsts::UserSections::Subsection::DylinkNeeded);
1115+
o << U32LEB(wasm->dylinkSection->neededDynlibs.size());
1116+
for (auto& neededDynlib : wasm->dylinkSection->neededDynlibs) {
1117+
writeInlineString(neededDynlib.c_str());
1118+
}
1119+
finishSubsection(substart);
1120+
}
1121+
1122+
finishSection(start);
1123+
}
1124+
10911125
void WasmBinaryWriter::writeDebugLocation(const Function::DebugLocation& loc) {
10921126
if (loc == lastDebugLocation) {
10931127
return;
@@ -1435,6 +1469,7 @@ void WasmBinaryBuilder::read() {
14351469
}
14361470

14371471
void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
1472+
BYN_TRACE("== readUserSection\n");
14381473
auto oldPos = pos;
14391474
Name sectionName = getInlineString();
14401475
size_t read = pos - oldPos;
@@ -1452,6 +1487,8 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
14521487
readFeatures(payloadLen);
14531488
} else if (sectionName.equals(BinaryConsts::UserSections::Dylink)) {
14541489
readDylink(payloadLen);
1490+
} else if (sectionName.equals(BinaryConsts::UserSections::Dylink0)) {
1491+
readDylink0(payloadLen);
14551492
} else {
14561493
// an unfamiliar custom section
14571494
if (sectionName.equals(BinaryConsts::UserSections::Linking)) {
@@ -3252,6 +3289,7 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
32523289

32533290
auto sectionPos = pos;
32543291

3292+
wasm.dylinkSection->isLegacy = true;
32553293
wasm.dylinkSection->memorySize = getU32LEB();
32563294
wasm.dylinkSection->memoryAlignment = getU32LEB();
32573295
wasm.dylinkSection->tableSize = getU32LEB();
@@ -3267,7 +3305,48 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
32673305
wasm.dylinkSection->tail = {tail.first, tail.second};
32683306

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

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)