aot_loader.c 115 KB

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