WasmObjectFile.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/ArrayRef.h"
  9. #include "llvm/ADT/DenseSet.h"
  10. #include "llvm/ADT/SmallSet.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/ADT/StringSet.h"
  13. #include "llvm/ADT/StringSwitch.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/BinaryFormat/Wasm.h"
  16. #include "llvm/MC/SubtargetFeature.h"
  17. #include "llvm/Object/Binary.h"
  18. #include "llvm/Object/Error.h"
  19. #include "llvm/Object/ObjectFile.h"
  20. #include "llvm/Object/SymbolicFile.h"
  21. #include "llvm/Object/Wasm.h"
  22. #include "llvm/Support/Endian.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/LEB128.h"
  26. #include "llvm/Support/ScopedPrinter.h"
  27. #include <algorithm>
  28. #include <cassert>
  29. #include <cstdint>
  30. #include <cstring>
  31. #define DEBUG_TYPE "wasm-object"
  32. using namespace llvm;
  33. using namespace object;
  34. void WasmSymbol::print(raw_ostream &Out) const {
  35. Out << "Name=" << Info.Name
  36. << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) << ", Flags=0x"
  37. << Twine::utohexstr(Info.Flags);
  38. if (!isTypeData()) {
  39. Out << ", ElemIndex=" << Info.ElementIndex;
  40. } else if (isDefined()) {
  41. Out << ", Segment=" << Info.DataRef.Segment;
  42. Out << ", Offset=" << Info.DataRef.Offset;
  43. Out << ", Size=" << Info.DataRef.Size;
  44. }
  45. }
  46. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  47. LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); }
  48. #endif
  49. Expected<std::unique_ptr<WasmObjectFile>>
  50. ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
  51. Error Err = Error::success();
  52. auto ObjectFile = std::make_unique<WasmObjectFile>(Buffer, Err);
  53. if (Err)
  54. return std::move(Err);
  55. return std::move(ObjectFile);
  56. }
  57. #define VARINT7_MAX ((1 << 7) - 1)
  58. #define VARINT7_MIN (-(1 << 7))
  59. #define VARUINT7_MAX (1 << 7)
  60. #define VARUINT1_MAX (1)
  61. static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) {
  62. if (Ctx.Ptr == Ctx.End)
  63. report_fatal_error("EOF while reading uint8");
  64. return *Ctx.Ptr++;
  65. }
  66. static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) {
  67. if (Ctx.Ptr + 4 > Ctx.End)
  68. report_fatal_error("EOF while reading uint32");
  69. uint32_t Result = support::endian::read32le(Ctx.Ptr);
  70. Ctx.Ptr += 4;
  71. return Result;
  72. }
  73. static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) {
  74. if (Ctx.Ptr + 4 > Ctx.End)
  75. report_fatal_error("EOF while reading float64");
  76. int32_t Result = 0;
  77. memcpy(&Result, Ctx.Ptr, sizeof(Result));
  78. Ctx.Ptr += sizeof(Result);
  79. return Result;
  80. }
  81. static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) {
  82. if (Ctx.Ptr + 8 > Ctx.End)
  83. report_fatal_error("EOF while reading float64");
  84. int64_t Result = 0;
  85. memcpy(&Result, Ctx.Ptr, sizeof(Result));
  86. Ctx.Ptr += sizeof(Result);
  87. return Result;
  88. }
  89. static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) {
  90. unsigned Count;
  91. const char *Error = nullptr;
  92. uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
  93. if (Error)
  94. report_fatal_error(Error);
  95. Ctx.Ptr += Count;
  96. return Result;
  97. }
  98. static StringRef readString(WasmObjectFile::ReadContext &Ctx) {
  99. uint32_t StringLen = readULEB128(Ctx);
  100. if (Ctx.Ptr + StringLen > Ctx.End)
  101. report_fatal_error("EOF while reading string");
  102. StringRef Return =
  103. StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
  104. Ctx.Ptr += StringLen;
  105. return Return;
  106. }
  107. static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) {
  108. unsigned Count;
  109. const char *Error = nullptr;
  110. uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
  111. if (Error)
  112. report_fatal_error(Error);
  113. Ctx.Ptr += Count;
  114. return Result;
  115. }
  116. static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) {
  117. int64_t Result = readLEB128(Ctx);
  118. if (Result > VARUINT1_MAX || Result < 0)
  119. report_fatal_error("LEB is outside Varuint1 range");
  120. return Result;
  121. }
  122. static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) {
  123. int64_t Result = readLEB128(Ctx);
  124. if (Result > INT32_MAX || Result < INT32_MIN)
  125. report_fatal_error("LEB is outside Varint32 range");
  126. return Result;
  127. }
  128. static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) {
  129. uint64_t Result = readULEB128(Ctx);
  130. if (Result > UINT32_MAX)
  131. report_fatal_error("LEB is outside Varuint32 range");
  132. return Result;
  133. }
  134. static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) {
  135. return readLEB128(Ctx);
  136. }
  137. static uint64_t readVaruint64(WasmObjectFile::ReadContext &Ctx) {
  138. return readULEB128(Ctx);
  139. }
  140. static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) {
  141. return readUint8(Ctx);
  142. }
  143. static Error readInitExpr(wasm::WasmInitExpr &Expr,
  144. WasmObjectFile::ReadContext &Ctx) {
  145. auto Start = Ctx.Ptr;
  146. Expr.Extended = false;
  147. Expr.Inst.Opcode = readOpcode(Ctx);
  148. switch (Expr.Inst.Opcode) {
  149. case wasm::WASM_OPCODE_I32_CONST:
  150. Expr.Inst.Value.Int32 = readVarint32(Ctx);
  151. break;
  152. case wasm::WASM_OPCODE_I64_CONST:
  153. Expr.Inst.Value.Int64 = readVarint64(Ctx);
  154. break;
  155. case wasm::WASM_OPCODE_F32_CONST:
  156. Expr.Inst.Value.Float32 = readFloat32(Ctx);
  157. break;
  158. case wasm::WASM_OPCODE_F64_CONST:
  159. Expr.Inst.Value.Float64 = readFloat64(Ctx);
  160. break;
  161. case wasm::WASM_OPCODE_GLOBAL_GET:
  162. Expr.Inst.Value.Global = readULEB128(Ctx);
  163. break;
  164. case wasm::WASM_OPCODE_REF_NULL: {
  165. wasm::ValType Ty = static_cast<wasm::ValType>(readULEB128(Ctx));
  166. if (Ty != wasm::ValType::EXTERNREF) {
  167. return make_error<GenericBinaryError>("invalid type for ref.null",
  168. object_error::parse_failed);
  169. }
  170. break;
  171. }
  172. default:
  173. Expr.Extended = true;
  174. }
  175. if (!Expr.Extended) {
  176. uint8_t EndOpcode = readOpcode(Ctx);
  177. if (EndOpcode != wasm::WASM_OPCODE_END)
  178. Expr.Extended = true;
  179. }
  180. if (Expr.Extended) {
  181. Ctx.Ptr = Start;
  182. while (true) {
  183. uint8_t Opcode = readOpcode(Ctx);
  184. switch (Opcode) {
  185. case wasm::WASM_OPCODE_I32_CONST:
  186. case wasm::WASM_OPCODE_GLOBAL_GET:
  187. case wasm::WASM_OPCODE_REF_NULL:
  188. case wasm::WASM_OPCODE_I64_CONST:
  189. case wasm::WASM_OPCODE_F32_CONST:
  190. case wasm::WASM_OPCODE_F64_CONST:
  191. readULEB128(Ctx);
  192. break;
  193. case wasm::WASM_OPCODE_I32_ADD:
  194. case wasm::WASM_OPCODE_I32_SUB:
  195. case wasm::WASM_OPCODE_I32_MUL:
  196. case wasm::WASM_OPCODE_I64_ADD:
  197. case wasm::WASM_OPCODE_I64_SUB:
  198. case wasm::WASM_OPCODE_I64_MUL:
  199. break;
  200. case wasm::WASM_OPCODE_END:
  201. Expr.Body = ArrayRef<uint8_t>(Start, Ctx.Ptr - Start);
  202. return Error::success();
  203. default:
  204. return make_error<GenericBinaryError>(
  205. Twine("invalid opcode in init_expr: ") + Twine(unsigned(Opcode)),
  206. object_error::parse_failed);
  207. }
  208. }
  209. }
  210. return Error::success();
  211. }
  212. static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) {
  213. wasm::WasmLimits Result;
  214. Result.Flags = readVaruint32(Ctx);
  215. Result.Minimum = readVaruint64(Ctx);
  216. if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
  217. Result.Maximum = readVaruint64(Ctx);
  218. return Result;
  219. }
  220. static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx) {
  221. wasm::WasmTableType TableType;
  222. TableType.ElemType = readUint8(Ctx);
  223. TableType.Limits = readLimits(Ctx);
  224. return TableType;
  225. }
  226. static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx,
  227. WasmSectionOrderChecker &Checker) {
  228. Section.Offset = Ctx.Ptr - Ctx.Start;
  229. Section.Type = readUint8(Ctx);
  230. LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
  231. uint32_t Size = readVaruint32(Ctx);
  232. if (Size == 0)
  233. return make_error<StringError>("zero length section",
  234. object_error::parse_failed);
  235. if (Ctx.Ptr + Size > Ctx.End)
  236. return make_error<StringError>("section too large",
  237. object_error::parse_failed);
  238. if (Section.Type == wasm::WASM_SEC_CUSTOM) {
  239. WasmObjectFile::ReadContext SectionCtx;
  240. SectionCtx.Start = Ctx.Ptr;
  241. SectionCtx.Ptr = Ctx.Ptr;
  242. SectionCtx.End = Ctx.Ptr + Size;
  243. Section.Name = readString(SectionCtx);
  244. uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start;
  245. Ctx.Ptr += SectionNameSize;
  246. Size -= SectionNameSize;
  247. }
  248. if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) {
  249. return make_error<StringError>("out of order section type: " +
  250. llvm::to_string(Section.Type),
  251. object_error::parse_failed);
  252. }
  253. Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
  254. Ctx.Ptr += Size;
  255. return Error::success();
  256. }
  257. WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
  258. : ObjectFile(Binary::ID_Wasm, Buffer) {
  259. ErrorAsOutParameter ErrAsOutParam(&Err);
  260. Header.Magic = getData().substr(0, 4);
  261. if (Header.Magic != StringRef("\0asm", 4)) {
  262. Err = make_error<StringError>("invalid magic number",
  263. object_error::parse_failed);
  264. return;
  265. }
  266. ReadContext Ctx;
  267. Ctx.Start = getData().bytes_begin();
  268. Ctx.Ptr = Ctx.Start + 4;
  269. Ctx.End = Ctx.Start + getData().size();
  270. if (Ctx.Ptr + 4 > Ctx.End) {
  271. Err = make_error<StringError>("missing version number",
  272. object_error::parse_failed);
  273. return;
  274. }
  275. Header.Version = readUint32(Ctx);
  276. if (Header.Version != wasm::WasmVersion) {
  277. Err = make_error<StringError>("invalid version number: " +
  278. Twine(Header.Version),
  279. object_error::parse_failed);
  280. return;
  281. }
  282. WasmSectionOrderChecker Checker;
  283. while (Ctx.Ptr < Ctx.End) {
  284. WasmSection Sec;
  285. if ((Err = readSection(Sec, Ctx, Checker)))
  286. return;
  287. if ((Err = parseSection(Sec)))
  288. return;
  289. Sections.push_back(Sec);
  290. }
  291. }
  292. Error WasmObjectFile::parseSection(WasmSection &Sec) {
  293. ReadContext Ctx;
  294. Ctx.Start = Sec.Content.data();
  295. Ctx.End = Ctx.Start + Sec.Content.size();
  296. Ctx.Ptr = Ctx.Start;
  297. switch (Sec.Type) {
  298. case wasm::WASM_SEC_CUSTOM:
  299. return parseCustomSection(Sec, Ctx);
  300. case wasm::WASM_SEC_TYPE:
  301. return parseTypeSection(Ctx);
  302. case wasm::WASM_SEC_IMPORT:
  303. return parseImportSection(Ctx);
  304. case wasm::WASM_SEC_FUNCTION:
  305. return parseFunctionSection(Ctx);
  306. case wasm::WASM_SEC_TABLE:
  307. return parseTableSection(Ctx);
  308. case wasm::WASM_SEC_MEMORY:
  309. return parseMemorySection(Ctx);
  310. case wasm::WASM_SEC_TAG:
  311. return parseTagSection(Ctx);
  312. case wasm::WASM_SEC_GLOBAL:
  313. return parseGlobalSection(Ctx);
  314. case wasm::WASM_SEC_EXPORT:
  315. return parseExportSection(Ctx);
  316. case wasm::WASM_SEC_START:
  317. return parseStartSection(Ctx);
  318. case wasm::WASM_SEC_ELEM:
  319. return parseElemSection(Ctx);
  320. case wasm::WASM_SEC_CODE:
  321. return parseCodeSection(Ctx);
  322. case wasm::WASM_SEC_DATA:
  323. return parseDataSection(Ctx);
  324. case wasm::WASM_SEC_DATACOUNT:
  325. return parseDataCountSection(Ctx);
  326. default:
  327. return make_error<GenericBinaryError>(
  328. "invalid section type: " + Twine(Sec.Type), object_error::parse_failed);
  329. }
  330. }
  331. Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
  332. // Legacy "dylink" section support.
  333. // See parseDylink0Section for the current "dylink.0" section parsing.
  334. HasDylinkSection = true;
  335. DylinkInfo.MemorySize = readVaruint32(Ctx);
  336. DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
  337. DylinkInfo.TableSize = readVaruint32(Ctx);
  338. DylinkInfo.TableAlignment = readVaruint32(Ctx);
  339. uint32_t Count = readVaruint32(Ctx);
  340. while (Count--) {
  341. DylinkInfo.Needed.push_back(readString(Ctx));
  342. }
  343. if (Ctx.Ptr != Ctx.End)
  344. return make_error<GenericBinaryError>("dylink section ended prematurely",
  345. object_error::parse_failed);
  346. return Error::success();
  347. }
  348. Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) {
  349. // See
  350. // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
  351. HasDylinkSection = true;
  352. const uint8_t *OrigEnd = Ctx.End;
  353. while (Ctx.Ptr < OrigEnd) {
  354. Ctx.End = OrigEnd;
  355. uint8_t Type = readUint8(Ctx);
  356. uint32_t Size = readVaruint32(Ctx);
  357. LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
  358. << "\n");
  359. Ctx.End = Ctx.Ptr + Size;
  360. uint32_t Count;
  361. switch (Type) {
  362. case wasm::WASM_DYLINK_MEM_INFO:
  363. DylinkInfo.MemorySize = readVaruint32(Ctx);
  364. DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
  365. DylinkInfo.TableSize = readVaruint32(Ctx);
  366. DylinkInfo.TableAlignment = readVaruint32(Ctx);
  367. break;
  368. case wasm::WASM_DYLINK_NEEDED:
  369. Count = readVaruint32(Ctx);
  370. while (Count--) {
  371. DylinkInfo.Needed.push_back(readString(Ctx));
  372. }
  373. break;
  374. case wasm::WASM_DYLINK_EXPORT_INFO: {
  375. uint32_t Count = readVaruint32(Ctx);
  376. while (Count--) {
  377. DylinkInfo.ExportInfo.push_back({readString(Ctx), readVaruint32(Ctx)});
  378. }
  379. break;
  380. }
  381. case wasm::WASM_DYLINK_IMPORT_INFO: {
  382. uint32_t Count = readVaruint32(Ctx);
  383. while (Count--) {
  384. DylinkInfo.ImportInfo.push_back(
  385. {readString(Ctx), readString(Ctx), readVaruint32(Ctx)});
  386. }
  387. break;
  388. }
  389. default:
  390. LLVM_DEBUG(dbgs() << "unknown dylink.0 sub-section: " << Type << "\n");
  391. Ctx.Ptr += Size;
  392. break;
  393. }
  394. if (Ctx.Ptr != Ctx.End) {
  395. return make_error<GenericBinaryError>(
  396. "dylink.0 sub-section ended prematurely", object_error::parse_failed);
  397. }
  398. }
  399. if (Ctx.Ptr != Ctx.End)
  400. return make_error<GenericBinaryError>("dylink.0 section ended prematurely",
  401. object_error::parse_failed);
  402. return Error::success();
  403. }
  404. Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
  405. llvm::DenseSet<uint64_t> SeenFunctions;
  406. llvm::DenseSet<uint64_t> SeenGlobals;
  407. llvm::DenseSet<uint64_t> SeenSegments;
  408. while (Ctx.Ptr < Ctx.End) {
  409. uint8_t Type = readUint8(Ctx);
  410. uint32_t Size = readVaruint32(Ctx);
  411. const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
  412. switch (Type) {
  413. case wasm::WASM_NAMES_FUNCTION:
  414. case wasm::WASM_NAMES_GLOBAL:
  415. case wasm::WASM_NAMES_DATA_SEGMENT: {
  416. uint32_t Count = readVaruint32(Ctx);
  417. while (Count--) {
  418. uint32_t Index = readVaruint32(Ctx);
  419. StringRef Name = readString(Ctx);
  420. wasm::NameType nameType = wasm::NameType::FUNCTION;
  421. if (Type == wasm::WASM_NAMES_FUNCTION) {
  422. if (!SeenFunctions.insert(Index).second)
  423. return make_error<GenericBinaryError>(
  424. "function named more than once", object_error::parse_failed);
  425. if (!isValidFunctionIndex(Index) || Name.empty())
  426. return make_error<GenericBinaryError>("invalid function name entry",
  427. object_error::parse_failed);
  428. if (isDefinedFunctionIndex(Index))
  429. getDefinedFunction(Index).DebugName = Name;
  430. } else if (Type == wasm::WASM_NAMES_GLOBAL) {
  431. nameType = wasm::NameType::GLOBAL;
  432. if (!SeenGlobals.insert(Index).second)
  433. return make_error<GenericBinaryError>("global named more than once",
  434. object_error::parse_failed);
  435. if (!isValidGlobalIndex(Index) || Name.empty())
  436. return make_error<GenericBinaryError>("invalid global name entry",
  437. object_error::parse_failed);
  438. } else {
  439. nameType = wasm::NameType::DATA_SEGMENT;
  440. if (!SeenSegments.insert(Index).second)
  441. return make_error<GenericBinaryError>(
  442. "segment named more than once", object_error::parse_failed);
  443. if (Index > DataSegments.size())
  444. return make_error<GenericBinaryError>("invalid data segment name entry",
  445. object_error::parse_failed);
  446. }
  447. DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name});
  448. }
  449. break;
  450. }
  451. // Ignore local names for now
  452. case wasm::WASM_NAMES_LOCAL:
  453. default:
  454. Ctx.Ptr += Size;
  455. break;
  456. }
  457. if (Ctx.Ptr != SubSectionEnd)
  458. return make_error<GenericBinaryError>(
  459. "name sub-section ended prematurely", object_error::parse_failed);
  460. }
  461. if (Ctx.Ptr != Ctx.End)
  462. return make_error<GenericBinaryError>("name section ended prematurely",
  463. object_error::parse_failed);
  464. return Error::success();
  465. }
  466. Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
  467. HasLinkingSection = true;
  468. LinkingData.Version = readVaruint32(Ctx);
  469. if (LinkingData.Version != wasm::WasmMetadataVersion) {
  470. return make_error<GenericBinaryError>(
  471. "unexpected metadata version: " + Twine(LinkingData.Version) +
  472. " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
  473. object_error::parse_failed);
  474. }
  475. const uint8_t *OrigEnd = Ctx.End;
  476. while (Ctx.Ptr < OrigEnd) {
  477. Ctx.End = OrigEnd;
  478. uint8_t Type = readUint8(Ctx);
  479. uint32_t Size = readVaruint32(Ctx);
  480. LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
  481. << "\n");
  482. Ctx.End = Ctx.Ptr + Size;
  483. switch (Type) {
  484. case wasm::WASM_SYMBOL_TABLE:
  485. if (Error Err = parseLinkingSectionSymtab(Ctx))
  486. return Err;
  487. break;
  488. case wasm::WASM_SEGMENT_INFO: {
  489. uint32_t Count = readVaruint32(Ctx);
  490. if (Count > DataSegments.size())
  491. return make_error<GenericBinaryError>("too many segment names",
  492. object_error::parse_failed);
  493. for (uint32_t I = 0; I < Count; I++) {
  494. DataSegments[I].Data.Name = readString(Ctx);
  495. DataSegments[I].Data.Alignment = readVaruint32(Ctx);
  496. DataSegments[I].Data.LinkingFlags = readVaruint32(Ctx);
  497. }
  498. break;
  499. }
  500. case wasm::WASM_INIT_FUNCS: {
  501. uint32_t Count = readVaruint32(Ctx);
  502. LinkingData.InitFunctions.reserve(Count);
  503. for (uint32_t I = 0; I < Count; I++) {
  504. wasm::WasmInitFunc Init;
  505. Init.Priority = readVaruint32(Ctx);
  506. Init.Symbol = readVaruint32(Ctx);
  507. if (!isValidFunctionSymbol(Init.Symbol))
  508. return make_error<GenericBinaryError>("invalid function symbol: " +
  509. Twine(Init.Symbol),
  510. object_error::parse_failed);
  511. LinkingData.InitFunctions.emplace_back(Init);
  512. }
  513. break;
  514. }
  515. case wasm::WASM_COMDAT_INFO:
  516. if (Error Err = parseLinkingSectionComdat(Ctx))
  517. return Err;
  518. break;
  519. default:
  520. Ctx.Ptr += Size;
  521. break;
  522. }
  523. if (Ctx.Ptr != Ctx.End)
  524. return make_error<GenericBinaryError>(
  525. "linking sub-section ended prematurely", object_error::parse_failed);
  526. }
  527. if (Ctx.Ptr != OrigEnd)
  528. return make_error<GenericBinaryError>("linking section ended prematurely",
  529. object_error::parse_failed);
  530. return Error::success();
  531. }
  532. Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
  533. uint32_t Count = readVaruint32(Ctx);
  534. LinkingData.SymbolTable.reserve(Count);
  535. Symbols.reserve(Count);
  536. StringSet<> SymbolNames;
  537. std::vector<wasm::WasmImport *> ImportedGlobals;
  538. std::vector<wasm::WasmImport *> ImportedFunctions;
  539. std::vector<wasm::WasmImport *> ImportedTags;
  540. std::vector<wasm::WasmImport *> ImportedTables;
  541. ImportedGlobals.reserve(Imports.size());
  542. ImportedFunctions.reserve(Imports.size());
  543. ImportedTags.reserve(Imports.size());
  544. ImportedTables.reserve(Imports.size());
  545. for (auto &I : Imports) {
  546. if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION)
  547. ImportedFunctions.emplace_back(&I);
  548. else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
  549. ImportedGlobals.emplace_back(&I);
  550. else if (I.Kind == wasm::WASM_EXTERNAL_TAG)
  551. ImportedTags.emplace_back(&I);
  552. else if (I.Kind == wasm::WASM_EXTERNAL_TABLE)
  553. ImportedTables.emplace_back(&I);
  554. }
  555. while (Count--) {
  556. wasm::WasmSymbolInfo Info;
  557. const wasm::WasmSignature *Signature = nullptr;
  558. const wasm::WasmGlobalType *GlobalType = nullptr;
  559. const wasm::WasmTableType *TableType = nullptr;
  560. Info.Kind = readUint8(Ctx);
  561. Info.Flags = readVaruint32(Ctx);
  562. bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
  563. switch (Info.Kind) {
  564. case wasm::WASM_SYMBOL_TYPE_FUNCTION:
  565. Info.ElementIndex = readVaruint32(Ctx);
  566. if (!isValidFunctionIndex(Info.ElementIndex) ||
  567. IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
  568. return make_error<GenericBinaryError>("invalid function symbol index",
  569. object_error::parse_failed);
  570. if (IsDefined) {
  571. Info.Name = readString(Ctx);
  572. unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
  573. wasm::WasmFunction &Function = Functions[FuncIndex];
  574. Signature = &Signatures[Function.SigIndex];
  575. if (Function.SymbolName.empty())
  576. Function.SymbolName = Info.Name;
  577. } else {
  578. wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
  579. if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
  580. Info.Name = readString(Ctx);
  581. Info.ImportName = Import.Field;
  582. } else {
  583. Info.Name = Import.Field;
  584. }
  585. Signature = &Signatures[Import.SigIndex];
  586. Info.ImportModule = Import.Module;
  587. }
  588. break;
  589. case wasm::WASM_SYMBOL_TYPE_GLOBAL:
  590. Info.ElementIndex = readVaruint32(Ctx);
  591. if (!isValidGlobalIndex(Info.ElementIndex) ||
  592. IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
  593. return make_error<GenericBinaryError>("invalid global symbol index",
  594. object_error::parse_failed);
  595. if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
  596. wasm::WASM_SYMBOL_BINDING_WEAK)
  597. return make_error<GenericBinaryError>("undefined weak global symbol",
  598. object_error::parse_failed);
  599. if (IsDefined) {
  600. Info.Name = readString(Ctx);
  601. unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
  602. wasm::WasmGlobal &Global = Globals[GlobalIndex];
  603. GlobalType = &Global.Type;
  604. if (Global.SymbolName.empty())
  605. Global.SymbolName = Info.Name;
  606. } else {
  607. wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
  608. if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
  609. Info.Name = readString(Ctx);
  610. Info.ImportName = Import.Field;
  611. } else {
  612. Info.Name = Import.Field;
  613. }
  614. GlobalType = &Import.Global;
  615. Info.ImportModule = Import.Module;
  616. }
  617. break;
  618. case wasm::WASM_SYMBOL_TYPE_TABLE:
  619. Info.ElementIndex = readVaruint32(Ctx);
  620. if (!isValidTableNumber(Info.ElementIndex) ||
  621. IsDefined != isDefinedTableNumber(Info.ElementIndex))
  622. return make_error<GenericBinaryError>("invalid table symbol index",
  623. object_error::parse_failed);
  624. if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
  625. wasm::WASM_SYMBOL_BINDING_WEAK)
  626. return make_error<GenericBinaryError>("undefined weak table symbol",
  627. object_error::parse_failed);
  628. if (IsDefined) {
  629. Info.Name = readString(Ctx);
  630. unsigned TableNumber = Info.ElementIndex - NumImportedTables;
  631. wasm::WasmTable &Table = Tables[TableNumber];
  632. TableType = &Table.Type;
  633. if (Table.SymbolName.empty())
  634. Table.SymbolName = Info.Name;
  635. } else {
  636. wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex];
  637. if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
  638. Info.Name = readString(Ctx);
  639. Info.ImportName = Import.Field;
  640. } else {
  641. Info.Name = Import.Field;
  642. }
  643. TableType = &Import.Table;
  644. Info.ImportModule = Import.Module;
  645. }
  646. break;
  647. case wasm::WASM_SYMBOL_TYPE_DATA:
  648. Info.Name = readString(Ctx);
  649. if (IsDefined) {
  650. auto Index = readVaruint32(Ctx);
  651. if (Index >= DataSegments.size())
  652. return make_error<GenericBinaryError>("invalid data symbol index",
  653. object_error::parse_failed);
  654. auto Offset = readVaruint64(Ctx);
  655. auto Size = readVaruint64(Ctx);
  656. size_t SegmentSize = DataSegments[Index].Data.Content.size();
  657. if (Offset > SegmentSize)
  658. return make_error<GenericBinaryError>(
  659. "invalid data symbol offset: `" + Info.Name + "` (offset: " +
  660. Twine(Offset) + " segment size: " + Twine(SegmentSize) + ")",
  661. object_error::parse_failed);
  662. Info.DataRef = wasm::WasmDataReference{Index, Offset, Size};
  663. }
  664. break;
  665. case wasm::WASM_SYMBOL_TYPE_SECTION: {
  666. if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
  667. wasm::WASM_SYMBOL_BINDING_LOCAL)
  668. return make_error<GenericBinaryError>(
  669. "section symbols must have local binding",
  670. object_error::parse_failed);
  671. Info.ElementIndex = readVaruint32(Ctx);
  672. // Use somewhat unique section name as symbol name.
  673. StringRef SectionName = Sections[Info.ElementIndex].Name;
  674. Info.Name = SectionName;
  675. break;
  676. }
  677. case wasm::WASM_SYMBOL_TYPE_TAG: {
  678. Info.ElementIndex = readVaruint32(Ctx);
  679. if (!isValidTagIndex(Info.ElementIndex) ||
  680. IsDefined != isDefinedTagIndex(Info.ElementIndex))
  681. return make_error<GenericBinaryError>("invalid tag symbol index",
  682. object_error::parse_failed);
  683. if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
  684. wasm::WASM_SYMBOL_BINDING_WEAK)
  685. return make_error<GenericBinaryError>("undefined weak global symbol",
  686. object_error::parse_failed);
  687. if (IsDefined) {
  688. Info.Name = readString(Ctx);
  689. unsigned TagIndex = Info.ElementIndex - NumImportedTags;
  690. wasm::WasmTag &Tag = Tags[TagIndex];
  691. Signature = &Signatures[Tag.SigIndex];
  692. if (Tag.SymbolName.empty())
  693. Tag.SymbolName = Info.Name;
  694. } else {
  695. wasm::WasmImport &Import = *ImportedTags[Info.ElementIndex];
  696. if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
  697. Info.Name = readString(Ctx);
  698. Info.ImportName = Import.Field;
  699. } else {
  700. Info.Name = Import.Field;
  701. }
  702. Signature = &Signatures[Import.SigIndex];
  703. Info.ImportModule = Import.Module;
  704. }
  705. break;
  706. }
  707. default:
  708. return make_error<GenericBinaryError>("invalid symbol type: " +
  709. Twine(unsigned(Info.Kind)),
  710. object_error::parse_failed);
  711. }
  712. if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
  713. wasm::WASM_SYMBOL_BINDING_LOCAL &&
  714. !SymbolNames.insert(Info.Name).second)
  715. return make_error<GenericBinaryError>("duplicate symbol name " +
  716. Twine(Info.Name),
  717. object_error::parse_failed);
  718. LinkingData.SymbolTable.emplace_back(Info);
  719. Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, TableType,
  720. Signature);
  721. LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
  722. }
  723. return Error::success();
  724. }
  725. Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
  726. uint32_t ComdatCount = readVaruint32(Ctx);
  727. StringSet<> ComdatSet;
  728. for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
  729. StringRef Name = readString(Ctx);
  730. if (Name.empty() || !ComdatSet.insert(Name).second)
  731. return make_error<GenericBinaryError>("bad/duplicate COMDAT name " +
  732. Twine(Name),
  733. object_error::parse_failed);
  734. LinkingData.Comdats.emplace_back(Name);
  735. uint32_t Flags = readVaruint32(Ctx);
  736. if (Flags != 0)
  737. return make_error<GenericBinaryError>("unsupported COMDAT flags",
  738. object_error::parse_failed);
  739. uint32_t EntryCount = readVaruint32(Ctx);
  740. while (EntryCount--) {
  741. unsigned Kind = readVaruint32(Ctx);
  742. unsigned Index = readVaruint32(Ctx);
  743. switch (Kind) {
  744. default:
  745. return make_error<GenericBinaryError>("invalid COMDAT entry type",
  746. object_error::parse_failed);
  747. case wasm::WASM_COMDAT_DATA:
  748. if (Index >= DataSegments.size())
  749. return make_error<GenericBinaryError>(
  750. "COMDAT data index out of range", object_error::parse_failed);
  751. if (DataSegments[Index].Data.Comdat != UINT32_MAX)
  752. return make_error<GenericBinaryError>("data segment in two COMDATs",
  753. object_error::parse_failed);
  754. DataSegments[Index].Data.Comdat = ComdatIndex;
  755. break;
  756. case wasm::WASM_COMDAT_FUNCTION:
  757. if (!isDefinedFunctionIndex(Index))
  758. return make_error<GenericBinaryError>(
  759. "COMDAT function index out of range", object_error::parse_failed);
  760. if (getDefinedFunction(Index).Comdat != UINT32_MAX)
  761. return make_error<GenericBinaryError>("function in two COMDATs",
  762. object_error::parse_failed);
  763. getDefinedFunction(Index).Comdat = ComdatIndex;
  764. break;
  765. case wasm::WASM_COMDAT_SECTION:
  766. if (Index >= Sections.size())
  767. return make_error<GenericBinaryError>(
  768. "COMDAT section index out of range", object_error::parse_failed);
  769. if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM)
  770. return make_error<GenericBinaryError>(
  771. "non-custom section in a COMDAT", object_error::parse_failed);
  772. Sections[Index].Comdat = ComdatIndex;
  773. break;
  774. }
  775. }
  776. }
  777. return Error::success();
  778. }
  779. Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
  780. llvm::SmallSet<StringRef, 3> FieldsSeen;
  781. uint32_t Fields = readVaruint32(Ctx);
  782. for (size_t I = 0; I < Fields; ++I) {
  783. StringRef FieldName = readString(Ctx);
  784. if (!FieldsSeen.insert(FieldName).second)
  785. return make_error<GenericBinaryError>(
  786. "producers section does not have unique fields",
  787. object_error::parse_failed);
  788. std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr;
  789. if (FieldName == "language") {
  790. ProducerVec = &ProducerInfo.Languages;
  791. } else if (FieldName == "processed-by") {
  792. ProducerVec = &ProducerInfo.Tools;
  793. } else if (FieldName == "sdk") {
  794. ProducerVec = &ProducerInfo.SDKs;
  795. } else {
  796. return make_error<GenericBinaryError>(
  797. "producers section field is not named one of language, processed-by, "
  798. "or sdk",
  799. object_error::parse_failed);
  800. }
  801. uint32_t ValueCount = readVaruint32(Ctx);
  802. llvm::SmallSet<StringRef, 8> ProducersSeen;
  803. for (size_t J = 0; J < ValueCount; ++J) {
  804. StringRef Name = readString(Ctx);
  805. StringRef Version = readString(Ctx);
  806. if (!ProducersSeen.insert(Name).second) {
  807. return make_error<GenericBinaryError>(
  808. "producers section contains repeated producer",
  809. object_error::parse_failed);
  810. }
  811. ProducerVec->emplace_back(std::string(Name), std::string(Version));
  812. }
  813. }
  814. if (Ctx.Ptr != Ctx.End)
  815. return make_error<GenericBinaryError>("producers section ended prematurely",
  816. object_error::parse_failed);
  817. return Error::success();
  818. }
  819. Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
  820. llvm::SmallSet<std::string, 8> FeaturesSeen;
  821. uint32_t FeatureCount = readVaruint32(Ctx);
  822. for (size_t I = 0; I < FeatureCount; ++I) {
  823. wasm::WasmFeatureEntry Feature;
  824. Feature.Prefix = readUint8(Ctx);
  825. switch (Feature.Prefix) {
  826. case wasm::WASM_FEATURE_PREFIX_USED:
  827. case wasm::WASM_FEATURE_PREFIX_REQUIRED:
  828. case wasm::WASM_FEATURE_PREFIX_DISALLOWED:
  829. break;
  830. default:
  831. return make_error<GenericBinaryError>("unknown feature policy prefix",
  832. object_error::parse_failed);
  833. }
  834. Feature.Name = std::string(readString(Ctx));
  835. if (!FeaturesSeen.insert(Feature.Name).second)
  836. return make_error<GenericBinaryError>(
  837. "target features section contains repeated feature \"" +
  838. Feature.Name + "\"",
  839. object_error::parse_failed);
  840. TargetFeatures.push_back(Feature);
  841. }
  842. if (Ctx.Ptr != Ctx.End)
  843. return make_error<GenericBinaryError>(
  844. "target features section ended prematurely",
  845. object_error::parse_failed);
  846. return Error::success();
  847. }
  848. Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
  849. uint32_t SectionIndex = readVaruint32(Ctx);
  850. if (SectionIndex >= Sections.size())
  851. return make_error<GenericBinaryError>("invalid section index",
  852. object_error::parse_failed);
  853. WasmSection &Section = Sections[SectionIndex];
  854. uint32_t RelocCount = readVaruint32(Ctx);
  855. uint32_t EndOffset = Section.Content.size();
  856. uint32_t PreviousOffset = 0;
  857. while (RelocCount--) {
  858. wasm::WasmRelocation Reloc = {};
  859. uint32_t type = readVaruint32(Ctx);
  860. Reloc.Type = type;
  861. Reloc.Offset = readVaruint32(Ctx);
  862. if (Reloc.Offset < PreviousOffset)
  863. return make_error<GenericBinaryError>("relocations not in offset order",
  864. object_error::parse_failed);
  865. PreviousOffset = Reloc.Offset;
  866. Reloc.Index = readVaruint32(Ctx);
  867. switch (type) {
  868. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  869. case wasm::R_WASM_TABLE_INDEX_SLEB:
  870. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  871. case wasm::R_WASM_TABLE_INDEX_I32:
  872. case wasm::R_WASM_TABLE_INDEX_I64:
  873. case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
  874. case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
  875. if (!isValidFunctionSymbol(Reloc.Index))
  876. return make_error<GenericBinaryError>(
  877. "invalid relocation function index", object_error::parse_failed);
  878. break;
  879. case wasm::R_WASM_TABLE_NUMBER_LEB:
  880. if (!isValidTableSymbol(Reloc.Index))
  881. return make_error<GenericBinaryError>("invalid relocation table index",
  882. object_error::parse_failed);
  883. break;
  884. case wasm::R_WASM_TYPE_INDEX_LEB:
  885. if (Reloc.Index >= Signatures.size())
  886. return make_error<GenericBinaryError>("invalid relocation type index",
  887. object_error::parse_failed);
  888. break;
  889. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  890. // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data
  891. // symbols to refer to their GOT entries.
  892. if (!isValidGlobalSymbol(Reloc.Index) &&
  893. !isValidDataSymbol(Reloc.Index) &&
  894. !isValidFunctionSymbol(Reloc.Index))
  895. return make_error<GenericBinaryError>("invalid relocation global index",
  896. object_error::parse_failed);
  897. break;
  898. case wasm::R_WASM_GLOBAL_INDEX_I32:
  899. if (!isValidGlobalSymbol(Reloc.Index))
  900. return make_error<GenericBinaryError>("invalid relocation global index",
  901. object_error::parse_failed);
  902. break;
  903. case wasm::R_WASM_TAG_INDEX_LEB:
  904. if (!isValidTagSymbol(Reloc.Index))
  905. return make_error<GenericBinaryError>("invalid relocation tag index",
  906. object_error::parse_failed);
  907. break;
  908. case wasm::R_WASM_MEMORY_ADDR_LEB:
  909. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  910. case wasm::R_WASM_MEMORY_ADDR_I32:
  911. case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
  912. case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
  913. case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
  914. if (!isValidDataSymbol(Reloc.Index))
  915. return make_error<GenericBinaryError>("invalid relocation data index",
  916. object_error::parse_failed);
  917. Reloc.Addend = readVarint32(Ctx);
  918. break;
  919. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  920. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  921. case wasm::R_WASM_MEMORY_ADDR_I64:
  922. case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
  923. case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
  924. if (!isValidDataSymbol(Reloc.Index))
  925. return make_error<GenericBinaryError>("invalid relocation data index",
  926. object_error::parse_failed);
  927. Reloc.Addend = readVarint64(Ctx);
  928. break;
  929. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  930. if (!isValidFunctionSymbol(Reloc.Index))
  931. return make_error<GenericBinaryError>(
  932. "invalid relocation function index", object_error::parse_failed);
  933. Reloc.Addend = readVarint32(Ctx);
  934. break;
  935. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  936. if (!isValidFunctionSymbol(Reloc.Index))
  937. return make_error<GenericBinaryError>(
  938. "invalid relocation function index", object_error::parse_failed);
  939. Reloc.Addend = readVarint64(Ctx);
  940. break;
  941. case wasm::R_WASM_SECTION_OFFSET_I32:
  942. if (!isValidSectionSymbol(Reloc.Index))
  943. return make_error<GenericBinaryError>(
  944. "invalid relocation section index", object_error::parse_failed);
  945. Reloc.Addend = readVarint32(Ctx);
  946. break;
  947. default:
  948. return make_error<GenericBinaryError>("invalid relocation type: " +
  949. Twine(type),
  950. object_error::parse_failed);
  951. }
  952. // Relocations must fit inside the section, and must appear in order. They
  953. // also shouldn't overlap a function/element boundary, but we don't bother
  954. // to check that.
  955. uint64_t Size = 5;
  956. if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
  957. Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
  958. Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
  959. Size = 10;
  960. if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 ||
  961. Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
  962. Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
  963. Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
  964. Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
  965. Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32)
  966. Size = 4;
  967. if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 ||
  968. Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
  969. Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64)
  970. Size = 8;
  971. if (Reloc.Offset + Size > EndOffset)
  972. return make_error<GenericBinaryError>("invalid relocation offset",
  973. object_error::parse_failed);
  974. Section.Relocations.push_back(Reloc);
  975. }
  976. if (Ctx.Ptr != Ctx.End)
  977. return make_error<GenericBinaryError>("reloc section ended prematurely",
  978. object_error::parse_failed);
  979. return Error::success();
  980. }
  981. Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
  982. if (Sec.Name == "dylink") {
  983. if (Error Err = parseDylinkSection(Ctx))
  984. return Err;
  985. } else if (Sec.Name == "dylink.0") {
  986. if (Error Err = parseDylink0Section(Ctx))
  987. return Err;
  988. } else if (Sec.Name == "name") {
  989. if (Error Err = parseNameSection(Ctx))
  990. return Err;
  991. } else if (Sec.Name == "linking") {
  992. if (Error Err = parseLinkingSection(Ctx))
  993. return Err;
  994. } else if (Sec.Name == "producers") {
  995. if (Error Err = parseProducersSection(Ctx))
  996. return Err;
  997. } else if (Sec.Name == "target_features") {
  998. if (Error Err = parseTargetFeaturesSection(Ctx))
  999. return Err;
  1000. } else if (Sec.Name.startswith("reloc.")) {
  1001. if (Error Err = parseRelocSection(Sec.Name, Ctx))
  1002. return Err;
  1003. }
  1004. return Error::success();
  1005. }
  1006. Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
  1007. uint32_t Count = readVaruint32(Ctx);
  1008. Signatures.reserve(Count);
  1009. while (Count--) {
  1010. wasm::WasmSignature Sig;
  1011. uint8_t Form = readUint8(Ctx);
  1012. if (Form != wasm::WASM_TYPE_FUNC) {
  1013. return make_error<GenericBinaryError>("invalid signature type",
  1014. object_error::parse_failed);
  1015. }
  1016. uint32_t ParamCount = readVaruint32(Ctx);
  1017. Sig.Params.reserve(ParamCount);
  1018. while (ParamCount--) {
  1019. uint32_t ParamType = readUint8(Ctx);
  1020. Sig.Params.push_back(wasm::ValType(ParamType));
  1021. }
  1022. uint32_t ReturnCount = readVaruint32(Ctx);
  1023. while (ReturnCount--) {
  1024. uint32_t ReturnType = readUint8(Ctx);
  1025. Sig.Returns.push_back(wasm::ValType(ReturnType));
  1026. }
  1027. Signatures.push_back(std::move(Sig));
  1028. }
  1029. if (Ctx.Ptr != Ctx.End)
  1030. return make_error<GenericBinaryError>("type section ended prematurely",
  1031. object_error::parse_failed);
  1032. return Error::success();
  1033. }
  1034. Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
  1035. uint32_t Count = readVaruint32(Ctx);
  1036. uint32_t NumTypes = Signatures.size();
  1037. Imports.reserve(Count);
  1038. for (uint32_t I = 0; I < Count; I++) {
  1039. wasm::WasmImport Im;
  1040. Im.Module = readString(Ctx);
  1041. Im.Field = readString(Ctx);
  1042. Im.Kind = readUint8(Ctx);
  1043. switch (Im.Kind) {
  1044. case wasm::WASM_EXTERNAL_FUNCTION:
  1045. NumImportedFunctions++;
  1046. Im.SigIndex = readVaruint32(Ctx);
  1047. if (Im.SigIndex >= NumTypes)
  1048. return make_error<GenericBinaryError>("invalid function type",
  1049. object_error::parse_failed);
  1050. break;
  1051. case wasm::WASM_EXTERNAL_GLOBAL:
  1052. NumImportedGlobals++;
  1053. Im.Global.Type = readUint8(Ctx);
  1054. Im.Global.Mutable = readVaruint1(Ctx);
  1055. break;
  1056. case wasm::WASM_EXTERNAL_MEMORY:
  1057. Im.Memory = readLimits(Ctx);
  1058. if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
  1059. HasMemory64 = true;
  1060. break;
  1061. case wasm::WASM_EXTERNAL_TABLE: {
  1062. Im.Table = readTableType(Ctx);
  1063. NumImportedTables++;
  1064. auto ElemType = Im.Table.ElemType;
  1065. if (ElemType != wasm::WASM_TYPE_FUNCREF &&
  1066. ElemType != wasm::WASM_TYPE_EXTERNREF)
  1067. return make_error<GenericBinaryError>("invalid table element type",
  1068. object_error::parse_failed);
  1069. break;
  1070. }
  1071. case wasm::WASM_EXTERNAL_TAG:
  1072. NumImportedTags++;
  1073. if (readUint8(Ctx) != 0) // Reserved 'attribute' field
  1074. return make_error<GenericBinaryError>("invalid attribute",
  1075. object_error::parse_failed);
  1076. Im.SigIndex = readVaruint32(Ctx);
  1077. if (Im.SigIndex >= NumTypes)
  1078. return make_error<GenericBinaryError>("invalid tag type",
  1079. object_error::parse_failed);
  1080. break;
  1081. default:
  1082. return make_error<GenericBinaryError>("unexpected import kind",
  1083. object_error::parse_failed);
  1084. }
  1085. Imports.push_back(Im);
  1086. }
  1087. if (Ctx.Ptr != Ctx.End)
  1088. return make_error<GenericBinaryError>("import section ended prematurely",
  1089. object_error::parse_failed);
  1090. return Error::success();
  1091. }
  1092. Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
  1093. uint32_t Count = readVaruint32(Ctx);
  1094. Functions.reserve(Count);
  1095. uint32_t NumTypes = Signatures.size();
  1096. while (Count--) {
  1097. uint32_t Type = readVaruint32(Ctx);
  1098. if (Type >= NumTypes)
  1099. return make_error<GenericBinaryError>("invalid function type",
  1100. object_error::parse_failed);
  1101. wasm::WasmFunction F;
  1102. F.SigIndex = Type;
  1103. Functions.push_back(F);
  1104. }
  1105. if (Ctx.Ptr != Ctx.End)
  1106. return make_error<GenericBinaryError>("function section ended prematurely",
  1107. object_error::parse_failed);
  1108. return Error::success();
  1109. }
  1110. Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
  1111. TableSection = Sections.size();
  1112. uint32_t Count = readVaruint32(Ctx);
  1113. Tables.reserve(Count);
  1114. while (Count--) {
  1115. wasm::WasmTable T;
  1116. T.Type = readTableType(Ctx);
  1117. T.Index = NumImportedTables + Tables.size();
  1118. Tables.push_back(T);
  1119. auto ElemType = Tables.back().Type.ElemType;
  1120. if (ElemType != wasm::WASM_TYPE_FUNCREF &&
  1121. ElemType != wasm::WASM_TYPE_EXTERNREF) {
  1122. return make_error<GenericBinaryError>("invalid table element type",
  1123. object_error::parse_failed);
  1124. }
  1125. }
  1126. if (Ctx.Ptr != Ctx.End)
  1127. return make_error<GenericBinaryError>("table section ended prematurely",
  1128. object_error::parse_failed);
  1129. return Error::success();
  1130. }
  1131. Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
  1132. uint32_t Count = readVaruint32(Ctx);
  1133. Memories.reserve(Count);
  1134. while (Count--) {
  1135. auto Limits = readLimits(Ctx);
  1136. if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
  1137. HasMemory64 = true;
  1138. Memories.push_back(Limits);
  1139. }
  1140. if (Ctx.Ptr != Ctx.End)
  1141. return make_error<GenericBinaryError>("memory section ended prematurely",
  1142. object_error::parse_failed);
  1143. return Error::success();
  1144. }
  1145. Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
  1146. TagSection = Sections.size();
  1147. uint32_t Count = readVaruint32(Ctx);
  1148. Tags.reserve(Count);
  1149. uint32_t NumTypes = Signatures.size();
  1150. while (Count--) {
  1151. if (readUint8(Ctx) != 0) // Reserved 'attribute' field
  1152. return make_error<GenericBinaryError>("invalid attribute",
  1153. object_error::parse_failed);
  1154. uint32_t Type = readVaruint32(Ctx);
  1155. if (Type >= NumTypes)
  1156. return make_error<GenericBinaryError>("invalid tag type",
  1157. object_error::parse_failed);
  1158. wasm::WasmTag Tag;
  1159. Tag.Index = NumImportedTags + Tags.size();
  1160. Tag.SigIndex = Type;
  1161. Tags.push_back(Tag);
  1162. }
  1163. if (Ctx.Ptr != Ctx.End)
  1164. return make_error<GenericBinaryError>("tag section ended prematurely",
  1165. object_error::parse_failed);
  1166. return Error::success();
  1167. }
  1168. Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
  1169. GlobalSection = Sections.size();
  1170. uint32_t Count = readVaruint32(Ctx);
  1171. Globals.reserve(Count);
  1172. while (Count--) {
  1173. wasm::WasmGlobal Global;
  1174. Global.Index = NumImportedGlobals + Globals.size();
  1175. Global.Type.Type = readUint8(Ctx);
  1176. Global.Type.Mutable = readVaruint1(Ctx);
  1177. if (Error Err = readInitExpr(Global.InitExpr, Ctx))
  1178. return Err;
  1179. Globals.push_back(Global);
  1180. }
  1181. if (Ctx.Ptr != Ctx.End)
  1182. return make_error<GenericBinaryError>("global section ended prematurely",
  1183. object_error::parse_failed);
  1184. return Error::success();
  1185. }
  1186. Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
  1187. uint32_t Count = readVaruint32(Ctx);
  1188. Exports.reserve(Count);
  1189. for (uint32_t I = 0; I < Count; I++) {
  1190. wasm::WasmExport Ex;
  1191. Ex.Name = readString(Ctx);
  1192. Ex.Kind = readUint8(Ctx);
  1193. Ex.Index = readVaruint32(Ctx);
  1194. switch (Ex.Kind) {
  1195. case wasm::WASM_EXTERNAL_FUNCTION:
  1196. if (!isDefinedFunctionIndex(Ex.Index))
  1197. return make_error<GenericBinaryError>("invalid function export",
  1198. object_error::parse_failed);
  1199. getDefinedFunction(Ex.Index).ExportName = Ex.Name;
  1200. break;
  1201. case wasm::WASM_EXTERNAL_GLOBAL:
  1202. if (!isValidGlobalIndex(Ex.Index))
  1203. return make_error<GenericBinaryError>("invalid global export",
  1204. object_error::parse_failed);
  1205. break;
  1206. case wasm::WASM_EXTERNAL_TAG:
  1207. if (!isValidTagIndex(Ex.Index))
  1208. return make_error<GenericBinaryError>("invalid tag export",
  1209. object_error::parse_failed);
  1210. break;
  1211. case wasm::WASM_EXTERNAL_MEMORY:
  1212. case wasm::WASM_EXTERNAL_TABLE:
  1213. break;
  1214. default:
  1215. return make_error<GenericBinaryError>("unexpected export kind",
  1216. object_error::parse_failed);
  1217. }
  1218. Exports.push_back(Ex);
  1219. }
  1220. if (Ctx.Ptr != Ctx.End)
  1221. return make_error<GenericBinaryError>("export section ended prematurely",
  1222. object_error::parse_failed);
  1223. return Error::success();
  1224. }
  1225. bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
  1226. return Index < NumImportedFunctions + Functions.size();
  1227. }
  1228. bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
  1229. return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
  1230. }
  1231. bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
  1232. return Index < NumImportedGlobals + Globals.size();
  1233. }
  1234. bool WasmObjectFile::isValidTableNumber(uint32_t Index) const {
  1235. return Index < NumImportedTables + Tables.size();
  1236. }
  1237. bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
  1238. return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
  1239. }
  1240. bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const {
  1241. return Index >= NumImportedTables && isValidTableNumber(Index);
  1242. }
  1243. bool WasmObjectFile::isValidTagIndex(uint32_t Index) const {
  1244. return Index < NumImportedTags + Tags.size();
  1245. }
  1246. bool WasmObjectFile::isDefinedTagIndex(uint32_t Index) const {
  1247. return Index >= NumImportedTags && isValidTagIndex(Index);
  1248. }
  1249. bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
  1250. return Index < Symbols.size() && Symbols[Index].isTypeFunction();
  1251. }
  1252. bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const {
  1253. return Index < Symbols.size() && Symbols[Index].isTypeTable();
  1254. }
  1255. bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
  1256. return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
  1257. }
  1258. bool WasmObjectFile::isValidTagSymbol(uint32_t Index) const {
  1259. return Index < Symbols.size() && Symbols[Index].isTypeTag();
  1260. }
  1261. bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
  1262. return Index < Symbols.size() && Symbols[Index].isTypeData();
  1263. }
  1264. bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
  1265. return Index < Symbols.size() && Symbols[Index].isTypeSection();
  1266. }
  1267. wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
  1268. assert(isDefinedFunctionIndex(Index));
  1269. return Functions[Index - NumImportedFunctions];
  1270. }
  1271. const wasm::WasmFunction &
  1272. WasmObjectFile::getDefinedFunction(uint32_t Index) const {
  1273. assert(isDefinedFunctionIndex(Index));
  1274. return Functions[Index - NumImportedFunctions];
  1275. }
  1276. wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) {
  1277. assert(isDefinedGlobalIndex(Index));
  1278. return Globals[Index - NumImportedGlobals];
  1279. }
  1280. wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
  1281. assert(isDefinedTagIndex(Index));
  1282. return Tags[Index - NumImportedTags];
  1283. }
  1284. Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
  1285. StartFunction = readVaruint32(Ctx);
  1286. if (!isValidFunctionIndex(StartFunction))
  1287. return make_error<GenericBinaryError>("invalid start function",
  1288. object_error::parse_failed);
  1289. return Error::success();
  1290. }
  1291. Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
  1292. CodeSection = Sections.size();
  1293. uint32_t FunctionCount = readVaruint32(Ctx);
  1294. if (FunctionCount != Functions.size()) {
  1295. return make_error<GenericBinaryError>("invalid function count",
  1296. object_error::parse_failed);
  1297. }
  1298. for (uint32_t i = 0; i < FunctionCount; i++) {
  1299. wasm::WasmFunction& Function = Functions[i];
  1300. const uint8_t *FunctionStart = Ctx.Ptr;
  1301. uint32_t Size = readVaruint32(Ctx);
  1302. const uint8_t *FunctionEnd = Ctx.Ptr + Size;
  1303. Function.CodeOffset = Ctx.Ptr - FunctionStart;
  1304. Function.Index = NumImportedFunctions + i;
  1305. Function.CodeSectionOffset = FunctionStart - Ctx.Start;
  1306. Function.Size = FunctionEnd - FunctionStart;
  1307. uint32_t NumLocalDecls = readVaruint32(Ctx);
  1308. Function.Locals.reserve(NumLocalDecls);
  1309. while (NumLocalDecls--) {
  1310. wasm::WasmLocalDecl Decl;
  1311. Decl.Count = readVaruint32(Ctx);
  1312. Decl.Type = readUint8(Ctx);
  1313. Function.Locals.push_back(Decl);
  1314. }
  1315. uint32_t BodySize = FunctionEnd - Ctx.Ptr;
  1316. Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
  1317. // This will be set later when reading in the linking metadata section.
  1318. Function.Comdat = UINT32_MAX;
  1319. Ctx.Ptr += BodySize;
  1320. assert(Ctx.Ptr == FunctionEnd);
  1321. }
  1322. if (Ctx.Ptr != Ctx.End)
  1323. return make_error<GenericBinaryError>("code section ended prematurely",
  1324. object_error::parse_failed);
  1325. return Error::success();
  1326. }
  1327. Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
  1328. uint32_t Count = readVaruint32(Ctx);
  1329. ElemSegments.reserve(Count);
  1330. while (Count--) {
  1331. wasm::WasmElemSegment Segment;
  1332. Segment.Flags = readVaruint32(Ctx);
  1333. uint32_t SupportedFlags = wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER |
  1334. wasm::WASM_ELEM_SEGMENT_IS_PASSIVE |
  1335. wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS;
  1336. if (Segment.Flags & ~SupportedFlags)
  1337. return make_error<GenericBinaryError>(
  1338. "Unsupported flags for element segment", object_error::parse_failed);
  1339. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
  1340. Segment.TableNumber = readVaruint32(Ctx);
  1341. else
  1342. Segment.TableNumber = 0;
  1343. if (!isValidTableNumber(Segment.TableNumber))
  1344. return make_error<GenericBinaryError>("invalid TableNumber",
  1345. object_error::parse_failed);
  1346. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) {
  1347. Segment.Offset.Extended = false;
  1348. Segment.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST;
  1349. Segment.Offset.Inst.Value.Int32 = 0;
  1350. } else {
  1351. if (Error Err = readInitExpr(Segment.Offset, Ctx))
  1352. return Err;
  1353. }
  1354. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) {
  1355. Segment.ElemKind = readUint8(Ctx);
  1356. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) {
  1357. if (Segment.ElemKind != uint8_t(wasm::ValType::FUNCREF) &&
  1358. Segment.ElemKind != uint8_t(wasm::ValType::EXTERNREF)) {
  1359. return make_error<GenericBinaryError>("invalid reference type",
  1360. object_error::parse_failed);
  1361. }
  1362. } else {
  1363. if (Segment.ElemKind != 0)
  1364. return make_error<GenericBinaryError>("invalid elemtype",
  1365. object_error::parse_failed);
  1366. Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF);
  1367. }
  1368. } else {
  1369. Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF);
  1370. }
  1371. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS)
  1372. return make_error<GenericBinaryError>(
  1373. "elem segment init expressions not yet implemented",
  1374. object_error::parse_failed);
  1375. uint32_t NumElems = readVaruint32(Ctx);
  1376. while (NumElems--) {
  1377. Segment.Functions.push_back(readVaruint32(Ctx));
  1378. }
  1379. ElemSegments.push_back(Segment);
  1380. }
  1381. if (Ctx.Ptr != Ctx.End)
  1382. return make_error<GenericBinaryError>("elem section ended prematurely",
  1383. object_error::parse_failed);
  1384. return Error::success();
  1385. }
  1386. Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
  1387. DataSection = Sections.size();
  1388. uint32_t Count = readVaruint32(Ctx);
  1389. if (DataCount && Count != *DataCount)
  1390. return make_error<GenericBinaryError>(
  1391. "number of data segments does not match DataCount section");
  1392. DataSegments.reserve(Count);
  1393. while (Count--) {
  1394. WasmSegment Segment;
  1395. Segment.Data.InitFlags = readVaruint32(Ctx);
  1396. Segment.Data.MemoryIndex =
  1397. (Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
  1398. ? readVaruint32(Ctx)
  1399. : 0;
  1400. if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
  1401. if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
  1402. return Err;
  1403. } else {
  1404. Segment.Data.Offset.Extended = false;
  1405. Segment.Data.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST;
  1406. Segment.Data.Offset.Inst.Value.Int32 = 0;
  1407. }
  1408. uint32_t Size = readVaruint32(Ctx);
  1409. if (Size > (size_t)(Ctx.End - Ctx.Ptr))
  1410. return make_error<GenericBinaryError>("invalid segment size",
  1411. object_error::parse_failed);
  1412. Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
  1413. // The rest of these Data fields are set later, when reading in the linking
  1414. // metadata section.
  1415. Segment.Data.Alignment = 0;
  1416. Segment.Data.LinkingFlags = 0;
  1417. Segment.Data.Comdat = UINT32_MAX;
  1418. Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
  1419. Ctx.Ptr += Size;
  1420. DataSegments.push_back(Segment);
  1421. }
  1422. if (Ctx.Ptr != Ctx.End)
  1423. return make_error<GenericBinaryError>("data section ended prematurely",
  1424. object_error::parse_failed);
  1425. return Error::success();
  1426. }
  1427. Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
  1428. DataCount = readVaruint32(Ctx);
  1429. return Error::success();
  1430. }
  1431. const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
  1432. return Header;
  1433. }
  1434. void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; }
  1435. Expected<uint32_t> WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
  1436. uint32_t Result = SymbolRef::SF_None;
  1437. const WasmSymbol &Sym = getWasmSymbol(Symb);
  1438. LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
  1439. if (Sym.isBindingWeak())
  1440. Result |= SymbolRef::SF_Weak;
  1441. if (!Sym.isBindingLocal())
  1442. Result |= SymbolRef::SF_Global;
  1443. if (Sym.isHidden())
  1444. Result |= SymbolRef::SF_Hidden;
  1445. if (!Sym.isDefined())
  1446. Result |= SymbolRef::SF_Undefined;
  1447. if (Sym.isTypeFunction())
  1448. Result |= SymbolRef::SF_Executable;
  1449. return Result;
  1450. }
  1451. basic_symbol_iterator WasmObjectFile::symbol_begin() const {
  1452. DataRefImpl Ref;
  1453. Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
  1454. Ref.d.b = 0; // Symbol index
  1455. return BasicSymbolRef(Ref, this);
  1456. }
  1457. basic_symbol_iterator WasmObjectFile::symbol_end() const {
  1458. DataRefImpl Ref;
  1459. Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
  1460. Ref.d.b = Symbols.size(); // Symbol index
  1461. return BasicSymbolRef(Ref, this);
  1462. }
  1463. const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const {
  1464. return Symbols[Symb.d.b];
  1465. }
  1466. const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
  1467. return getWasmSymbol(Symb.getRawDataRefImpl());
  1468. }
  1469. Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
  1470. return getWasmSymbol(Symb).Info.Name;
  1471. }
  1472. Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
  1473. auto &Sym = getWasmSymbol(Symb);
  1474. if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION &&
  1475. isDefinedFunctionIndex(Sym.Info.ElementIndex))
  1476. return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset;
  1477. else
  1478. return getSymbolValue(Symb);
  1479. }
  1480. uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const {
  1481. switch (Sym.Info.Kind) {
  1482. case wasm::WASM_SYMBOL_TYPE_FUNCTION:
  1483. case wasm::WASM_SYMBOL_TYPE_GLOBAL:
  1484. case wasm::WASM_SYMBOL_TYPE_TAG:
  1485. case wasm::WASM_SYMBOL_TYPE_TABLE:
  1486. return Sym.Info.ElementIndex;
  1487. case wasm::WASM_SYMBOL_TYPE_DATA: {
  1488. // The value of a data symbol is the segment offset, plus the symbol
  1489. // offset within the segment.
  1490. uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
  1491. const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
  1492. if (Segment.Offset.Extended) {
  1493. llvm_unreachable("extended init exprs not supported");
  1494. } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
  1495. return Segment.Offset.Inst.Value.Int32 + Sym.Info.DataRef.Offset;
  1496. } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
  1497. return Segment.Offset.Inst.Value.Int64 + Sym.Info.DataRef.Offset;
  1498. } else {
  1499. llvm_unreachable("unknown init expr opcode");
  1500. }
  1501. }
  1502. case wasm::WASM_SYMBOL_TYPE_SECTION:
  1503. return 0;
  1504. }
  1505. llvm_unreachable("invalid symbol type");
  1506. }
  1507. uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
  1508. return getWasmSymbolValue(getWasmSymbol(Symb));
  1509. }
  1510. uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
  1511. llvm_unreachable("not yet implemented");
  1512. return 0;
  1513. }
  1514. uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
  1515. llvm_unreachable("not yet implemented");
  1516. return 0;
  1517. }
  1518. Expected<SymbolRef::Type>
  1519. WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
  1520. const WasmSymbol &Sym = getWasmSymbol(Symb);
  1521. switch (Sym.Info.Kind) {
  1522. case wasm::WASM_SYMBOL_TYPE_FUNCTION:
  1523. return SymbolRef::ST_Function;
  1524. case wasm::WASM_SYMBOL_TYPE_GLOBAL:
  1525. return SymbolRef::ST_Other;
  1526. case wasm::WASM_SYMBOL_TYPE_DATA:
  1527. return SymbolRef::ST_Data;
  1528. case wasm::WASM_SYMBOL_TYPE_SECTION:
  1529. return SymbolRef::ST_Debug;
  1530. case wasm::WASM_SYMBOL_TYPE_TAG:
  1531. return SymbolRef::ST_Other;
  1532. case wasm::WASM_SYMBOL_TYPE_TABLE:
  1533. return SymbolRef::ST_Other;
  1534. }
  1535. llvm_unreachable("unknown WasmSymbol::SymbolType");
  1536. return SymbolRef::ST_Other;
  1537. }
  1538. Expected<section_iterator>
  1539. WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
  1540. const WasmSymbol &Sym = getWasmSymbol(Symb);
  1541. if (Sym.isUndefined())
  1542. return section_end();
  1543. DataRefImpl Ref;
  1544. Ref.d.a = getSymbolSectionIdImpl(Sym);
  1545. return section_iterator(SectionRef(Ref, this));
  1546. }
  1547. uint32_t WasmObjectFile::getSymbolSectionId(SymbolRef Symb) const {
  1548. const WasmSymbol &Sym = getWasmSymbol(Symb);
  1549. return getSymbolSectionIdImpl(Sym);
  1550. }
  1551. uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const {
  1552. switch (Sym.Info.Kind) {
  1553. case wasm::WASM_SYMBOL_TYPE_FUNCTION:
  1554. return CodeSection;
  1555. case wasm::WASM_SYMBOL_TYPE_GLOBAL:
  1556. return GlobalSection;
  1557. case wasm::WASM_SYMBOL_TYPE_DATA:
  1558. return DataSection;
  1559. case wasm::WASM_SYMBOL_TYPE_SECTION:
  1560. return Sym.Info.ElementIndex;
  1561. case wasm::WASM_SYMBOL_TYPE_TAG:
  1562. return TagSection;
  1563. case wasm::WASM_SYMBOL_TYPE_TABLE:
  1564. return TableSection;
  1565. default:
  1566. llvm_unreachable("unknown WasmSymbol::SymbolType");
  1567. }
  1568. }
  1569. void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
  1570. Expected<StringRef> WasmObjectFile::getSectionName(DataRefImpl Sec) const {
  1571. const WasmSection &S = Sections[Sec.d.a];
  1572. if (S.Type == wasm::WASM_SEC_CUSTOM)
  1573. return S.Name;
  1574. if (S.Type > wasm::WASM_SEC_LAST_KNOWN)
  1575. return createStringError(object_error::invalid_section_index, "");
  1576. return wasm::sectionTypeToString(S.Type);
  1577. }
  1578. uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
  1579. uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const {
  1580. return Sec.d.a;
  1581. }
  1582. uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
  1583. const WasmSection &S = Sections[Sec.d.a];
  1584. return S.Content.size();
  1585. }
  1586. Expected<ArrayRef<uint8_t>>
  1587. WasmObjectFile::getSectionContents(DataRefImpl Sec) const {
  1588. const WasmSection &S = Sections[Sec.d.a];
  1589. // This will never fail since wasm sections can never be empty (user-sections
  1590. // must have a name and non-user sections each have a defined structure).
  1591. return S.Content;
  1592. }
  1593. uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
  1594. return 1;
  1595. }
  1596. bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
  1597. return false;
  1598. }
  1599. bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
  1600. return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
  1601. }
  1602. bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
  1603. return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
  1604. }
  1605. bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
  1606. bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
  1607. relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
  1608. DataRefImpl RelocRef;
  1609. RelocRef.d.a = Ref.d.a;
  1610. RelocRef.d.b = 0;
  1611. return relocation_iterator(RelocationRef(RelocRef, this));
  1612. }
  1613. relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
  1614. const WasmSection &Sec = getWasmSection(Ref);
  1615. DataRefImpl RelocRef;
  1616. RelocRef.d.a = Ref.d.a;
  1617. RelocRef.d.b = Sec.Relocations.size();
  1618. return relocation_iterator(RelocationRef(RelocRef, this));
  1619. }
  1620. void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { Rel.d.b++; }
  1621. uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
  1622. const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
  1623. return Rel.Offset;
  1624. }
  1625. symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const {
  1626. const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
  1627. if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB)
  1628. return symbol_end();
  1629. DataRefImpl Sym;
  1630. Sym.d.a = 1;
  1631. Sym.d.b = Rel.Index;
  1632. return symbol_iterator(SymbolRef(Sym, this));
  1633. }
  1634. uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
  1635. const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
  1636. return Rel.Type;
  1637. }
  1638. void WasmObjectFile::getRelocationTypeName(
  1639. DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
  1640. const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
  1641. StringRef Res = "Unknown";
  1642. #define WASM_RELOC(name, value) \
  1643. case wasm::name: \
  1644. Res = #name; \
  1645. break;
  1646. switch (Rel.Type) {
  1647. #include "llvm/BinaryFormat/WasmRelocs.def"
  1648. }
  1649. #undef WASM_RELOC
  1650. Result.append(Res.begin(), Res.end());
  1651. }
  1652. section_iterator WasmObjectFile::section_begin() const {
  1653. DataRefImpl Ref;
  1654. Ref.d.a = 0;
  1655. return section_iterator(SectionRef(Ref, this));
  1656. }
  1657. section_iterator WasmObjectFile::section_end() const {
  1658. DataRefImpl Ref;
  1659. Ref.d.a = Sections.size();
  1660. return section_iterator(SectionRef(Ref, this));
  1661. }
  1662. uint8_t WasmObjectFile::getBytesInAddress() const {
  1663. return HasMemory64 ? 8 : 4;
  1664. }
  1665. StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
  1666. Triple::ArchType WasmObjectFile::getArch() const {
  1667. return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
  1668. }
  1669. Expected<SubtargetFeatures> WasmObjectFile::getFeatures() const {
  1670. return SubtargetFeatures();
  1671. }
  1672. bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; }
  1673. bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; }
  1674. const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
  1675. assert(Ref.d.a < Sections.size());
  1676. return Sections[Ref.d.a];
  1677. }
  1678. const WasmSection &
  1679. WasmObjectFile::getWasmSection(const SectionRef &Section) const {
  1680. return getWasmSection(Section.getRawDataRefImpl());
  1681. }
  1682. const wasm::WasmRelocation &
  1683. WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
  1684. return getWasmRelocation(Ref.getRawDataRefImpl());
  1685. }
  1686. const wasm::WasmRelocation &
  1687. WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
  1688. assert(Ref.d.a < Sections.size());
  1689. const WasmSection &Sec = Sections[Ref.d.a];
  1690. assert(Ref.d.b < Sec.Relocations.size());
  1691. return Sec.Relocations[Ref.d.b];
  1692. }
  1693. int WasmSectionOrderChecker::getSectionOrder(unsigned ID,
  1694. StringRef CustomSectionName) {
  1695. switch (ID) {
  1696. case wasm::WASM_SEC_CUSTOM:
  1697. return StringSwitch<unsigned>(CustomSectionName)
  1698. .Case("dylink", WASM_SEC_ORDER_DYLINK)
  1699. .Case("dylink.0", WASM_SEC_ORDER_DYLINK)
  1700. .Case("linking", WASM_SEC_ORDER_LINKING)
  1701. .StartsWith("reloc.", WASM_SEC_ORDER_RELOC)
  1702. .Case("name", WASM_SEC_ORDER_NAME)
  1703. .Case("producers", WASM_SEC_ORDER_PRODUCERS)
  1704. .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES)
  1705. .Default(WASM_SEC_ORDER_NONE);
  1706. case wasm::WASM_SEC_TYPE:
  1707. return WASM_SEC_ORDER_TYPE;
  1708. case wasm::WASM_SEC_IMPORT:
  1709. return WASM_SEC_ORDER_IMPORT;
  1710. case wasm::WASM_SEC_FUNCTION:
  1711. return WASM_SEC_ORDER_FUNCTION;
  1712. case wasm::WASM_SEC_TABLE:
  1713. return WASM_SEC_ORDER_TABLE;
  1714. case wasm::WASM_SEC_MEMORY:
  1715. return WASM_SEC_ORDER_MEMORY;
  1716. case wasm::WASM_SEC_GLOBAL:
  1717. return WASM_SEC_ORDER_GLOBAL;
  1718. case wasm::WASM_SEC_EXPORT:
  1719. return WASM_SEC_ORDER_EXPORT;
  1720. case wasm::WASM_SEC_START:
  1721. return WASM_SEC_ORDER_START;
  1722. case wasm::WASM_SEC_ELEM:
  1723. return WASM_SEC_ORDER_ELEM;
  1724. case wasm::WASM_SEC_CODE:
  1725. return WASM_SEC_ORDER_CODE;
  1726. case wasm::WASM_SEC_DATA:
  1727. return WASM_SEC_ORDER_DATA;
  1728. case wasm::WASM_SEC_DATACOUNT:
  1729. return WASM_SEC_ORDER_DATACOUNT;
  1730. case wasm::WASM_SEC_TAG:
  1731. return WASM_SEC_ORDER_TAG;
  1732. default:
  1733. return WASM_SEC_ORDER_NONE;
  1734. }
  1735. }
  1736. // Represents the edges in a directed graph where any node B reachable from node
  1737. // A is not allowed to appear before A in the section ordering, but may appear
  1738. // afterward.
  1739. int WasmSectionOrderChecker::DisallowedPredecessors
  1740. [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
  1741. // WASM_SEC_ORDER_NONE
  1742. {},
  1743. // WASM_SEC_ORDER_TYPE
  1744. {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
  1745. // WASM_SEC_ORDER_IMPORT
  1746. {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
  1747. // WASM_SEC_ORDER_FUNCTION
  1748. {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
  1749. // WASM_SEC_ORDER_TABLE
  1750. {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
  1751. // WASM_SEC_ORDER_MEMORY
  1752. {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
  1753. // WASM_SEC_ORDER_TAG
  1754. {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
  1755. // WASM_SEC_ORDER_GLOBAL
  1756. {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
  1757. // WASM_SEC_ORDER_EXPORT
  1758. {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
  1759. // WASM_SEC_ORDER_START
  1760. {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
  1761. // WASM_SEC_ORDER_ELEM
  1762. {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
  1763. // WASM_SEC_ORDER_DATACOUNT
  1764. {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
  1765. // WASM_SEC_ORDER_CODE
  1766. {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
  1767. // WASM_SEC_ORDER_DATA
  1768. {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
  1769. // Custom Sections
  1770. // WASM_SEC_ORDER_DYLINK
  1771. {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
  1772. // WASM_SEC_ORDER_LINKING
  1773. {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
  1774. // WASM_SEC_ORDER_RELOC (can be repeated)
  1775. {},
  1776. // WASM_SEC_ORDER_NAME
  1777. {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
  1778. // WASM_SEC_ORDER_PRODUCERS
  1779. {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
  1780. // WASM_SEC_ORDER_TARGET_FEATURES
  1781. {WASM_SEC_ORDER_TARGET_FEATURES}};
  1782. bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID,
  1783. StringRef CustomSectionName) {
  1784. int Order = getSectionOrder(ID, CustomSectionName);
  1785. if (Order == WASM_SEC_ORDER_NONE)
  1786. return true;
  1787. // Disallowed predecessors we need to check for
  1788. SmallVector<int, WASM_NUM_SEC_ORDERS> WorkList;
  1789. // Keep track of completed checks to avoid repeating work
  1790. bool Checked[WASM_NUM_SEC_ORDERS] = {};
  1791. int Curr = Order;
  1792. while (true) {
  1793. // Add new disallowed predecessors to work list
  1794. for (size_t I = 0;; ++I) {
  1795. int Next = DisallowedPredecessors[Curr][I];
  1796. if (Next == WASM_SEC_ORDER_NONE)
  1797. break;
  1798. if (Checked[Next])
  1799. continue;
  1800. WorkList.push_back(Next);
  1801. Checked[Next] = true;
  1802. }
  1803. if (WorkList.empty())
  1804. break;
  1805. // Consider next disallowed predecessor
  1806. Curr = WorkList.pop_back_val();
  1807. if (Seen[Curr])
  1808. return false;
  1809. }
  1810. // Have not seen any disallowed predecessors
  1811. Seen[Order] = true;
  1812. return true;
  1813. }