@@ -1067,7 +1067,7 @@ void WasmBinaryWriter::writeFeaturesSection() {
1067
1067
finishSection (start);
1068
1068
}
1069
1069
1070
- void WasmBinaryWriter::writeDylinkSection () {
1070
+ void WasmBinaryWriter::writeLegacyDylinkSection () {
1071
1071
if (!wasm->dylinkSection ) {
1072
1072
return ;
1073
1073
}
@@ -1082,9 +1082,41 @@ void WasmBinaryWriter::writeDylinkSection() {
1082
1082
for (auto & neededDynlib : wasm->dylinkSection ->neededDynlibs ) {
1083
1083
writeInlineString (neededDynlib.c_str ());
1084
1084
}
1085
+ finishSection (start);
1086
+ }
1085
1087
1086
- writeData (wasm->dylinkSection ->tail .data (), wasm->dylinkSection ->tail .size ());
1088
+ void WasmBinaryWriter::writeDylinkSection () {
1089
+ if (!wasm->dylinkSection ) {
1090
+ return ;
1091
+ }
1092
+
1093
+ if (wasm->dylinkSection ->isLegacy ) {
1094
+ writeLegacyDylinkSection ();
1095
+ return ;
1096
+ }
1097
+
1098
+ auto start = startSection (BinaryConsts::User);
1099
+ writeInlineString (BinaryConsts::UserSections::Dylink0);
1100
+
1101
+ auto substart =
1102
+ startSubsection (BinaryConsts::UserSections::Subsection::DylinkMemInfo);
1103
+ o << U32LEB (wasm->dylinkSection ->memorySize );
1104
+ o << U32LEB (wasm->dylinkSection ->memoryAlignment );
1105
+ o << U32LEB (wasm->dylinkSection ->tableSize );
1106
+ o << U32LEB (wasm->dylinkSection ->tableAlignment );
1107
+ finishSubsection (substart);
1108
+
1109
+ if (wasm->dylinkSection ->neededDynlibs .size ()) {
1110
+ substart =
1111
+ startSubsection (BinaryConsts::UserSections::Subsection::DylinkNeeded);
1112
+ o << U32LEB (wasm->dylinkSection ->neededDynlibs .size ());
1113
+ for (auto & neededDynlib : wasm->dylinkSection ->neededDynlibs ) {
1114
+ writeInlineString (neededDynlib.c_str ());
1115
+ }
1116
+ finishSubsection (substart);
1117
+ }
1087
1118
1119
+ writeData (wasm->dylinkSection ->tail .data (), wasm->dylinkSection ->tail .size ());
1088
1120
finishSection (start);
1089
1121
}
1090
1122
@@ -1435,6 +1467,7 @@ void WasmBinaryBuilder::read() {
1435
1467
}
1436
1468
1437
1469
void WasmBinaryBuilder::readUserSection (size_t payloadLen) {
1470
+ BYN_TRACE (" == readUserSection\n " );
1438
1471
auto oldPos = pos;
1439
1472
Name sectionName = getInlineString ();
1440
1473
size_t read = pos - oldPos;
@@ -1452,6 +1485,8 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
1452
1485
readFeatures (payloadLen);
1453
1486
} else if (sectionName.equals (BinaryConsts::UserSections::Dylink)) {
1454
1487
readDylink (payloadLen);
1488
+ } else if (sectionName.equals (BinaryConsts::UserSections::Dylink0)) {
1489
+ readDylink0 (payloadLen);
1455
1490
} else {
1456
1491
// an unfamiliar custom section
1457
1492
if (sectionName.equals (BinaryConsts::UserSections::Linking)) {
@@ -3252,6 +3287,7 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
3252
3287
3253
3288
auto sectionPos = pos;
3254
3289
3290
+ wasm.dylinkSection ->isLegacy = true ;
3255
3291
wasm.dylinkSection ->memorySize = getU32LEB ();
3256
3292
wasm.dylinkSection ->memoryAlignment = getU32LEB ();
3257
3293
wasm.dylinkSection ->tableSize = getU32LEB ();
@@ -3262,12 +3298,50 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
3262
3298
wasm.dylinkSection ->neededDynlibs .push_back (getInlineString ());
3263
3299
}
3264
3300
3265
- size_t remaining = (sectionPos + payloadLen) - pos;
3266
- auto tail = getByteView (remaining);
3267
- wasm.dylinkSection ->tail = {tail.first , tail.second };
3268
-
3269
3301
if (pos != sectionPos + payloadLen) {
3270
- throwError (" bad features section size" );
3302
+ throwError (" bad dylink section size" );
3303
+ }
3304
+ }
3305
+
3306
+ void WasmBinaryBuilder::readDylink0 (size_t payloadLen) {
3307
+ BYN_TRACE (" == readDylink0\n " );
3308
+ auto sectionPos = pos;
3309
+ uint32_t lastType = 0 ;
3310
+
3311
+ wasm.dylinkSection = make_unique<DylinkSection>();
3312
+ while (pos < sectionPos + payloadLen) {
3313
+ auto oldPos = pos;
3314
+ auto dylinkType = getU32LEB ();
3315
+ if (lastType && dylinkType <= lastType) {
3316
+ std::cerr << " warning: out-of-order dylink.0 subsection: " << dylinkType
3317
+ << std::endl;
3318
+ }
3319
+ lastType = dylinkType;
3320
+ auto subsectionSize = getU32LEB ();
3321
+ auto subsectionPos = pos;
3322
+ if (dylinkType == BinaryConsts::UserSections::Subsection::DylinkMemInfo) {
3323
+ wasm.dylinkSection ->memorySize = getU32LEB ();
3324
+ wasm.dylinkSection ->memoryAlignment = getU32LEB ();
3325
+ wasm.dylinkSection ->tableSize = getU32LEB ();
3326
+ wasm.dylinkSection ->tableAlignment = getU32LEB ();
3327
+ } else if (dylinkType ==
3328
+ BinaryConsts::UserSections::Subsection::DylinkNeeded) {
3329
+ size_t numNeededDynlibs = getU32LEB ();
3330
+ for (size_t i = 0 ; i < numNeededDynlibs; ++i) {
3331
+ wasm.dylinkSection ->neededDynlibs .push_back (getInlineString ());
3332
+ }
3333
+ } else {
3334
+ // Unknown subsection. Stop parsing now and store the rest of
3335
+ // the section verbatim.
3336
+ pos = oldPos;
3337
+ size_t remaining = (sectionPos + payloadLen) - pos;
3338
+ auto tail = getByteView (remaining);
3339
+ wasm.dylinkSection ->tail = {tail.first , tail.second };
3340
+ break ;
3341
+ }
3342
+ if (pos != subsectionPos + subsectionSize) {
3343
+ throwError (" bad dylink.0 subsection position change" );
3344
+ }
3271
3345
}
3272
3346
}
3273
3347
0 commit comments