aot_loader.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_runtime.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "aot_reloc.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../common/wasm_native.h"
  11. #include "../compilation/aot.h"
  12. #if WASM_ENABLE_JIT != 0
  13. #include "../compilation/aot_llvm.h"
  14. #include "../interpreter/wasm_loader.h"
  15. #endif
  16. #if WASM_ENABLE_DEBUG_AOT != 0
  17. #include "debug/elf_parser.h"
  18. #include "debug/jit_debug.h"
  19. #endif
  20. #define XMM_PLT_PREFIX "__xmm@"
  21. #define REAL_PLT_PREFIX "__real@"
  22. static void
  23. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  24. {
  25. if (error_buf != NULL) {
  26. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  27. string);
  28. }
  29. }
  30. static void
  31. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  32. {
  33. va_list args;
  34. char buf[128];
  35. if (error_buf != NULL) {
  36. va_start(args, format);
  37. vsnprintf(buf, sizeof(buf), format, args);
  38. va_end(args);
  39. snprintf(error_buf, error_buf_size, "AOT module load failed: %s", buf);
  40. }
  41. }
  42. #define exchange_uint8(p_data) (void)0
  43. static void
  44. exchange_uint16(uint8 *p_data)
  45. {
  46. uint8 value = *p_data;
  47. *p_data = *(p_data + 1);
  48. *(p_data + 1) = value;
  49. }
  50. static void
  51. exchange_uint32(uint8 *p_data)
  52. {
  53. uint8 value = *p_data;
  54. *p_data = *(p_data + 3);
  55. *(p_data + 3) = value;
  56. value = *(p_data + 1);
  57. *(p_data + 1) = *(p_data + 2);
  58. *(p_data + 2) = value;
  59. }
  60. static void
  61. exchange_uint64(uint8 *pData)
  62. {
  63. uint32 value;
  64. value = *(uint32 *)pData;
  65. *(uint32 *)pData = *(uint32 *)(pData + 4);
  66. *(uint32 *)(pData + 4) = value;
  67. exchange_uint32(pData);
  68. exchange_uint32(pData + 4);
  69. }
  70. static union {
  71. int a;
  72. char b;
  73. } __ue = { .a = 1 };
  74. #define is_little_endian() (__ue.b == 1)
  75. static bool
  76. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  77. char *error_buf, uint32 error_buf_size)
  78. {
  79. if (buf + length < buf || buf + length > buf_end) {
  80. set_error_buf(error_buf, error_buf_size, "unexpect end");
  81. return false;
  82. }
  83. return true;
  84. }
  85. #define CHECK_BUF(buf, buf_end, length) \
  86. do { \
  87. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  88. goto fail; \
  89. } \
  90. } while (0)
  91. static uint8 *
  92. align_ptr(const uint8 *p, uint32 b)
  93. {
  94. uintptr_t v = (uintptr_t)p;
  95. uintptr_t m = b - 1;
  96. return (uint8 *)((v + m) & ~m);
  97. }
  98. static inline uint64
  99. GET_U64_FROM_ADDR(uint32 *addr)
  100. {
  101. union {
  102. uint64 val;
  103. uint32 parts[2];
  104. } u;
  105. u.parts[0] = addr[0];
  106. u.parts[1] = addr[1];
  107. return u.val;
  108. }
  109. #define TEMPLATE_READ(p, p_end, res, type) \
  110. do { \
  111. if (sizeof(type) != sizeof(uint64)) \
  112. p = (uint8 *)align_ptr(p, sizeof(type)); \
  113. else \
  114. /* align 4 bytes if type is uint64 */ \
  115. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  116. CHECK_BUF(p, p_end, sizeof(type)); \
  117. if (sizeof(type) != sizeof(uint64)) \
  118. res = *(type *)p; \
  119. else \
  120. res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
  121. if (!is_little_endian()) \
  122. exchange_##type((uint8 *)&res); \
  123. p += sizeof(type); \
  124. } while (0)
  125. #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
  126. #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
  127. #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
  128. #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
  129. #define read_byte_array(p, p_end, addr, len) \
  130. do { \
  131. CHECK_BUF(p, p_end, len); \
  132. memcpy(addr, p, len); \
  133. p += len; \
  134. } while (0)
  135. #define read_string(p, p_end, str) \
  136. do { \
  137. if (!(str = load_string((uint8 **)&p, p_end, module, \
  138. is_load_from_file_buf, error_buf, \
  139. error_buf_size))) \
  140. goto fail; \
  141. } while (0)
  142. /* Legal values for bin_type */
  143. #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
  144. #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
  145. #define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
  146. #define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
  147. #define BIN_TYPE_COFF32 4 /* 32-bit little endian */
  148. #define BIN_TYPE_COFF64 6 /* 64-bit little endian */
  149. /* Legal values for e_type (object file type). */
  150. #define E_TYPE_NONE 0 /* No file type */
  151. #define E_TYPE_REL 1 /* Relocatable file */
  152. #define E_TYPE_EXEC 2 /* Executable file */
  153. #define E_TYPE_DYN 3 /* Shared object file */
  154. /* Legal values for e_machine (architecture). */
  155. #define E_MACHINE_386 3 /* Intel 80386 */
  156. #define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
  157. #define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
  158. #define E_MACHINE_ARM 40 /* ARM/Thumb */
  159. #define E_MACHINE_AARCH64 183 /* AArch64 */
  160. #define E_MACHINE_ARC 45 /* Argonaut RISC Core */
  161. #define E_MACHINE_IA_64 50 /* Intel Merced */
  162. #define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
  163. #define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
  164. #define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */
  165. #define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
  166. #define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
  167. #define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
  168. #define E_MACHINE_WIN_I386 0x14c /* Windows i386 architecture */
  169. #define E_MACHINE_WIN_X86_64 0x8664 /* Windows x86-64 architecture */
  170. /* Legal values for e_version */
  171. #define E_VERSION_CURRENT 1 /* Current version */
  172. static void *
  173. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  174. {
  175. void *mem;
  176. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  177. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  178. return NULL;
  179. }
  180. memset(mem, 0, (uint32)size);
  181. return mem;
  182. }
  183. static char *
  184. const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  185. char *error_buf, uint32 error_buf_size)
  186. {
  187. HashMap *set = module->const_str_set;
  188. char *c_str, *value;
  189. /* Create const string set if it isn't created */
  190. if (!set
  191. && !(set = module->const_str_set = bh_hash_map_create(
  192. 32, false, (HashFunc)wasm_string_hash,
  193. (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) {
  194. set_error_buf(error_buf, error_buf_size,
  195. "create const string set failed");
  196. return NULL;
  197. }
  198. /* Lookup const string set, use the string if found */
  199. if (!(c_str = loader_malloc((uint32)len + 1, error_buf, error_buf_size))) {
  200. return NULL;
  201. }
  202. bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len);
  203. c_str[len] = '\0';
  204. if ((value = bh_hash_map_find(set, c_str))) {
  205. wasm_runtime_free(c_str);
  206. return value;
  207. }
  208. if (!bh_hash_map_insert(set, c_str, c_str)) {
  209. set_error_buf(error_buf, error_buf_size,
  210. "insert string to hash map failed");
  211. wasm_runtime_free(c_str);
  212. return NULL;
  213. }
  214. return c_str;
  215. }
  216. static char *
  217. load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  218. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  219. {
  220. uint8 *p = *p_buf;
  221. const uint8 *p_end = buf_end;
  222. char *str;
  223. uint16 str_len;
  224. CHECK_BUF(p, p_end, 1);
  225. if (*p & 0x80) {
  226. /* The string has been adjusted */
  227. str = (char *)++p;
  228. /* Ensure the whole string is in range */
  229. do {
  230. CHECK_BUF(p, p_end, 1);
  231. } while (*p++ != '\0');
  232. }
  233. else {
  234. /* The string hasn't been adjusted */
  235. read_uint16(p, p_end, str_len);
  236. CHECK_BUF(p, p_end, str_len);
  237. if (str_len == 0) {
  238. str = "";
  239. }
  240. else if (p[str_len - 1] == '\0') {
  241. /* The string is terminated with '\0', use it directly */
  242. str = (char *)p;
  243. }
  244. else if (is_load_from_file_buf) {
  245. /* As the file buffer can be referred to after loading,
  246. we use the 2 bytes of size to adjust the string:
  247. mark the flag with the highest bit of size[0],
  248. move string 1 byte backward and then append '\0' */
  249. *(p - 2) |= 0x80;
  250. bh_memmove_s(p - 1, (uint32)(str_len + 1), p, (uint32)str_len);
  251. p[str_len - 1] = '\0';
  252. str = (char *)(p - 1);
  253. }
  254. else {
  255. /* Load from sections, the file buffer cannot be reffered to
  256. after loading, we must create another string and insert it
  257. into const string set */
  258. if (!(str = const_str_set_insert((uint8 *)p, str_len, module,
  259. error_buf, error_buf_size))) {
  260. goto fail;
  261. }
  262. }
  263. p += str_len;
  264. }
  265. *p_buf = p;
  266. return str;
  267. fail:
  268. return NULL;
  269. }
  270. static bool
  271. get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
  272. uint32 target_buf_size, char *error_buf,
  273. uint32 error_buf_size)
  274. {
  275. char *machine_type = NULL;
  276. switch (target_info->e_machine) {
  277. case E_MACHINE_X86_64:
  278. case E_MACHINE_WIN_X86_64:
  279. machine_type = "x86_64";
  280. break;
  281. case E_MACHINE_386:
  282. case E_MACHINE_WIN_I386:
  283. machine_type = "i386";
  284. break;
  285. case E_MACHINE_ARM:
  286. case E_MACHINE_AARCH64:
  287. machine_type = target_info->arch;
  288. break;
  289. case E_MACHINE_MIPS:
  290. machine_type = "mips";
  291. break;
  292. case E_MACHINE_XTENSA:
  293. machine_type = "xtensa";
  294. break;
  295. case E_MACHINE_RISCV:
  296. machine_type = "riscv";
  297. break;
  298. case E_MACHINE_ARC_COMPACT:
  299. case E_MACHINE_ARC_COMPACT2:
  300. machine_type = "arc";
  301. break;
  302. default:
  303. set_error_buf_v(error_buf, error_buf_size,
  304. "unknown machine type %d", target_info->e_machine);
  305. return false;
  306. }
  307. if (strncmp(target_info->arch, machine_type, strlen(machine_type))) {
  308. set_error_buf_v(
  309. error_buf, error_buf_size,
  310. "machine type (%s) isn't consistent with target type (%s)",
  311. machine_type, target_info->arch);
  312. return false;
  313. }
  314. snprintf(target_buf, target_buf_size, "%s", target_info->arch);
  315. return true;
  316. }
  317. static bool
  318. check_machine_info(AOTTargetInfo *target_info, char *error_buf,
  319. uint32 error_buf_size)
  320. {
  321. char target_expected[32], target_got[32];
  322. get_current_target(target_expected, sizeof(target_expected));
  323. if (!get_aot_file_target(target_info, target_got, sizeof(target_got),
  324. error_buf, error_buf_size))
  325. return false;
  326. if (strncmp(target_expected, target_got, strlen(target_expected))) {
  327. set_error_buf_v(error_buf, error_buf_size,
  328. "invalid target type, expected %s but got %s",
  329. target_expected, target_got);
  330. return false;
  331. }
  332. return true;
  333. }
  334. static bool
  335. load_target_info_section(const uint8 *buf, const uint8 *buf_end,
  336. AOTModule *module, char *error_buf,
  337. uint32 error_buf_size)
  338. {
  339. AOTTargetInfo target_info;
  340. const uint8 *p = buf, *p_end = buf_end;
  341. bool is_target_little_endian, is_target_64_bit;
  342. read_uint16(p, p_end, target_info.bin_type);
  343. read_uint16(p, p_end, target_info.abi_type);
  344. read_uint16(p, p_end, target_info.e_type);
  345. read_uint16(p, p_end, target_info.e_machine);
  346. read_uint32(p, p_end, target_info.e_version);
  347. read_uint32(p, p_end, target_info.e_flags);
  348. read_uint32(p, p_end, target_info.reserved);
  349. read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
  350. if (p != buf_end) {
  351. set_error_buf(error_buf, error_buf_size, "invalid section size");
  352. return false;
  353. }
  354. /* Check target endian type */
  355. is_target_little_endian = target_info.bin_type & 1 ? false : true;
  356. if (is_little_endian() != is_target_little_endian) {
  357. set_error_buf_v(error_buf, error_buf_size,
  358. "invalid target endian type, expected %s but got %s",
  359. is_little_endian() ? "little endian" : "big endian",
  360. is_target_little_endian ? "little endian"
  361. : "big endian");
  362. return false;
  363. }
  364. /* Check target bit width */
  365. is_target_64_bit = target_info.bin_type & 2 ? true : false;
  366. if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) {
  367. set_error_buf_v(error_buf, error_buf_size,
  368. "invalid target bit width, expected %s but got %s",
  369. sizeof(void *) == 8 ? "64-bit" : "32-bit",
  370. is_target_64_bit ? "64-bit" : "32-bit");
  371. return false;
  372. }
  373. /* Check target elf file type */
  374. if (target_info.e_type != E_TYPE_REL) {
  375. set_error_buf(error_buf, error_buf_size,
  376. "invalid object file type, "
  377. "expected relocatable file type but got others");
  378. return false;
  379. }
  380. /* Check machine info */
  381. if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
  382. return false;
  383. }
  384. if (target_info.e_version != E_VERSION_CURRENT) {
  385. set_error_buf(error_buf, error_buf_size, "invalid elf file version");
  386. return false;
  387. }
  388. return true;
  389. fail:
  390. return false;
  391. }
  392. static void *
  393. get_native_symbol_by_name(const char *name)
  394. {
  395. void *func = NULL;
  396. uint32 symnum = 0;
  397. SymbolMap *sym = NULL;
  398. sym = get_target_symbol_map(&symnum);
  399. while (symnum--) {
  400. if (strcmp(sym->symbol_name, name) == 0) {
  401. func = sym->symbol_addr;
  402. break;
  403. }
  404. sym++;
  405. }
  406. return func;
  407. }
  408. static bool
  409. load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
  410. AOTModule *module, bool is_load_from_file_buf,
  411. char *error_buf, uint32 error_buf_size)
  412. {
  413. const uint8 *p = buf, *p_end = buf_end;
  414. uint32 cnt;
  415. int32 i;
  416. const char *symbol;
  417. read_uint32(p, p_end, cnt);
  418. module->native_symbol_count = cnt;
  419. if (cnt > 0) {
  420. module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *));
  421. if (module->native_symbol_list == NULL) {
  422. set_error_buf(error_buf, error_buf_size,
  423. "malloc native symbol list failed");
  424. goto fail;
  425. }
  426. for (i = cnt - 1; i >= 0; i--) {
  427. read_string(p, p_end, symbol);
  428. module->native_symbol_list[i] = get_native_symbol_by_name(symbol);
  429. if (module->native_symbol_list[i] == NULL) {
  430. set_error_buf_v(error_buf, error_buf_size,
  431. "missing native symbol: %s", symbol);
  432. goto fail;
  433. }
  434. }
  435. }
  436. return true;
  437. fail:
  438. return false;
  439. }
  440. static bool
  441. load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  442. bool is_load_from_file_buf, char *error_buf,
  443. uint32 error_buf_size)
  444. {
  445. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  446. const uint8 *p = buf, *p_end = buf_end;
  447. uint32 *aux_func_indexes;
  448. const char **aux_func_names;
  449. uint32 name_type, subsection_size;
  450. uint32 previous_name_type = 0;
  451. uint32 num_func_name;
  452. uint32 func_index;
  453. uint32 previous_func_index = ~0U;
  454. uint32 name_index;
  455. int i = 0;
  456. uint32 name_len;
  457. uint64 size;
  458. if (p >= p_end) {
  459. set_error_buf(error_buf, error_buf_size, "unexpected end");
  460. return false;
  461. }
  462. read_uint32(p, p_end, name_len);
  463. if (name_len != 4 || p + name_len > p_end) {
  464. set_error_buf(error_buf, error_buf_size, "unexpected end");
  465. return false;
  466. }
  467. if (memcmp(p, "name", 4) != 0) {
  468. set_error_buf(error_buf, error_buf_size, "invalid custom name section");
  469. return false;
  470. }
  471. p += name_len;
  472. while (p < p_end) {
  473. read_uint32(p, p_end, name_type);
  474. if (i != 0) {
  475. if (name_type == previous_name_type) {
  476. set_error_buf(error_buf, error_buf_size,
  477. "duplicate sub-section");
  478. return false;
  479. }
  480. if (name_type < previous_name_type) {
  481. set_error_buf(error_buf, error_buf_size,
  482. "out-of-order sub-section");
  483. return false;
  484. }
  485. }
  486. previous_name_type = name_type;
  487. read_uint32(p, p_end, subsection_size);
  488. CHECK_BUF(p, p_end, subsection_size);
  489. switch (name_type) {
  490. case SUB_SECTION_TYPE_FUNC:
  491. if (subsection_size) {
  492. read_uint32(p, p_end, num_func_name);
  493. if (num_func_name
  494. > module->import_func_count + module->func_count) {
  495. set_error_buf(error_buf, error_buf_size,
  496. "function name count out of bounds");
  497. return false;
  498. }
  499. module->aux_func_name_count = num_func_name;
  500. /* Allocate memory */
  501. size = sizeof(uint32) * (uint64)module->aux_func_name_count;
  502. if (!(aux_func_indexes = module->aux_func_indexes =
  503. loader_malloc(size, error_buf, error_buf_size))) {
  504. return false;
  505. }
  506. size =
  507. sizeof(char **) * (uint64)module->aux_func_name_count;
  508. if (!(aux_func_names = module->aux_func_names =
  509. loader_malloc(size, error_buf, error_buf_size))) {
  510. return false;
  511. }
  512. for (name_index = 0; name_index < num_func_name;
  513. name_index++) {
  514. read_uint32(p, p_end, func_index);
  515. if (name_index != 0
  516. && func_index == previous_func_index) {
  517. set_error_buf(error_buf, error_buf_size,
  518. "duplicate function name");
  519. return false;
  520. }
  521. if (name_index != 0
  522. && func_index < previous_func_index) {
  523. set_error_buf(error_buf, error_buf_size,
  524. "out-of-order function index ");
  525. return false;
  526. }
  527. if (func_index
  528. >= module->import_func_count + module->func_count) {
  529. set_error_buf(error_buf, error_buf_size,
  530. "function index out of bounds");
  531. return false;
  532. }
  533. previous_func_index = func_index;
  534. *(aux_func_indexes + name_index) = func_index;
  535. read_string(p, p_end, *(aux_func_names + name_index));
  536. #if 0
  537. LOG_DEBUG("func_index %u -> aux_func_name = %s\n",
  538. func_index, *(aux_func_names + name_index));
  539. #endif
  540. }
  541. }
  542. break;
  543. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  544. */
  545. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  546. default:
  547. p = p + subsection_size;
  548. break;
  549. }
  550. i++;
  551. }
  552. return true;
  553. fail:
  554. return false;
  555. #else
  556. return true;
  557. #endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */
  558. }
  559. static bool
  560. load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  561. bool is_load_from_file_buf, char *error_buf,
  562. uint32 error_buf_size)
  563. {
  564. const uint8 *p = buf, *p_end = buf_end;
  565. uint32 sub_section_type;
  566. read_uint32(p, p_end, sub_section_type);
  567. buf = p;
  568. switch (sub_section_type) {
  569. case AOT_CUSTOM_SECTION_NATIVE_SYMBOL:
  570. if (!load_native_symbol_section(buf, buf_end, module,
  571. is_load_from_file_buf, error_buf,
  572. error_buf_size))
  573. goto fail;
  574. break;
  575. case AOT_CUSTOM_SECTION_NAME:
  576. if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
  577. error_buf, error_buf_size))
  578. goto fail;
  579. break;
  580. default:
  581. break;
  582. }
  583. return true;
  584. fail:
  585. return false;
  586. }
  587. static void
  588. destroy_import_memories(AOTImportMemory *import_memories, bool is_jit_mode)
  589. {
  590. if (!is_jit_mode)
  591. wasm_runtime_free(import_memories);
  592. }
  593. static void
  594. destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count,
  595. bool is_jit_mode)
  596. {
  597. if (!is_jit_mode) {
  598. uint32 i;
  599. for (i = 0; i < count; i++)
  600. if (data_list[i])
  601. wasm_runtime_free(data_list[i]);
  602. wasm_runtime_free(data_list);
  603. }
  604. }
  605. static bool
  606. load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  607. AOTModule *module, char *error_buf,
  608. uint32 error_buf_size)
  609. {
  610. const uint8 *buf = *p_buf;
  611. AOTMemInitData **data_list;
  612. uint64 size;
  613. uint32 i;
  614. /* Allocate memory */
  615. size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
  616. if (!(module->mem_init_data_list = data_list =
  617. loader_malloc(size, error_buf, error_buf_size))) {
  618. return false;
  619. }
  620. /* Create each memory data segment */
  621. for (i = 0; i < module->mem_init_data_count; i++) {
  622. uint32 init_expr_type, byte_count;
  623. uint64 init_expr_value;
  624. uint32 is_passive;
  625. uint32 memory_index;
  626. read_uint32(buf, buf_end, is_passive);
  627. read_uint32(buf, buf_end, memory_index);
  628. read_uint32(buf, buf_end, init_expr_type);
  629. read_uint64(buf, buf_end, init_expr_value);
  630. read_uint32(buf, buf_end, byte_count);
  631. size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
  632. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  633. return false;
  634. }
  635. #if WASM_ENABLE_BULK_MEMORY != 0
  636. /* is_passive and memory_index is only used in bulk memory mode */
  637. data_list[i]->is_passive = (bool)is_passive;
  638. data_list[i]->memory_index = memory_index;
  639. #endif
  640. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  641. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  642. data_list[i]->byte_count = byte_count;
  643. read_byte_array(buf, buf_end, data_list[i]->bytes,
  644. data_list[i]->byte_count);
  645. }
  646. *p_buf = buf;
  647. return true;
  648. fail:
  649. return false;
  650. }
  651. static bool
  652. load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  653. char *error_buf, uint32 error_buf_size)
  654. {
  655. uint32 i;
  656. uint64 total_size;
  657. const uint8 *buf = *p_buf;
  658. read_uint32(buf, buf_end, module->import_memory_count);
  659. /* We don't support import_memory_count > 0 currently */
  660. bh_assert(module->import_memory_count == 0);
  661. read_uint32(buf, buf_end, module->memory_count);
  662. total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
  663. if (!(module->memories =
  664. loader_malloc(total_size, error_buf, error_buf_size))) {
  665. return false;
  666. }
  667. for (i = 0; i < module->memory_count; i++) {
  668. read_uint32(buf, buf_end, module->memories[i].memory_flags);
  669. read_uint32(buf, buf_end, module->memories[i].num_bytes_per_page);
  670. read_uint32(buf, buf_end, module->memories[i].mem_init_page_count);
  671. read_uint32(buf, buf_end, module->memories[i].mem_max_page_count);
  672. }
  673. read_uint32(buf, buf_end, module->mem_init_data_count);
  674. /* load memory init data list */
  675. if (module->mem_init_data_count > 0
  676. && !load_mem_init_data_list(&buf, buf_end, module, error_buf,
  677. error_buf_size))
  678. return false;
  679. *p_buf = buf;
  680. return true;
  681. fail:
  682. return false;
  683. }
  684. static void
  685. destroy_import_tables(AOTImportTable *import_tables, bool is_jit_mode)
  686. {
  687. if (!is_jit_mode)
  688. wasm_runtime_free(import_tables);
  689. }
  690. static void
  691. destroy_tables(AOTTable *tables, bool is_jit_mode)
  692. {
  693. if (!is_jit_mode)
  694. wasm_runtime_free(tables);
  695. }
  696. static void
  697. destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count,
  698. bool is_jit_mode)
  699. {
  700. if (!is_jit_mode) {
  701. uint32 i;
  702. for (i = 0; i < count; i++)
  703. if (data_list[i])
  704. wasm_runtime_free(data_list[i]);
  705. wasm_runtime_free(data_list);
  706. }
  707. }
  708. static bool
  709. load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
  710. AOTModule *module, char *error_buf,
  711. uint32 error_buf_size)
  712. {
  713. const uint8 *buf = *p_buf;
  714. AOTImportTable *import_table;
  715. uint64 size;
  716. uint32 i, possible_grow;
  717. /* Allocate memory */
  718. size = sizeof(AOTImportTable) * (uint64)module->import_table_count;
  719. if (!(module->import_tables = import_table =
  720. loader_malloc(size, error_buf, error_buf_size))) {
  721. return false;
  722. }
  723. /* keep sync with aot_emit_table_info() aot_emit_aot_file */
  724. for (i = 0; i < module->import_table_count; i++, import_table++) {
  725. read_uint32(buf, buf_end, import_table->table_init_size);
  726. read_uint32(buf, buf_end, import_table->table_max_size);
  727. read_uint32(buf, buf_end, possible_grow);
  728. import_table->possible_grow = (possible_grow & 0x1);
  729. }
  730. *p_buf = buf;
  731. return true;
  732. fail:
  733. return false;
  734. }
  735. static bool
  736. load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  737. char *error_buf, uint32 error_buf_size)
  738. {
  739. const uint8 *buf = *p_buf;
  740. AOTTable *table;
  741. uint64 size;
  742. uint32 i, possible_grow;
  743. /* Allocate memory */
  744. size = sizeof(AOTTable) * (uint64)module->table_count;
  745. if (!(module->tables = table =
  746. loader_malloc(size, error_buf, error_buf_size))) {
  747. return false;
  748. }
  749. /* Create each table data segment */
  750. for (i = 0; i < module->table_count; i++, table++) {
  751. read_uint32(buf, buf_end, table->elem_type);
  752. read_uint32(buf, buf_end, table->table_flags);
  753. read_uint32(buf, buf_end, table->table_init_size);
  754. read_uint32(buf, buf_end, table->table_max_size);
  755. read_uint32(buf, buf_end, possible_grow);
  756. table->possible_grow = (possible_grow & 0x1);
  757. }
  758. *p_buf = buf;
  759. return true;
  760. fail:
  761. return false;
  762. }
  763. static bool
  764. load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  765. AOTModule *module, char *error_buf,
  766. uint32 error_buf_size)
  767. {
  768. const uint8 *buf = *p_buf;
  769. AOTTableInitData **data_list;
  770. uint64 size;
  771. uint32 i;
  772. /* Allocate memory */
  773. size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
  774. if (!(module->table_init_data_list = data_list =
  775. loader_malloc(size, error_buf, error_buf_size))) {
  776. return false;
  777. }
  778. /* Create each table data segment */
  779. for (i = 0; i < module->table_init_data_count; i++) {
  780. uint32 mode, elem_type;
  781. uint32 table_index, init_expr_type, func_index_count;
  782. uint64 init_expr_value, size1;
  783. read_uint32(buf, buf_end, mode);
  784. read_uint32(buf, buf_end, elem_type);
  785. read_uint32(buf, buf_end, table_index);
  786. read_uint32(buf, buf_end, init_expr_type);
  787. read_uint64(buf, buf_end, init_expr_value);
  788. read_uint32(buf, buf_end, func_index_count);
  789. size1 = sizeof(uint32) * (uint64)func_index_count;
  790. size = offsetof(AOTTableInitData, func_indexes) + size1;
  791. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  792. return false;
  793. }
  794. data_list[i]->mode = mode;
  795. data_list[i]->elem_type = elem_type;
  796. data_list[i]->is_dropped = false;
  797. data_list[i]->table_index = table_index;
  798. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  799. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  800. data_list[i]->func_index_count = func_index_count;
  801. read_byte_array(buf, buf_end, data_list[i]->func_indexes,
  802. (uint32)size1);
  803. }
  804. *p_buf = buf;
  805. return true;
  806. fail:
  807. return false;
  808. }
  809. static bool
  810. load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  811. char *error_buf, uint32 error_buf_size)
  812. {
  813. const uint8 *buf = *p_buf;
  814. read_uint32(buf, buf_end, module->import_table_count);
  815. if (module->import_table_count > 0
  816. && !load_import_table_list(&buf, buf_end, module, error_buf,
  817. error_buf_size))
  818. return false;
  819. read_uint32(buf, buf_end, module->table_count);
  820. if (module->table_count > 0
  821. && !load_table_list(&buf, buf_end, module, error_buf, error_buf_size))
  822. return false;
  823. read_uint32(buf, buf_end, module->table_init_data_count);
  824. /* load table init data list */
  825. if (module->table_init_data_count > 0
  826. && !load_table_init_data_list(&buf, buf_end, module, error_buf,
  827. error_buf_size))
  828. return false;
  829. *p_buf = buf;
  830. return true;
  831. fail:
  832. return false;
  833. }
  834. static void
  835. destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode)
  836. {
  837. if (!is_jit_mode) {
  838. uint32 i;
  839. for (i = 0; i < count; i++)
  840. if (func_types[i])
  841. wasm_runtime_free(func_types[i]);
  842. wasm_runtime_free(func_types);
  843. }
  844. }
  845. static bool
  846. load_func_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  847. char *error_buf, uint32 error_buf_size)
  848. {
  849. const uint8 *buf = *p_buf;
  850. AOTFuncType **func_types;
  851. uint64 size;
  852. uint32 i;
  853. /* Allocate memory */
  854. size = sizeof(AOTFuncType *) * (uint64)module->func_type_count;
  855. if (!(module->func_types = func_types =
  856. loader_malloc(size, error_buf, error_buf_size))) {
  857. return false;
  858. }
  859. /* Create each function type */
  860. for (i = 0; i < module->func_type_count; i++) {
  861. uint32 param_count, result_count;
  862. uint32 param_cell_num, ret_cell_num;
  863. uint64 size1;
  864. read_uint32(buf, buf_end, param_count);
  865. read_uint32(buf, buf_end, result_count);
  866. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  867. set_error_buf(error_buf, error_buf_size,
  868. "param count or result count too large");
  869. return false;
  870. }
  871. size1 = (uint64)param_count + (uint64)result_count;
  872. size = offsetof(AOTFuncType, types) + size1;
  873. if (!(func_types[i] = loader_malloc(size, error_buf, error_buf_size))) {
  874. return false;
  875. }
  876. func_types[i]->param_count = (uint16)param_count;
  877. func_types[i]->result_count = (uint16)result_count;
  878. read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1);
  879. param_cell_num = wasm_get_cell_num(func_types[i]->types, param_count);
  880. ret_cell_num =
  881. wasm_get_cell_num(func_types[i]->types + param_count, result_count);
  882. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  883. set_error_buf(error_buf, error_buf_size,
  884. "param count or result count too large");
  885. return false;
  886. }
  887. func_types[i]->param_cell_num = (uint16)param_cell_num;
  888. func_types[i]->ret_cell_num = (uint16)ret_cell_num;
  889. }
  890. *p_buf = buf;
  891. return true;
  892. fail:
  893. return false;
  894. }
  895. static bool
  896. load_func_type_info(const uint8 **p_buf, const uint8 *buf_end,
  897. AOTModule *module, char *error_buf, uint32 error_buf_size)
  898. {
  899. const uint8 *buf = *p_buf;
  900. read_uint32(buf, buf_end, module->func_type_count);
  901. /* load function type */
  902. if (module->func_type_count > 0
  903. && !load_func_types(&buf, buf_end, module, error_buf, error_buf_size))
  904. return false;
  905. *p_buf = buf;
  906. return true;
  907. fail:
  908. return false;
  909. }
  910. static void
  911. destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode)
  912. {
  913. if (!is_jit_mode)
  914. wasm_runtime_free(import_globals);
  915. }
  916. static bool
  917. load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
  918. AOTModule *module, bool is_load_from_file_buf,
  919. char *error_buf, uint32 error_buf_size)
  920. {
  921. const uint8 *buf = *p_buf;
  922. AOTImportGlobal *import_globals;
  923. uint64 size;
  924. uint32 i, data_offset = 0;
  925. #if WASM_ENABLE_LIBC_BUILTIN != 0
  926. WASMGlobalImport tmp_global;
  927. #endif
  928. /* Allocate memory */
  929. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  930. if (!(module->import_globals = import_globals =
  931. loader_malloc(size, error_buf, error_buf_size))) {
  932. return false;
  933. }
  934. /* Create each import global */
  935. for (i = 0; i < module->import_global_count; i++) {
  936. buf = (uint8 *)align_ptr(buf, 2);
  937. read_uint8(buf, buf_end, import_globals[i].type);
  938. read_uint8(buf, buf_end, import_globals[i].is_mutable);
  939. read_string(buf, buf_end, import_globals[i].module_name);
  940. read_string(buf, buf_end, import_globals[i].global_name);
  941. #if WASM_ENABLE_LIBC_BUILTIN != 0
  942. if (wasm_native_lookup_libc_builtin_global(
  943. import_globals[i].module_name, import_globals[i].global_name,
  944. &tmp_global)) {
  945. if (tmp_global.type != import_globals[i].type
  946. || tmp_global.is_mutable != import_globals[i].is_mutable) {
  947. set_error_buf(error_buf, error_buf_size,
  948. "incompatible import type");
  949. return false;
  950. }
  951. import_globals[i].global_data_linked =
  952. tmp_global.global_data_linked;
  953. }
  954. #endif
  955. import_globals[i].size = wasm_value_type_size(import_globals[i].type);
  956. import_globals[i].data_offset = data_offset;
  957. data_offset += import_globals[i].size;
  958. module->global_data_size += import_globals[i].size;
  959. }
  960. *p_buf = buf;
  961. return true;
  962. fail:
  963. return false;
  964. }
  965. static bool
  966. load_import_global_info(const uint8 **p_buf, const uint8 *buf_end,
  967. AOTModule *module, bool is_load_from_file_buf,
  968. char *error_buf, uint32 error_buf_size)
  969. {
  970. const uint8 *buf = *p_buf;
  971. read_uint32(buf, buf_end, module->import_global_count);
  972. /* load import globals */
  973. if (module->import_global_count > 0
  974. && !load_import_globals(&buf, buf_end, module, is_load_from_file_buf,
  975. error_buf, error_buf_size))
  976. return false;
  977. *p_buf = buf;
  978. return true;
  979. fail:
  980. return false;
  981. }
  982. static void
  983. destroy_globals(AOTGlobal *globals, bool is_jit_mode)
  984. {
  985. if (!is_jit_mode)
  986. wasm_runtime_free(globals);
  987. }
  988. static bool
  989. load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  990. char *error_buf, uint32 error_buf_size)
  991. {
  992. const uint8 *buf = *p_buf;
  993. AOTGlobal *globals;
  994. uint64 size;
  995. uint32 i, data_offset = 0;
  996. AOTImportGlobal *last_import_global;
  997. /* Allocate memory */
  998. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  999. if (!(module->globals = globals =
  1000. loader_malloc(size, error_buf, error_buf_size))) {
  1001. return false;
  1002. }
  1003. if (module->import_global_count > 0) {
  1004. last_import_global =
  1005. &module->import_globals[module->import_global_count - 1];
  1006. data_offset =
  1007. last_import_global->data_offset + last_import_global->size;
  1008. }
  1009. /* Create each global */
  1010. for (i = 0; i < module->global_count; i++) {
  1011. uint16 init_expr_type;
  1012. read_uint8(buf, buf_end, globals[i].type);
  1013. read_uint8(buf, buf_end, globals[i].is_mutable);
  1014. read_uint16(buf, buf_end, init_expr_type);
  1015. if (init_expr_type != INIT_EXPR_TYPE_V128_CONST) {
  1016. read_uint64(buf, buf_end, globals[i].init_expr.u.i64);
  1017. }
  1018. else {
  1019. uint64 *i64x2 = (uint64 *)globals[i].init_expr.u.v128.i64x2;
  1020. CHECK_BUF(buf, buf_end, sizeof(uint64) * 2);
  1021. wasm_runtime_read_v128(buf, &i64x2[0], &i64x2[1]);
  1022. buf += sizeof(uint64) * 2;
  1023. }
  1024. globals[i].init_expr.init_expr_type = (uint8)init_expr_type;
  1025. globals[i].size = wasm_value_type_size(globals[i].type);
  1026. globals[i].data_offset = data_offset;
  1027. data_offset += globals[i].size;
  1028. module->global_data_size += globals[i].size;
  1029. }
  1030. *p_buf = buf;
  1031. return true;
  1032. fail:
  1033. return false;
  1034. }
  1035. static bool
  1036. load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1037. char *error_buf, uint32 error_buf_size)
  1038. {
  1039. const uint8 *buf = *p_buf;
  1040. read_uint32(buf, buf_end, module->global_count);
  1041. /* load globals */
  1042. if (module->global_count > 0
  1043. && !load_globals(&buf, buf_end, module, error_buf, error_buf_size))
  1044. return false;
  1045. *p_buf = buf;
  1046. return true;
  1047. fail:
  1048. return false;
  1049. }
  1050. static void
  1051. destroy_import_funcs(AOTImportFunc *import_funcs, bool is_jit_mode)
  1052. {
  1053. if (!is_jit_mode)
  1054. wasm_runtime_free(import_funcs);
  1055. }
  1056. static bool
  1057. load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1058. bool is_load_from_file_buf, char *error_buf,
  1059. uint32 error_buf_size)
  1060. {
  1061. const char *module_name, *field_name;
  1062. const uint8 *buf = *p_buf;
  1063. AOTImportFunc *import_funcs;
  1064. uint64 size;
  1065. uint32 i;
  1066. /* Allocate memory */
  1067. size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
  1068. if (!(module->import_funcs = import_funcs =
  1069. loader_malloc(size, error_buf, error_buf_size))) {
  1070. return false;
  1071. }
  1072. /* Create each import func */
  1073. for (i = 0; i < module->import_func_count; i++) {
  1074. read_uint16(buf, buf_end, import_funcs[i].func_type_index);
  1075. if (import_funcs[i].func_type_index >= module->func_type_count) {
  1076. set_error_buf(error_buf, error_buf_size, "unknown type");
  1077. return false;
  1078. }
  1079. import_funcs[i].func_type =
  1080. module->func_types[import_funcs[i].func_type_index];
  1081. read_string(buf, buf_end, import_funcs[i].module_name);
  1082. read_string(buf, buf_end, import_funcs[i].func_name);
  1083. module_name = import_funcs[i].module_name;
  1084. field_name = import_funcs[i].func_name;
  1085. import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol(
  1086. module_name, field_name, import_funcs[i].func_type,
  1087. &import_funcs[i].signature, &import_funcs[i].attachment,
  1088. &import_funcs[i].call_conv_raw);
  1089. #if WASM_ENABLE_LIBC_WASI != 0
  1090. if (!strcmp(import_funcs[i].module_name, "wasi_unstable")
  1091. || !strcmp(import_funcs[i].module_name, "wasi_snapshot_preview1"))
  1092. module->is_wasi_module = true;
  1093. #endif
  1094. }
  1095. *p_buf = buf;
  1096. return true;
  1097. fail:
  1098. return false;
  1099. }
  1100. static bool
  1101. load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
  1102. AOTModule *module, bool is_load_from_file_buf,
  1103. char *error_buf, uint32 error_buf_size)
  1104. {
  1105. const uint8 *buf = *p_buf;
  1106. read_uint32(buf, buf_end, module->import_func_count);
  1107. /* load import funcs */
  1108. if (module->import_func_count > 0
  1109. && !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf,
  1110. error_buf, error_buf_size))
  1111. return false;
  1112. *p_buf = buf;
  1113. return true;
  1114. fail:
  1115. return false;
  1116. }
  1117. static void
  1118. destroy_object_data_sections(AOTObjectDataSection *data_sections,
  1119. uint32 data_section_count)
  1120. {
  1121. uint32 i;
  1122. AOTObjectDataSection *data_section = data_sections;
  1123. for (i = 0; i < data_section_count; i++, data_section++)
  1124. if (data_section->data)
  1125. os_munmap(data_section->data, data_section->size);
  1126. wasm_runtime_free(data_sections);
  1127. }
  1128. static bool
  1129. load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
  1130. AOTModule *module, bool is_load_from_file_buf,
  1131. char *error_buf, uint32 error_buf_size)
  1132. {
  1133. const uint8 *buf = *p_buf;
  1134. AOTObjectDataSection *data_sections;
  1135. uint64 size;
  1136. uint32 i;
  1137. /* Allocate memory */
  1138. size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
  1139. if (!(module->data_sections = data_sections =
  1140. loader_malloc(size, error_buf, error_buf_size))) {
  1141. return false;
  1142. }
  1143. /* Create each data section */
  1144. for (i = 0; i < module->data_section_count; i++) {
  1145. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
  1146. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1147. || defined(BUILD_TARGET_RISCV64_LP64D) \
  1148. || defined(BUILD_TARGET_RISCV64_LP64)
  1149. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1150. relocation for R_X86_64_32/32S/PC32 */
  1151. int map_flags = MMAP_MAP_32BIT;
  1152. #else
  1153. int map_flags = MMAP_MAP_NONE;
  1154. #endif
  1155. read_string(buf, buf_end, data_sections[i].name);
  1156. read_uint32(buf, buf_end, data_sections[i].size);
  1157. /* Allocate memory for data */
  1158. if (data_sections[i].size > 0
  1159. && !(data_sections[i].data = os_mmap(NULL, data_sections[i].size,
  1160. map_prot, map_flags))) {
  1161. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  1162. return false;
  1163. }
  1164. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1165. #if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \
  1166. && !defined(BH_PLATFORM_DARWIN)
  1167. /* address must be in the first 2 Gigabytes of
  1168. the process address space */
  1169. bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
  1170. #endif
  1171. #endif
  1172. read_byte_array(buf, buf_end, data_sections[i].data,
  1173. data_sections[i].size);
  1174. }
  1175. *p_buf = buf;
  1176. return true;
  1177. fail:
  1178. return false;
  1179. }
  1180. static bool
  1181. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  1182. AOTModule *module, bool is_load_from_file_buf,
  1183. char *error_buf, uint32 error_buf_size)
  1184. {
  1185. const uint8 *buf = *p_buf;
  1186. read_uint32(buf, buf_end, module->data_section_count);
  1187. /* load object data sections */
  1188. if (module->data_section_count > 0
  1189. && !load_object_data_sections(&buf, buf_end, module,
  1190. is_load_from_file_buf, error_buf,
  1191. error_buf_size))
  1192. return false;
  1193. *p_buf = buf;
  1194. return true;
  1195. fail:
  1196. return false;
  1197. }
  1198. static bool
  1199. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  1200. AOTModule *module, bool is_load_from_file_buf,
  1201. char *error_buf, uint32 error_buf_size)
  1202. {
  1203. const uint8 *p = buf, *p_end = buf_end;
  1204. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  1205. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  1206. || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size)
  1207. || !load_import_global_info(&p, p_end, module, is_load_from_file_buf,
  1208. error_buf, error_buf_size)
  1209. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  1210. || !load_import_func_info(&p, p_end, module, is_load_from_file_buf,
  1211. error_buf, error_buf_size))
  1212. return false;
  1213. /* load function count and start function index */
  1214. read_uint32(p, p_end, module->func_count);
  1215. read_uint32(p, p_end, module->start_func_index);
  1216. /* check start function index */
  1217. if (module->start_func_index != (uint32)-1
  1218. && (module->start_func_index
  1219. >= module->import_func_count + module->func_count)) {
  1220. set_error_buf(error_buf, error_buf_size,
  1221. "invalid start function index");
  1222. return false;
  1223. }
  1224. read_uint32(p, p_end, module->aux_data_end_global_index);
  1225. read_uint32(p, p_end, module->aux_data_end);
  1226. read_uint32(p, p_end, module->aux_heap_base_global_index);
  1227. read_uint32(p, p_end, module->aux_heap_base);
  1228. read_uint32(p, p_end, module->aux_stack_top_global_index);
  1229. read_uint32(p, p_end, module->aux_stack_bottom);
  1230. read_uint32(p, p_end, module->aux_stack_size);
  1231. if (!load_object_data_sections_info(&p, p_end, module,
  1232. is_load_from_file_buf, error_buf,
  1233. error_buf_size))
  1234. return false;
  1235. if (p != p_end) {
  1236. set_error_buf(error_buf, error_buf_size,
  1237. "invalid init data section size");
  1238. return false;
  1239. }
  1240. return true;
  1241. fail:
  1242. return false;
  1243. }
  1244. static bool
  1245. load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1246. char *error_buf, uint32 error_buf_size)
  1247. {
  1248. uint8 *plt_base;
  1249. if (module->func_count > 0 && buf_end == buf) {
  1250. set_error_buf(error_buf, error_buf_size, "invalid code size");
  1251. return false;
  1252. }
  1253. /* The layout is: literal size + literal + code (with plt table) */
  1254. read_uint32(buf, buf_end, module->literal_size);
  1255. /* literal data is at beginning of the text section */
  1256. module->literal = (uint8 *)buf;
  1257. module->code = (void *)(buf + module->literal_size);
  1258. module->code_size = (uint32)(buf_end - (uint8 *)module->code);
  1259. #if WASM_ENABLE_DEBUG_AOT != 0
  1260. module->elf_size = module->code_size;
  1261. if (is_ELF(module->code)) {
  1262. /* Now code points to an ELF object, we pull it down to .text section */
  1263. uint64 offset;
  1264. uint64 size;
  1265. char *buf = module->code;
  1266. module->elf_hdr = buf;
  1267. if (!get_text_section(buf, &offset, &size)) {
  1268. set_error_buf(error_buf, error_buf_size,
  1269. "get text section of ELF failed");
  1270. return false;
  1271. }
  1272. module->code = buf + offset;
  1273. module->code_size -= (uint32)offset;
  1274. }
  1275. #endif
  1276. if ((module->code_size > 0) && (module->native_symbol_count == 0)) {
  1277. plt_base = (uint8 *)buf_end - get_plt_table_size();
  1278. init_plt_table(plt_base);
  1279. }
  1280. return true;
  1281. fail:
  1282. return false;
  1283. }
  1284. static bool
  1285. load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1286. char *error_buf, uint32 error_buf_size)
  1287. {
  1288. const uint8 *p = buf, *p_end = buf_end;
  1289. uint32 i;
  1290. uint64 size, text_offset;
  1291. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1292. RUNTIME_FUNCTION *rtl_func_table;
  1293. AOTUnwindInfo *unwind_info;
  1294. uint32 unwind_info_offset = module->code_size - sizeof(AOTUnwindInfo);
  1295. uint32 unwind_code_offset = unwind_info_offset - PLT_ITEM_SIZE;
  1296. #endif
  1297. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1298. unwind_info = (AOTUnwindInfo *)((uint8 *)module->code + module->code_size
  1299. - sizeof(AOTUnwindInfo));
  1300. unwind_info->Version = 1;
  1301. unwind_info->Flags = UNW_FLAG_NHANDLER;
  1302. *(uint32 *)&unwind_info->UnwindCode[0] = unwind_code_offset;
  1303. size = sizeof(RUNTIME_FUNCTION) * (uint64)module->func_count;
  1304. if (size > 0
  1305. && !(rtl_func_table = module->rtl_func_table =
  1306. loader_malloc(size, error_buf, error_buf_size))) {
  1307. return false;
  1308. }
  1309. #endif
  1310. size = sizeof(void *) * (uint64)module->func_count;
  1311. if (size > 0
  1312. && !(module->func_ptrs =
  1313. loader_malloc(size, error_buf, error_buf_size))) {
  1314. return false;
  1315. }
  1316. for (i = 0; i < module->func_count; i++) {
  1317. if (sizeof(void *) == 8) {
  1318. read_uint64(p, p_end, text_offset);
  1319. }
  1320. else {
  1321. uint32 text_offset32;
  1322. read_uint32(p, p_end, text_offset32);
  1323. text_offset = text_offset32;
  1324. }
  1325. if (text_offset >= module->code_size) {
  1326. set_error_buf(error_buf, error_buf_size,
  1327. "invalid function code offset");
  1328. return false;
  1329. }
  1330. module->func_ptrs[i] = (uint8 *)module->code + text_offset;
  1331. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  1332. /* bits[0] of thumb function address must be 1 */
  1333. module->func_ptrs[i] = (void *)((uintptr_t)module->func_ptrs[i] | 1);
  1334. #endif
  1335. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1336. rtl_func_table[i].BeginAddress = (DWORD)text_offset;
  1337. if (i > 0) {
  1338. rtl_func_table[i - 1].EndAddress = rtl_func_table[i].BeginAddress;
  1339. }
  1340. rtl_func_table[i].UnwindInfoAddress = (DWORD)unwind_info_offset;
  1341. #endif
  1342. }
  1343. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1344. if (module->func_count > 0) {
  1345. rtl_func_table[module->func_count - 1].EndAddress =
  1346. (DWORD)(module->code_size - get_plt_table_size());
  1347. if (!RtlAddFunctionTable(rtl_func_table, module->func_count,
  1348. (DWORD64)(uintptr_t)module->code)) {
  1349. set_error_buf(error_buf, error_buf_size,
  1350. "add dynamic function table failed");
  1351. return false;
  1352. }
  1353. module->rtl_func_table_registered = true;
  1354. }
  1355. #endif
  1356. /* Set start function when function pointers are resolved */
  1357. if (module->start_func_index != (uint32)-1) {
  1358. if (module->start_func_index >= module->import_func_count)
  1359. module->start_function =
  1360. module->func_ptrs[module->start_func_index
  1361. - module->import_func_count];
  1362. else
  1363. /* TODO: fix start function can be import function issue */
  1364. module->start_function = NULL;
  1365. }
  1366. else {
  1367. module->start_function = NULL;
  1368. }
  1369. size = sizeof(uint32) * (uint64)module->func_count;
  1370. if (size > 0
  1371. && !(module->func_type_indexes =
  1372. loader_malloc(size, error_buf, error_buf_size))) {
  1373. return false;
  1374. }
  1375. for (i = 0; i < module->func_count; i++) {
  1376. read_uint32(p, p_end, module->func_type_indexes[i]);
  1377. if (module->func_type_indexes[i] >= module->func_type_count) {
  1378. set_error_buf(error_buf, error_buf_size, "unknown type");
  1379. return false;
  1380. }
  1381. }
  1382. if (p != buf_end) {
  1383. set_error_buf(error_buf, error_buf_size,
  1384. "invalid function section size");
  1385. return false;
  1386. }
  1387. return true;
  1388. fail:
  1389. return false;
  1390. }
  1391. static void
  1392. destroy_exports(AOTExport *exports, bool is_jit_mode)
  1393. {
  1394. if (!is_jit_mode)
  1395. wasm_runtime_free(exports);
  1396. }
  1397. static bool
  1398. load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1399. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  1400. {
  1401. const uint8 *buf = *p_buf;
  1402. AOTExport *exports;
  1403. uint64 size;
  1404. uint32 i;
  1405. /* Allocate memory */
  1406. size = sizeof(AOTExport) * (uint64)module->export_count;
  1407. if (!(module->exports = exports =
  1408. loader_malloc(size, error_buf, error_buf_size))) {
  1409. return false;
  1410. }
  1411. /* Create each export */
  1412. for (i = 0; i < module->export_count; i++) {
  1413. read_uint32(buf, buf_end, exports[i].index);
  1414. read_uint8(buf, buf_end, exports[i].kind);
  1415. read_string(buf, buf_end, exports[i].name);
  1416. #if 0 /* TODO: check kind and index */
  1417. if (export_funcs[i].index >=
  1418. module->func_count + module->import_func_count) {
  1419. set_error_buf(error_buf, error_buf_size,
  1420. "function index is out of range");
  1421. return false;
  1422. }
  1423. #endif
  1424. }
  1425. *p_buf = buf;
  1426. return true;
  1427. fail:
  1428. return false;
  1429. }
  1430. static bool
  1431. load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1432. bool is_load_from_file_buf, char *error_buf,
  1433. uint32 error_buf_size)
  1434. {
  1435. const uint8 *p = buf, *p_end = buf_end;
  1436. /* load export functions */
  1437. read_uint32(p, p_end, module->export_count);
  1438. if (module->export_count > 0
  1439. && !load_exports(&p, p_end, module, is_load_from_file_buf, error_buf,
  1440. error_buf_size))
  1441. return false;
  1442. if (p != p_end) {
  1443. set_error_buf(error_buf, error_buf_size, "invalid export section size");
  1444. return false;
  1445. }
  1446. return true;
  1447. fail:
  1448. return false;
  1449. }
  1450. static void *
  1451. get_data_section_addr(AOTModule *module, const char *section_name,
  1452. uint32 *p_data_size)
  1453. {
  1454. uint32 i;
  1455. AOTObjectDataSection *data_section = module->data_sections;
  1456. for (i = 0; i < module->data_section_count; i++, data_section++) {
  1457. if (!strcmp(data_section->name, section_name)) {
  1458. if (p_data_size)
  1459. *p_data_size = data_section->size;
  1460. return data_section->data;
  1461. }
  1462. }
  1463. return NULL;
  1464. }
  1465. static void *
  1466. resolve_target_sym(const char *symbol, int32 *p_index)
  1467. {
  1468. uint32 i, num = 0;
  1469. SymbolMap *target_sym_map;
  1470. if (!(target_sym_map = get_target_symbol_map(&num)))
  1471. return NULL;
  1472. for (i = 0; i < num; i++)
  1473. if (!strcmp(target_sym_map[i].symbol_name, symbol)) {
  1474. *p_index = (int32)i;
  1475. return target_sym_map[i].symbol_addr;
  1476. }
  1477. return NULL;
  1478. }
  1479. static bool
  1480. is_literal_relocation(const char *reloc_sec_name)
  1481. {
  1482. return !strcmp(reloc_sec_name, ".rela.literal");
  1483. }
  1484. #if defined(BH_PLATFORM_WINDOWS)
  1485. static bool
  1486. str2uint32(const char *buf, uint32 *p_res)
  1487. {
  1488. uint32 res = 0, val;
  1489. const char *buf_end = buf + 8;
  1490. char ch;
  1491. while (buf < buf_end) {
  1492. ch = *buf++;
  1493. if (ch >= '0' && ch <= '9')
  1494. val = ch - '0';
  1495. else if (ch >= 'a' && ch <= 'f')
  1496. val = ch - 'a' + 0xA;
  1497. else if (ch >= 'A' && ch <= 'F')
  1498. val = ch - 'A' + 0xA;
  1499. else
  1500. return false;
  1501. res = (res << 4) | val;
  1502. }
  1503. *p_res = res;
  1504. return true;
  1505. }
  1506. static bool
  1507. str2uint64(const char *buf, uint64 *p_res)
  1508. {
  1509. uint64 res = 0, val;
  1510. const char *buf_end = buf + 16;
  1511. char ch;
  1512. while (buf < buf_end) {
  1513. ch = *buf++;
  1514. if (ch >= '0' && ch <= '9')
  1515. val = ch - '0';
  1516. else if (ch >= 'a' && ch <= 'f')
  1517. val = ch - 'a' + 0xA;
  1518. else if (ch >= 'A' && ch <= 'F')
  1519. val = ch - 'A' + 0xA;
  1520. else
  1521. return false;
  1522. res = (res << 4) | val;
  1523. }
  1524. *p_res = res;
  1525. return true;
  1526. }
  1527. #endif
  1528. static bool
  1529. do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
  1530. char *error_buf, uint32 error_buf_size)
  1531. {
  1532. bool is_literal = is_literal_relocation(group->section_name);
  1533. uint8 *aot_text = is_literal ? module->literal : module->code;
  1534. uint32 aot_text_size =
  1535. is_literal ? module->literal_size : module->code_size;
  1536. uint32 i, func_index, symbol_len;
  1537. #if defined(BH_PLATFORM_WINDOWS)
  1538. uint32 xmm_plt_index = 0, real_plt_index = 0, float_plt_index = 0;
  1539. #endif
  1540. char symbol_buf[128] = { 0 }, *symbol, *p;
  1541. void *symbol_addr;
  1542. AOTRelocation *relocation = group->relocations;
  1543. if (group->relocation_count > 0 && !aot_text) {
  1544. set_error_buf(error_buf, error_buf_size,
  1545. "invalid text relocation count");
  1546. return false;
  1547. }
  1548. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1549. int32 symbol_index = -1;
  1550. symbol_len = (uint32)strlen(relocation->symbol_name);
  1551. if (symbol_len + 1 <= sizeof(symbol_buf))
  1552. symbol = symbol_buf;
  1553. else {
  1554. if (!(symbol = loader_malloc(symbol_len + 1, error_buf,
  1555. error_buf_size))) {
  1556. return false;
  1557. }
  1558. }
  1559. memcpy(symbol, relocation->symbol_name, symbol_len);
  1560. symbol[symbol_len] = '\0';
  1561. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  1562. p = symbol + strlen(AOT_FUNC_PREFIX);
  1563. if (*p == '\0'
  1564. || (func_index = (uint32)atoi(p)) > module->func_count) {
  1565. set_error_buf_v(error_buf, error_buf_size,
  1566. "invalid import symbol %s", symbol);
  1567. goto check_symbol_fail;
  1568. }
  1569. symbol_addr = module->func_ptrs[func_index];
  1570. }
  1571. else if (!strcmp(symbol, ".text")) {
  1572. symbol_addr = module->code;
  1573. }
  1574. else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata")
  1575. || !strcmp(symbol, ".rdata")
  1576. || !strcmp(symbol, ".rodata")
  1577. /* ".rodata.cst4/8/16/.." */
  1578. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))) {
  1579. symbol_addr = get_data_section_addr(module, symbol, NULL);
  1580. if (!symbol_addr) {
  1581. set_error_buf_v(error_buf, error_buf_size,
  1582. "invalid data section (%s)", symbol);
  1583. goto check_symbol_fail;
  1584. }
  1585. }
  1586. else if (!strcmp(symbol, ".literal")) {
  1587. symbol_addr = module->literal;
  1588. }
  1589. #if defined(BH_PLATFORM_WINDOWS)
  1590. /* Relocation for symbols which start with "__xmm@" or "__real@" and
  1591. end with the xmm value or real value. In Windows PE file, the data
  1592. is stored in some individual ".rdata" sections. We simply create
  1593. extra plt data, parse the values from the symbols and stored them
  1594. into the extra plt data. */
  1595. else if (!strcmp(group->section_name, ".text")
  1596. && !strncmp(symbol, XMM_PLT_PREFIX, strlen(XMM_PLT_PREFIX))
  1597. && strlen(symbol) == strlen(XMM_PLT_PREFIX) + 32) {
  1598. char xmm_buf[17] = { 0 };
  1599. symbol_addr = module->extra_plt_data + xmm_plt_index * 16;
  1600. bh_memcpy_s(xmm_buf, sizeof(xmm_buf),
  1601. symbol + strlen(XMM_PLT_PREFIX) + 16, 16);
  1602. if (!str2uint64(xmm_buf, (uint64 *)symbol_addr)) {
  1603. set_error_buf_v(error_buf, error_buf_size,
  1604. "resolve symbol %s failed", symbol);
  1605. goto check_symbol_fail;
  1606. }
  1607. bh_memcpy_s(xmm_buf, sizeof(xmm_buf),
  1608. symbol + strlen(XMM_PLT_PREFIX), 16);
  1609. if (!str2uint64(xmm_buf, (uint64 *)((uint8 *)symbol_addr + 8))) {
  1610. set_error_buf_v(error_buf, error_buf_size,
  1611. "resolve symbol %s failed", symbol);
  1612. goto check_symbol_fail;
  1613. }
  1614. xmm_plt_index++;
  1615. }
  1616. else if (!strcmp(group->section_name, ".text")
  1617. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  1618. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 16) {
  1619. char real_buf[17] = { 0 };
  1620. symbol_addr = module->extra_plt_data + module->xmm_plt_count * 16
  1621. + real_plt_index * 8;
  1622. bh_memcpy_s(real_buf, sizeof(real_buf),
  1623. symbol + strlen(REAL_PLT_PREFIX), 16);
  1624. if (!str2uint64(real_buf, (uint64 *)symbol_addr)) {
  1625. set_error_buf_v(error_buf, error_buf_size,
  1626. "resolve symbol %s failed", symbol);
  1627. goto check_symbol_fail;
  1628. }
  1629. real_plt_index++;
  1630. }
  1631. else if (!strcmp(group->section_name, ".text")
  1632. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  1633. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 8) {
  1634. char float_buf[9] = { 0 };
  1635. symbol_addr = module->extra_plt_data + module->xmm_plt_count * 16
  1636. + module->real_plt_count * 8 + float_plt_index * 4;
  1637. bh_memcpy_s(float_buf, sizeof(float_buf),
  1638. symbol + strlen(REAL_PLT_PREFIX), 8);
  1639. if (!str2uint32(float_buf, (uint32 *)symbol_addr)) {
  1640. set_error_buf_v(error_buf, error_buf_size,
  1641. "resolve symbol %s failed", symbol);
  1642. goto check_symbol_fail;
  1643. }
  1644. float_plt_index++;
  1645. }
  1646. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  1647. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  1648. set_error_buf_v(error_buf, error_buf_size,
  1649. "resolve symbol %s failed", symbol);
  1650. goto check_symbol_fail;
  1651. }
  1652. if (symbol != symbol_buf)
  1653. wasm_runtime_free(symbol);
  1654. if (!apply_relocation(
  1655. module, aot_text, aot_text_size, relocation->relocation_offset,
  1656. relocation->relocation_addend, relocation->relocation_type,
  1657. symbol_addr, symbol_index, error_buf, error_buf_size))
  1658. return false;
  1659. }
  1660. return true;
  1661. check_symbol_fail:
  1662. if (symbol != symbol_buf)
  1663. wasm_runtime_free(symbol);
  1664. return false;
  1665. }
  1666. static bool
  1667. do_data_relocation(AOTModule *module, AOTRelocationGroup *group,
  1668. char *error_buf, uint32 error_buf_size)
  1669. {
  1670. uint8 *data_addr;
  1671. uint32 data_size = 0, i;
  1672. AOTRelocation *relocation = group->relocations;
  1673. void *symbol_addr;
  1674. char *symbol, *data_section_name;
  1675. if (!strncmp(group->section_name, ".rela.", 6)) {
  1676. data_section_name = group->section_name + strlen(".rela");
  1677. }
  1678. else if (!strncmp(group->section_name, ".rel.", 5)) {
  1679. data_section_name = group->section_name + strlen(".rel");
  1680. }
  1681. else if (!strcmp(group->section_name, ".rdata")) {
  1682. data_section_name = group->section_name;
  1683. }
  1684. else {
  1685. set_error_buf(error_buf, error_buf_size,
  1686. "invalid data relocation section name");
  1687. return false;
  1688. }
  1689. data_addr = get_data_section_addr(module, data_section_name, &data_size);
  1690. if (group->relocation_count > 0 && !data_addr) {
  1691. set_error_buf(error_buf, error_buf_size,
  1692. "invalid data relocation count");
  1693. return false;
  1694. }
  1695. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1696. symbol = relocation->symbol_name;
  1697. if (!strcmp(symbol, ".text")) {
  1698. symbol_addr = module->code;
  1699. }
  1700. else {
  1701. set_error_buf_v(error_buf, error_buf_size,
  1702. "invalid relocation symbol %s", symbol);
  1703. return false;
  1704. }
  1705. if (!apply_relocation(
  1706. module, data_addr, data_size, relocation->relocation_offset,
  1707. relocation->relocation_addend, relocation->relocation_type,
  1708. symbol_addr, -1, error_buf, error_buf_size))
  1709. return false;
  1710. }
  1711. return true;
  1712. }
  1713. static bool
  1714. validate_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *offsets, uint32 count,
  1715. char *error_buf, uint32 error_buf_size)
  1716. {
  1717. uint32 i, str_len_addr = 0;
  1718. uint16 str_len;
  1719. for (i = 0; i < count; i++) {
  1720. if (offsets[i] != str_len_addr)
  1721. return false;
  1722. read_uint16(buf, buf_end, str_len);
  1723. str_len_addr += (uint32)sizeof(uint16) + str_len;
  1724. str_len_addr = align_uint(str_len_addr, 2);
  1725. buf += str_len;
  1726. buf = (uint8 *)align_ptr(buf, 2);
  1727. }
  1728. if (buf == buf_end)
  1729. return true;
  1730. fail:
  1731. return false;
  1732. }
  1733. static bool
  1734. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  1735. AOTModule *module, bool is_load_from_file_buf,
  1736. char *error_buf, uint32 error_buf_size)
  1737. {
  1738. AOTRelocationGroup *groups = NULL, *group;
  1739. uint32 symbol_count = 0;
  1740. uint32 group_count = 0, i, j;
  1741. uint64 size;
  1742. uint32 *symbol_offsets, total_string_len;
  1743. uint8 *symbol_buf, *symbol_buf_end;
  1744. int map_prot, map_flags;
  1745. bool ret = false;
  1746. read_uint32(buf, buf_end, symbol_count);
  1747. symbol_offsets = (uint32 *)buf;
  1748. for (i = 0; i < symbol_count; i++) {
  1749. CHECK_BUF(buf, buf_end, sizeof(uint32));
  1750. buf += sizeof(uint32);
  1751. }
  1752. read_uint32(buf, buf_end, total_string_len);
  1753. symbol_buf = (uint8 *)buf;
  1754. symbol_buf_end = symbol_buf + total_string_len;
  1755. if (!validate_symbol_table(symbol_buf, symbol_buf_end, symbol_offsets,
  1756. symbol_count, error_buf, error_buf_size)) {
  1757. set_error_buf(error_buf, error_buf_size,
  1758. "validate symbol table failed");
  1759. goto fail;
  1760. }
  1761. #if defined(BH_PLATFORM_WINDOWS)
  1762. buf = symbol_buf_end;
  1763. read_uint32(buf, buf_end, group_count);
  1764. for (i = 0; i < group_count; i++) {
  1765. uint32 name_index, relocation_count;
  1766. uint16 group_name_len;
  1767. uint8 *group_name;
  1768. /* section name address is 4 bytes aligned. */
  1769. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  1770. read_uint32(buf, buf_end, name_index);
  1771. if (name_index >= symbol_count) {
  1772. set_error_buf(error_buf, error_buf_size,
  1773. "symbol index out of range");
  1774. goto fail;
  1775. }
  1776. group_name = symbol_buf + symbol_offsets[name_index];
  1777. group_name_len = *(uint16 *)group_name;
  1778. group_name += sizeof(uint16);
  1779. read_uint32(buf, buf_end, relocation_count);
  1780. for (j = 0; j < relocation_count; j++) {
  1781. AOTRelocation relocation = { 0 };
  1782. uint32 symbol_index, offset32;
  1783. int32 addend32;
  1784. uint16 symbol_name_len;
  1785. uint8 *symbol_name;
  1786. if (sizeof(void *) == 8) {
  1787. read_uint64(buf, buf_end, relocation.relocation_offset);
  1788. read_uint64(buf, buf_end, relocation.relocation_addend);
  1789. }
  1790. else {
  1791. read_uint32(buf, buf_end, offset32);
  1792. relocation.relocation_offset = (uint64)offset32;
  1793. read_uint32(buf, buf_end, addend32);
  1794. relocation.relocation_addend = (int64)addend32;
  1795. }
  1796. read_uint32(buf, buf_end, relocation.relocation_type);
  1797. read_uint32(buf, buf_end, symbol_index);
  1798. if (symbol_index >= symbol_count) {
  1799. set_error_buf(error_buf, error_buf_size,
  1800. "symbol index out of range");
  1801. goto fail;
  1802. }
  1803. symbol_name = symbol_buf + symbol_offsets[symbol_index];
  1804. symbol_name_len = *(uint16 *)symbol_name;
  1805. symbol_name += sizeof(uint16);
  1806. char group_name_buf[128] = { 0 };
  1807. char symbol_name_buf[128] = { 0 };
  1808. memcpy(group_name_buf, group_name, group_name_len);
  1809. memcpy(symbol_name_buf, symbol_name, symbol_name_len);
  1810. if (group_name_len == strlen(".text")
  1811. && !strncmp(group_name, ".text", strlen(".text"))) {
  1812. if (symbol_name_len == strlen(XMM_PLT_PREFIX) + 32
  1813. && !strncmp(symbol_name, XMM_PLT_PREFIX,
  1814. strlen(XMM_PLT_PREFIX))) {
  1815. module->xmm_plt_count++;
  1816. }
  1817. else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 16
  1818. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  1819. strlen(REAL_PLT_PREFIX))) {
  1820. module->real_plt_count++;
  1821. }
  1822. else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 8
  1823. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  1824. strlen(REAL_PLT_PREFIX))) {
  1825. module->float_plt_count++;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. /* Allocate memory for extra plt data */
  1831. size = sizeof(uint64) * 2 * module->xmm_plt_count
  1832. + sizeof(uint64) * module->real_plt_count
  1833. + sizeof(uint32) * module->float_plt_count;
  1834. if (size > 0) {
  1835. map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  1836. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1837. relocation for R_X86_64_32/32S/PC32 */
  1838. map_flags = MMAP_MAP_32BIT;
  1839. if (size > UINT32_MAX
  1840. || !(module->extra_plt_data =
  1841. os_mmap(NULL, (uint32)size, map_prot, map_flags))) {
  1842. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  1843. goto fail;
  1844. }
  1845. module->extra_plt_data_size = (uint32)size;
  1846. }
  1847. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  1848. buf = symbol_buf_end;
  1849. read_uint32(buf, buf_end, group_count);
  1850. /* Allocate memory for relocation groups */
  1851. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  1852. if (size > 0
  1853. && !(groups = loader_malloc(size, error_buf, error_buf_size))) {
  1854. goto fail;
  1855. }
  1856. /* Load each relocation group */
  1857. for (i = 0, group = groups; i < group_count; i++, group++) {
  1858. AOTRelocation *relocation;
  1859. uint32 name_index;
  1860. uint8 *name_addr;
  1861. /* section name address is 4 bytes aligned. */
  1862. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  1863. read_uint32(buf, buf_end, name_index);
  1864. if (name_index >= symbol_count) {
  1865. set_error_buf(error_buf, error_buf_size,
  1866. "symbol index out of range");
  1867. goto fail;
  1868. }
  1869. name_addr = symbol_buf + symbol_offsets[name_index];
  1870. read_string(name_addr, buf_end, group->section_name);
  1871. read_uint32(buf, buf_end, group->relocation_count);
  1872. /* Allocate memory for relocations */
  1873. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  1874. if (!(group->relocations = relocation =
  1875. loader_malloc(size, error_buf, error_buf_size))) {
  1876. ret = false;
  1877. goto fail;
  1878. }
  1879. /* Load each relocation */
  1880. for (j = 0; j < group->relocation_count; j++, relocation++) {
  1881. uint32 symbol_index;
  1882. uint8 *symbol_addr;
  1883. if (sizeof(void *) == 8) {
  1884. read_uint64(buf, buf_end, relocation->relocation_offset);
  1885. read_uint64(buf, buf_end, relocation->relocation_addend);
  1886. }
  1887. else {
  1888. uint32 offset32, addend32;
  1889. read_uint32(buf, buf_end, offset32);
  1890. relocation->relocation_offset = (uint64)offset32;
  1891. read_uint32(buf, buf_end, addend32);
  1892. relocation->relocation_addend = (uint64)addend32;
  1893. }
  1894. read_uint32(buf, buf_end, relocation->relocation_type);
  1895. read_uint32(buf, buf_end, symbol_index);
  1896. if (symbol_index >= symbol_count) {
  1897. set_error_buf(error_buf, error_buf_size,
  1898. "symbol index out of range");
  1899. goto fail;
  1900. }
  1901. symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  1902. read_string(symbol_addr, buf_end, relocation->symbol_name);
  1903. }
  1904. if (!strcmp(group->section_name, ".rel.text")
  1905. || !strcmp(group->section_name, ".rela.text")
  1906. || !strcmp(group->section_name, ".rela.literal")
  1907. #ifdef BH_PLATFORM_WINDOWS
  1908. || !strcmp(group->section_name, ".text")
  1909. #endif
  1910. ) {
  1911. if (module->native_symbol_count > 0) {
  1912. set_error_buf(error_buf, error_buf_size,
  1913. "cannot apply relocation to text section "
  1914. "for aot file generated with "
  1915. "\"--enable-indirect-mode\" flag");
  1916. goto fail;
  1917. }
  1918. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  1919. goto fail;
  1920. }
  1921. else {
  1922. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  1923. goto fail;
  1924. }
  1925. }
  1926. /* Set read only for AOT code and some data sections */
  1927. map_prot = MMAP_PROT_READ | MMAP_PROT_EXEC;
  1928. if (module->code) {
  1929. /* The layout is: literal size + literal + code (with plt table) */
  1930. uint8 *mmap_addr = module->literal - sizeof(uint32);
  1931. uint32 total_size =
  1932. sizeof(uint32) + module->literal_size + module->code_size;
  1933. os_mprotect(mmap_addr, total_size, map_prot);
  1934. }
  1935. map_prot = MMAP_PROT_READ;
  1936. #if defined(BH_PLATFORM_WINDOWS)
  1937. if (module->extra_plt_data) {
  1938. os_mprotect(module->extra_plt_data, module->extra_plt_data_size,
  1939. map_prot);
  1940. }
  1941. #endif
  1942. for (i = 0; i < module->data_section_count; i++) {
  1943. AOTObjectDataSection *data_section = module->data_sections + i;
  1944. if (!strcmp(data_section->name, ".rdata")
  1945. || !strcmp(data_section->name, ".rodata")
  1946. /* ".rodata.cst4/8/16/.." */
  1947. || !strncmp(data_section->name, ".rodata.cst",
  1948. strlen(".rodata.cst"))) {
  1949. os_mprotect(data_section->data, data_section->size, map_prot);
  1950. }
  1951. }
  1952. ret = true;
  1953. fail:
  1954. if (groups) {
  1955. for (i = 0, group = groups; i < group_count; i++, group++)
  1956. if (group->relocations)
  1957. wasm_runtime_free(group->relocations);
  1958. wasm_runtime_free(groups);
  1959. }
  1960. (void)map_flags;
  1961. return ret;
  1962. }
  1963. static bool
  1964. load_from_sections(AOTModule *module, AOTSection *sections,
  1965. bool is_load_from_file_buf, char *error_buf,
  1966. uint32 error_buf_size)
  1967. {
  1968. AOTSection *section = sections;
  1969. const uint8 *buf, *buf_end;
  1970. uint32 last_section_type = (uint32)-1, section_type;
  1971. uint32 i, func_index, func_type_index;
  1972. AOTFuncType *func_type;
  1973. AOTExport *exports;
  1974. while (section) {
  1975. buf = section->section_body;
  1976. buf_end = buf + section->section_body_size;
  1977. /* Check sections */
  1978. section_type = (uint32)section->section_type;
  1979. if ((last_section_type == (uint32)-1
  1980. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  1981. || (last_section_type != (uint32)-1
  1982. && (section_type != last_section_type + 1
  1983. && section_type != AOT_SECTION_TYPE_CUSTOM))) {
  1984. set_error_buf(error_buf, error_buf_size, "invalid section order");
  1985. return false;
  1986. }
  1987. last_section_type = section_type;
  1988. switch (section_type) {
  1989. case AOT_SECTION_TYPE_TARGET_INFO:
  1990. if (!load_target_info_section(buf, buf_end, module, error_buf,
  1991. error_buf_size))
  1992. return false;
  1993. break;
  1994. case AOT_SECTION_TYPE_INIT_DATA:
  1995. if (!load_init_data_section(buf, buf_end, module,
  1996. is_load_from_file_buf, error_buf,
  1997. error_buf_size))
  1998. return false;
  1999. break;
  2000. case AOT_SECTION_TYPE_TEXT:
  2001. if (!load_text_section(buf, buf_end, module, error_buf,
  2002. error_buf_size))
  2003. return false;
  2004. break;
  2005. case AOT_SECTION_TYPE_FUNCTION:
  2006. if (!load_function_section(buf, buf_end, module, error_buf,
  2007. error_buf_size))
  2008. return false;
  2009. break;
  2010. case AOT_SECTION_TYPE_EXPORT:
  2011. if (!load_export_section(buf, buf_end, module,
  2012. is_load_from_file_buf, error_buf,
  2013. error_buf_size))
  2014. return false;
  2015. break;
  2016. case AOT_SECTION_TYPE_RELOCATION:
  2017. if (!load_relocation_section(buf, buf_end, module,
  2018. is_load_from_file_buf, error_buf,
  2019. error_buf_size))
  2020. return false;
  2021. break;
  2022. case AOT_SECTION_TYPE_CUSTOM:
  2023. if (!load_custom_section(buf, buf_end, module,
  2024. is_load_from_file_buf, error_buf,
  2025. error_buf_size))
  2026. return false;
  2027. break;
  2028. default:
  2029. set_error_buf(error_buf, error_buf_size,
  2030. "invalid aot section type");
  2031. return false;
  2032. }
  2033. section = section->next;
  2034. }
  2035. if (last_section_type != AOT_SECTION_TYPE_RELOCATION
  2036. && last_section_type != AOT_SECTION_TYPE_CUSTOM) {
  2037. set_error_buf(error_buf, error_buf_size, "section missing");
  2038. return false;
  2039. }
  2040. /* Resolve malloc and free function */
  2041. module->malloc_func_index = (uint32)-1;
  2042. module->free_func_index = (uint32)-1;
  2043. module->retain_func_index = (uint32)-1;
  2044. exports = module->exports;
  2045. for (i = 0; i < module->export_count; i++) {
  2046. if (exports[i].kind == EXPORT_KIND_FUNC
  2047. && exports[i].index >= module->import_func_count) {
  2048. if (!strcmp(exports[i].name, "malloc")) {
  2049. func_index = exports[i].index - module->import_func_count;
  2050. func_type_index = module->func_type_indexes[func_index];
  2051. func_type = module->func_types[func_type_index];
  2052. if (func_type->param_count == 1 && func_type->result_count == 1
  2053. && func_type->types[0] == VALUE_TYPE_I32
  2054. && func_type->types[1] == VALUE_TYPE_I32) {
  2055. bh_assert(module->malloc_func_index == (uint32)-1);
  2056. module->malloc_func_index = func_index;
  2057. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2058. exports[i].name, exports[i].index);
  2059. }
  2060. }
  2061. else if (!strcmp(exports[i].name, "__new")) {
  2062. func_index = exports[i].index - module->import_func_count;
  2063. func_type_index = module->func_type_indexes[func_index];
  2064. func_type = module->func_types[func_type_index];
  2065. if (func_type->param_count == 2 && func_type->result_count == 1
  2066. && func_type->types[0] == VALUE_TYPE_I32
  2067. && func_type->types[1] == VALUE_TYPE_I32
  2068. && func_type->types[2] == VALUE_TYPE_I32) {
  2069. uint32 j;
  2070. WASMExport *export_tmp;
  2071. bh_assert(module->malloc_func_index == (uint32)-1);
  2072. module->malloc_func_index = func_index;
  2073. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2074. exports[i].name, exports[i].index);
  2075. /* resolve retain function.
  2076. If not find, reset malloc function index */
  2077. export_tmp = module->exports;
  2078. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2079. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2080. && (!strcmp(export_tmp->name, "__retain")
  2081. || !strcmp(export_tmp->name, "__pin"))) {
  2082. func_index =
  2083. export_tmp->index - module->import_func_count;
  2084. func_type_index =
  2085. module->func_type_indexes[func_index];
  2086. func_type = module->func_types[func_type_index];
  2087. if (func_type->param_count == 1
  2088. && func_type->result_count == 1
  2089. && func_type->types[0] == VALUE_TYPE_I32
  2090. && func_type->types[1] == VALUE_TYPE_I32) {
  2091. bh_assert(module->retain_func_index
  2092. == (uint32)-1);
  2093. module->retain_func_index = export_tmp->index;
  2094. LOG_VERBOSE("Found retain function, name: %s, "
  2095. "index: %u",
  2096. export_tmp->name,
  2097. export_tmp->index);
  2098. break;
  2099. }
  2100. }
  2101. }
  2102. if (j == module->export_count) {
  2103. module->malloc_func_index = (uint32)-1;
  2104. LOG_VERBOSE("Can't find retain function,"
  2105. "reset malloc function index to -1");
  2106. }
  2107. }
  2108. }
  2109. else if ((!strcmp(exports[i].name, "free"))
  2110. || (!strcmp(exports[i].name, "__release"))
  2111. || (!strcmp(exports[i].name, "__unpin"))) {
  2112. func_index = exports[i].index - module->import_func_count;
  2113. func_type_index = module->func_type_indexes[func_index];
  2114. func_type = module->func_types[func_type_index];
  2115. if (func_type->param_count == 1 && func_type->result_count == 0
  2116. && func_type->types[0] == VALUE_TYPE_I32) {
  2117. bh_assert(module->free_func_index == (uint32)-1);
  2118. module->free_func_index = func_index;
  2119. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2120. exports[i].name, exports[i].index);
  2121. }
  2122. }
  2123. }
  2124. }
  2125. /* Flush data cache before executing AOT code,
  2126. * otherwise unpredictable behavior can occur. */
  2127. os_dcache_flush();
  2128. #if WASM_ENABLE_MEMORY_TRACING != 0
  2129. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  2130. #endif
  2131. #if WASM_ENABLE_DEBUG_AOT != 0
  2132. if (!jit_code_entry_create(module->elf_hdr, module->elf_size)) {
  2133. set_error_buf(error_buf, error_buf_size,
  2134. "create jit code entry failed");
  2135. return false;
  2136. }
  2137. #endif
  2138. return true;
  2139. }
  2140. static AOTModule *
  2141. create_module(char *error_buf, uint32 error_buf_size)
  2142. {
  2143. AOTModule *module =
  2144. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size);
  2145. if (!module) {
  2146. return NULL;
  2147. }
  2148. module->module_type = Wasm_Module_AoT;
  2149. return module;
  2150. }
  2151. AOTModule *
  2152. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  2153. uint32 error_buf_size)
  2154. {
  2155. AOTModule *module = create_module(error_buf, error_buf_size);
  2156. if (!module)
  2157. return NULL;
  2158. if (!load_from_sections(module, section_list, false, error_buf,
  2159. error_buf_size)) {
  2160. aot_unload(module);
  2161. return NULL;
  2162. }
  2163. LOG_VERBOSE("Load module from sections success.\n");
  2164. return module;
  2165. }
  2166. static void
  2167. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  2168. {
  2169. AOTSection *section = section_list, *next;
  2170. while (section) {
  2171. next = section->next;
  2172. if (destroy_aot_text && section->section_type == AOT_SECTION_TYPE_TEXT
  2173. && section->section_body)
  2174. os_munmap((uint8 *)section->section_body,
  2175. section->section_body_size);
  2176. wasm_runtime_free(section);
  2177. section = next;
  2178. }
  2179. }
  2180. static bool
  2181. resolve_native_symbols(const uint8 *buf, uint32 size, uint32 *p_count,
  2182. char *error_buf, uint32 error_buf_size)
  2183. {
  2184. const uint8 *p = buf, *p_end = buf + size;
  2185. uint32 section_type;
  2186. uint32 section_size = 0;
  2187. p += 8;
  2188. while (p < p_end) {
  2189. read_uint32(p, p_end, section_type);
  2190. if (section_type <= AOT_SECTION_TYPE_SIGANATURE
  2191. || section_type == AOT_SECTION_TYPE_CUSTOM) {
  2192. read_uint32(p, p_end, section_size);
  2193. CHECK_BUF(p, p_end, section_size);
  2194. if (section_type == AOT_SECTION_TYPE_CUSTOM) {
  2195. read_uint32(p, p_end, section_type);
  2196. if (section_type == AOT_CUSTOM_SECTION_NATIVE_SYMBOL) {
  2197. /* Read the count of native symbol */
  2198. read_uint32(p, p_end, *p_count);
  2199. return true;
  2200. }
  2201. p -= sizeof(uint32);
  2202. }
  2203. }
  2204. else if (section_type > AOT_SECTION_TYPE_SIGANATURE) {
  2205. set_error_buf(error_buf, error_buf_size,
  2206. "resolve native symbol failed");
  2207. break;
  2208. }
  2209. p += section_size;
  2210. }
  2211. return true;
  2212. fail:
  2213. return false;
  2214. }
  2215. static bool
  2216. create_sections(AOTModule *module, const uint8 *buf, uint32 size,
  2217. AOTSection **p_section_list, char *error_buf,
  2218. uint32 error_buf_size)
  2219. {
  2220. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  2221. const uint8 *p = buf, *p_end = buf + size;
  2222. bool destroy_aot_text = false;
  2223. uint32 native_symbol_count = 0;
  2224. uint32 section_type;
  2225. uint32 section_size;
  2226. uint64 total_size;
  2227. uint8 *aot_text;
  2228. if (!resolve_native_symbols(buf, size, &native_symbol_count, error_buf,
  2229. error_buf_size)) {
  2230. goto fail;
  2231. }
  2232. module->native_symbol_count = native_symbol_count;
  2233. p += 8;
  2234. while (p < p_end) {
  2235. read_uint32(p, p_end, section_type);
  2236. if (section_type < AOT_SECTION_TYPE_SIGANATURE
  2237. || section_type == AOT_SECTION_TYPE_CUSTOM) {
  2238. read_uint32(p, p_end, section_size);
  2239. CHECK_BUF(p, p_end, section_size);
  2240. if (!(section = loader_malloc(sizeof(AOTSection), error_buf,
  2241. error_buf_size))) {
  2242. goto fail;
  2243. }
  2244. memset(section, 0, sizeof(AOTSection));
  2245. section->section_type = (int32)section_type;
  2246. section->section_body = (uint8 *)p;
  2247. section->section_body_size = section_size;
  2248. if (section_type == AOT_SECTION_TYPE_TEXT) {
  2249. if ((section_size > 0) && (native_symbol_count == 0)) {
  2250. int map_prot =
  2251. MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  2252. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  2253. || defined(BUILD_TARGET_RISCV64_LP64D) \
  2254. || defined(BUILD_TARGET_RISCV64_LP64)
  2255. /* aot code and data in x86_64 must be in range 0 to 2G due
  2256. to relocation for R_X86_64_32/32S/PC32 */
  2257. int map_flags = MMAP_MAP_32BIT;
  2258. #else
  2259. int map_flags = MMAP_MAP_NONE;
  2260. #endif
  2261. total_size =
  2262. (uint64)section_size + aot_get_plt_table_size();
  2263. total_size = (total_size + 3) & ~((uint64)3);
  2264. if (total_size >= UINT32_MAX
  2265. || !(aot_text = os_mmap(NULL, (uint32)total_size,
  2266. map_prot, map_flags))) {
  2267. wasm_runtime_free(section);
  2268. set_error_buf(error_buf, error_buf_size,
  2269. "mmap memory failed");
  2270. goto fail;
  2271. }
  2272. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  2273. #if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \
  2274. && !defined(BH_PLATFORM_DARWIN)
  2275. /* address must be in the first 2 Gigabytes of
  2276. the process address space */
  2277. bh_assert((uintptr_t)aot_text < INT32_MAX);
  2278. #endif
  2279. #endif
  2280. bh_memcpy_s(aot_text, (uint32)total_size,
  2281. section->section_body, (uint32)section_size);
  2282. section->section_body = aot_text;
  2283. destroy_aot_text = true;
  2284. if ((uint32)total_size > section->section_body_size) {
  2285. memset(aot_text + (uint32)section_size, 0,
  2286. (uint32)total_size - section_size);
  2287. section->section_body_size = (uint32)total_size;
  2288. }
  2289. }
  2290. }
  2291. if (!section_list)
  2292. section_list = section_list_end = section;
  2293. else {
  2294. section_list_end->next = section;
  2295. section_list_end = section;
  2296. }
  2297. p += section_size;
  2298. }
  2299. else {
  2300. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2301. goto fail;
  2302. }
  2303. }
  2304. if (!section_list) {
  2305. set_error_buf(error_buf, error_buf_size, "create section list failed");
  2306. return false;
  2307. }
  2308. *p_section_list = section_list;
  2309. return true;
  2310. fail:
  2311. if (section_list)
  2312. destroy_sections(section_list, destroy_aot_text);
  2313. return false;
  2314. }
  2315. static bool
  2316. load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf,
  2317. uint32 error_buf_size)
  2318. {
  2319. const uint8 *buf_end = buf + size;
  2320. const uint8 *p = buf, *p_end = buf_end;
  2321. uint32 magic_number, version;
  2322. AOTSection *section_list = NULL;
  2323. bool ret;
  2324. read_uint32(p, p_end, magic_number);
  2325. if (magic_number != AOT_MAGIC_NUMBER) {
  2326. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  2327. return false;
  2328. }
  2329. read_uint32(p, p_end, version);
  2330. if (version != AOT_CURRENT_VERSION) {
  2331. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  2332. return false;
  2333. }
  2334. if (!create_sections(module, buf, size, &section_list, error_buf,
  2335. error_buf_size))
  2336. return false;
  2337. ret = load_from_sections(module, section_list, true, error_buf,
  2338. error_buf_size);
  2339. if (!ret) {
  2340. /* If load_from_sections() fails, then aot text is destroyed
  2341. in destroy_sections() */
  2342. destroy_sections(section_list,
  2343. module->native_symbol_count == 0 ? true : false);
  2344. /* aot_unload() won't destroy aot text again */
  2345. module->code = NULL;
  2346. }
  2347. else {
  2348. /* If load_from_sections() succeeds, then aot text is set to
  2349. module->code and will be destroyed in aot_unload() */
  2350. destroy_sections(section_list, false);
  2351. }
  2352. return ret;
  2353. fail:
  2354. return false;
  2355. }
  2356. AOTModule *
  2357. aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf,
  2358. uint32 error_buf_size)
  2359. {
  2360. AOTModule *module = create_module(error_buf, error_buf_size);
  2361. if (!module)
  2362. return NULL;
  2363. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2364. aot_unload(module);
  2365. return NULL;
  2366. }
  2367. LOG_VERBOSE("Load module success.\n");
  2368. return module;
  2369. }
  2370. #if WASM_ENABLE_JIT != 0
  2371. static AOTModule *
  2372. aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  2373. char *error_buf, uint32 error_buf_size)
  2374. {
  2375. uint32 i;
  2376. uint64 size;
  2377. char func_name[32];
  2378. AOTModule *module;
  2379. #if WASM_ENABLE_LAZY_JIT != 0
  2380. LLVMOrcThreadSafeModuleRef ts_module;
  2381. LLVMOrcJITDylibRef main_dylib;
  2382. LLVMErrorRef error;
  2383. LLVMOrcJITTargetAddress func_addr = 0;
  2384. #endif
  2385. /* Allocate memory for module */
  2386. if (!(module =
  2387. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) {
  2388. return NULL;
  2389. }
  2390. module->module_type = Wasm_Module_AoT;
  2391. module->import_memory_count = comp_data->import_memory_count;
  2392. module->import_memories = comp_data->import_memories;
  2393. module->memory_count = comp_data->memory_count;
  2394. if (module->memory_count) {
  2395. size = sizeof(AOTMemory) * (uint64)module->memory_count;
  2396. if (!(module->memories =
  2397. loader_malloc(size, error_buf, error_buf_size))) {
  2398. goto fail1;
  2399. }
  2400. bh_memcpy_s(module->memories, (uint32)size, comp_data->memories,
  2401. (uint32)size);
  2402. }
  2403. module->mem_init_data_list = comp_data->mem_init_data_list;
  2404. module->mem_init_data_count = comp_data->mem_init_data_count;
  2405. module->import_table_count = comp_data->import_table_count;
  2406. module->import_tables = comp_data->import_tables;
  2407. module->table_count = comp_data->table_count;
  2408. module->tables = comp_data->tables;
  2409. module->table_init_data_list = comp_data->table_init_data_list;
  2410. module->table_init_data_count = comp_data->table_init_data_count;
  2411. module->func_type_count = comp_data->func_type_count;
  2412. module->func_types = comp_data->func_types;
  2413. module->import_global_count = comp_data->import_global_count;
  2414. module->import_globals = comp_data->import_globals;
  2415. module->global_count = comp_data->global_count;
  2416. module->globals = comp_data->globals;
  2417. module->global_count = comp_data->global_count;
  2418. module->globals = comp_data->globals;
  2419. module->global_data_size = comp_data->global_data_size;
  2420. module->import_func_count = comp_data->import_func_count;
  2421. module->import_funcs = comp_data->import_funcs;
  2422. module->func_count = comp_data->func_count;
  2423. /* Allocate memory for function pointers */
  2424. size = (uint64)module->func_count * sizeof(void *);
  2425. if (size > 0
  2426. && !(module->func_ptrs =
  2427. loader_malloc(size, error_buf, error_buf_size))) {
  2428. goto fail2;
  2429. }
  2430. #if WASM_ENABLE_LAZY_JIT != 0
  2431. bh_assert(comp_ctx->lazy_orcjit);
  2432. main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->lazy_orcjit);
  2433. if (!main_dylib) {
  2434. set_error_buf(error_buf, error_buf_size,
  2435. "failed to get dynmaic library reference");
  2436. goto fail3;
  2437. }
  2438. ts_module = LLVMOrcCreateNewThreadSafeModule(comp_ctx->module,
  2439. comp_ctx->ts_context);
  2440. if (!ts_module) {
  2441. set_error_buf(error_buf, error_buf_size,
  2442. "failed to create thread safe module");
  2443. goto fail3;
  2444. }
  2445. if ((error = LLVMOrcLLLazyJITAddLLVMIRModule(comp_ctx->lazy_orcjit,
  2446. main_dylib, ts_module))) {
  2447. /*
  2448. * If adding the ThreadSafeModule fails then we need to clean it up
  2449. * ourselves. If adding it succeeds the JIT will manage the memory.
  2450. */
  2451. aot_handle_llvm_errmsg(error_buf, error_buf_size,
  2452. "failed to addIRModule: ", error);
  2453. goto fail4;
  2454. }
  2455. for (i = 0; i < comp_data->func_count; i++) {
  2456. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2457. if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->lazy_orcjit, &func_addr,
  2458. func_name))) {
  2459. aot_handle_llvm_errmsg(error_buf, error_buf_size,
  2460. "cannot lookup: ", error);
  2461. goto fail3;
  2462. }
  2463. module->func_ptrs[i] = (void *)func_addr;
  2464. func_addr = 0;
  2465. }
  2466. #else
  2467. /* Resolve function addresses */
  2468. bh_assert(comp_ctx->exec_engine);
  2469. for (i = 0; i < comp_data->func_count; i++) {
  2470. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2471. if (!(module->func_ptrs[i] = (void *)LLVMGetFunctionAddress(
  2472. comp_ctx->exec_engine, func_name))) {
  2473. set_error_buf(error_buf, error_buf_size,
  2474. "get function address failed");
  2475. goto fail3;
  2476. }
  2477. }
  2478. #endif /* WASM_ENABLE_LAZY_JIT != 0 */
  2479. /* Allocation memory for function type indexes */
  2480. size = (uint64)module->func_count * sizeof(uint32);
  2481. if (size > 0
  2482. && !(module->func_type_indexes =
  2483. loader_malloc(size, error_buf, error_buf_size))) {
  2484. goto fail3;
  2485. }
  2486. for (i = 0; i < comp_data->func_count; i++)
  2487. module->func_type_indexes[i] = comp_data->funcs[i]->func_type_index;
  2488. module->export_count = comp_data->wasm_module->export_count;
  2489. module->exports = comp_data->wasm_module->exports;
  2490. module->start_func_index = comp_data->start_func_index;
  2491. if (comp_data->start_func_index != (uint32)-1) {
  2492. bh_assert(comp_data->start_func_index
  2493. < module->import_func_count + module->func_count);
  2494. /* TODO: fix issue that start func cannot be import func */
  2495. if (comp_data->start_func_index >= module->import_func_count) {
  2496. module->start_function =
  2497. module->func_ptrs[comp_data->start_func_index
  2498. - module->import_func_count];
  2499. }
  2500. }
  2501. module->malloc_func_index = comp_data->malloc_func_index;
  2502. module->free_func_index = comp_data->free_func_index;
  2503. module->retain_func_index = comp_data->retain_func_index;
  2504. module->aux_data_end_global_index = comp_data->aux_data_end_global_index;
  2505. module->aux_data_end = comp_data->aux_data_end;
  2506. module->aux_heap_base_global_index = comp_data->aux_heap_base_global_index;
  2507. module->aux_heap_base = comp_data->aux_heap_base;
  2508. module->aux_stack_top_global_index = comp_data->aux_stack_top_global_index;
  2509. module->aux_stack_bottom = comp_data->aux_stack_bottom;
  2510. module->aux_stack_size = comp_data->aux_stack_size;
  2511. module->code = NULL;
  2512. module->code_size = 0;
  2513. module->is_jit_mode = true;
  2514. module->wasm_module = comp_data->wasm_module;
  2515. module->comp_ctx = comp_ctx;
  2516. module->comp_data = comp_data;
  2517. #if WASM_ENABLE_LIBC_WASI != 0
  2518. module->is_wasi_module = comp_data->wasm_module->is_wasi_module;
  2519. #endif
  2520. return module;
  2521. #if WASM_ENABLE_LAZY_JIT != 0
  2522. fail4:
  2523. LLVMOrcDisposeThreadSafeModule(ts_module);
  2524. #endif
  2525. fail3:
  2526. if (module->func_ptrs)
  2527. wasm_runtime_free(module->func_ptrs);
  2528. fail2:
  2529. if (module->memory_count > 0)
  2530. wasm_runtime_free(module->memories);
  2531. fail1:
  2532. wasm_runtime_free(module);
  2533. return NULL;
  2534. }
  2535. AOTModule *
  2536. aot_convert_wasm_module(WASMModule *wasm_module, char *error_buf,
  2537. uint32 error_buf_size)
  2538. {
  2539. AOTCompData *comp_data;
  2540. AOTCompContext *comp_ctx;
  2541. AOTModule *aot_module;
  2542. AOTCompOption option = { 0 };
  2543. char *aot_last_error;
  2544. comp_data = aot_create_comp_data(wasm_module);
  2545. if (!comp_data) {
  2546. aot_last_error = aot_get_last_error();
  2547. bh_assert(aot_last_error != NULL);
  2548. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2549. return NULL;
  2550. }
  2551. option.is_jit_mode = true;
  2552. #if WASM_ENABLE_BULK_MEMORY != 0
  2553. option.enable_bulk_memory = true;
  2554. #endif
  2555. #if WASM_ENABLE_THREAD_MGR != 0
  2556. option.enable_thread_mgr = true;
  2557. #endif
  2558. #if WASM_ENABLE_TAIL_CALL != 0
  2559. option.enable_tail_call = true;
  2560. #endif
  2561. #if WASM_ENABLE_SIMD != 0
  2562. option.enable_simd = true;
  2563. #endif
  2564. #if WASM_ENABLE_REF_TYPES != 0
  2565. option.enable_ref_types = true;
  2566. #endif
  2567. option.enable_aux_stack_check = true;
  2568. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  2569. option.enable_aux_stack_frame = true;
  2570. #endif
  2571. comp_ctx = aot_create_comp_context(comp_data, &option);
  2572. if (!comp_ctx) {
  2573. aot_last_error = aot_get_last_error();
  2574. bh_assert(aot_last_error != NULL);
  2575. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2576. goto fail1;
  2577. }
  2578. if (!aot_compile_wasm(comp_ctx)) {
  2579. aot_last_error = aot_get_last_error();
  2580. bh_assert(aot_last_error != NULL);
  2581. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2582. goto fail2;
  2583. }
  2584. aot_module =
  2585. aot_load_from_comp_data(comp_data, comp_ctx, error_buf, error_buf_size);
  2586. if (!aot_module) {
  2587. goto fail2;
  2588. }
  2589. return aot_module;
  2590. fail2:
  2591. aot_destroy_comp_context(comp_ctx);
  2592. fail1:
  2593. aot_destroy_comp_data(comp_data);
  2594. return NULL;
  2595. }
  2596. #endif
  2597. void
  2598. aot_unload(AOTModule *module)
  2599. {
  2600. #if WASM_ENABLE_JIT != 0
  2601. if (module->comp_data)
  2602. aot_destroy_comp_data(module->comp_data);
  2603. if (module->comp_ctx)
  2604. aot_destroy_comp_context(module->comp_ctx);
  2605. if (module->wasm_module)
  2606. wasm_loader_unload(module->wasm_module);
  2607. #endif
  2608. if (module->import_memories)
  2609. destroy_import_memories(module->import_memories, module->is_jit_mode);
  2610. if (module->memories)
  2611. wasm_runtime_free(module->memories);
  2612. if (module->mem_init_data_list)
  2613. destroy_mem_init_data_list(module->mem_init_data_list,
  2614. module->mem_init_data_count,
  2615. module->is_jit_mode);
  2616. if (module->native_symbol_list)
  2617. wasm_runtime_free(module->native_symbol_list);
  2618. if (module->import_tables)
  2619. destroy_import_tables(module->import_tables, module->is_jit_mode);
  2620. if (module->tables)
  2621. destroy_tables(module->tables, module->is_jit_mode);
  2622. if (module->table_init_data_list)
  2623. destroy_table_init_data_list(module->table_init_data_list,
  2624. module->table_init_data_count,
  2625. module->is_jit_mode);
  2626. if (module->func_types)
  2627. destroy_func_types(module->func_types, module->func_type_count,
  2628. module->is_jit_mode);
  2629. if (module->import_globals)
  2630. destroy_import_globals(module->import_globals, module->is_jit_mode);
  2631. if (module->globals)
  2632. destroy_globals(module->globals, module->is_jit_mode);
  2633. if (module->import_funcs)
  2634. destroy_import_funcs(module->import_funcs, module->is_jit_mode);
  2635. if (module->exports)
  2636. destroy_exports(module->exports, module->is_jit_mode);
  2637. if (module->func_type_indexes)
  2638. wasm_runtime_free(module->func_type_indexes);
  2639. if (module->func_ptrs)
  2640. wasm_runtime_free(module->func_ptrs);
  2641. if (module->const_str_set)
  2642. bh_hash_map_destroy(module->const_str_set);
  2643. if (module->code && (module->native_symbol_count == 0)) {
  2644. /* The layout is: literal size + literal + code (with plt table) */
  2645. uint8 *mmap_addr = module->literal - sizeof(uint32);
  2646. uint32 total_size =
  2647. sizeof(uint32) + module->literal_size + module->code_size;
  2648. os_munmap(mmap_addr, total_size);
  2649. }
  2650. #if defined(BH_PLATFORM_WINDOWS)
  2651. if (module->extra_plt_data) {
  2652. os_munmap(module->extra_plt_data, module->extra_plt_data_size);
  2653. }
  2654. #endif
  2655. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  2656. if (module->rtl_func_table) {
  2657. if (module->rtl_func_table_registered)
  2658. RtlDeleteFunctionTable(module->rtl_func_table);
  2659. wasm_runtime_free(module->rtl_func_table);
  2660. }
  2661. #endif
  2662. if (module->data_sections)
  2663. destroy_object_data_sections(module->data_sections,
  2664. module->data_section_count);
  2665. #if WASM_ENABLE_DEBUG_AOT != 0
  2666. jit_code_entry_destroy(module->elf_hdr);
  2667. #endif
  2668. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2669. if (module->aux_func_indexes) {
  2670. wasm_runtime_free(module->aux_func_indexes);
  2671. }
  2672. if (module->aux_func_names) {
  2673. wasm_runtime_free(module->aux_func_names);
  2674. }
  2675. #endif
  2676. wasm_runtime_free(module);
  2677. }
  2678. uint32
  2679. aot_get_plt_table_size()
  2680. {
  2681. return get_plt_table_size();
  2682. }