RawImageBase.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. #include "RawImage.h"
  2. namespace hybridclr
  3. {
  4. namespace metadata
  5. {
  6. LoadImageErrorCode RawImageBase::Load(const void* rawImageData, size_t length)
  7. {
  8. _imageData = (const byte*)rawImageData;
  9. _imageLength = uint32_t(length);
  10. _ptrRawDataEnd = _imageData + length;
  11. uint32_t metadataRva = 0;
  12. uint32_t metadataSize = 0;
  13. LoadImageErrorCode err = LoadCLIHeader(_entryPointToken, metadataRva, metadataSize);
  14. if (err != LoadImageErrorCode::OK)
  15. {
  16. return err;
  17. }
  18. err = LoadStreamHeaders(metadataRva, metadataSize);
  19. if (err != LoadImageErrorCode::OK)
  20. {
  21. return err;
  22. }
  23. err = PostLoadStreams();
  24. if (err != LoadImageErrorCode::OK)
  25. {
  26. return err;
  27. }
  28. err = ValidateStreams();
  29. if (err != LoadImageErrorCode::OK)
  30. {
  31. return err;
  32. }
  33. err = LoadTables();
  34. if (err != LoadImageErrorCode::OK)
  35. {
  36. return err;
  37. }
  38. err = PostLoadTables();
  39. if (err != LoadImageErrorCode::OK)
  40. {
  41. return err;
  42. }
  43. return LoadImageErrorCode::OK;
  44. }
  45. LoadImageErrorCode RawImageBase::LoadStreamHeaders(uint32_t metadataRva, uint32_t metadataSize)
  46. {
  47. uint32_t metaOffset;
  48. if (!TranslateRVAToImageOffset(metadataRva, metaOffset))
  49. {
  50. return LoadImageErrorCode::BAD_IMAGE;
  51. }
  52. if (metaOffset >= _imageLength)
  53. {
  54. return LoadImageErrorCode::BAD_IMAGE;
  55. }
  56. const MetadataRootPartial* ptrMetaRoot = (const MetadataRootPartial*)(_imageData + metaOffset);
  57. if (ptrMetaRoot->signature != 0x424A5342)
  58. {
  59. return LoadImageErrorCode::BAD_IMAGE;
  60. }
  61. //std::cout << "version:" << (const char*)&(ptrMetaRoot->versionFirstByte) << std::endl;
  62. const byte* ptrMetaData = (const byte*)ptrMetaRoot;
  63. uint16_t numStreamHeader = *(uint16_t*)(ptrMetaData + 16 + ptrMetaRoot->length + 2);
  64. const StreamHeader* ptrStreamHeaders = (const StreamHeader*)(ptrMetaData + 16 + ptrMetaRoot->length + 4);
  65. const StreamHeader* curSH = ptrStreamHeaders;
  66. const size_t maxStreamNameSize = 16;
  67. for (int i = 0; i < numStreamHeader; i++)
  68. {
  69. //std::cout << "name:" << (char*)curSH->name << ", offset:" << curSH->offset << ", size:" << curSH->size << std::endl;
  70. if (curSH->offset >= metadataSize)
  71. {
  72. return LoadImageErrorCode::BAD_IMAGE;
  73. }
  74. CliStream* rs = nullptr;
  75. CliStream nonStandardStream;
  76. CliStream pdbStream;
  77. if (!std::strncmp(curSH->name, "#~", maxStreamNameSize))
  78. {
  79. rs = &_streamTables;
  80. }
  81. else if (!std::strncmp(curSH->name, "#Strings", maxStreamNameSize))
  82. {
  83. rs = &_streamStringHeap;
  84. }
  85. else if (!std::strncmp(curSH->name, "#US", maxStreamNameSize))
  86. {
  87. rs = &_streamUS;
  88. }
  89. else if (!std::strncmp(curSH->name, "#GUID", maxStreamNameSize))
  90. {
  91. rs = &_streamGuidHeap;
  92. if (curSH->size % 16 != 0)
  93. {
  94. return LoadImageErrorCode::BAD_IMAGE;
  95. }
  96. }
  97. else if (!std::strncmp(curSH->name, "#Blob", maxStreamNameSize))
  98. {
  99. rs = &_streamBlobHeap;
  100. }
  101. else if (!std::strncmp(curSH->name, "#-", maxStreamNameSize))
  102. {
  103. rs = &nonStandardStream;
  104. }
  105. else if (!std::strncmp(curSH->name, "#Pdb", maxStreamNameSize))
  106. {
  107. rs = &pdbStream;
  108. }
  109. else
  110. {
  111. //std::cerr << "unknown stream name:" << curSH->name << std::endl;
  112. return LoadImageErrorCode::BAD_IMAGE;
  113. }
  114. rs->data = ptrMetaData + curSH->offset;
  115. rs->size = curSH->size;
  116. rs->name = curSH->name;
  117. size_t sizeOfStream = 8 + (std::strlen(curSH->name) / 4 + 1) * 4;
  118. curSH = (const StreamHeader*)((byte*)curSH + sizeOfStream);
  119. }
  120. return LoadImageErrorCode::OK;
  121. }
  122. LoadImageErrorCode RawImageBase::ValidateStreams()
  123. {
  124. {
  125. uint32_t stringNum = 0;
  126. const byte* cur;
  127. for (cur = _streamStringHeap.data; cur < _streamStringHeap.data + _streamStringHeap.size; )
  128. {
  129. ++stringNum;
  130. //std::cout << "#strings.[" << stringNum << "] " << cur << std::endl;
  131. cur += std::strlen((const char*)cur) + 1;
  132. }
  133. if (cur != _streamStringHeap.data + _streamStringHeap.size)
  134. {
  135. //std::cerr << "bad #strings" << std::endl;
  136. return LoadImageErrorCode::BAD_IMAGE;
  137. }
  138. //std::cout << "=== #String. num:" << stringNum << std::endl;
  139. }
  140. {
  141. //std::cout << "=== #GUID. num:" << _streamGuidHeap.size / 16 << std::endl;
  142. }
  143. {
  144. const byte* cur;
  145. uint32_t usNum = 0;
  146. uint32_t lengthSize;
  147. for (cur = _streamUS.data; cur < _streamUS.data + _streamUS.size;)
  148. {
  149. ++usNum;
  150. uint32_t stringLength = BlobReader::ReadCompressedUint32(cur, lengthSize);
  151. cur += lengthSize;
  152. //std::cout << "#us.[" << usNum << "].size:" << stringLength << std::endl;
  153. cur += stringLength;
  154. }
  155. if (cur != _streamUS.data + _streamUS.size)
  156. {
  157. //std::cerr << "bad #US" << std::endl;
  158. return LoadImageErrorCode::BAD_IMAGE;
  159. }
  160. //std::cout << "=== #US. num:" << usNum << std::endl;
  161. }
  162. {
  163. const byte* cur;
  164. uint32_t blobNum = 0;
  165. uint32_t lengthSize;
  166. for (cur = _streamBlobHeap.data; cur < _streamBlobHeap.data + _streamBlobHeap.size;)
  167. {
  168. ++blobNum;
  169. uint32_t stringLength = BlobReader::ReadCompressedUint32(cur, lengthSize);
  170. cur += lengthSize;
  171. //std::cout << "#blob.[" << blobNum << "].size:" << stringLength << std::endl;
  172. cur += stringLength;
  173. }
  174. if (cur != _streamBlobHeap.data + _streamBlobHeap.size)
  175. {
  176. //std::cerr << "bad #Blob" << std::endl;
  177. return LoadImageErrorCode::BAD_IMAGE;
  178. }
  179. //std::cout << "=== #Blob. num:" << blobNum << std::endl;
  180. return LoadImageErrorCode::OK;
  181. }
  182. }
  183. LoadImageErrorCode RawImageBase::LoadTables()
  184. {
  185. const TableStreamHeader* ptrTableHeader = (const TableStreamHeader*)_streamTables.data;
  186. if (ptrTableHeader->reserved != 0 || ptrTableHeader->majorVersion != 2 || ptrTableHeader->minorVersion != 0)
  187. {
  188. return LoadImageErrorCode::BAD_IMAGE;
  189. }
  190. if ((ptrTableHeader->heapSizes & ~0x7))
  191. {
  192. return LoadImageErrorCode::BAD_IMAGE;
  193. }
  194. _4byteStringIndex = ptrTableHeader->heapSizes & 0x1;
  195. _4byteGUIDIndex = ptrTableHeader->heapSizes & 0x2;
  196. _4byteBlobIndex = ptrTableHeader->heapSizes & 0x4;
  197. uint64_t validMask = ((uint64_t)1 << TABLE_NUM) - 1;
  198. if (ptrTableHeader->valid & ~validMask)
  199. {
  200. return LoadImageErrorCode::BAD_IMAGE;
  201. }
  202. // sorted include not exist table, so check is not need.
  203. //if (ptrTableHeader->sorted & ~validMask)
  204. //{
  205. // return LoadImageErrorCode::BAD_IMAGE;
  206. //}
  207. uint32_t validTableNum = GetNotZeroBitCount(ptrTableHeader->valid);
  208. //std::cout << "valid table num:" << validTableNum << std::endl;
  209. //printf("#~ size:%0x\n", _streamTables.size);
  210. const uint32_t* tableRowNums = (uint32_t*)(_streamTables.data + 24);
  211. const byte* tableDataBegin = _streamTables.data + 24 + 4 * validTableNum;
  212. {
  213. int curValidTableIndex = 0;
  214. for (int i = 0; i <= MAX_TABLE_INDEX; i++)
  215. {
  216. uint64_t mask = (uint64_t)1 << i;
  217. _tables[i] = {};
  218. if (ptrTableHeader->valid & mask)
  219. {
  220. uint32_t rowNum = tableRowNums[curValidTableIndex];
  221. _tables[i].rowNum = rowNum;
  222. ++curValidTableIndex;
  223. }
  224. }
  225. }
  226. BuildTableRowMetas();
  227. int curValidTableIndex = 0;
  228. const byte* curTableData = tableDataBegin;
  229. for (int i = 0; i <= MAX_TABLE_INDEX; i++)
  230. {
  231. uint64_t mask = (uint64_t)1 << i;
  232. bool sorted = ptrTableHeader->sorted & mask;
  233. if (ptrTableHeader->valid & mask)
  234. {
  235. uint32_t rowNum = tableRowNums[curValidTableIndex];
  236. uint32_t metaDataRowSize = ComputTableRowMetaDataSize((TableType)i);
  237. //uint64_t offset = curTableData - _imageData;
  238. _tables[i] = { curTableData, metaDataRowSize, rowNum, true, sorted };
  239. curTableData += metaDataRowSize * rowNum;
  240. //std::cout << "table:" << i << " ," << curValidTableIndex << ", row_size:" << metaDataRowSize << ", row_num:" << rowNum << std::endl;
  241. //printf("table:[%d][%d] offset:%0llx row_size:%d row_count:%d\n", i, curValidTableIndex, offset, metaDataRowSize, rowNum);
  242. ++curValidTableIndex;
  243. }
  244. else
  245. {
  246. _tables[i] = { nullptr, 0, 0, false, sorted };
  247. }
  248. }
  249. return LoadImageErrorCode::OK;
  250. }
  251. void RawImageBase::BuildTableRowMetas()
  252. {
  253. {
  254. auto& table = _tableRowMetas[(int)TableType::MODULE];
  255. table.push_back({ 2 });
  256. table.push_back({ ComputStringIndexByte() });
  257. table.push_back({ ComputGUIDIndexByte() });
  258. table.push_back({ ComputGUIDIndexByte() });
  259. table.push_back({ ComputGUIDIndexByte() });
  260. }
  261. {
  262. auto& table = _tableRowMetas[(int)TableType::TYPEREF];
  263. table.push_back({ ComputTableIndexByte(TableType::MODULE, TableType::MODULEREF, TableType::ASSEMBLYREF, TableType::TYPEREF, TagBits::ResoulutionScope) });
  264. table.push_back({ ComputStringIndexByte() });
  265. table.push_back({ ComputStringIndexByte() });
  266. }
  267. {
  268. auto& table = _tableRowMetas[(int)TableType::TYPEDEF];
  269. table.push_back({ 4 });
  270. table.push_back({ ComputStringIndexByte() });
  271. table.push_back({ ComputStringIndexByte() });
  272. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef) });
  273. table.push_back({ ComputTableIndexByte(TableType::FIELD) });
  274. table.push_back({ ComputTableIndexByte(TableType::METHOD) });
  275. }
  276. {
  277. auto& table = _tableRowMetas[(int)TableType::FIELD_POINTER];
  278. table.push_back({ ComputTableIndexByte(TableType::FIELD) });
  279. }
  280. {
  281. auto& table = _tableRowMetas[(int)TableType::FIELD];
  282. table.push_back({ 2 });
  283. table.push_back({ ComputStringIndexByte() });
  284. table.push_back({ ComputBlobIndexByte() });
  285. }
  286. {
  287. auto& table = _tableRowMetas[(int)TableType::METHOD_POINTER];
  288. table.push_back({ ComputTableIndexByte(TableType::METHOD) });
  289. }
  290. {
  291. auto& table = _tableRowMetas[(int)TableType::METHOD];
  292. table.push_back({ 4 });
  293. table.push_back({ 2 });
  294. table.push_back({ 2 });
  295. table.push_back({ ComputStringIndexByte() });
  296. table.push_back({ ComputBlobIndexByte() });
  297. table.push_back({ ComputTableIndexByte(TableType::PARAM) });
  298. }
  299. {
  300. auto& table = _tableRowMetas[(int)TableType::PARAM_POINTER];
  301. table.push_back({ ComputTableIndexByte(TableType::PARAM) });
  302. }
  303. {
  304. auto& table = _tableRowMetas[(int)TableType::PARAM];
  305. table.push_back({ 2 });
  306. table.push_back({ 2 });
  307. table.push_back({ ComputStringIndexByte() });
  308. }
  309. {
  310. auto& table = _tableRowMetas[(int)TableType::INTERFACEIMPL];
  311. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  312. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef) });
  313. }
  314. {
  315. auto& table = _tableRowMetas[(int)TableType::MEMBERREF];
  316. table.push_back({ ComputTableIndexByte(TableType::METHOD, TableType::MODULEREF, TableType::TYPEDEF, TableType::TYPEREF, TagBits::MemberRefParent) });
  317. table.push_back({ ComputStringIndexByte() });
  318. table.push_back({ ComputBlobIndexByte() });
  319. }
  320. {
  321. auto& table = _tableRowMetas[(int)TableType::CONSTANT];
  322. table.push_back({ 2 });
  323. table.push_back({ ComputTableIndexByte(TableType::PARAM, TableType::FIELD, TableType::PROPERTY, TagBits::HasConstant) });
  324. table.push_back({ ComputBlobIndexByte() });
  325. }
  326. {
  327. auto& table = _tableRowMetas[(int)TableType::CUSTOMATTRIBUTE];
  328. table.push_back({ ComputTableIndexByte(HasCustomAttributeAssociateTables, sizeof(HasCustomAttributeAssociateTables) / sizeof(TableType), TagBits::HasCustomAttribute) });
  329. table.push_back({ ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::CustomAttributeType) });
  330. table.push_back({ ComputBlobIndexByte() });
  331. }
  332. {
  333. auto& table = _tableRowMetas[(int)TableType::FIELDMARSHAL];
  334. table.push_back({ ComputTableIndexByte(TableType::FIELD, TableType::PARAM, TagBits::HasFieldMarshal) });
  335. table.push_back({ ComputBlobIndexByte() });
  336. }
  337. {
  338. auto& table = _tableRowMetas[(int)TableType::DECLSECURITY];
  339. table.push_back({ 2 });
  340. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF, TableType::METHOD, TableType::ASSEMBLY, TagBits::HasDeclSecurity) });
  341. table.push_back({ ComputBlobIndexByte() });
  342. }
  343. {
  344. auto& table = _tableRowMetas[(int)TableType::CLASSLAYOUT];
  345. table.push_back({ 2 });
  346. table.push_back({ 4 });
  347. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  348. }
  349. {
  350. auto& table = _tableRowMetas[(int)TableType::FIELDLAYOUT];
  351. table.push_back({ 4 });
  352. table.push_back({ ComputTableIndexByte(TableType::FIELD) });
  353. }
  354. {
  355. auto& table = _tableRowMetas[(int)TableType::STANDALONESIG];
  356. table.push_back({ ComputBlobIndexByte() });
  357. }
  358. {
  359. auto& table = _tableRowMetas[(int)TableType::EVENTMAP];
  360. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  361. table.push_back({ ComputTableIndexByte(TableType::EVENT) });
  362. }
  363. {
  364. auto& table = _tableRowMetas[(int)TableType::EVENT_POINTER];
  365. table.push_back({ ComputTableIndexByte(TableType::EVENT) });
  366. }
  367. {
  368. auto& table = _tableRowMetas[(int)TableType::EVENT];
  369. table.push_back({ 2 });
  370. table.push_back({ ComputStringIndexByte() });
  371. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef) });
  372. }
  373. {
  374. auto& table = _tableRowMetas[(int)TableType::PROPERTYMAP];
  375. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  376. table.push_back({ ComputTableIndexByte(TableType::PROPERTY) });
  377. }
  378. {
  379. auto& table = _tableRowMetas[(int)TableType::PROPERTY_POINTER];
  380. table.push_back({ ComputTableIndexByte(TableType::PROPERTY) });
  381. }
  382. {
  383. auto& table = _tableRowMetas[(int)TableType::PROPERTY];
  384. table.push_back({ 2 });
  385. table.push_back({ ComputStringIndexByte() });
  386. table.push_back({ ComputBlobIndexByte() });
  387. }
  388. {
  389. auto& table = _tableRowMetas[(int)TableType::METHODSEMANTICS];
  390. table.push_back({ 2 });
  391. table.push_back({ ComputTableIndexByte(TableType::METHOD) });
  392. table.push_back({ ComputTableIndexByte(TableType::EVENT, TableType::PROPERTY, TagBits::HasSemantics) });
  393. }
  394. {
  395. auto& table = _tableRowMetas[(int)TableType::METHODIMPL];
  396. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  397. table.push_back({ ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::MethodDefOrRef) });
  398. table.push_back({ ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::MethodDefOrRef) });
  399. }
  400. {
  401. auto& table = _tableRowMetas[(int)TableType::MODULEREF];
  402. table.push_back({ ComputStringIndexByte() });
  403. }
  404. {
  405. auto& table = _tableRowMetas[(int)TableType::TYPESPEC];
  406. table.push_back({ ComputBlobIndexByte() });
  407. }
  408. {
  409. auto& table = _tableRowMetas[(int)TableType::IMPLMAP];
  410. table.push_back({ 2 });
  411. table.push_back({ ComputTableIndexByte(TableType::FIELD, TableType::METHOD, TagBits::MemberForwarded) });
  412. table.push_back({ ComputStringIndexByte() });
  413. table.push_back({ ComputTableIndexByte(TableType::MODULEREF) });
  414. }
  415. {
  416. auto& table = _tableRowMetas[(int)TableType::FIELDRVA];
  417. table.push_back({ 4 });
  418. table.push_back({ ComputTableIndexByte(TableType::FIELD) });
  419. }
  420. {
  421. auto& table = _tableRowMetas[(int)TableType::ASSEMBLY];
  422. table.push_back({ 4 });
  423. table.push_back({ 2 });
  424. table.push_back({ 2 });
  425. table.push_back({ 2 });
  426. table.push_back({ 2 });
  427. table.push_back({ 4 });
  428. table.push_back({ ComputBlobIndexByte() });
  429. table.push_back({ ComputStringIndexByte() });
  430. table.push_back({ ComputStringIndexByte() });
  431. }
  432. {
  433. auto& table = _tableRowMetas[(int)TableType::ASSEMBLYPROCESSOR];
  434. table.push_back({ 4 });
  435. }
  436. {
  437. auto& table = _tableRowMetas[(int)TableType::ASSEMBLYOS];
  438. table.push_back({ 4 });
  439. table.push_back({ 4 });
  440. table.push_back({ 4 });
  441. }
  442. {
  443. auto& table = _tableRowMetas[(int)TableType::ASSEMBLYREF];
  444. table.push_back({ 2 });
  445. table.push_back({ 2 });
  446. table.push_back({ 2 });
  447. table.push_back({ 2 });
  448. table.push_back({ 4 });
  449. table.push_back({ ComputBlobIndexByte() });
  450. table.push_back({ ComputStringIndexByte() });
  451. table.push_back({ ComputStringIndexByte() });
  452. table.push_back({ ComputBlobIndexByte() });
  453. }
  454. {
  455. auto& table = _tableRowMetas[(int)TableType::ASSEMBLYREFPROCESSOR];
  456. table.push_back({ 4 });
  457. table.push_back({ ComputTableIndexByte(TableType::ASSEMBLYREF) });
  458. }
  459. {
  460. auto& table = _tableRowMetas[(int)TableType::ASSEMBLYREFOS];
  461. table.push_back({ 4 });
  462. table.push_back({ 4 });
  463. table.push_back({ 4 });
  464. table.push_back({ ComputTableIndexByte(TableType::ASSEMBLYREF) });
  465. }
  466. {
  467. auto& table = _tableRowMetas[(int)TableType::FILE];
  468. table.push_back({ 4 });
  469. table.push_back({ ComputStringIndexByte() });
  470. table.push_back({ ComputBlobIndexByte() });
  471. }
  472. {
  473. auto& table = _tableRowMetas[(int)TableType::EXPORTEDTYPE];
  474. table.push_back({ 4 });
  475. table.push_back({ 4 });
  476. table.push_back({ ComputStringIndexByte() });
  477. table.push_back({ ComputStringIndexByte() });
  478. table.push_back({ ComputTableIndexByte(TableType::FILE, TableType::EXPORTEDTYPE, TableType::ASSEMBLY, TagBits::Implementation) });
  479. }
  480. {
  481. auto& table = _tableRowMetas[(int)TableType::MANIFESTRESOURCE];
  482. table.push_back({ 4 });
  483. table.push_back({ 4 });
  484. table.push_back({ ComputStringIndexByte() });
  485. table.push_back({ ComputTableIndexByte(TableType::FILE, TableType::ASSEMBLYREF, TagBits::Implementation) });
  486. }
  487. {
  488. auto& table = _tableRowMetas[(int)TableType::NESTEDCLASS];
  489. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  490. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF) });
  491. }
  492. {
  493. auto& table = _tableRowMetas[(int)TableType::GENERICPARAM];
  494. table.push_back({ 2 });
  495. table.push_back({ 2 });
  496. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF, TableType::METHOD, TagBits::TypeOrMethodDef) });
  497. table.push_back({ ComputStringIndexByte() });
  498. }
  499. {
  500. auto& table = _tableRowMetas[(int)TableType::METHODSPEC];
  501. table.push_back({ ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::MethodDefOrRef) });
  502. table.push_back({ ComputBlobIndexByte() });
  503. }
  504. {
  505. auto& table = _tableRowMetas[(int)TableType::GENERICPARAMCONSTRAINT];
  506. table.push_back({ ComputTableIndexByte(TableType::GENERICPARAM) });
  507. table.push_back({ ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef) });
  508. }
  509. {
  510. auto& table = _tableRowMetas[(int)TableType::DOCUMENT];
  511. table.push_back({ ComputBlobIndexByte() });
  512. table.push_back({ ComputGUIDIndexByte() });
  513. table.push_back({ ComputBlobIndexByte() });
  514. table.push_back({ ComputGUIDIndexByte() });
  515. }
  516. {
  517. auto& table = _tableRowMetas[(int)TableType::METHODBODY];
  518. table.push_back({ ComputTableIndexByte(TableType::DOCUMENT) });
  519. table.push_back({ ComputBlobIndexByte() });
  520. }
  521. {
  522. auto& table = _tableRowMetas[(int)TableType::LOCALSCOPE];
  523. table.push_back({ ComputTableIndexByte(TableType::METHOD) });
  524. table.push_back({ ComputTableIndexByte(TableType::IMPORTSCOPE) });
  525. table.push_back({ ComputTableIndexByte(TableType::LOCALVARIABLE) });
  526. table.push_back({ ComputTableIndexByte(TableType::LOCALCONSTANT) });
  527. table.push_back({ 4 });
  528. table.push_back({ 4 });
  529. }
  530. {
  531. auto& table = _tableRowMetas[(int)TableType::LOCALVARIABLE];
  532. table.push_back({ 2 });
  533. table.push_back({ 2 });
  534. table.push_back({ ComputStringIndexByte() });
  535. }
  536. {
  537. auto& table = _tableRowMetas[(int)TableType::LOCALCONSTANT];
  538. table.push_back({ ComputStringIndexByte() });
  539. table.push_back({ ComputBlobIndexByte() });
  540. }
  541. {
  542. auto& table = _tableRowMetas[(int)TableType::IMPORTSCOPE];
  543. table.push_back({ ComputTableIndexByte(TableType::IMPORTSCOPE) });
  544. table.push_back({ ComputBlobIndexByte() });
  545. }
  546. {
  547. auto& table = _tableRowMetas[(int)TableType::STATEMACHINEMETHOD];
  548. table.push_back({ ComputTableIndexByte(TableType::METHOD) });
  549. table.push_back({ ComputTableIndexByte(TableType::METHOD) });
  550. }
  551. {
  552. auto& table = _tableRowMetas[(int)TableType::CUSTOMDEBUGINFORMATION];
  553. table.push_back({ ComputTableIndexByte(HasCustomDebugInformation, sizeof(HasCustomDebugInformation)/sizeof(TableType), TagBits::HasCustomDebugInformation) });
  554. table.push_back({ ComputGUIDIndexByte() });
  555. table.push_back({ ComputBlobIndexByte() });
  556. }
  557. for (int i = 0; i < TABLE_NUM; i++)
  558. {
  559. auto& table = _tableRowMetas[i];
  560. if (table.empty())
  561. {
  562. IL2CPP_ASSERT(_tables[i].rowNum == 0 && _tables[i].rowMetaDataSize == 0);
  563. }
  564. else
  565. {
  566. uint32_t totalSize = 0;
  567. for (auto& col : table)
  568. {
  569. col.offset = totalSize;
  570. totalSize += col.size;
  571. }
  572. uint32_t computSize = ComputTableRowMetaDataSize((TableType)i);
  573. if (computSize != totalSize)
  574. {
  575. IL2CPP_ASSERT(totalSize == computSize);
  576. }
  577. }
  578. }
  579. }
  580. uint32_t RawImageBase::ComputTableRowMetaDataSize(TableType tableIndex) const
  581. {
  582. switch (tableIndex)
  583. {
  584. case TableType::MODULE: return 2 + ComputStringIndexByte() + ComputGUIDIndexByte() * 3;
  585. case TableType::TYPEREF: return ComputTableIndexByte(TableType::MODULE, TableType::MODULEREF, TableType::ASSEMBLYREF, TableType::TYPEREF, TagBits::ResoulutionScope)
  586. + ComputStringIndexByte() * 2;
  587. case TableType::TYPEDEF: return 4 + ComputStringIndexByte() * 2
  588. + ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef)
  589. + ComputTableIndexByte(TableType::FIELD)
  590. + ComputTableIndexByte(TableType::METHOD);
  591. case TableType::FIELD_POINTER: return ComputTableIndexByte(TableType::FIELD);
  592. case TableType::FIELD: return 2 + ComputStringIndexByte() + ComputBlobIndexByte();
  593. case TableType::METHOD_POINTER: return ComputTableIndexByte(TableType::METHOD);
  594. case TableType::METHOD: return 4 + 2 + 2 + ComputStringIndexByte() + ComputBlobIndexByte() + ComputTableIndexByte(TableType::PARAM);
  595. case TableType::PARAM_POINTER: return ComputTableIndexByte(TableType::PARAM);
  596. case TableType::PARAM: return 2 + 2 + ComputStringIndexByte();
  597. case TableType::INTERFACEIMPL: return ComputTableIndexByte(TableType::TYPEDEF)
  598. + ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef);
  599. case TableType::MEMBERREF: return ComputTableIndexByte(TableType::METHOD, TableType::MODULEREF, TableType::TYPEDEF, TableType::TYPEREF, TagBits::MemberRefParent)
  600. + ComputStringIndexByte() + ComputBlobIndexByte();
  601. case TableType::CONSTANT: return 2
  602. + ComputTableIndexByte(TableType::PARAM, TableType::FIELD, TableType::PROPERTY, TagBits::HasConstant)
  603. + ComputBlobIndexByte();
  604. case TableType::CUSTOMATTRIBUTE: return ComputTableIndexByte(HasCustomAttributeAssociateTables, sizeof(HasCustomAttributeAssociateTables) / sizeof(TableType), TagBits::HasCustomAttribute)
  605. + ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::CustomAttributeType)
  606. + ComputBlobIndexByte();
  607. case TableType::FIELDMARSHAL: return ComputTableIndexByte(TableType::FIELD, TableType::PARAM, TagBits::HasFieldMarshal)
  608. + ComputBlobIndexByte();
  609. case TableType::DECLSECURITY: return 2 + ComputTableIndexByte(TableType::TYPEDEF, TableType::METHOD, TableType::ASSEMBLY, TagBits::HasDeclSecurity)
  610. + ComputBlobIndexByte();
  611. case TableType::CLASSLAYOUT: return 2 + 4 + ComputTableIndexByte(TableType::TYPEDEF);
  612. case TableType::FIELDLAYOUT: return 4 + ComputTableIndexByte(TableType::FIELD);
  613. case TableType::STANDALONESIG: return ComputBlobIndexByte();
  614. case TableType::EVENTMAP: return ComputTableIndexByte(TableType::TYPEDEF) + ComputTableIndexByte(TableType::EVENT);
  615. case TableType::EVENT_POINTER: return ComputTableIndexByte(TableType::EVENT);
  616. case TableType::EVENT: return 2
  617. + ComputStringIndexByte()
  618. + ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef);
  619. case TableType::PROPERTYMAP: return ComputTableIndexByte(TableType::TYPEDEF)
  620. + ComputTableIndexByte(TableType::PROPERTY);
  621. case TableType::PROPERTY_POINTER: return ComputTableIndexByte(TableType::PROPERTY);
  622. case TableType::PROPERTY: return 2 + ComputStringIndexByte() + ComputBlobIndexByte();
  623. case TableType::METHODSEMANTICS: return 2
  624. + ComputTableIndexByte(TableType::METHOD)
  625. + ComputTableIndexByte(TableType::EVENT, TableType::PROPERTY, TagBits::HasSemantics);
  626. case TableType::METHODIMPL: return ComputTableIndexByte(TableType::TYPEDEF)
  627. + ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::MethodDefOrRef)
  628. + ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::MethodDefOrRef);
  629. case TableType::MODULEREF: return ComputStringIndexByte();
  630. case TableType::TYPESPEC: return ComputBlobIndexByte();
  631. case TableType::IMPLMAP: return 2
  632. + ComputTableIndexByte(TableType::FIELD, TableType::METHOD, TagBits::MemberForwarded)
  633. + ComputStringIndexByte()
  634. + ComputTableIndexByte(TableType::MODULEREF);
  635. case TableType::FIELDRVA: return 4 + ComputTableIndexByte(TableType::FIELD);
  636. case TableType::UNUSED6:
  637. case TableType::UNUSED7:
  638. LogPanic("unused table type");
  639. return 0;
  640. case TableType::ASSEMBLY: return 4 + 4 * 2 + 4
  641. + ComputBlobIndexByte()
  642. + ComputStringIndexByte() * 2;
  643. case TableType::ASSEMBLYPROCESSOR: return 4;
  644. case TableType::ASSEMBLYOS: return 4 + 4 + 4;
  645. case TableType::ASSEMBLYREF: return 2 * 4 + 4
  646. + ComputBlobIndexByte()
  647. + ComputStringIndexByte() * 2
  648. + ComputBlobIndexByte();
  649. case TableType::ASSEMBLYREFPROCESSOR: return 4 + ComputTableIndexByte(TableType::ASSEMBLYREF);
  650. case TableType::ASSEMBLYREFOS: return 4 * 3 + ComputTableIndexByte(TableType::ASSEMBLYREF);
  651. case TableType::FILE: return 4 + ComputStringIndexByte() + ComputBlobIndexByte();
  652. case TableType::EXPORTEDTYPE: return 4 + 4
  653. + ComputStringIndexByte() * 2
  654. + ComputTableIndexByte(TableType::FILE, TableType::EXPORTEDTYPE, TableType::ASSEMBLY, TagBits::Implementation);
  655. case TableType::MANIFESTRESOURCE: return 4 + 4
  656. + ComputStringIndexByte()
  657. + ComputTableIndexByte(TableType::FILE, TableType::ASSEMBLYREF, TagBits::Implementation);
  658. case TableType::NESTEDCLASS: return ComputTableIndexByte(TableType::TYPEDEF) * 2;
  659. case TableType::GENERICPARAM: return 2 + 2
  660. + ComputTableIndexByte(TableType::TYPEDEF, TableType::METHOD, TagBits::TypeOrMethodDef)
  661. + ComputStringIndexByte();
  662. case TableType::METHODSPEC: return ComputTableIndexByte(TableType::METHOD, TableType::MEMBERREF, TagBits::MethodDefOrRef)
  663. + ComputBlobIndexByte();
  664. case TableType::GENERICPARAMCONSTRAINT: return ComputTableIndexByte(TableType::GENERICPARAM)
  665. + ComputTableIndexByte(TableType::TYPEDEF, TableType::TYPEREF, TableType::TYPESPEC, TagBits::TypeDefOrRef);
  666. case TableType::UNUSED8:
  667. case TableType::UNUSED9:
  668. case TableType::UNUSED10:
  669. LogPanic("unused table type");
  670. return 0;
  671. case TableType::DOCUMENT: return ComputBlobIndexByte() + ComputGUIDIndexByte() + ComputBlobIndexByte() + ComputGUIDIndexByte();
  672. case TableType::METHODBODY: return ComputTableIndexByte(TableType::DOCUMENT) + ComputBlobIndexByte();
  673. case TableType::LOCALSCOPE: return ComputTableIndexByte(TableType::METHOD) + ComputTableIndexByte(TableType::IMPORTSCOPE)
  674. + ComputTableIndexByte(TableType::LOCALVARIABLE) + ComputTableIndexByte(TableType::LOCALCONSTANT) + 4 + 4;
  675. case TableType::LOCALVARIABLE: return 2 + 2 + ComputStringIndexByte();
  676. case TableType::LOCALCONSTANT: return ComputStringIndexByte() + ComputBlobIndexByte();
  677. case TableType::IMPORTSCOPE: return ComputTableIndexByte(TableType::IMPORTSCOPE) + ComputBlobIndexByte();
  678. case TableType::STATEMACHINEMETHOD: return ComputTableIndexByte(TableType::METHOD) * 2;
  679. case TableType::CUSTOMDEBUGINFORMATION: return ComputTableIndexByte(HasCustomDebugInformation, sizeof(HasCustomDebugInformation) / sizeof(TableType), TagBits::HasCustomDebugInformation)
  680. + ComputGUIDIndexByte() + ComputBlobIndexByte();
  681. default:
  682. LogPanic("unknown table type");
  683. return 0;
  684. }
  685. }
  686. }
  687. }