aot_loader.c 119 KB

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