aot_emit_aot_file.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_compiler.h"
  6. #include "../aot/aot_runtime.h"
  7. #define PUT_U64_TO_ADDR(addr, value) \
  8. do { \
  9. union { \
  10. uint64 val; \
  11. uint32 parts[2]; \
  12. } u; \
  13. u.val = (value); \
  14. ((uint32 *)(addr))[0] = u.parts[0]; \
  15. ((uint32 *)(addr))[1] = u.parts[1]; \
  16. } while (0)
  17. #define CHECK_SIZE(size) \
  18. do { \
  19. if (size == (uint32)-1) { \
  20. aot_set_last_error("get symbol size failed."); \
  21. return (uint32)-1; \
  22. } \
  23. } while (0)
  24. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  25. static bool
  26. check_utf8_str(const uint8 *str, uint32 len)
  27. {
  28. /* The valid ranges are taken from page 125, below link
  29. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  30. const uint8 *p = str, *p_end = str + len;
  31. uint8 chr;
  32. while (p < p_end) {
  33. chr = *p;
  34. if (chr < 0x80) {
  35. p++;
  36. }
  37. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  38. if (p[1] < 0x80 || p[1] > 0xBF) {
  39. return false;
  40. }
  41. p += 2;
  42. }
  43. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  44. if (chr == 0xE0) {
  45. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  46. return false;
  47. }
  48. }
  49. else if (chr == 0xED) {
  50. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  51. return false;
  52. }
  53. }
  54. else if (chr >= 0xE1 && chr <= 0xEF) {
  55. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  56. return false;
  57. }
  58. }
  59. p += 3;
  60. }
  61. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  62. if (chr == 0xF0) {
  63. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  64. || p[3] < 0x80 || p[3] > 0xBF) {
  65. return false;
  66. }
  67. }
  68. else if (chr >= 0xF1 && chr <= 0xF3) {
  69. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  70. || p[3] < 0x80 || p[3] > 0xBF) {
  71. return false;
  72. }
  73. }
  74. else if (chr == 0xF4) {
  75. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  76. || p[3] < 0x80 || p[3] > 0xBF) {
  77. return false;
  78. }
  79. }
  80. p += 4;
  81. }
  82. else {
  83. return false;
  84. }
  85. }
  86. return (p == p_end);
  87. }
  88. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  89. /* Internal function in object file */
  90. typedef struct AOTObjectFunc {
  91. char *func_name;
  92. /* text offset of aot_func#n */
  93. uint64 text_offset;
  94. /* text offset of aot_func_internal#n */
  95. uint64 text_offset_of_aot_func_internal;
  96. } AOTObjectFunc;
  97. /* Symbol table list node */
  98. typedef struct AOTSymbolNode {
  99. struct AOTSymbolNode *next;
  100. uint32 str_len;
  101. char *symbol;
  102. } AOTSymbolNode;
  103. typedef struct AOTSymbolList {
  104. AOTSymbolNode *head;
  105. AOTSymbolNode *end;
  106. uint32 len;
  107. } AOTSymbolList;
  108. /* AOT object data */
  109. typedef struct AOTObjectData {
  110. AOTCompContext *comp_ctx;
  111. LLVMMemoryBufferRef mem_buf;
  112. LLVMBinaryRef binary;
  113. AOTTargetInfo target_info;
  114. void *text;
  115. uint32 text_size;
  116. void *text_unlikely;
  117. uint32 text_unlikely_size;
  118. void *text_hot;
  119. uint32 text_hot_size;
  120. /* literal data and size */
  121. void *literal;
  122. uint32 literal_size;
  123. AOTObjectDataSection *data_sections;
  124. uint32 data_sections_count;
  125. AOTObjectFunc *funcs;
  126. uint32 func_count;
  127. AOTSymbolList symbol_list;
  128. AOTRelocationGroup *relocation_groups;
  129. uint32 relocation_group_count;
  130. const char *stack_sizes_section_name;
  131. uint32 stack_sizes_offset;
  132. uint32 *stack_sizes;
  133. } AOTObjectData;
  134. #if 0
  135. static void dump_buf(uint8 *buf, uint32 size, char *title)
  136. {
  137. int i;
  138. printf("------ %s -------", title);
  139. for (i = 0; i < size; i++) {
  140. if ((i % 16) == 0)
  141. printf("\n");
  142. printf("%02x ", (unsigned char)buf[i]);
  143. }
  144. printf("\n\n");
  145. }
  146. #endif
  147. static bool
  148. is_32bit_binary(const AOTObjectData *obj_data)
  149. {
  150. /* bit 1: 0 is 32-bit, 1 is 64-bit */
  151. return obj_data->target_info.bin_type & 2 ? false : true;
  152. }
  153. static bool
  154. is_little_endian_binary(const AOTObjectData *obj_data)
  155. {
  156. /* bit 0: 0 is little-endian, 1 is big-endian */
  157. return obj_data->target_info.bin_type & 1 ? false : true;
  158. }
  159. static bool
  160. str_starts_with(const char *str, const char *prefix)
  161. {
  162. size_t len_pre = strlen(prefix), len_str = strlen(str);
  163. return (len_str >= len_pre) && !memcmp(str, prefix, len_pre);
  164. }
  165. static uint32
  166. get_file_header_size()
  167. {
  168. /* magic number (4 bytes) + version (4 bytes) */
  169. return sizeof(uint32) + sizeof(uint32);
  170. }
  171. static uint32
  172. get_string_size(AOTCompContext *comp_ctx, const char *s)
  173. {
  174. /* string size (2 bytes) + string content */
  175. return (uint32)sizeof(uint16) + (uint32)strlen(s) +
  176. /* emit string with '\0' only in XIP mode */
  177. (comp_ctx->is_indirect_mode ? 1 : 0);
  178. }
  179. static uint32
  180. get_target_info_section_size()
  181. {
  182. return sizeof(AOTTargetInfo);
  183. }
  184. static uint32
  185. get_mem_init_data_size(AOTMemInitData *mem_init_data)
  186. {
  187. /* init expr type (4 bytes) + init expr value (8 bytes)
  188. + byte count (4 bytes) + bytes */
  189. uint32 total_size = (uint32)(sizeof(uint32) + sizeof(uint64)
  190. + sizeof(uint32) + mem_init_data->byte_count);
  191. /* bulk_memory enabled:
  192. is_passive (4 bytes) + memory_index (4 bytes)
  193. bulk memory disabled:
  194. placeholder (4 bytes) + placeholder (4 bytes)
  195. */
  196. total_size += (sizeof(uint32) + sizeof(uint32));
  197. return total_size;
  198. }
  199. static uint32
  200. get_mem_init_data_list_size(AOTMemInitData **mem_init_data_list,
  201. uint32 mem_init_data_count)
  202. {
  203. AOTMemInitData **mem_init_data = mem_init_data_list;
  204. uint32 size = 0, i;
  205. for (i = 0; i < mem_init_data_count; i++, mem_init_data++) {
  206. size = align_uint(size, 4);
  207. size += get_mem_init_data_size(*mem_init_data);
  208. }
  209. return size;
  210. }
  211. static uint32
  212. get_import_memory_size(AOTCompData *comp_data)
  213. {
  214. /* currently we only emit import_memory_count = 0 */
  215. return sizeof(uint32);
  216. }
  217. static uint32
  218. get_memory_size(AOTCompData *comp_data)
  219. {
  220. /* memory_count + count * (memory_flags + num_bytes_per_page +
  221. init_page_count + max_page_count) */
  222. return (uint32)(sizeof(uint32)
  223. + comp_data->memory_count * sizeof(uint32) * 4);
  224. }
  225. static uint32
  226. get_mem_info_size(AOTCompData *comp_data)
  227. {
  228. /* import_memory_size + memory_size
  229. + init_data_count + init_data_list */
  230. return get_import_memory_size(comp_data) + get_memory_size(comp_data)
  231. + (uint32)sizeof(uint32)
  232. + get_mem_init_data_list_size(comp_data->mem_init_data_list,
  233. comp_data->mem_init_data_count);
  234. }
  235. static uint32
  236. get_table_init_data_size(AOTTableInitData *table_init_data)
  237. {
  238. /*
  239. * mode (4 bytes), elem_type (4 bytes)
  240. *
  241. * table_index(4 bytes) + init expr type (4 bytes) + init expr value (8
  242. * bytes)
  243. * + func index count (4 bytes) + func indexes
  244. */
  245. return (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
  246. + sizeof(uint64) + sizeof(uint32)
  247. + sizeof(uint32) * table_init_data->func_index_count);
  248. }
  249. static uint32
  250. get_table_init_data_list_size(AOTTableInitData **table_init_data_list,
  251. uint32 table_init_data_count)
  252. {
  253. /*
  254. * ------------------------------
  255. * | table_init_data_count
  256. * ------------------------------
  257. * | | U32 mode
  258. * | AOTTableInitData[N] | U32 elem_type
  259. * | | U32 table_index
  260. * | | U32 offset.init_expr_type
  261. * | | U64 offset.u.i64
  262. * | | U32 func_index_count
  263. * | | U32[func_index_count]
  264. * ------------------------------
  265. */
  266. AOTTableInitData **table_init_data = table_init_data_list;
  267. uint32 size = 0, i;
  268. size = (uint32)sizeof(uint32);
  269. for (i = 0; i < table_init_data_count; i++, table_init_data++) {
  270. size = align_uint(size, 4);
  271. size += get_table_init_data_size(*table_init_data);
  272. }
  273. return size;
  274. }
  275. static uint32
  276. get_import_table_size(AOTCompData *comp_data)
  277. {
  278. /*
  279. * ------------------------------
  280. * | import_table_count
  281. * ------------------------------
  282. * | | U32 table_init_size
  283. * | | ----------------------
  284. * | AOTImpotTable[N] | U32 table_init_size
  285. * | | ----------------------
  286. * | | U32 possible_grow (convenient than U8)
  287. * ------------------------------
  288. */
  289. return (uint32)(sizeof(uint32)
  290. + comp_data->import_table_count * (sizeof(uint32) * 3));
  291. }
  292. static uint32
  293. get_table_size(AOTCompData *comp_data)
  294. {
  295. /*
  296. * ------------------------------
  297. * | table_count
  298. * ------------------------------
  299. * | | U32 elem_type
  300. * | AOTTable[N] | U32 table_flags
  301. * | | U32 table_init_size
  302. * | | U32 table_max_size
  303. * | | U32 possible_grow (convenient than U8)
  304. * ------------------------------
  305. */
  306. return (uint32)(sizeof(uint32)
  307. + comp_data->table_count * (sizeof(uint32) * 5));
  308. }
  309. static uint32
  310. get_table_info_size(AOTCompData *comp_data)
  311. {
  312. /*
  313. * ------------------------------
  314. * | import_table_count
  315. * ------------------------------
  316. * |
  317. * | AOTImportTable[import_table_count]
  318. * |
  319. * ------------------------------
  320. * | table_count
  321. * ------------------------------
  322. * |
  323. * | AOTTable[table_count]
  324. * |
  325. * ------------------------------
  326. * | table_init_data_count
  327. * ------------------------------
  328. * |
  329. * | AOTTableInitData*[table_init_data_count]
  330. * |
  331. * ------------------------------
  332. */
  333. return get_import_table_size(comp_data) + get_table_size(comp_data)
  334. + get_table_init_data_list_size(comp_data->table_init_data_list,
  335. comp_data->table_init_data_count);
  336. }
  337. static uint32
  338. get_func_type_size(AOTFuncType *func_type)
  339. {
  340. /* param count + result count + types */
  341. return (uint32)sizeof(uint32) * 2 + func_type->param_count
  342. + func_type->result_count;
  343. }
  344. static uint32
  345. get_func_types_size(AOTFuncType **func_types, uint32 func_type_count)
  346. {
  347. AOTFuncType **func_type = func_types;
  348. uint32 size = 0, i;
  349. for (i = 0; i < func_type_count; i++, func_type++) {
  350. size = align_uint(size, 4);
  351. size += get_func_type_size(*func_type);
  352. }
  353. return size;
  354. }
  355. static uint32
  356. get_func_type_info_size(AOTCompData *comp_data)
  357. {
  358. /* func type count + func type list */
  359. return (uint32)sizeof(uint32)
  360. + get_func_types_size(comp_data->func_types,
  361. comp_data->func_type_count);
  362. }
  363. static uint32
  364. get_import_global_size(AOTCompContext *comp_ctx, AOTImportGlobal *import_global)
  365. {
  366. /* type (1 byte) + is_mutable (1 byte) + module_name + global_name */
  367. uint32 size = (uint32)sizeof(uint8) * 2
  368. + get_string_size(comp_ctx, import_global->module_name);
  369. size = align_uint(size, 2);
  370. size += get_string_size(comp_ctx, import_global->global_name);
  371. return size;
  372. }
  373. static uint32
  374. get_import_globals_size(AOTCompContext *comp_ctx,
  375. AOTImportGlobal *import_globals,
  376. uint32 import_global_count)
  377. {
  378. AOTImportGlobal *import_global = import_globals;
  379. uint32 size = 0, i;
  380. for (i = 0; i < import_global_count; i++, import_global++) {
  381. size = align_uint(size, 2);
  382. size += get_import_global_size(comp_ctx, import_global);
  383. }
  384. return size;
  385. }
  386. static uint32
  387. get_import_global_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
  388. {
  389. /* import global count + import globals */
  390. return (uint32)sizeof(uint32)
  391. + get_import_globals_size(comp_ctx, comp_data->import_globals,
  392. comp_data->import_global_count);
  393. }
  394. static uint32
  395. get_global_size(AOTGlobal *global)
  396. {
  397. if (global->init_expr.init_expr_type != INIT_EXPR_TYPE_V128_CONST)
  398. /* type (1 byte) + is_mutable (1 byte)
  399. + init expr type (2 byes) + init expr value (8 byes) */
  400. return sizeof(uint8) * 2 + sizeof(uint16) + sizeof(uint64);
  401. else
  402. /* type (1 byte) + is_mutable (1 byte)
  403. + init expr type (2 byes) + v128 value (16 byes) */
  404. return sizeof(uint8) * 2 + sizeof(uint16) + sizeof(uint64) * 2;
  405. }
  406. static uint32
  407. get_globals_size(AOTGlobal *globals, uint32 global_count)
  408. {
  409. AOTGlobal *global = globals;
  410. uint32 size = 0, i;
  411. for (i = 0; i < global_count; i++, global++) {
  412. size = align_uint(size, 4);
  413. size += get_global_size(global);
  414. }
  415. return size;
  416. }
  417. static uint32
  418. get_global_info_size(AOTCompData *comp_data)
  419. {
  420. /* global count + globals */
  421. return (uint32)sizeof(uint32)
  422. + get_globals_size(comp_data->globals, comp_data->global_count);
  423. }
  424. static uint32
  425. get_import_func_size(AOTCompContext *comp_ctx, AOTImportFunc *import_func)
  426. {
  427. /* type index (2 bytes) + module_name + func_name */
  428. uint32 size = (uint32)sizeof(uint16)
  429. + get_string_size(comp_ctx, import_func->module_name);
  430. size = align_uint(size, 2);
  431. size += get_string_size(comp_ctx, import_func->func_name);
  432. return size;
  433. }
  434. static uint32
  435. get_import_funcs_size(AOTCompContext *comp_ctx, AOTImportFunc *import_funcs,
  436. uint32 import_func_count)
  437. {
  438. AOTImportFunc *import_func = import_funcs;
  439. uint32 size = 0, i;
  440. for (i = 0; i < import_func_count; i++, import_func++) {
  441. size = align_uint(size, 2);
  442. size += get_import_func_size(comp_ctx, import_func);
  443. }
  444. return size;
  445. }
  446. static uint32
  447. get_import_func_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
  448. {
  449. /* import func count + import funcs */
  450. return (uint32)sizeof(uint32)
  451. + get_import_funcs_size(comp_ctx, comp_data->import_funcs,
  452. comp_data->import_func_count);
  453. }
  454. static uint32
  455. get_object_data_sections_size(AOTCompContext *comp_ctx,
  456. AOTObjectDataSection *data_sections,
  457. uint32 data_sections_count)
  458. {
  459. AOTObjectDataSection *data_section = data_sections;
  460. uint32 size = 0, i;
  461. for (i = 0; i < data_sections_count; i++, data_section++) {
  462. /* name + size + data */
  463. size = align_uint(size, 2);
  464. size += get_string_size(comp_ctx, data_section->name);
  465. size = align_uint(size, 4);
  466. size += (uint32)sizeof(uint32);
  467. size += data_section->size;
  468. }
  469. return size;
  470. }
  471. static uint32
  472. get_object_data_section_info_size(AOTCompContext *comp_ctx,
  473. AOTObjectData *obj_data)
  474. {
  475. /* data sections count + data sections */
  476. return (uint32)sizeof(uint32)
  477. + get_object_data_sections_size(comp_ctx, obj_data->data_sections,
  478. obj_data->data_sections_count);
  479. }
  480. static uint32
  481. get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  482. AOTObjectData *obj_data)
  483. {
  484. uint32 size = 0;
  485. size += get_mem_info_size(comp_data);
  486. size = align_uint(size, 4);
  487. size += get_table_info_size(comp_data);
  488. size = align_uint(size, 4);
  489. size += get_func_type_info_size(comp_data);
  490. size = align_uint(size, 4);
  491. size += get_import_global_info_size(comp_ctx, comp_data);
  492. size = align_uint(size, 4);
  493. size += get_global_info_size(comp_data);
  494. size = align_uint(size, 4);
  495. size += get_import_func_info_size(comp_ctx, comp_data);
  496. /* func count + start func index */
  497. size = align_uint(size, 4);
  498. size += (uint32)sizeof(uint32) * 2;
  499. /* aux data/heap/stack data */
  500. size += sizeof(uint32) * 7;
  501. size += get_object_data_section_info_size(comp_ctx, obj_data);
  502. return size;
  503. }
  504. static uint32
  505. get_text_section_size(AOTObjectData *obj_data)
  506. {
  507. return sizeof(uint32) + align_uint(obj_data->literal_size, 4)
  508. + align_uint(obj_data->text_size, 4)
  509. + align_uint(obj_data->text_unlikely_size, 4)
  510. + align_uint(obj_data->text_hot_size, 4);
  511. }
  512. static uint32
  513. get_func_section_size(AOTCompData *comp_data, AOTObjectData *obj_data)
  514. {
  515. /* text offsets + function type indexs */
  516. uint32 size = 0;
  517. if (is_32bit_binary(obj_data))
  518. size = (uint32)sizeof(uint32) * comp_data->func_count;
  519. else
  520. size = (uint32)sizeof(uint64) * comp_data->func_count;
  521. size += (uint32)sizeof(uint32) * comp_data->func_count;
  522. return size;
  523. }
  524. static uint32
  525. get_export_size(AOTCompContext *comp_ctx, AOTExport *export)
  526. {
  527. /* export index + export kind + 1 byte padding + export name */
  528. return (uint32)sizeof(uint32) + sizeof(uint8) + 1
  529. + get_string_size(comp_ctx, export->name);
  530. }
  531. static uint32
  532. get_exports_size(AOTCompContext *comp_ctx, AOTExport *exports,
  533. uint32 export_count)
  534. {
  535. AOTExport *export = exports;
  536. uint32 size = 0, i;
  537. for (i = 0; i < export_count; i++, export ++) {
  538. size = align_uint(size, 4);
  539. size += get_export_size(comp_ctx, export);
  540. }
  541. return size;
  542. }
  543. static uint32
  544. get_export_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
  545. {
  546. /* export count + exports */
  547. return (uint32)sizeof(uint32)
  548. + get_exports_size(comp_ctx, comp_data->wasm_module->exports,
  549. comp_data->wasm_module->export_count);
  550. }
  551. static uint32
  552. get_relocation_size(AOTRelocation *relocation, bool is_32bin)
  553. {
  554. /* offset + addend + relocation type + symbol name */
  555. uint32 size = 0;
  556. if (is_32bin)
  557. size = sizeof(uint32) * 2; /* offset and addend */
  558. else
  559. size = sizeof(uint64) * 2; /* offset and addend */
  560. size += (uint32)sizeof(uint32); /* relocation type */
  561. size += (uint32)sizeof(uint32); /* symbol name index */
  562. return size;
  563. }
  564. static uint32
  565. get_relocations_size(AOTObjectData *obj_data,
  566. AOTRelocationGroup *relocation_group,
  567. AOTRelocation *relocations, uint32 relocation_count,
  568. bool is_32bin)
  569. {
  570. AOTRelocation *relocation = relocations;
  571. uint32 size = 0, i;
  572. for (i = 0; i < relocation_count; i++, relocation++) {
  573. /* ignore the relocations to aot_func_internal#n in text section
  574. for windows platform since they will be applied in
  575. aot_emit_text_section */
  576. if ((!strcmp(relocation_group->section_name, ".text")
  577. || !strcmp(relocation_group->section_name, ".ltext"))
  578. && !strncmp(relocation->symbol_name, AOT_FUNC_INTERNAL_PREFIX,
  579. strlen(AOT_FUNC_INTERNAL_PREFIX))
  580. && ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
  581. /* Windows AOT_COFF64_BIN_TYPE */
  582. && obj_data->target_info.bin_type == 6
  583. /* IMAGE_REL_AMD64_REL32 in windows x86_64 */
  584. && relocation->relocation_type == 4)
  585. || (!strncmp(obj_data->comp_ctx->target_arch, "i386", 4)
  586. /* Windows AOT_COFF32_BIN_TYPE */
  587. && obj_data->target_info.bin_type == 4
  588. /* IMAGE_REL_I386_REL32 in windows x86_32 */
  589. && relocation->relocation_type == 20))) {
  590. continue;
  591. }
  592. size = align_uint(size, 4);
  593. size += get_relocation_size(relocation, is_32bin);
  594. }
  595. return size;
  596. }
  597. static uint32
  598. get_relocation_group_size(AOTObjectData *obj_data,
  599. AOTRelocationGroup *relocation_group, bool is_32bin)
  600. {
  601. uint32 size = 0;
  602. /* section name index + relocation count + relocations */
  603. size += (uint32)sizeof(uint32);
  604. size += (uint32)sizeof(uint32);
  605. size += get_relocations_size(obj_data, relocation_group,
  606. relocation_group->relocations,
  607. relocation_group->relocation_count, is_32bin);
  608. return size;
  609. }
  610. static uint32
  611. get_relocation_groups_size(AOTObjectData *obj_data,
  612. AOTRelocationGroup *relocation_groups,
  613. uint32 relocation_group_count, bool is_32bin)
  614. {
  615. AOTRelocationGroup *relocation_group = relocation_groups;
  616. uint32 size = 0, i;
  617. for (i = 0; i < relocation_group_count; i++, relocation_group++) {
  618. size = align_uint(size, 4);
  619. size += get_relocation_group_size(obj_data, relocation_group, is_32bin);
  620. }
  621. return size;
  622. }
  623. /* return the index (in order of insertion) of the symbol,
  624. create if not exits, -1 if failed */
  625. static uint32
  626. get_relocation_symbol_index(const char *symbol_name, bool *is_new,
  627. AOTSymbolList *symbol_list)
  628. {
  629. AOTSymbolNode *sym;
  630. uint32 index = 0;
  631. sym = symbol_list->head;
  632. while (sym) {
  633. if (!strcmp(sym->symbol, symbol_name)) {
  634. if (is_new)
  635. *is_new = false;
  636. return index;
  637. }
  638. sym = sym->next;
  639. index++;
  640. }
  641. /* Not found in symbol_list, add it */
  642. sym = wasm_runtime_malloc(sizeof(AOTSymbolNode));
  643. if (!sym) {
  644. return (uint32)-1;
  645. }
  646. memset(sym, 0, sizeof(AOTSymbolNode));
  647. sym->symbol = (char *)symbol_name;
  648. sym->str_len = (uint32)strlen(symbol_name);
  649. if (!symbol_list->head) {
  650. symbol_list->head = symbol_list->end = sym;
  651. }
  652. else {
  653. symbol_list->end->next = sym;
  654. symbol_list->end = sym;
  655. }
  656. symbol_list->len++;
  657. if (is_new)
  658. *is_new = true;
  659. return index;
  660. }
  661. static uint32
  662. get_relocation_symbol_size(AOTCompContext *comp_ctx, AOTRelocation *relocation,
  663. AOTSymbolList *symbol_list)
  664. {
  665. uint32 size = 0, index = 0;
  666. bool is_new = false;
  667. index = get_relocation_symbol_index(relocation->symbol_name, &is_new,
  668. symbol_list);
  669. CHECK_SIZE(index);
  670. if (is_new) {
  671. size += get_string_size(comp_ctx, relocation->symbol_name);
  672. size = align_uint(size, 2);
  673. }
  674. relocation->symbol_index = index;
  675. return size;
  676. }
  677. static uint32
  678. get_relocations_symbol_size(AOTCompContext *comp_ctx,
  679. AOTRelocation *relocations, uint32 relocation_count,
  680. AOTSymbolList *symbol_list)
  681. {
  682. AOTRelocation *relocation = relocations;
  683. uint32 size = 0, curr_size, i;
  684. for (i = 0; i < relocation_count; i++, relocation++) {
  685. curr_size =
  686. get_relocation_symbol_size(comp_ctx, relocation, symbol_list);
  687. CHECK_SIZE(curr_size);
  688. size += curr_size;
  689. }
  690. return size;
  691. }
  692. static uint32
  693. get_relocation_group_symbol_size(AOTCompContext *comp_ctx,
  694. AOTRelocationGroup *relocation_group,
  695. AOTSymbolList *symbol_list)
  696. {
  697. uint32 size = 0, index = 0, curr_size;
  698. bool is_new = false;
  699. index = get_relocation_symbol_index(relocation_group->section_name, &is_new,
  700. symbol_list);
  701. CHECK_SIZE(index);
  702. if (is_new) {
  703. size += get_string_size(comp_ctx, relocation_group->section_name);
  704. size = align_uint(size, 2);
  705. }
  706. relocation_group->name_index = index;
  707. curr_size = get_relocations_symbol_size(
  708. comp_ctx, relocation_group->relocations,
  709. relocation_group->relocation_count, symbol_list);
  710. CHECK_SIZE(curr_size);
  711. size += curr_size;
  712. return size;
  713. }
  714. static uint32
  715. get_relocation_groups_symbol_size(AOTCompContext *comp_ctx,
  716. AOTRelocationGroup *relocation_groups,
  717. uint32 relocation_group_count,
  718. AOTSymbolList *symbol_list)
  719. {
  720. AOTRelocationGroup *relocation_group = relocation_groups;
  721. uint32 size = 0, curr_size, i;
  722. for (i = 0; i < relocation_group_count; i++, relocation_group++) {
  723. curr_size = get_relocation_group_symbol_size(comp_ctx, relocation_group,
  724. symbol_list);
  725. CHECK_SIZE(curr_size);
  726. size += curr_size;
  727. }
  728. return size;
  729. }
  730. static uint32
  731. get_symbol_size_from_symbol_list(AOTCompContext *comp_ctx,
  732. AOTSymbolList *symbol_list)
  733. {
  734. AOTSymbolNode *sym;
  735. uint32 size = 0;
  736. sym = symbol_list->head;
  737. while (sym) {
  738. /* (uint16)str_len + str */
  739. size += get_string_size(comp_ctx, sym->symbol);
  740. size = align_uint(size, 2);
  741. sym = sym->next;
  742. }
  743. return size;
  744. }
  745. static uint32
  746. get_relocation_section_symbol_size(AOTCompContext *comp_ctx,
  747. AOTObjectData *obj_data)
  748. {
  749. AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
  750. uint32 relocation_group_count = obj_data->relocation_group_count;
  751. uint32 string_count = 0, symbol_table_size = 0;
  752. /* section size will be calculated twice,
  753. get symbol size from symbol list directly in the second calculation */
  754. if (obj_data->symbol_list.len > 0) {
  755. symbol_table_size =
  756. get_symbol_size_from_symbol_list(comp_ctx, &obj_data->symbol_list);
  757. }
  758. else {
  759. symbol_table_size = get_relocation_groups_symbol_size(
  760. comp_ctx, relocation_groups, relocation_group_count,
  761. &obj_data->symbol_list);
  762. }
  763. CHECK_SIZE(symbol_table_size);
  764. string_count = obj_data->symbol_list.len;
  765. /* string_count + string_offsets + total_string_len
  766. + [str (string_len + str)] */
  767. return (uint32)(sizeof(uint32) + sizeof(uint32) * string_count
  768. + sizeof(uint32) + symbol_table_size);
  769. }
  770. static uint32
  771. get_relocation_section_size(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
  772. {
  773. AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
  774. uint32 relocation_group_count = obj_data->relocation_group_count;
  775. uint32 symbol_table_size = 0;
  776. symbol_table_size = get_relocation_section_symbol_size(comp_ctx, obj_data);
  777. CHECK_SIZE(symbol_table_size);
  778. symbol_table_size = align_uint(symbol_table_size, 4);
  779. /* relocation group count + symbol_table + relocation groups */
  780. return (uint32)sizeof(uint32) + symbol_table_size
  781. + get_relocation_groups_size(obj_data, relocation_groups,
  782. relocation_group_count,
  783. is_32bit_binary(obj_data));
  784. }
  785. static uint32
  786. get_native_symbol_list_size(AOTCompContext *comp_ctx)
  787. {
  788. uint32 len = 0;
  789. AOTNativeSymbol *sym = NULL;
  790. sym = bh_list_first_elem(&comp_ctx->native_symbols);
  791. while (sym) {
  792. len = align_uint(len, 2);
  793. len += get_string_size(comp_ctx, sym->symbol);
  794. sym = bh_list_elem_next(sym);
  795. }
  796. return len;
  797. }
  798. static uint32
  799. get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data);
  800. static uint32
  801. get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  802. AOTObjectData *obj_data)
  803. {
  804. uint32 size = 0;
  805. uint32 size_custom_section = 0;
  806. /* aot file header */
  807. size += get_file_header_size();
  808. /* target info section */
  809. size = align_uint(size, 4);
  810. /* section id + section size */
  811. size += (uint32)sizeof(uint32) * 2;
  812. size += get_target_info_section_size();
  813. /* init data section */
  814. size = align_uint(size, 4);
  815. /* section id + section size */
  816. size += (uint32)sizeof(uint32) * 2;
  817. size += get_init_data_section_size(comp_ctx, comp_data, obj_data);
  818. /* text section */
  819. size = align_uint(size, 4);
  820. /* section id + section size */
  821. size += (uint32)sizeof(uint32) * 2;
  822. size += get_text_section_size(obj_data);
  823. /* function section */
  824. size = align_uint(size, 4);
  825. /* section id + section size */
  826. size += (uint32)sizeof(uint32) * 2;
  827. size += get_func_section_size(comp_data, obj_data);
  828. /* export section */
  829. size = align_uint(size, 4);
  830. /* section id + section size */
  831. size += (uint32)sizeof(uint32) * 2;
  832. size += get_export_section_size(comp_ctx, comp_data);
  833. /* relocation section */
  834. size = align_uint(size, 4);
  835. /* section id + section size */
  836. size += (uint32)sizeof(uint32) * 2;
  837. size += get_relocation_section_size(comp_ctx, obj_data);
  838. if (get_native_symbol_list_size(comp_ctx) > 0) {
  839. /* emit only when there are native symbols */
  840. size = align_uint(size, 4);
  841. /* section id + section size + sub section id + symbol count */
  842. size += (uint32)sizeof(uint32) * 4;
  843. size += get_native_symbol_list_size(comp_ctx);
  844. }
  845. size_custom_section = get_custom_sections_size(comp_ctx, comp_data);
  846. if (size_custom_section > 0) {
  847. size = align_uint(size, 4);
  848. size += size_custom_section;
  849. }
  850. return size;
  851. }
  852. #define exchange_uint8(p_data) (void)0
  853. static void
  854. exchange_uint16(uint8 *p_data)
  855. {
  856. uint8 value = *p_data;
  857. *p_data = *(p_data + 1);
  858. *(p_data + 1) = value;
  859. }
  860. static void
  861. exchange_uint32(uint8 *p_data)
  862. {
  863. uint8 value = *p_data;
  864. *p_data = *(p_data + 3);
  865. *(p_data + 3) = value;
  866. value = *(p_data + 1);
  867. *(p_data + 1) = *(p_data + 2);
  868. *(p_data + 2) = value;
  869. }
  870. static void
  871. exchange_uint64(uint8 *p_data)
  872. {
  873. uint32 value;
  874. value = *(uint32 *)p_data;
  875. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  876. *(uint32 *)(p_data + 4) = value;
  877. exchange_uint32(p_data);
  878. exchange_uint32(p_data + 4);
  879. }
  880. static void
  881. exchange_uint128(uint8 *p_data)
  882. {
  883. /* swap high 64bit and low 64bit */
  884. uint64 value = *(uint64 *)p_data;
  885. *(uint64 *)p_data = *(uint64 *)(p_data + 8);
  886. *(uint64 *)(p_data + 8) = value;
  887. /* exchange high 64bit */
  888. exchange_uint64(p_data);
  889. /* exchange low 64bit */
  890. exchange_uint64(p_data + 8);
  891. }
  892. static union {
  893. int a;
  894. char b;
  895. } __ue = { .a = 1 };
  896. #define is_little_endian() (__ue.b == 1)
  897. #define CHECK_BUF(length) \
  898. do { \
  899. if (buf + offset + length > buf_end) { \
  900. aot_set_last_error("buf overflow"); \
  901. return false; \
  902. } \
  903. } while (0)
  904. #define EMIT_U8(v) \
  905. do { \
  906. CHECK_BUF(1); \
  907. *(uint8 *)(buf + offset) = (uint8)v; \
  908. offset++; \
  909. } while (0)
  910. #define EMIT_U16(v) \
  911. do { \
  912. uint16 t = (uint16)v; \
  913. CHECK_BUF(2); \
  914. if (!is_little_endian()) \
  915. exchange_uint16((uint8 *)&t); \
  916. *(uint16 *)(buf + offset) = t; \
  917. offset += (uint32)sizeof(uint16); \
  918. } while (0)
  919. #define EMIT_U32(v) \
  920. do { \
  921. uint32 t = (uint32)v; \
  922. CHECK_BUF(4); \
  923. if (!is_little_endian()) \
  924. exchange_uint32((uint8 *)&t); \
  925. *(uint32 *)(buf + offset) = t; \
  926. offset += (uint32)sizeof(uint32); \
  927. } while (0)
  928. #define EMIT_U64(v) \
  929. do { \
  930. uint64 t = (uint64)v; \
  931. CHECK_BUF(8); \
  932. if (!is_little_endian()) \
  933. exchange_uint64((uint8 *)&t); \
  934. PUT_U64_TO_ADDR(buf + offset, t); \
  935. offset += (uint32)sizeof(uint64); \
  936. } while (0)
  937. #define EMIT_V128(v) \
  938. do { \
  939. uint64 *t = (uint64 *)v.i64x2; \
  940. CHECK_BUF(16); \
  941. if (!is_little_endian()) \
  942. exchange_uint128((uint8 *)t); \
  943. PUT_U64_TO_ADDR(buf + offset, t[0]); \
  944. offset += (uint32)sizeof(uint64); \
  945. PUT_U64_TO_ADDR(buf + offset, t[1]); \
  946. offset += (uint32)sizeof(uint64); \
  947. } while (0)
  948. #define EMIT_BUF(v, len) \
  949. do { \
  950. CHECK_BUF(len); \
  951. memcpy(buf + offset, v, len); \
  952. offset += len; \
  953. } while (0)
  954. #define EMIT_STR(s) \
  955. do { \
  956. uint32 str_len = (uint32)strlen(s); \
  957. if (str_len > INT16_MAX) { \
  958. aot_set_last_error("emit string failed: " \
  959. "string too long"); \
  960. return false; \
  961. } \
  962. if (comp_ctx->is_indirect_mode) \
  963. /* emit '\0' only in XIP mode */ \
  964. str_len++; \
  965. EMIT_U16(str_len); \
  966. EMIT_BUF(s, str_len); \
  967. } while (0)
  968. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  969. static bool
  970. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  971. uint64 *p_result)
  972. {
  973. const uint8 *buf = *p_buf;
  974. uint64 result = 0;
  975. uint32 shift = 0;
  976. uint32 offset = 0, bcnt = 0;
  977. uint64 byte;
  978. while (true) {
  979. /* uN or SN must not exceed ceil(N/7) bytes */
  980. if (bcnt + 1 > (maxbits + 6) / 7) {
  981. aot_set_last_error("integer representation too long");
  982. return false;
  983. }
  984. if (buf + offset + 1 > buf_end) {
  985. aot_set_last_error("unexpected end of section or function");
  986. return false;
  987. }
  988. byte = buf[offset];
  989. offset += 1;
  990. result |= ((byte & 0x7f) << shift);
  991. shift += 7;
  992. bcnt += 1;
  993. if ((byte & 0x80) == 0) {
  994. break;
  995. }
  996. }
  997. if (!sign && maxbits == 32 && shift >= maxbits) {
  998. /* The top bits set represent values > 32 bits */
  999. if (((uint8)byte) & 0xf0)
  1000. goto fail_integer_too_large;
  1001. }
  1002. else if (sign && maxbits == 32) {
  1003. if (shift < maxbits) {
  1004. /* Sign extend, second highest bit is the sign bit */
  1005. if ((uint8)byte & 0x40)
  1006. result |= (~((uint64)0)) << shift;
  1007. }
  1008. else {
  1009. /* The top bits should be a sign-extension of the sign bit */
  1010. bool sign_bit_set = ((uint8)byte) & 0x8;
  1011. int top_bits = ((uint8)byte) & 0xf0;
  1012. if ((sign_bit_set && top_bits != 0x70)
  1013. || (!sign_bit_set && top_bits != 0))
  1014. goto fail_integer_too_large;
  1015. }
  1016. }
  1017. else if (sign && maxbits == 64) {
  1018. if (shift < maxbits) {
  1019. /* Sign extend, second highest bit is the sign bit */
  1020. if ((uint8)byte & 0x40)
  1021. result |= (~((uint64)0)) << shift;
  1022. }
  1023. else {
  1024. /* The top bits should be a sign-extension of the sign bit */
  1025. bool sign_bit_set = ((uint8)byte) & 0x1;
  1026. int top_bits = ((uint8)byte) & 0xfe;
  1027. if ((sign_bit_set && top_bits != 0x7e)
  1028. || (!sign_bit_set && top_bits != 0))
  1029. goto fail_integer_too_large;
  1030. }
  1031. }
  1032. *p_buf += offset;
  1033. *p_result = result;
  1034. return true;
  1035. fail_integer_too_large:
  1036. aot_set_last_error("integer too large");
  1037. return false;
  1038. }
  1039. #define read_leb_uint32(p, p_end, res) \
  1040. do { \
  1041. uint64 res64; \
  1042. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64)) \
  1043. goto fail; \
  1044. res = (uint32)res64; \
  1045. } while (0)
  1046. static uint32
  1047. get_name_section_size(AOTCompData *comp_data)
  1048. {
  1049. const uint8 *p = comp_data->name_section_buf,
  1050. *p_end = comp_data->name_section_buf_end;
  1051. uint8 *buf, *buf_end;
  1052. uint32 name_type, subsection_size;
  1053. uint32 previous_name_type = 0;
  1054. uint32 num_func_name;
  1055. uint32 func_index;
  1056. uint32 previous_func_index = ~0U;
  1057. uint32 func_name_len;
  1058. uint32 name_index;
  1059. int i = 0;
  1060. uint32 name_len;
  1061. uint32 offset = 0;
  1062. uint32 max_aot_buf_size = 0;
  1063. if (p >= p_end) {
  1064. aot_set_last_error("unexpected end");
  1065. return 0;
  1066. }
  1067. max_aot_buf_size = 4 * (uint32)(p_end - p);
  1068. if (!(buf = comp_data->aot_name_section_buf =
  1069. wasm_runtime_malloc(max_aot_buf_size))) {
  1070. aot_set_last_error("allocate memory for custom name section failed.");
  1071. return 0;
  1072. }
  1073. buf_end = buf + max_aot_buf_size;
  1074. read_leb_uint32(p, p_end, name_len);
  1075. offset = align_uint(offset, 4);
  1076. EMIT_U32(name_len);
  1077. if (name_len == 0 || p + name_len > p_end) {
  1078. aot_set_last_error("unexpected end");
  1079. return 0;
  1080. }
  1081. if (!check_utf8_str(p, name_len)) {
  1082. aot_set_last_error("invalid UTF-8 encoding");
  1083. return 0;
  1084. }
  1085. if (memcmp(p, "name", 4) != 0) {
  1086. aot_set_last_error("invalid custom name section");
  1087. return 0;
  1088. }
  1089. EMIT_BUF(p, name_len);
  1090. p += name_len;
  1091. while (p < p_end) {
  1092. read_leb_uint32(p, p_end, name_type);
  1093. if (i != 0) {
  1094. if (name_type == previous_name_type) {
  1095. aot_set_last_error("duplicate sub-section");
  1096. return 0;
  1097. }
  1098. if (name_type < previous_name_type) {
  1099. aot_set_last_error("out-of-order sub-section");
  1100. return 0;
  1101. }
  1102. }
  1103. previous_name_type = name_type;
  1104. read_leb_uint32(p, p_end, subsection_size);
  1105. switch (name_type) {
  1106. case SUB_SECTION_TYPE_FUNC:
  1107. if (subsection_size) {
  1108. offset = align_uint(offset, 4);
  1109. EMIT_U32(name_type);
  1110. EMIT_U32(subsection_size);
  1111. read_leb_uint32(p, p_end, num_func_name);
  1112. EMIT_U32(num_func_name);
  1113. for (name_index = 0; name_index < num_func_name;
  1114. name_index++) {
  1115. read_leb_uint32(p, p_end, func_index);
  1116. offset = align_uint(offset, 4);
  1117. EMIT_U32(func_index);
  1118. if (func_index == previous_func_index) {
  1119. aot_set_last_error("duplicate function name");
  1120. return 0;
  1121. }
  1122. if (func_index < previous_func_index
  1123. && previous_func_index != ~0U) {
  1124. aot_set_last_error("out-of-order function index ");
  1125. return 0;
  1126. }
  1127. previous_func_index = func_index;
  1128. read_leb_uint32(p, p_end, func_name_len);
  1129. offset = align_uint(offset, 2);
  1130. EMIT_U16(func_name_len);
  1131. EMIT_BUF(p, func_name_len);
  1132. p += func_name_len;
  1133. }
  1134. }
  1135. break;
  1136. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  1137. */
  1138. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  1139. default:
  1140. p = p + subsection_size;
  1141. break;
  1142. }
  1143. i++;
  1144. }
  1145. return offset;
  1146. fail:
  1147. return 0;
  1148. }
  1149. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  1150. static uint32
  1151. get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
  1152. {
  1153. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  1154. uint32 size = 0, i;
  1155. for (i = 0; i < comp_ctx->custom_sections_count; i++) {
  1156. const char *section_name = comp_ctx->custom_sections_wp[i];
  1157. const uint8 *content = NULL;
  1158. uint32 length = 0;
  1159. if (strcmp(section_name, "name") == 0) {
  1160. /* custom name section */
  1161. comp_data->aot_name_section_size = get_name_section_size(comp_data);
  1162. if (comp_data->aot_name_section_size == 0) {
  1163. LOG_WARNING("Can't find custom section [name], ignore it");
  1164. continue;
  1165. }
  1166. size = align_uint(size, 4);
  1167. /* section id + section size + sub section id */
  1168. size += (uint32)sizeof(uint32) * 3;
  1169. size += comp_data->aot_name_section_size;
  1170. continue;
  1171. }
  1172. content = wasm_loader_get_custom_section(comp_data->wasm_module,
  1173. section_name, &length);
  1174. if (!content) {
  1175. LOG_WARNING("Can't find custom section [%s], ignore it",
  1176. section_name);
  1177. continue;
  1178. }
  1179. size = align_uint(size, 4);
  1180. /* section id + section size + sub section id */
  1181. size += (uint32)sizeof(uint32) * 3;
  1182. /* section name and len */
  1183. size += get_string_size(comp_ctx, section_name);
  1184. /* section content */
  1185. size += length;
  1186. }
  1187. return size;
  1188. #else
  1189. return 0;
  1190. #endif
  1191. }
  1192. static bool
  1193. aot_emit_file_header(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1194. AOTCompData *comp_data, AOTObjectData *obj_data)
  1195. {
  1196. uint32 offset = *p_offset;
  1197. uint32 aot_curr_version = AOT_CURRENT_VERSION;
  1198. EMIT_U8('\0');
  1199. EMIT_U8('a');
  1200. EMIT_U8('o');
  1201. EMIT_U8('t');
  1202. EMIT_U32(aot_curr_version);
  1203. *p_offset = offset;
  1204. return true;
  1205. }
  1206. static bool
  1207. aot_emit_target_info_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1208. AOTCompData *comp_data, AOTObjectData *obj_data)
  1209. {
  1210. uint32 offset = *p_offset;
  1211. uint32 section_size = get_target_info_section_size();
  1212. AOTTargetInfo *target_info = &obj_data->target_info;
  1213. *p_offset = offset = align_uint(offset, 4);
  1214. EMIT_U32(AOT_SECTION_TYPE_TARGET_INFO);
  1215. EMIT_U32(section_size);
  1216. EMIT_U16(target_info->bin_type);
  1217. EMIT_U16(target_info->abi_type);
  1218. EMIT_U16(target_info->e_type);
  1219. EMIT_U16(target_info->e_machine);
  1220. EMIT_U32(target_info->e_version);
  1221. EMIT_U32(target_info->e_flags);
  1222. EMIT_U32(target_info->reserved);
  1223. EMIT_BUF(target_info->arch, sizeof(target_info->arch));
  1224. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1225. aot_set_last_error("emit target info failed.");
  1226. return false;
  1227. }
  1228. *p_offset = offset;
  1229. return true;
  1230. }
  1231. static bool
  1232. aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1233. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1234. AOTObjectData *obj_data)
  1235. {
  1236. uint32 offset = *p_offset, i;
  1237. AOTMemInitData **init_datas = comp_data->mem_init_data_list;
  1238. *p_offset = offset = align_uint(offset, 4);
  1239. /* Emit import memory count, only emit 0 currently.
  1240. TODO: emit the actual import memory count and
  1241. the full import memory info. */
  1242. EMIT_U32(0);
  1243. /* Emit memory count */
  1244. EMIT_U32(comp_data->memory_count);
  1245. /* Emit memory items */
  1246. for (i = 0; i < comp_data->memory_count; i++) {
  1247. EMIT_U32(comp_data->memories[i].memory_flags);
  1248. EMIT_U32(comp_data->memories[i].num_bytes_per_page);
  1249. EMIT_U32(comp_data->memories[i].mem_init_page_count);
  1250. EMIT_U32(comp_data->memories[i].mem_max_page_count);
  1251. }
  1252. /* Emit mem init data count */
  1253. EMIT_U32(comp_data->mem_init_data_count);
  1254. /* Emit mem init data items */
  1255. for (i = 0; i < comp_data->mem_init_data_count; i++) {
  1256. offset = align_uint(offset, 4);
  1257. #if WASM_ENABLE_BULK_MEMORY != 0
  1258. if (comp_ctx->enable_bulk_memory) {
  1259. EMIT_U32(init_datas[i]->is_passive);
  1260. EMIT_U32(init_datas[i]->memory_index);
  1261. }
  1262. else
  1263. #endif
  1264. {
  1265. /* emit two placeholder to keep the same size */
  1266. EMIT_U32(0);
  1267. EMIT_U32(0);
  1268. }
  1269. EMIT_U32(init_datas[i]->offset.init_expr_type);
  1270. EMIT_U64(init_datas[i]->offset.u.i64);
  1271. EMIT_U32(init_datas[i]->byte_count);
  1272. EMIT_BUF(init_datas[i]->bytes, init_datas[i]->byte_count);
  1273. }
  1274. if (offset - *p_offset != get_mem_info_size(comp_data)) {
  1275. aot_set_last_error("emit memory info failed.");
  1276. return false;
  1277. }
  1278. *p_offset = offset;
  1279. return true;
  1280. }
  1281. static bool
  1282. aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1283. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1284. AOTObjectData *obj_data)
  1285. {
  1286. uint32 offset = *p_offset, i, j;
  1287. AOTTableInitData **init_datas = comp_data->table_init_data_list;
  1288. *p_offset = offset = align_uint(offset, 4);
  1289. /* Emit import table count */
  1290. EMIT_U32(comp_data->import_table_count);
  1291. /* Emit table items */
  1292. for (i = 0; i < comp_data->import_table_count; i++) {
  1293. /* TODO:
  1294. * EMIT_STR(comp_data->import_tables[i].module_name );
  1295. * EMIT_STR(comp_data->import_tables[i].table_name);
  1296. */
  1297. EMIT_U32(comp_data->import_tables[i].elem_type);
  1298. EMIT_U32(comp_data->import_tables[i].table_init_size);
  1299. EMIT_U32(comp_data->import_tables[i].table_max_size);
  1300. EMIT_U32(comp_data->import_tables[i].possible_grow & 0x000000FF);
  1301. }
  1302. /* Emit table count */
  1303. EMIT_U32(comp_data->table_count);
  1304. /* Emit table items */
  1305. for (i = 0; i < comp_data->table_count; i++) {
  1306. EMIT_U32(comp_data->tables[i].elem_type);
  1307. EMIT_U32(comp_data->tables[i].table_flags);
  1308. EMIT_U32(comp_data->tables[i].table_init_size);
  1309. EMIT_U32(comp_data->tables[i].table_max_size);
  1310. EMIT_U32(comp_data->tables[i].possible_grow & 0x000000FF);
  1311. }
  1312. /* Emit table init data count */
  1313. EMIT_U32(comp_data->table_init_data_count);
  1314. /* Emit table init data items */
  1315. for (i = 0; i < comp_data->table_init_data_count; i++) {
  1316. offset = align_uint(offset, 4);
  1317. EMIT_U32(init_datas[i]->mode);
  1318. EMIT_U32(init_datas[i]->elem_type);
  1319. EMIT_U32(init_datas[i]->table_index);
  1320. EMIT_U32(init_datas[i]->offset.init_expr_type);
  1321. EMIT_U64(init_datas[i]->offset.u.i64);
  1322. EMIT_U32(init_datas[i]->func_index_count);
  1323. for (j = 0; j < init_datas[i]->func_index_count; j++)
  1324. EMIT_U32(init_datas[i]->func_indexes[j]);
  1325. }
  1326. if (offset - *p_offset != get_table_info_size(comp_data)) {
  1327. aot_set_last_error("emit table info failed.");
  1328. return false;
  1329. }
  1330. *p_offset = offset;
  1331. return true;
  1332. }
  1333. static bool
  1334. aot_emit_func_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1335. AOTCompData *comp_data, AOTObjectData *obj_data)
  1336. {
  1337. uint32 offset = *p_offset, i;
  1338. AOTFuncType **func_types = comp_data->func_types;
  1339. *p_offset = offset = align_uint(offset, 4);
  1340. EMIT_U32(comp_data->func_type_count);
  1341. for (i = 0; i < comp_data->func_type_count; i++) {
  1342. offset = align_uint(offset, 4);
  1343. EMIT_U32(func_types[i]->param_count);
  1344. EMIT_U32(func_types[i]->result_count);
  1345. EMIT_BUF(func_types[i]->types,
  1346. func_types[i]->param_count + func_types[i]->result_count);
  1347. }
  1348. if (offset - *p_offset != get_func_type_info_size(comp_data)) {
  1349. aot_set_last_error("emit function type info failed.");
  1350. return false;
  1351. }
  1352. *p_offset = offset;
  1353. return true;
  1354. }
  1355. static bool
  1356. aot_emit_import_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1357. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1358. AOTObjectData *obj_data)
  1359. {
  1360. uint32 offset = *p_offset, i;
  1361. AOTImportGlobal *import_global = comp_data->import_globals;
  1362. *p_offset = offset = align_uint(offset, 4);
  1363. EMIT_U32(comp_data->import_global_count);
  1364. for (i = 0; i < comp_data->import_global_count; i++, import_global++) {
  1365. offset = align_uint(offset, 2);
  1366. EMIT_U8(import_global->type);
  1367. EMIT_U8(import_global->is_mutable);
  1368. EMIT_STR(import_global->module_name);
  1369. offset = align_uint(offset, 2);
  1370. EMIT_STR(import_global->global_name);
  1371. }
  1372. if (offset - *p_offset
  1373. != get_import_global_info_size(comp_ctx, comp_data)) {
  1374. aot_set_last_error("emit import global info failed.");
  1375. return false;
  1376. }
  1377. *p_offset = offset;
  1378. return true;
  1379. }
  1380. static bool
  1381. aot_emit_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1382. AOTCompData *comp_data, AOTObjectData *obj_data)
  1383. {
  1384. uint32 offset = *p_offset, i;
  1385. AOTGlobal *global = comp_data->globals;
  1386. *p_offset = offset = align_uint(offset, 4);
  1387. EMIT_U32(comp_data->global_count);
  1388. for (i = 0; i < comp_data->global_count; i++, global++) {
  1389. offset = align_uint(offset, 4);
  1390. EMIT_U8(global->type);
  1391. EMIT_U8(global->is_mutable);
  1392. EMIT_U16(global->init_expr.init_expr_type);
  1393. if (global->init_expr.init_expr_type != INIT_EXPR_TYPE_V128_CONST)
  1394. EMIT_U64(global->init_expr.u.i64);
  1395. else
  1396. EMIT_V128(global->init_expr.u.v128);
  1397. }
  1398. if (offset - *p_offset != get_global_info_size(comp_data)) {
  1399. aot_set_last_error("emit global info failed.");
  1400. return false;
  1401. }
  1402. *p_offset = offset;
  1403. return true;
  1404. }
  1405. static bool
  1406. aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1407. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1408. AOTObjectData *obj_data)
  1409. {
  1410. uint32 offset = *p_offset, i;
  1411. AOTImportFunc *import_func = comp_data->import_funcs;
  1412. *p_offset = offset = align_uint(offset, 4);
  1413. EMIT_U32(comp_data->import_func_count);
  1414. for (i = 0; i < comp_data->import_func_count; i++, import_func++) {
  1415. offset = align_uint(offset, 2);
  1416. EMIT_U16(import_func->func_type_index);
  1417. EMIT_STR(import_func->module_name);
  1418. offset = align_uint(offset, 2);
  1419. EMIT_STR(import_func->func_name);
  1420. }
  1421. if (offset - *p_offset != get_import_func_info_size(comp_ctx, comp_data)) {
  1422. aot_set_last_error("emit import function info failed.");
  1423. return false;
  1424. }
  1425. *p_offset = offset;
  1426. return true;
  1427. }
  1428. static bool
  1429. aot_emit_object_data_section_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1430. AOTCompContext *comp_ctx,
  1431. AOTObjectData *obj_data)
  1432. {
  1433. uint32 offset = *p_offset, i;
  1434. AOTObjectDataSection *data_section = obj_data->data_sections;
  1435. *p_offset = offset = align_uint(offset, 4);
  1436. EMIT_U32(obj_data->data_sections_count);
  1437. for (i = 0; i < obj_data->data_sections_count; i++, data_section++) {
  1438. offset = align_uint(offset, 2);
  1439. EMIT_STR(data_section->name);
  1440. offset = align_uint(offset, 4);
  1441. EMIT_U32(data_section->size);
  1442. if (obj_data->stack_sizes_section_name != NULL
  1443. && !strcmp(obj_data->stack_sizes_section_name,
  1444. data_section->name)) {
  1445. uint32 ss_offset = obj_data->stack_sizes_offset;
  1446. uint32 ss_size =
  1447. obj_data->func_count * sizeof(*obj_data->stack_sizes);
  1448. LOG_VERBOSE("Replacing stack_sizes in %s section, offset %" PRIu32
  1449. ", size %" PRIu32,
  1450. obj_data->stack_sizes_section_name, ss_offset, ss_size);
  1451. bh_assert(ss_offset + ss_size <= data_section->size);
  1452. /* 0 .. ss_offset */
  1453. if (ss_offset > 0) {
  1454. EMIT_BUF(data_section->data, ss_offset);
  1455. }
  1456. /* ss_offset .. ss_offset+ss_size */
  1457. EMIT_BUF(obj_data->stack_sizes, ss_size);
  1458. /* ss_offset+ss_size .. data_section->size */
  1459. if (data_section->size > ss_offset + ss_size) {
  1460. EMIT_BUF(data_section->data + ss_offset + ss_size,
  1461. data_section->size - (ss_offset + ss_size));
  1462. }
  1463. }
  1464. else {
  1465. EMIT_BUF(data_section->data, data_section->size);
  1466. }
  1467. }
  1468. if (offset - *p_offset
  1469. != get_object_data_section_info_size(comp_ctx, obj_data)) {
  1470. aot_set_last_error("emit object data section info failed.");
  1471. return false;
  1472. }
  1473. *p_offset = offset;
  1474. return true;
  1475. }
  1476. static bool
  1477. aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1478. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1479. AOTObjectData *obj_data)
  1480. {
  1481. uint32 section_size =
  1482. get_init_data_section_size(comp_ctx, comp_data, obj_data);
  1483. uint32 offset = *p_offset;
  1484. *p_offset = offset = align_uint(offset, 4);
  1485. EMIT_U32(AOT_SECTION_TYPE_INIT_DATA);
  1486. EMIT_U32(section_size);
  1487. if (!aot_emit_mem_info(buf, buf_end, &offset, comp_ctx, comp_data, obj_data)
  1488. || !aot_emit_table_info(buf, buf_end, &offset, comp_ctx, comp_data,
  1489. obj_data)
  1490. || !aot_emit_func_type_info(buf, buf_end, &offset, comp_data, obj_data)
  1491. || !aot_emit_import_global_info(buf, buf_end, &offset, comp_ctx,
  1492. comp_data, obj_data)
  1493. || !aot_emit_global_info(buf, buf_end, &offset, comp_data, obj_data)
  1494. || !aot_emit_import_func_info(buf, buf_end, &offset, comp_ctx,
  1495. comp_data, obj_data))
  1496. return false;
  1497. offset = align_uint(offset, 4);
  1498. EMIT_U32(comp_data->func_count);
  1499. EMIT_U32(comp_data->start_func_index);
  1500. EMIT_U32(comp_data->aux_data_end_global_index);
  1501. EMIT_U32(comp_data->aux_data_end);
  1502. EMIT_U32(comp_data->aux_heap_base_global_index);
  1503. EMIT_U32(comp_data->aux_heap_base);
  1504. EMIT_U32(comp_data->aux_stack_top_global_index);
  1505. EMIT_U32(comp_data->aux_stack_bottom);
  1506. EMIT_U32(comp_data->aux_stack_size);
  1507. if (!aot_emit_object_data_section_info(buf, buf_end, &offset, comp_ctx,
  1508. obj_data))
  1509. return false;
  1510. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1511. aot_set_last_error("emit init data section failed.");
  1512. return false;
  1513. }
  1514. *p_offset = offset;
  1515. return true;
  1516. }
  1517. static bool
  1518. aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1519. AOTCompData *comp_data, AOTObjectData *obj_data)
  1520. {
  1521. uint32 section_size = get_text_section_size(obj_data);
  1522. uint32 offset = *p_offset;
  1523. uint8 placeholder = 0;
  1524. AOTRelocationGroup *relocation_group;
  1525. AOTRelocation *relocation;
  1526. uint32 i, j, relocation_count;
  1527. uint8 *text;
  1528. *p_offset = offset = align_uint(offset, 4);
  1529. EMIT_U32(AOT_SECTION_TYPE_TEXT);
  1530. EMIT_U32(section_size);
  1531. EMIT_U32(obj_data->literal_size);
  1532. if (obj_data->literal_size > 0) {
  1533. EMIT_BUF(obj_data->literal, obj_data->literal_size);
  1534. while (offset & 3)
  1535. EMIT_BUF(&placeholder, 1);
  1536. }
  1537. text = buf + offset;
  1538. if (obj_data->text_size > 0) {
  1539. EMIT_BUF(obj_data->text, obj_data->text_size);
  1540. while (offset & 3)
  1541. EMIT_BUF(&placeholder, 1);
  1542. }
  1543. if (obj_data->text_unlikely_size > 0) {
  1544. EMIT_BUF(obj_data->text_unlikely, obj_data->text_unlikely_size);
  1545. while (offset & 3)
  1546. EMIT_BUF(&placeholder, 1);
  1547. }
  1548. if (obj_data->text_hot_size > 0) {
  1549. EMIT_BUF(obj_data->text_hot, obj_data->text_hot_size);
  1550. while (offset & 3)
  1551. EMIT_BUF(&placeholder, 1);
  1552. }
  1553. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1554. aot_set_last_error("emit text section failed.");
  1555. return false;
  1556. }
  1557. /* apply relocations to aot_func_internal#n in text section for
  1558. windows platform */
  1559. if ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
  1560. /* Windows AOT_COFF64_BIN_TYPE */
  1561. && obj_data->target_info.bin_type == 6)
  1562. || (!strncmp(obj_data->comp_ctx->target_arch, "i386", 4)
  1563. /* Windows AOT_COFF32_BIN_TYPE */
  1564. && obj_data->target_info.bin_type == 4)) {
  1565. relocation_group = obj_data->relocation_groups;
  1566. for (i = 0; i < obj_data->relocation_group_count;
  1567. i++, relocation_group++) {
  1568. /* relocation in text section */
  1569. if ((!strcmp(relocation_group->section_name, ".text")
  1570. || !strcmp(relocation_group->section_name, ".ltext"))) {
  1571. relocation = relocation_group->relocations;
  1572. relocation_count = relocation_group->relocation_count;
  1573. for (j = 0; j < relocation_count; j++) {
  1574. /* relocation to aot_func_internal#n */
  1575. if (str_starts_with(relocation->symbol_name,
  1576. AOT_FUNC_INTERNAL_PREFIX)
  1577. && ((obj_data->target_info.bin_type
  1578. == 6 /* AOT_COFF64_BIN_TYPE */
  1579. && relocation->relocation_type
  1580. == 4 /* IMAGE_REL_AMD64_REL32 */)
  1581. || (obj_data->target_info.bin_type
  1582. == 4 /* AOT_COFF32_BIN_TYPE */
  1583. && relocation->relocation_type
  1584. == 20 /* IMAGE_REL_I386_REL32 */))) {
  1585. uint32 func_idx =
  1586. atoi(relocation->symbol_name
  1587. + strlen(AOT_FUNC_INTERNAL_PREFIX));
  1588. uint64 text_offset, reloc_offset, reloc_addend;
  1589. bh_assert(func_idx < obj_data->func_count);
  1590. text_offset = obj_data->funcs[func_idx]
  1591. .text_offset_of_aot_func_internal;
  1592. reloc_offset = relocation->relocation_offset;
  1593. reloc_addend = relocation->relocation_addend;
  1594. /* S + A - P */
  1595. *(uint32 *)(text + reloc_offset) =
  1596. (uint32)(text_offset + reloc_addend - reloc_offset
  1597. - 4);
  1598. /* remove current relocation as it has been applied */
  1599. if (j < relocation_count - 1) {
  1600. uint32 move_size =
  1601. (uint32)(sizeof(AOTRelocation)
  1602. * (relocation_count - 1 - j));
  1603. bh_memmove_s(relocation, move_size, relocation + 1,
  1604. move_size);
  1605. }
  1606. relocation_group->relocation_count--;
  1607. }
  1608. else {
  1609. relocation++;
  1610. }
  1611. }
  1612. }
  1613. }
  1614. }
  1615. *p_offset = offset;
  1616. return true;
  1617. }
  1618. static bool
  1619. aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1620. AOTCompData *comp_data, AOTObjectData *obj_data)
  1621. {
  1622. uint32 section_size = get_func_section_size(comp_data, obj_data);
  1623. uint32 i, offset = *p_offset;
  1624. AOTObjectFunc *func = obj_data->funcs;
  1625. AOTFunc **funcs = comp_data->funcs;
  1626. *p_offset = offset = align_uint(offset, 4);
  1627. EMIT_U32(AOT_SECTION_TYPE_FUNCTION);
  1628. EMIT_U32(section_size);
  1629. for (i = 0; i < obj_data->func_count; i++, func++) {
  1630. if (is_32bit_binary(obj_data))
  1631. EMIT_U32(func->text_offset);
  1632. else
  1633. EMIT_U64(func->text_offset);
  1634. }
  1635. for (i = 0; i < comp_data->func_count; i++)
  1636. EMIT_U32(funcs[i]->func_type_index);
  1637. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1638. aot_set_last_error("emit function section failed.");
  1639. return false;
  1640. }
  1641. *p_offset = offset;
  1642. return true;
  1643. }
  1644. static bool
  1645. aot_emit_export_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1646. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1647. AOTObjectData *obj_data)
  1648. {
  1649. uint32 section_size = get_export_section_size(comp_ctx, comp_data);
  1650. AOTExport *export = comp_data->wasm_module->exports;
  1651. uint32 export_count = comp_data->wasm_module->export_count;
  1652. uint32 i, offset = *p_offset;
  1653. *p_offset = offset = align_uint(offset, 4);
  1654. EMIT_U32(AOT_SECTION_TYPE_EXPORT);
  1655. EMIT_U32(section_size);
  1656. EMIT_U32(export_count);
  1657. for (i = 0; i < export_count; i++, export ++) {
  1658. offset = align_uint(offset, 4);
  1659. EMIT_U32(export->index);
  1660. EMIT_U8(export->kind);
  1661. EMIT_U8(0);
  1662. EMIT_STR(export->name);
  1663. }
  1664. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1665. aot_set_last_error("emit export section failed.");
  1666. return false;
  1667. }
  1668. *p_offset = offset;
  1669. return true;
  1670. }
  1671. static bool
  1672. aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1673. AOTCompContext *comp_ctx,
  1674. AOTCompData *comp_data,
  1675. AOTObjectData *obj_data)
  1676. {
  1677. uint32 symbol_offset = 0, total_string_len = 0;
  1678. uint32 offset = *p_offset;
  1679. AOTSymbolNode *sym;
  1680. EMIT_U32(obj_data->symbol_list.len);
  1681. /* emit symbol offsets */
  1682. sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
  1683. while (sym) {
  1684. EMIT_U32(symbol_offset);
  1685. /* string_len + str[0 .. string_len - 1] */
  1686. symbol_offset += get_string_size(comp_ctx, sym->symbol);
  1687. symbol_offset = align_uint(symbol_offset, 2);
  1688. sym = sym->next;
  1689. }
  1690. /* emit total string len */
  1691. total_string_len = symbol_offset;
  1692. EMIT_U32(total_string_len);
  1693. /* emit symbols */
  1694. sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
  1695. while (sym) {
  1696. EMIT_STR(sym->symbol);
  1697. offset = align_uint(offset, 2);
  1698. sym = sym->next;
  1699. }
  1700. *p_offset = offset;
  1701. return true;
  1702. }
  1703. static bool
  1704. aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1705. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1706. AOTObjectData *obj_data)
  1707. {
  1708. uint32 section_size = get_relocation_section_size(comp_ctx, obj_data);
  1709. uint32 i, offset = *p_offset;
  1710. AOTRelocationGroup *relocation_group = obj_data->relocation_groups;
  1711. if (section_size == (uint32)-1)
  1712. return false;
  1713. *p_offset = offset = align_uint(offset, 4);
  1714. EMIT_U32(AOT_SECTION_TYPE_RELOCATION);
  1715. EMIT_U32(section_size);
  1716. aot_emit_relocation_symbol_table(buf, buf_end, &offset, comp_ctx, comp_data,
  1717. obj_data);
  1718. offset = align_uint(offset, 4);
  1719. EMIT_U32(obj_data->relocation_group_count);
  1720. /* emit each relocation group */
  1721. for (i = 0; i < obj_data->relocation_group_count; i++, relocation_group++) {
  1722. AOTRelocation *relocation = relocation_group->relocations;
  1723. uint32 j;
  1724. offset = align_uint(offset, 4);
  1725. EMIT_U32(relocation_group->name_index);
  1726. offset = align_uint(offset, 4);
  1727. EMIT_U32(relocation_group->relocation_count);
  1728. /* emit each relocation */
  1729. for (j = 0; j < relocation_group->relocation_count; j++, relocation++) {
  1730. offset = align_uint(offset, 4);
  1731. if (is_32bit_binary(obj_data)) {
  1732. EMIT_U32(relocation->relocation_offset);
  1733. EMIT_U32(relocation->relocation_addend);
  1734. }
  1735. else {
  1736. EMIT_U64(relocation->relocation_offset);
  1737. EMIT_U64(relocation->relocation_addend);
  1738. }
  1739. EMIT_U32(relocation->relocation_type);
  1740. EMIT_U32(relocation->symbol_index);
  1741. }
  1742. }
  1743. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1744. aot_set_last_error("emit relocation section failed.");
  1745. return false;
  1746. }
  1747. *p_offset = offset;
  1748. return true;
  1749. }
  1750. static bool
  1751. aot_emit_native_symbol(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1752. AOTCompContext *comp_ctx)
  1753. {
  1754. uint32 offset = *p_offset;
  1755. AOTNativeSymbol *sym = NULL;
  1756. if (bh_list_length(&comp_ctx->native_symbols) == 0)
  1757. /* emit only when there are native symbols */
  1758. return true;
  1759. *p_offset = offset = align_uint(offset, 4);
  1760. EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
  1761. /* sub section id + symbol count + symbol list */
  1762. EMIT_U32(sizeof(uint32) * 2 + get_native_symbol_list_size(comp_ctx));
  1763. EMIT_U32(AOT_CUSTOM_SECTION_NATIVE_SYMBOL);
  1764. EMIT_U32(bh_list_length(&comp_ctx->native_symbols));
  1765. sym = bh_list_first_elem(&comp_ctx->native_symbols);
  1766. while (sym) {
  1767. offset = align_uint(offset, 2);
  1768. EMIT_STR(sym->symbol);
  1769. sym = bh_list_elem_next(sym);
  1770. }
  1771. *p_offset = offset;
  1772. return true;
  1773. }
  1774. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  1775. static bool
  1776. aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1777. AOTCompData *comp_data, AOTCompContext *comp_ctx)
  1778. {
  1779. uint32 offset = *p_offset;
  1780. if (comp_data->aot_name_section_size == 0)
  1781. return true;
  1782. offset = align_uint(offset, 4);
  1783. EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
  1784. /* sub section id + name section size */
  1785. EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size);
  1786. EMIT_U32(AOT_CUSTOM_SECTION_NAME);
  1787. bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
  1788. comp_data->aot_name_section_buf,
  1789. (uint32)comp_data->aot_name_section_size);
  1790. offset += comp_data->aot_name_section_size;
  1791. *p_offset = offset;
  1792. LOG_DEBUG("emit name section");
  1793. return true;
  1794. }
  1795. #endif
  1796. static bool
  1797. aot_emit_custom_sections(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1798. AOTCompData *comp_data, AOTCompContext *comp_ctx)
  1799. {
  1800. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  1801. uint32 offset = *p_offset, i;
  1802. for (i = 0; i < comp_ctx->custom_sections_count; i++) {
  1803. const char *section_name = comp_ctx->custom_sections_wp[i];
  1804. const uint8 *content = NULL;
  1805. uint32 length = 0;
  1806. if (strcmp(section_name, "name") == 0) {
  1807. *p_offset = offset;
  1808. if (!aot_emit_name_section(buf, buf_end, p_offset, comp_data,
  1809. comp_ctx))
  1810. return false;
  1811. offset = *p_offset;
  1812. continue;
  1813. }
  1814. content = wasm_loader_get_custom_section(comp_data->wasm_module,
  1815. section_name, &length);
  1816. if (!content) {
  1817. /* Warning has been reported during calculating size */
  1818. continue;
  1819. }
  1820. offset = align_uint(offset, 4);
  1821. EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
  1822. /* sub section id + content */
  1823. EMIT_U32(sizeof(uint32) * 1 + get_string_size(comp_ctx, section_name)
  1824. + length);
  1825. EMIT_U32(AOT_CUSTOM_SECTION_RAW);
  1826. EMIT_STR(section_name);
  1827. bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf), content,
  1828. length);
  1829. offset += length;
  1830. }
  1831. *p_offset = offset;
  1832. #endif
  1833. return true;
  1834. }
  1835. typedef uint32 U32;
  1836. typedef int32 I32;
  1837. typedef uint16 U16;
  1838. typedef uint8 U8;
  1839. struct coff_hdr {
  1840. U16 u16Machine;
  1841. U16 u16NumSections;
  1842. U32 u32DateTimeStamp;
  1843. U32 u32SymTblPtr;
  1844. U32 u32NumSymbols;
  1845. U16 u16PeHdrSize;
  1846. U16 u16Characs;
  1847. };
  1848. #define E_TYPE_REL 1
  1849. #define E_TYPE_XIP 4
  1850. #define IMAGE_FILE_MACHINE_AMD64 0x8664
  1851. #define IMAGE_FILE_MACHINE_I386 0x014c
  1852. #define IMAGE_FILE_MACHINE_IA64 0x0200
  1853. #define AOT_COFF32_BIN_TYPE 4 /* 32-bit little endian */
  1854. #define AOT_COFF64_BIN_TYPE 6 /* 64-bit little endian */
  1855. #define EI_NIDENT 16
  1856. typedef uint32 elf32_word;
  1857. typedef int32 elf32_sword;
  1858. typedef uint16 elf32_half;
  1859. typedef uint32 elf32_off;
  1860. typedef uint32 elf32_addr;
  1861. struct elf32_ehdr {
  1862. unsigned char e_ident[EI_NIDENT]; /* ident bytes */
  1863. elf32_half e_type; /* file type */
  1864. elf32_half e_machine; /* target machine */
  1865. elf32_word e_version; /* file version */
  1866. elf32_addr e_entry; /* start address */
  1867. elf32_off e_phoff; /* phdr file offset */
  1868. elf32_off e_shoff; /* shdr file offset */
  1869. elf32_word e_flags; /* file flags */
  1870. elf32_half e_ehsize; /* sizeof ehdr */
  1871. elf32_half e_phentsize; /* sizeof phdr */
  1872. elf32_half e_phnum; /* number phdrs */
  1873. elf32_half e_shentsize; /* sizeof shdr */
  1874. elf32_half e_shnum; /* number shdrs */
  1875. elf32_half e_shstrndx; /* shdr string index */
  1876. };
  1877. struct elf32_rel {
  1878. elf32_addr r_offset;
  1879. elf32_word r_info;
  1880. } elf32_rel;
  1881. struct elf32_rela {
  1882. elf32_addr r_offset;
  1883. elf32_word r_info;
  1884. elf32_sword r_addend;
  1885. } elf32_rela;
  1886. typedef uint32 elf64_word;
  1887. typedef int32 elf64_sword;
  1888. typedef uint64 elf64_xword;
  1889. typedef int64 elf64_sxword;
  1890. typedef uint16 elf64_half;
  1891. typedef uint64 elf64_off;
  1892. typedef uint64 elf64_addr;
  1893. struct elf64_ehdr {
  1894. unsigned char e_ident[EI_NIDENT]; /* ident bytes */
  1895. elf64_half e_type; /* file type */
  1896. elf64_half e_machine; /* target machine */
  1897. elf64_word e_version; /* file version */
  1898. elf64_addr e_entry; /* start address */
  1899. elf64_off e_phoff; /* phdr file offset */
  1900. elf64_off e_shoff; /* shdr file offset */
  1901. elf64_word e_flags; /* file flags */
  1902. elf64_half e_ehsize; /* sizeof ehdr */
  1903. elf64_half e_phentsize; /* sizeof phdr */
  1904. elf64_half e_phnum; /* number phdrs */
  1905. elf64_half e_shentsize; /* sizeof shdr */
  1906. elf64_half e_shnum; /* number shdrs */
  1907. elf64_half e_shstrndx; /* shdr string index */
  1908. };
  1909. typedef struct elf64_rel {
  1910. elf64_addr r_offset;
  1911. elf64_xword r_info;
  1912. } elf64_rel;
  1913. typedef struct elf64_rela {
  1914. elf64_addr r_offset;
  1915. elf64_xword r_info;
  1916. elf64_sxword r_addend;
  1917. } elf64_rela;
  1918. #define SET_TARGET_INFO(f, v, type, little) \
  1919. do { \
  1920. type tmp = elf_header->v; \
  1921. if ((little && !is_little_endian()) \
  1922. || (!little && is_little_endian())) \
  1923. exchange_##type((uint8 *)&tmp); \
  1924. obj_data->target_info.f = tmp; \
  1925. } while (0)
  1926. static bool
  1927. aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
  1928. {
  1929. LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
  1930. const uint8 *elf_buf = (uint8 *)LLVMGetBufferStart(obj_data->mem_buf);
  1931. uint32 elf_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
  1932. if (bin_type != LLVMBinaryTypeCOFF && bin_type != LLVMBinaryTypeELF32L
  1933. && bin_type != LLVMBinaryTypeELF32B && bin_type != LLVMBinaryTypeELF64L
  1934. && bin_type != LLVMBinaryTypeELF64B
  1935. && bin_type != LLVMBinaryTypeMachO32L
  1936. && bin_type != LLVMBinaryTypeMachO32B
  1937. && bin_type != LLVMBinaryTypeMachO64L
  1938. && bin_type != LLVMBinaryTypeMachO64B) {
  1939. aot_set_last_error("invaid llvm binary bin_type.");
  1940. return false;
  1941. }
  1942. obj_data->target_info.bin_type = bin_type - LLVMBinaryTypeELF32L;
  1943. if (bin_type == LLVMBinaryTypeCOFF) {
  1944. struct coff_hdr *coff_header;
  1945. if (!elf_buf || elf_size < sizeof(struct coff_hdr)) {
  1946. aot_set_last_error("invalid coff_hdr buffer.");
  1947. return false;
  1948. }
  1949. coff_header = (struct coff_hdr *)elf_buf;
  1950. /* Emit eXecute In Place file type while in indirect mode */
  1951. if (comp_ctx->is_indirect_mode)
  1952. obj_data->target_info.e_type = E_TYPE_XIP;
  1953. else
  1954. obj_data->target_info.e_type = E_TYPE_REL;
  1955. obj_data->target_info.e_machine = coff_header->u16Machine;
  1956. obj_data->target_info.e_version = 1;
  1957. obj_data->target_info.e_flags = 0;
  1958. if (coff_header->u16Machine == IMAGE_FILE_MACHINE_AMD64
  1959. || coff_header->u16Machine == IMAGE_FILE_MACHINE_IA64)
  1960. obj_data->target_info.bin_type = AOT_COFF64_BIN_TYPE;
  1961. else if (coff_header->u16Machine == IMAGE_FILE_MACHINE_I386)
  1962. obj_data->target_info.bin_type = AOT_COFF32_BIN_TYPE;
  1963. }
  1964. else if (bin_type == LLVMBinaryTypeELF32L
  1965. || bin_type == LLVMBinaryTypeELF32B) {
  1966. struct elf32_ehdr *elf_header;
  1967. bool is_little_bin = bin_type == LLVMBinaryTypeELF32L;
  1968. if (!elf_buf || elf_size < sizeof(struct elf32_ehdr)) {
  1969. aot_set_last_error("invalid elf32 buffer.");
  1970. return false;
  1971. }
  1972. elf_header = (struct elf32_ehdr *)elf_buf;
  1973. /* Emit eXecute In Place file type while in indirect mode */
  1974. if (comp_ctx->is_indirect_mode)
  1975. elf_header->e_type = E_TYPE_XIP;
  1976. SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
  1977. SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
  1978. SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
  1979. SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
  1980. }
  1981. else if (bin_type == LLVMBinaryTypeELF64L
  1982. || bin_type == LLVMBinaryTypeELF64B) {
  1983. struct elf64_ehdr *elf_header;
  1984. bool is_little_bin = bin_type == LLVMBinaryTypeELF64L;
  1985. if (!elf_buf || elf_size < sizeof(struct elf64_ehdr)) {
  1986. aot_set_last_error("invalid elf64 buffer.");
  1987. return false;
  1988. }
  1989. elf_header = (struct elf64_ehdr *)elf_buf;
  1990. /* Emit eXecute In Place file type while in indirect mode */
  1991. if (comp_ctx->is_indirect_mode)
  1992. elf_header->e_type = E_TYPE_XIP;
  1993. SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
  1994. SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
  1995. SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
  1996. SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
  1997. }
  1998. else if (bin_type == LLVMBinaryTypeMachO32L
  1999. || bin_type == LLVMBinaryTypeMachO32B) {
  2000. /* TODO: parse file type of Mach-O 32 */
  2001. aot_set_last_error("invaid llvm binary bin_type.");
  2002. return false;
  2003. }
  2004. else if (bin_type == LLVMBinaryTypeMachO64L
  2005. || bin_type == LLVMBinaryTypeMachO64B) {
  2006. /* TODO: parse file type of Mach-O 64 */
  2007. aot_set_last_error("invaid llvm binary bin_type.");
  2008. return false;
  2009. }
  2010. bh_assert(sizeof(obj_data->target_info.arch)
  2011. == sizeof(comp_ctx->target_arch));
  2012. bh_memcpy_s(obj_data->target_info.arch, sizeof(obj_data->target_info.arch),
  2013. comp_ctx->target_arch, sizeof(comp_ctx->target_arch));
  2014. return true;
  2015. }
  2016. static bool
  2017. aot_resolve_text(AOTObjectData *obj_data)
  2018. {
  2019. #if WASM_ENABLE_DEBUG_AOT != 0
  2020. LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
  2021. if (bin_type == LLVMBinaryTypeELF32L || bin_type == LLVMBinaryTypeELF64L) {
  2022. obj_data->text = (char *)LLVMGetBufferStart(obj_data->mem_buf);
  2023. obj_data->text_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
  2024. }
  2025. else
  2026. #endif
  2027. {
  2028. LLVMSectionIteratorRef sec_itr;
  2029. char *name;
  2030. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2031. aot_set_last_error("llvm get section iterator failed.");
  2032. return false;
  2033. }
  2034. while (
  2035. !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  2036. if ((name = (char *)LLVMGetSectionName(sec_itr))) {
  2037. if (!strcmp(name, ".text") || !strcmp(name, ".ltext")) {
  2038. obj_data->text = (char *)LLVMGetSectionContents(sec_itr);
  2039. obj_data->text_size = (uint32)LLVMGetSectionSize(sec_itr);
  2040. }
  2041. else if (!strcmp(name, ".text.unlikely.")
  2042. || !strcmp(name, ".ltext.unlikely.")) {
  2043. obj_data->text_unlikely =
  2044. (char *)LLVMGetSectionContents(sec_itr);
  2045. obj_data->text_unlikely_size =
  2046. (uint32)LLVMGetSectionSize(sec_itr);
  2047. }
  2048. else if (!strcmp(name, ".text.hot.")
  2049. || !strcmp(name, ".ltext.hot.")) {
  2050. obj_data->text_hot =
  2051. (char *)LLVMGetSectionContents(sec_itr);
  2052. obj_data->text_hot_size =
  2053. (uint32)LLVMGetSectionSize(sec_itr);
  2054. }
  2055. }
  2056. LLVMMoveToNextSection(sec_itr);
  2057. }
  2058. LLVMDisposeSectionIterator(sec_itr);
  2059. }
  2060. return true;
  2061. }
  2062. static bool
  2063. aot_resolve_literal(AOTObjectData *obj_data)
  2064. {
  2065. LLVMSectionIteratorRef sec_itr;
  2066. char *name;
  2067. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2068. aot_set_last_error("llvm get section iterator failed.");
  2069. return false;
  2070. }
  2071. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  2072. if ((name = (char *)LLVMGetSectionName(sec_itr))
  2073. && !strcmp(name, ".literal")) {
  2074. obj_data->literal = (char *)LLVMGetSectionContents(sec_itr);
  2075. obj_data->literal_size = (uint32)LLVMGetSectionSize(sec_itr);
  2076. break;
  2077. }
  2078. LLVMMoveToNextSection(sec_itr);
  2079. }
  2080. LLVMDisposeSectionIterator(sec_itr);
  2081. return true;
  2082. }
  2083. static bool
  2084. get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count);
  2085. static bool
  2086. is_data_section(AOTObjectData *obj_data, LLVMSectionIteratorRef sec_itr,
  2087. char *section_name)
  2088. {
  2089. uint32 relocation_count = 0;
  2090. return (!strcmp(section_name, ".data") || !strcmp(section_name, ".sdata")
  2091. || !strcmp(section_name, ".rodata")
  2092. /* ".rodata.cst4/8/16/.." */
  2093. || !strncmp(section_name, ".rodata.cst", strlen(".rodata.cst"))
  2094. /* ".rodata.strn.m" */
  2095. || !strncmp(section_name, ".rodata.str", strlen(".rodata.str"))
  2096. || (!strcmp(section_name, ".rdata")
  2097. && get_relocations_count(sec_itr, &relocation_count)
  2098. && relocation_count > 0)
  2099. || !strcmp(section_name, aot_stack_sizes_section_name)
  2100. || (obj_data->comp_ctx->enable_llvm_pgo
  2101. && (!strncmp(section_name, "__llvm_prf_cnts", 15)
  2102. || !strncmp(section_name, "__llvm_prf_data", 15)
  2103. || !strncmp(section_name, "__llvm_prf_names", 16))));
  2104. }
  2105. static bool
  2106. get_object_data_sections_count(AOTObjectData *obj_data, uint32 *p_count)
  2107. {
  2108. LLVMSectionIteratorRef sec_itr;
  2109. char *name;
  2110. uint32 count = 0;
  2111. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2112. aot_set_last_error("llvm get section iterator failed.");
  2113. return false;
  2114. }
  2115. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  2116. if ((name = (char *)LLVMGetSectionName(sec_itr))
  2117. && (is_data_section(obj_data, sec_itr, name))) {
  2118. count++;
  2119. }
  2120. LLVMMoveToNextSection(sec_itr);
  2121. }
  2122. LLVMDisposeSectionIterator(sec_itr);
  2123. *p_count = count;
  2124. return true;
  2125. }
  2126. static bool
  2127. aot_resolve_object_data_sections(AOTObjectData *obj_data)
  2128. {
  2129. LLVMSectionIteratorRef sec_itr;
  2130. char *name;
  2131. AOTObjectDataSection *data_section;
  2132. uint32 sections_count;
  2133. uint32 size;
  2134. if (!get_object_data_sections_count(obj_data, &sections_count)) {
  2135. return false;
  2136. }
  2137. if (sections_count > 0) {
  2138. uint32 llvm_prf_cnts_idx = 0, llvm_prf_data_idx = 0;
  2139. char buf[32];
  2140. size = (uint32)sizeof(AOTObjectDataSection) * sections_count;
  2141. if (!(data_section = obj_data->data_sections =
  2142. wasm_runtime_malloc(size))) {
  2143. aot_set_last_error("allocate memory for data sections failed.");
  2144. return false;
  2145. }
  2146. memset(obj_data->data_sections, 0, size);
  2147. obj_data->data_sections_count = sections_count;
  2148. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2149. aot_set_last_error("llvm get section iterator failed.");
  2150. return false;
  2151. }
  2152. while (
  2153. !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  2154. if ((name = (char *)LLVMGetSectionName(sec_itr))
  2155. && (is_data_section(obj_data, sec_itr, name))) {
  2156. data_section->name = name;
  2157. if (obj_data->comp_ctx->enable_llvm_pgo
  2158. && !strcmp(name, "__llvm_prf_cnts")) {
  2159. snprintf(buf, sizeof(buf), "%s%u", name,
  2160. llvm_prf_cnts_idx++);
  2161. size = (uint32)(strlen(buf) + 1);
  2162. if (!(data_section->name = wasm_runtime_malloc(size))) {
  2163. aot_set_last_error(
  2164. "allocate memory for data section name failed.");
  2165. return false;
  2166. }
  2167. bh_memcpy_s(data_section->name, size, buf, size);
  2168. data_section->is_name_allocated = true;
  2169. }
  2170. else if (obj_data->comp_ctx->enable_llvm_pgo
  2171. && !strcmp(name, "__llvm_prf_data")) {
  2172. snprintf(buf, sizeof(buf), "%s%u", name,
  2173. llvm_prf_data_idx++);
  2174. size = (uint32)(strlen(buf) + 1);
  2175. if (!(data_section->name = wasm_runtime_malloc(size))) {
  2176. aot_set_last_error(
  2177. "allocate memory for data section name failed.");
  2178. return false;
  2179. }
  2180. bh_memcpy_s(data_section->name, size, buf, size);
  2181. data_section->is_name_allocated = true;
  2182. }
  2183. if (obj_data->comp_ctx->enable_llvm_pgo
  2184. && !strcmp(name, "__llvm_prf_names")) {
  2185. data_section->data = (uint8 *)aot_compress_aot_func_names(
  2186. obj_data->comp_ctx, &data_section->size);
  2187. data_section->is_data_allocated = true;
  2188. }
  2189. else {
  2190. data_section->data =
  2191. (uint8 *)LLVMGetSectionContents(sec_itr);
  2192. data_section->size = (uint32)LLVMGetSectionSize(sec_itr);
  2193. }
  2194. data_section++;
  2195. }
  2196. LLVMMoveToNextSection(sec_itr);
  2197. }
  2198. LLVMDisposeSectionIterator(sec_itr);
  2199. }
  2200. return true;
  2201. }
  2202. static bool
  2203. read_stack_usage_file(const AOTCompContext *comp_ctx, const char *filename,
  2204. uint32 *sizes, uint32 count)
  2205. {
  2206. FILE *fp = NULL;
  2207. if (filename == NULL) {
  2208. aot_set_last_error("no stack usage file is specified.");
  2209. return false;
  2210. }
  2211. fp = fopen(filename, "r");
  2212. if (fp == NULL) {
  2213. LOG_ERROR("failed to open stack usage file: %s", filename);
  2214. goto fail;
  2215. }
  2216. /*
  2217. * the file consists of lines like:
  2218. *
  2219. * WASM Module:aot_func#9 72 static
  2220. */
  2221. const char *aot_func_prefix = AOT_FUNC_PREFIX;
  2222. const char *aot_func_internal_prefix = AOT_FUNC_INTERNAL_PREFIX;
  2223. uint32 precheck_found = 0;
  2224. uint32 precheck_stack_size_max = 0;
  2225. uint32 precheck_stack_size_min = UINT32_MAX;
  2226. uint32 found = 0;
  2227. while (true) {
  2228. const char *prefix;
  2229. char line[100];
  2230. char *cp = fgets(line, sizeof(line), fp);
  2231. char *fn;
  2232. char *colon;
  2233. uintmax_t func_idx;
  2234. uintmax_t sz;
  2235. int ret;
  2236. if (cp == NULL) {
  2237. break;
  2238. }
  2239. /*
  2240. * Note: strrchr (not strchr) because a module name can contain
  2241. * colons.
  2242. */
  2243. colon = strrchr(cp, ':');
  2244. if (colon == NULL) {
  2245. goto fail;
  2246. }
  2247. fn = strstr(colon, aot_func_prefix);
  2248. if (fn != NULL) {
  2249. prefix = aot_func_prefix;
  2250. }
  2251. else {
  2252. fn = strstr(colon, aot_func_internal_prefix);
  2253. if (fn == NULL) {
  2254. LOG_ERROR("failed to parse stack usage line: %s", cp);
  2255. goto fail;
  2256. }
  2257. prefix = aot_func_internal_prefix;
  2258. }
  2259. ret = sscanf(fn + strlen(prefix), "%ju %ju static", &func_idx, &sz);
  2260. if (ret != 2) {
  2261. goto fail;
  2262. }
  2263. if (sz > UINT32_MAX) {
  2264. goto fail;
  2265. }
  2266. if (func_idx > UINT32_MAX) {
  2267. goto fail;
  2268. }
  2269. if (func_idx >= count) {
  2270. goto fail;
  2271. }
  2272. if (prefix == aot_func_prefix) {
  2273. if (sz < precheck_stack_size_min) {
  2274. precheck_stack_size_min = (uint32)sz;
  2275. }
  2276. if (sz > precheck_stack_size_max) {
  2277. precheck_stack_size_max = (uint32)sz;
  2278. }
  2279. precheck_found++;
  2280. continue;
  2281. }
  2282. sizes[func_idx] = (uint32)sz;
  2283. found++;
  2284. }
  2285. fclose(fp);
  2286. if (precheck_found != count) {
  2287. LOG_ERROR("%" PRIu32 " precheck entries found while %" PRIu32
  2288. " entries are expected",
  2289. precheck_found, count);
  2290. return false;
  2291. }
  2292. if (found != count) {
  2293. /*
  2294. * LLVM seems to eliminate calls to an empty function
  2295. * (and eliminate the function) even if it's marked noinline.
  2296. */
  2297. LOG_VERBOSE("%" PRIu32 " entries found while %" PRIu32
  2298. " entries are expected. Maybe LLVM optimization eliminated "
  2299. "some functions.",
  2300. found, count);
  2301. }
  2302. if (precheck_stack_size_min != precheck_stack_size_max) {
  2303. /*
  2304. * Note: this is too strict.
  2305. *
  2306. * actually, the stack consumption of the precheck functions
  2307. * can depend on the type of them.
  2308. * that is, depending on various factors including
  2309. * calling conventions and compilers, a function with many
  2310. * parameters can consume more stack, even if it merely does
  2311. * a tail-call to another function.
  2312. */
  2313. bool musttail = aot_target_precheck_can_use_musttail(comp_ctx);
  2314. if (musttail) {
  2315. LOG_WARNING(
  2316. "precheck functions use variable amount of stack. (%" PRIu32
  2317. " - %" PRIu32 ")",
  2318. precheck_stack_size_min, precheck_stack_size_max);
  2319. }
  2320. else {
  2321. LOG_VERBOSE("precheck functions use %" PRIu32 " - %" PRIu32
  2322. " bytes of stack.",
  2323. precheck_stack_size_min, precheck_stack_size_max);
  2324. }
  2325. }
  2326. else {
  2327. LOG_VERBOSE("precheck functions use %" PRIu32 " bytes of stack.",
  2328. precheck_stack_size_max);
  2329. }
  2330. if (precheck_stack_size_max >= 1024) {
  2331. LOG_WARNING("precheck functions themselves consume relatively large "
  2332. "amount of stack (%" PRIu32
  2333. "). Please ensure the runtime has large enough "
  2334. "WASM_STACK_GUARD_SIZE.",
  2335. precheck_stack_size_max);
  2336. }
  2337. return true;
  2338. fail:
  2339. if (fp != NULL)
  2340. fclose(fp);
  2341. aot_set_last_error("failed to read stack usage file.");
  2342. return false;
  2343. }
  2344. static bool
  2345. aot_resolve_stack_sizes(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
  2346. {
  2347. LLVMSectionIteratorRef sec_itr = NULL;
  2348. LLVMSymbolIteratorRef sym_itr;
  2349. const char *name;
  2350. if (!(sym_itr = LLVMObjectFileCopySymbolIterator(obj_data->binary))) {
  2351. aot_set_last_error("llvm get symbol iterator failed.");
  2352. return false;
  2353. }
  2354. while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
  2355. if ((name = LLVMGetSymbolName(sym_itr))
  2356. && (!strcmp(name, aot_stack_sizes_alias_name)
  2357. /* symbol of COFF32 starts with "_" */
  2358. || (obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
  2359. && !strncmp(name, "_", 1)
  2360. && !strcmp(name + 1, aot_stack_sizes_alias_name)))) {
  2361. #if 0 /* cf. https://github.com/llvm/llvm-project/issues/67765 */
  2362. uint64 sz = LLVMGetSymbolSize(sym_itr);
  2363. if (sz != sizeof(uint32) * obj_data->func_count
  2364. /* sz of COFF64/COFF32 is 0, ignore the check */
  2365. && obj_data->target_info.bin_type != AOT_COFF64_BIN_TYPE
  2366. && obj_data->target_info.bin_type != AOT_COFF32_BIN_TYPE) {
  2367. aot_set_last_error("stack_sizes had unexpected size.");
  2368. goto fail;
  2369. }
  2370. #endif
  2371. uint64 addr = LLVMGetSymbolAddress(sym_itr);
  2372. if (!(sec_itr =
  2373. LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2374. aot_set_last_error("llvm get section iterator failed.");
  2375. goto fail;
  2376. }
  2377. LLVMMoveToContainingSection(sec_itr, sym_itr);
  2378. const char *sec_name = LLVMGetSectionName(sec_itr);
  2379. LOG_VERBOSE("stack_sizes found in section %s offset %" PRIu64 ".",
  2380. sec_name, addr);
  2381. if (strcmp(sec_name, aot_stack_sizes_section_name) || addr != 0) {
  2382. aot_set_last_error(
  2383. "stack_sizes found at an unexpected location.");
  2384. goto fail;
  2385. }
  2386. /*
  2387. * Note: We can't always modify stack_sizes in-place.
  2388. * Eg. When WAMRC_LLC_COMPILER is used, LLVM sometimes uses
  2389. * read-only mmap of the temporary file to back
  2390. * LLVMGetSectionContents.
  2391. */
  2392. const uint32 *ro_stack_sizes =
  2393. (const uint32 *)(LLVMGetSectionContents(sec_itr) + addr);
  2394. uint32 i;
  2395. for (i = 0; i < obj_data->func_count; i++) {
  2396. /* Note: -1 == AOT_NEG_ONE from aot_create_stack_sizes */
  2397. if (ro_stack_sizes[i] != (uint32)-1) {
  2398. aot_set_last_error("unexpected data in stack_sizes.");
  2399. goto fail;
  2400. }
  2401. }
  2402. /*
  2403. * Record section/offset and construct a copy of stack_sizes.
  2404. * aot_emit_object_data_section_info will emit this copy.
  2405. */
  2406. obj_data->stack_sizes_section_name = sec_name;
  2407. obj_data->stack_sizes_offset = (uint32)addr;
  2408. obj_data->stack_sizes = wasm_runtime_malloc(
  2409. obj_data->func_count * sizeof(*obj_data->stack_sizes));
  2410. if (obj_data->stack_sizes == NULL) {
  2411. aot_set_last_error("failed to allocate memory.");
  2412. goto fail;
  2413. }
  2414. uint32 *stack_sizes = obj_data->stack_sizes;
  2415. for (i = 0; i < obj_data->func_count; i++) {
  2416. stack_sizes[i] = (uint32)-1;
  2417. }
  2418. if (!read_stack_usage_file(comp_ctx, comp_ctx->stack_usage_file,
  2419. stack_sizes, obj_data->func_count)) {
  2420. goto fail;
  2421. }
  2422. for (i = 0; i < obj_data->func_count; i++) {
  2423. const AOTFuncContext *func_ctx = comp_ctx->func_ctxes[i];
  2424. bool musttail = aot_target_precheck_can_use_musttail(comp_ctx);
  2425. unsigned int stack_consumption_to_call_wrapped_func =
  2426. musttail ? 0
  2427. : aot_estimate_stack_usage_for_function_call(
  2428. comp_ctx, func_ctx->aot_func->func_type);
  2429. /*
  2430. * LLVM seems to eliminate calls to an empty function
  2431. * (and eliminate the function) even if it's marked noinline.
  2432. *
  2433. * Note: -1 == AOT_NEG_ONE from aot_create_stack_sizes
  2434. */
  2435. if (stack_sizes[i] == (uint32)-1) {
  2436. if (func_ctx->stack_consumption_for_func_call != 0) {
  2437. /*
  2438. * This happens if a function calling another
  2439. * function has been optimized out.
  2440. *
  2441. * for example,
  2442. *
  2443. * (func $func
  2444. * (local i32)
  2445. * local.get 0
  2446. * if
  2447. * call $another
  2448. * end
  2449. * )
  2450. */
  2451. LOG_VERBOSE("AOT func#%" PRIu32
  2452. " had call(s) but eliminated?",
  2453. i);
  2454. }
  2455. else {
  2456. LOG_VERBOSE("AOT func#%" PRIu32 " eliminated?", i);
  2457. }
  2458. stack_sizes[i] = 0;
  2459. }
  2460. else {
  2461. LOG_VERBOSE("AOT func#%" PRIu32 " stack_size %u + %" PRIu32
  2462. " + %u",
  2463. i, stack_consumption_to_call_wrapped_func,
  2464. stack_sizes[i],
  2465. func_ctx->stack_consumption_for_func_call);
  2466. if (UINT32_MAX - stack_sizes[i]
  2467. < func_ctx->stack_consumption_for_func_call) {
  2468. aot_set_last_error("stack size overflow.");
  2469. goto fail;
  2470. }
  2471. stack_sizes[i] += func_ctx->stack_consumption_for_func_call;
  2472. if (UINT32_MAX - stack_sizes[i]
  2473. < stack_consumption_to_call_wrapped_func) {
  2474. aot_set_last_error("stack size overflow.");
  2475. goto fail;
  2476. }
  2477. stack_sizes[i] += stack_consumption_to_call_wrapped_func;
  2478. }
  2479. }
  2480. LLVMDisposeSectionIterator(sec_itr);
  2481. LLVMDisposeSymbolIterator(sym_itr);
  2482. return true;
  2483. }
  2484. LLVMMoveToNextSymbol(sym_itr);
  2485. }
  2486. aot_set_last_error("stack_sizes not found.");
  2487. fail:
  2488. if (sec_itr)
  2489. LLVMDisposeSectionIterator(sec_itr);
  2490. LLVMDisposeSymbolIterator(sym_itr);
  2491. return false;
  2492. }
  2493. static bool
  2494. aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
  2495. {
  2496. AOTObjectFunc *func;
  2497. LLVMSymbolIteratorRef sym_itr;
  2498. char *name, *prefix = AOT_FUNC_PREFIX;
  2499. uint32 func_index, total_size;
  2500. /* allocate memory for aot function */
  2501. obj_data->func_count = comp_ctx->comp_data->func_count;
  2502. if (obj_data->func_count) {
  2503. if ((comp_ctx->enable_stack_bound_check
  2504. || comp_ctx->enable_stack_estimation)
  2505. && !aot_resolve_stack_sizes(comp_ctx, obj_data))
  2506. return false;
  2507. total_size = (uint32)sizeof(AOTObjectFunc) * obj_data->func_count;
  2508. if (!(obj_data->funcs = wasm_runtime_malloc(total_size))) {
  2509. aot_set_last_error("allocate memory for functions failed.");
  2510. return false;
  2511. }
  2512. memset(obj_data->funcs, 0, total_size);
  2513. }
  2514. if (!(sym_itr = LLVMObjectFileCopySymbolIterator(obj_data->binary))) {
  2515. aot_set_last_error("llvm get symbol iterator failed.");
  2516. return false;
  2517. }
  2518. while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
  2519. if ((name = (char *)LLVMGetSymbolName(sym_itr))
  2520. && str_starts_with(name, prefix)) {
  2521. /* symbol aot_func#n */
  2522. func_index = (uint32)atoi(name + strlen(prefix));
  2523. if (func_index < obj_data->func_count) {
  2524. LLVMSectionIteratorRef contain_section;
  2525. char *contain_section_name;
  2526. func = obj_data->funcs + func_index;
  2527. func->func_name = name;
  2528. if (!(contain_section = LLVMObjectFileCopySectionIterator(
  2529. obj_data->binary))) {
  2530. aot_set_last_error("llvm get section iterator failed.");
  2531. LLVMDisposeSymbolIterator(sym_itr);
  2532. return false;
  2533. }
  2534. LLVMMoveToContainingSection(contain_section, sym_itr);
  2535. contain_section_name =
  2536. (char *)LLVMGetSectionName(contain_section);
  2537. LLVMDisposeSectionIterator(contain_section);
  2538. if (!strcmp(contain_section_name, ".text.unlikely.")
  2539. || !strcmp(contain_section_name, ".ltext.unlikely.")) {
  2540. func->text_offset = align_uint(obj_data->text_size, 4)
  2541. + LLVMGetSymbolAddress(sym_itr);
  2542. }
  2543. else if (!strcmp(contain_section_name, ".text.hot.")
  2544. || !strcmp(contain_section_name, ".ltext.hot.")) {
  2545. func->text_offset =
  2546. align_uint(obj_data->text_size, 4)
  2547. + align_uint(obj_data->text_unlikely_size, 4)
  2548. + LLVMGetSymbolAddress(sym_itr);
  2549. }
  2550. else {
  2551. func->text_offset = LLVMGetSymbolAddress(sym_itr);
  2552. }
  2553. }
  2554. }
  2555. else if ((name = (char *)LLVMGetSymbolName(sym_itr))
  2556. && str_starts_with(name, AOT_FUNC_INTERNAL_PREFIX)) {
  2557. /* symbol aot_func_internal#n */
  2558. func_index = (uint32)atoi(name + strlen(AOT_FUNC_INTERNAL_PREFIX));
  2559. if (func_index < obj_data->func_count) {
  2560. LLVMSectionIteratorRef contain_section;
  2561. char *contain_section_name;
  2562. func = obj_data->funcs + func_index;
  2563. if (!(contain_section = LLVMObjectFileCopySectionIterator(
  2564. obj_data->binary))) {
  2565. aot_set_last_error("llvm get section iterator failed.");
  2566. LLVMDisposeSymbolIterator(sym_itr);
  2567. return false;
  2568. }
  2569. LLVMMoveToContainingSection(contain_section, sym_itr);
  2570. contain_section_name =
  2571. (char *)LLVMGetSectionName(contain_section);
  2572. LLVMDisposeSectionIterator(contain_section);
  2573. if (!strcmp(contain_section_name, ".text.unlikely.")
  2574. || !strcmp(contain_section_name, ".ltext.unlikely.")) {
  2575. func->text_offset_of_aot_func_internal =
  2576. align_uint(obj_data->text_size, 4)
  2577. + LLVMGetSymbolAddress(sym_itr);
  2578. }
  2579. else if (!strcmp(contain_section_name, ".text.hot.")
  2580. || !strcmp(contain_section_name, ".ltext.hot.")) {
  2581. func->text_offset_of_aot_func_internal =
  2582. align_uint(obj_data->text_size, 4)
  2583. + align_uint(obj_data->text_unlikely_size, 4)
  2584. + LLVMGetSymbolAddress(sym_itr);
  2585. }
  2586. else {
  2587. func->text_offset_of_aot_func_internal =
  2588. LLVMGetSymbolAddress(sym_itr);
  2589. }
  2590. }
  2591. }
  2592. LLVMMoveToNextSymbol(sym_itr);
  2593. }
  2594. LLVMDisposeSymbolIterator(sym_itr);
  2595. return true;
  2596. }
  2597. static bool
  2598. get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count)
  2599. {
  2600. uint32 relocation_count = 0;
  2601. LLVMRelocationIteratorRef rel_itr;
  2602. if (!(rel_itr = LLVMGetRelocations(sec_itr))) {
  2603. aot_set_last_error("llvm get relocations failed.");
  2604. LLVMDisposeSectionIterator(sec_itr);
  2605. return false;
  2606. }
  2607. while (!LLVMIsRelocationIteratorAtEnd(sec_itr, rel_itr)) {
  2608. relocation_count++;
  2609. LLVMMoveToNextRelocation(rel_itr);
  2610. }
  2611. LLVMDisposeRelocationIterator(rel_itr);
  2612. *p_count = relocation_count;
  2613. return true;
  2614. }
  2615. static bool
  2616. aot_resolve_object_relocation_group(AOTObjectData *obj_data,
  2617. AOTRelocationGroup *group,
  2618. LLVMSectionIteratorRef rel_sec)
  2619. {
  2620. LLVMRelocationIteratorRef rel_itr;
  2621. AOTRelocation *relocation = group->relocations;
  2622. uint32 size;
  2623. bool is_binary_32bit = is_32bit_binary(obj_data);
  2624. bool is_binary_little_endian = is_little_endian_binary(obj_data);
  2625. bool has_addend = str_starts_with(group->section_name, ".rela");
  2626. uint8 *rela_content = NULL;
  2627. /* calculate relocations count and allocate memory */
  2628. if (!get_relocations_count(rel_sec, &group->relocation_count))
  2629. return false;
  2630. if (group->relocation_count == 0) {
  2631. aot_set_last_error("invalid relocations count");
  2632. return false;
  2633. }
  2634. size = (uint32)sizeof(AOTRelocation) * group->relocation_count;
  2635. if (!(relocation = group->relocations = wasm_runtime_malloc(size))) {
  2636. aot_set_last_error("allocate memory for relocations failed.");
  2637. return false;
  2638. }
  2639. memset(group->relocations, 0, size);
  2640. if (has_addend) {
  2641. uint64 rela_content_size;
  2642. /* LLVM doesn't provide C API to get relocation addend. So we have to
  2643. * parse it manually. */
  2644. rela_content = (uint8 *)LLVMGetSectionContents(rel_sec);
  2645. rela_content_size = LLVMGetSectionSize(rel_sec);
  2646. if (is_binary_32bit)
  2647. size = (uint32)sizeof(struct elf32_rela) * group->relocation_count;
  2648. else
  2649. size = (uint32)sizeof(struct elf64_rela) * group->relocation_count;
  2650. if (rela_content_size != (uint64)size) {
  2651. aot_set_last_error("invalid relocation section content.");
  2652. return false;
  2653. }
  2654. }
  2655. /* pares each relocation */
  2656. if (!(rel_itr = LLVMGetRelocations(rel_sec))) {
  2657. aot_set_last_error("llvm get relocations failed.");
  2658. return false;
  2659. }
  2660. while (!LLVMIsRelocationIteratorAtEnd(rel_sec, rel_itr)) {
  2661. uint64 offset = LLVMGetRelocationOffset(rel_itr);
  2662. uint64 type = LLVMGetRelocationType(rel_itr);
  2663. LLVMSymbolIteratorRef rel_sym = LLVMGetRelocationSymbol(rel_itr);
  2664. if (!rel_sym) {
  2665. aot_set_last_error("llvm get relocation symbol failed.");
  2666. goto fail;
  2667. }
  2668. /* parse relocation addend from reloction content */
  2669. if (has_addend) {
  2670. if (is_binary_32bit) {
  2671. int32 addend =
  2672. (int32)(((struct elf32_rela *)rela_content)->r_addend);
  2673. if (is_binary_little_endian != is_little_endian())
  2674. exchange_uint32((uint8 *)&addend);
  2675. relocation->relocation_addend = (int64)addend;
  2676. rela_content += sizeof(struct elf32_rela);
  2677. }
  2678. else {
  2679. int64 addend =
  2680. (int64)(((struct elf64_rela *)rela_content)->r_addend);
  2681. if (is_binary_little_endian != is_little_endian())
  2682. exchange_uint64((uint8 *)&addend);
  2683. relocation->relocation_addend = addend;
  2684. rela_content += sizeof(struct elf64_rela);
  2685. }
  2686. }
  2687. /* set relocation fields */
  2688. relocation->relocation_type = (uint32)type;
  2689. relocation->symbol_name = (char *)LLVMGetSymbolName(rel_sym);
  2690. relocation->relocation_offset = offset;
  2691. if (!strcmp(group->section_name, ".rela.text.unlikely.")
  2692. || !strcmp(group->section_name, ".rel.text.unlikely.")) {
  2693. relocation->relocation_offset += align_uint(obj_data->text_size, 4);
  2694. }
  2695. else if (!strcmp(group->section_name, ".rela.text.hot.")
  2696. || !strcmp(group->section_name, ".rel.text.hot.")) {
  2697. relocation->relocation_offset +=
  2698. align_uint(obj_data->text_size, 4)
  2699. + align_uint(obj_data->text_unlikely_size, 4);
  2700. }
  2701. if (!strcmp(relocation->symbol_name, ".text.unlikely.")) {
  2702. relocation->symbol_name = ".text";
  2703. relocation->relocation_addend += align_uint(obj_data->text_size, 4);
  2704. }
  2705. if (!strcmp(relocation->symbol_name, ".text.hot.")) {
  2706. relocation->symbol_name = ".text";
  2707. relocation->relocation_addend +=
  2708. align_uint(obj_data->text_size, 4)
  2709. + align_uint(obj_data->text_unlikely_size, 4);
  2710. }
  2711. /*
  2712. * Note: aot_stack_sizes_section_name section only contains
  2713. * stack_sizes table.
  2714. */
  2715. if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)
  2716. /* in windows 32, the symbol name may start with '_' */
  2717. || (strlen(relocation->symbol_name) > 0
  2718. && relocation->symbol_name[0] == '_'
  2719. && !strcmp(relocation->symbol_name + 1,
  2720. aot_stack_sizes_name))) {
  2721. /* discard const */
  2722. relocation->symbol_name = (char *)aot_stack_sizes_section_name;
  2723. }
  2724. if (obj_data->comp_ctx->enable_llvm_pgo
  2725. && (!strcmp(relocation->symbol_name, "__llvm_prf_cnts")
  2726. || !strcmp(relocation->symbol_name, "__llvm_prf_data"))) {
  2727. LLVMSectionIteratorRef sec_itr;
  2728. char buf[32], *section_name;
  2729. uint32 prof_section_idx = 0;
  2730. if (!(sec_itr =
  2731. LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2732. aot_set_last_error("llvm get section iterator failed.");
  2733. LLVMDisposeSymbolIterator(rel_sym);
  2734. goto fail;
  2735. }
  2736. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary,
  2737. sec_itr)) {
  2738. section_name = (char *)LLVMGetSectionName(sec_itr);
  2739. if (section_name
  2740. && !strcmp(section_name, relocation->symbol_name)) {
  2741. if (LLVMGetSectionContainsSymbol(sec_itr, rel_sym))
  2742. break;
  2743. prof_section_idx++;
  2744. }
  2745. LLVMMoveToNextSection(sec_itr);
  2746. }
  2747. LLVMDisposeSectionIterator(sec_itr);
  2748. if (!strcmp(group->section_name, ".rela.text")
  2749. || !strcmp(group->section_name, ".rel.text")) {
  2750. snprintf(buf, sizeof(buf), "%s%u", relocation->symbol_name,
  2751. prof_section_idx);
  2752. size = (uint32)(strlen(buf) + 1);
  2753. if (!(relocation->symbol_name = wasm_runtime_malloc(size))) {
  2754. aot_set_last_error(
  2755. "allocate memory for relocation symbol name failed.");
  2756. LLVMDisposeSymbolIterator(rel_sym);
  2757. goto fail;
  2758. }
  2759. bh_memcpy_s(relocation->symbol_name, size, buf, size);
  2760. relocation->is_symbol_name_allocated = true;
  2761. }
  2762. else if (!strncmp(group->section_name, ".rela__llvm_prf_data", 20)
  2763. || !strncmp(group->section_name, ".rel__llvm_prf_data",
  2764. 19)) {
  2765. snprintf(buf, sizeof(buf), "%s%u", relocation->symbol_name,
  2766. prof_section_idx);
  2767. size = (uint32)(strlen(buf) + 1);
  2768. if (!(relocation->symbol_name = wasm_runtime_malloc(size))) {
  2769. aot_set_last_error(
  2770. "allocate memory for relocation symbol name failed.");
  2771. LLVMDisposeSymbolIterator(rel_sym);
  2772. goto fail;
  2773. }
  2774. bh_memcpy_s(relocation->symbol_name, size, buf, size);
  2775. relocation->is_symbol_name_allocated = true;
  2776. }
  2777. }
  2778. /* for ".LCPIxxx", ".LJTIxxx", ".LBBxxx" and switch lookup table
  2779. * relocation, transform the symbol name to real section name and set
  2780. * addend to the offset of the symbol in the real section */
  2781. if (relocation->symbol_name
  2782. && (str_starts_with(relocation->symbol_name, ".LCPI")
  2783. || str_starts_with(relocation->symbol_name, ".LJTI")
  2784. || str_starts_with(relocation->symbol_name, ".LBB")
  2785. || str_starts_with(relocation->symbol_name,
  2786. ".Lswitch.table."))) {
  2787. /* change relocation->relocation_addend and
  2788. relocation->symbol_name */
  2789. LLVMSectionIteratorRef contain_section;
  2790. if (!(contain_section =
  2791. LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2792. aot_set_last_error("llvm get section iterator failed.");
  2793. goto fail;
  2794. }
  2795. LLVMMoveToContainingSection(contain_section, rel_sym);
  2796. if (LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary,
  2797. contain_section)) {
  2798. LLVMDisposeSectionIterator(contain_section);
  2799. aot_set_last_error("llvm get containing section failed.");
  2800. goto fail;
  2801. }
  2802. relocation->relocation_addend += LLVMGetSymbolAddress(rel_sym);
  2803. relocation->symbol_name =
  2804. (char *)LLVMGetSectionName(contain_section);
  2805. LLVMDisposeSectionIterator(contain_section);
  2806. }
  2807. LLVMDisposeSymbolIterator(rel_sym);
  2808. LLVMMoveToNextRelocation(rel_itr);
  2809. relocation++;
  2810. }
  2811. LLVMDisposeRelocationIterator(rel_itr);
  2812. return true;
  2813. fail:
  2814. LLVMDisposeRelocationIterator(rel_itr);
  2815. return false;
  2816. }
  2817. static bool
  2818. is_relocation_section_name(AOTObjectData *obj_data, char *section_name)
  2819. {
  2820. return (!strcmp(section_name, ".rela.text")
  2821. || !strcmp(section_name, ".rel.text")
  2822. || !strcmp(section_name, ".rela.text.unlikely.")
  2823. || !strcmp(section_name, ".rel.text.unlikely.")
  2824. || !strcmp(section_name, ".rela.text.hot.")
  2825. || !strcmp(section_name, ".rel.text.hot.")
  2826. || !strcmp(section_name, ".rela.ltext")
  2827. || !strcmp(section_name, ".rel.ltext")
  2828. || !strcmp(section_name, ".rela.ltext.unlikely.")
  2829. || !strcmp(section_name, ".rel.ltext.unlikely.")
  2830. || !strcmp(section_name, ".rela.ltext.hot.")
  2831. || !strcmp(section_name, ".rel.ltext.hot.")
  2832. || !strcmp(section_name, ".rela.literal")
  2833. || !strcmp(section_name, ".rela.data")
  2834. || !strcmp(section_name, ".rel.data")
  2835. || !strcmp(section_name, ".rela.sdata")
  2836. || !strcmp(section_name, ".rel.sdata")
  2837. || !strcmp(section_name, ".rela.rodata")
  2838. || !strcmp(section_name, ".rel.rodata")
  2839. || (obj_data->comp_ctx->enable_llvm_pgo
  2840. && (!strcmp(section_name, ".rela__llvm_prf_data")
  2841. || !strcmp(section_name, ".rel__llvm_prf_data")))
  2842. /* ".rela.rodata.cst4/8/16/.." */
  2843. || !strncmp(section_name, ".rela.rodata.cst",
  2844. strlen(".rela.rodata.cst"))
  2845. /* ".rel.rodata.cst4/8/16/.." */
  2846. || !strncmp(section_name, ".rel.rodata.cst",
  2847. strlen(".rel.rodata.cst")));
  2848. }
  2849. static bool
  2850. is_relocation_section(AOTObjectData *obj_data, LLVMSectionIteratorRef sec_itr)
  2851. {
  2852. uint32 count = 0;
  2853. char *name = (char *)LLVMGetSectionName(sec_itr);
  2854. if (name) {
  2855. if (is_relocation_section_name(obj_data, name))
  2856. return true;
  2857. else if ((!strcmp(name, ".text") || !strcmp(name, ".text.unlikely.")
  2858. || !strcmp(name, ".text.hot.") || !strcmp(name, ".rdata"))
  2859. && get_relocations_count(sec_itr, &count) && count > 0)
  2860. return true;
  2861. }
  2862. return false;
  2863. }
  2864. static bool
  2865. is_readonly_section(const char *name)
  2866. {
  2867. return !strcmp(name, ".rel.text") || !strcmp(name, ".rela.text")
  2868. || !strcmp(name, ".rel.ltext") || !strcmp(name, ".rela.ltext")
  2869. || !strcmp(name, ".rela.literal") || !strcmp(name, ".text")
  2870. || !strcmp(name, ".ltext");
  2871. }
  2872. static bool
  2873. get_relocation_groups_count(AOTObjectData *obj_data, uint32 *p_count)
  2874. {
  2875. uint32 count = 0;
  2876. LLVMSectionIteratorRef sec_itr;
  2877. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2878. aot_set_last_error("llvm get section iterator failed.");
  2879. return false;
  2880. }
  2881. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  2882. if (is_relocation_section(obj_data, sec_itr)) {
  2883. count++;
  2884. }
  2885. LLVMMoveToNextSection(sec_itr);
  2886. }
  2887. LLVMDisposeSectionIterator(sec_itr);
  2888. *p_count = count;
  2889. return true;
  2890. }
  2891. static bool
  2892. aot_resolve_object_relocation_groups(AOTObjectData *obj_data)
  2893. {
  2894. LLVMSectionIteratorRef sec_itr;
  2895. AOTRelocationGroup *relocation_group;
  2896. uint32 group_count, llvm_prf_data_idx = 0;
  2897. char *name;
  2898. uint32 size;
  2899. /* calculate relocation groups count and allocate memory */
  2900. if (!get_relocation_groups_count(obj_data, &group_count))
  2901. return false;
  2902. if (0 == (obj_data->relocation_group_count = group_count))
  2903. return true;
  2904. size = (uint32)sizeof(AOTRelocationGroup) * group_count;
  2905. if (!(relocation_group = obj_data->relocation_groups =
  2906. wasm_runtime_malloc(size))) {
  2907. aot_set_last_error("allocate memory for relocation groups failed.");
  2908. return false;
  2909. }
  2910. memset(obj_data->relocation_groups, 0, size);
  2911. /* resolve each relocation group */
  2912. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  2913. aot_set_last_error("llvm get section iterator failed.");
  2914. return false;
  2915. }
  2916. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  2917. if (is_relocation_section(obj_data, sec_itr)) {
  2918. name = (char *)LLVMGetSectionName(sec_itr);
  2919. relocation_group->section_name = name;
  2920. if (obj_data->comp_ctx->enable_llvm_pgo
  2921. && (!strcmp(name, ".rela__llvm_prf_data")
  2922. || !strcmp(name, ".rel__llvm_prf_data"))) {
  2923. char buf[32];
  2924. snprintf(buf, sizeof(buf), "%s%u", name, llvm_prf_data_idx);
  2925. size = (uint32)(strlen(buf) + 1);
  2926. if (!(relocation_group->section_name =
  2927. wasm_runtime_malloc(size))) {
  2928. aot_set_last_error(
  2929. "allocate memory for section name failed.");
  2930. LLVMDisposeSectionIterator(sec_itr);
  2931. return false;
  2932. }
  2933. bh_memcpy_s(relocation_group->section_name, size, buf, size);
  2934. relocation_group->is_section_name_allocated = true;
  2935. }
  2936. if (!aot_resolve_object_relocation_group(obj_data, relocation_group,
  2937. sec_itr)) {
  2938. LLVMDisposeSectionIterator(sec_itr);
  2939. return false;
  2940. }
  2941. if (obj_data->comp_ctx->enable_llvm_pgo
  2942. && (!strcmp(name, ".rela__llvm_prf_data")
  2943. || !strcmp(name, ".rel__llvm_prf_data"))) {
  2944. llvm_prf_data_idx++;
  2945. }
  2946. if (!strcmp(relocation_group->section_name, ".rela.text.unlikely.")
  2947. || !strcmp(relocation_group->section_name, ".rela.text.hot.")) {
  2948. relocation_group->section_name = ".rela.text";
  2949. }
  2950. else if (!strcmp(relocation_group->section_name,
  2951. ".rela.ltext.unlikely.")
  2952. || !strcmp(relocation_group->section_name,
  2953. ".rela.ltext.hot.")) {
  2954. relocation_group->section_name = ".rela.ltext";
  2955. }
  2956. else if (!strcmp(relocation_group->section_name,
  2957. ".rel.text.unlikely.")
  2958. || !strcmp(relocation_group->section_name,
  2959. ".rel.text.hot.")) {
  2960. relocation_group->section_name = ".rel.text";
  2961. }
  2962. else if (!strcmp(relocation_group->section_name,
  2963. ".rel.ltext.unlikely.")
  2964. || !strcmp(relocation_group->section_name,
  2965. ".rel.ltext.hot.")) {
  2966. relocation_group->section_name = ".rel.ltext";
  2967. }
  2968. /*
  2969. * Relocations in read-only sections are problematic,
  2970. * especially for XIP on platforms which don't have
  2971. * copy-on-write mappings.
  2972. */
  2973. if (obj_data->comp_ctx->is_indirect_mode
  2974. && is_readonly_section(relocation_group->section_name)) {
  2975. LOG_WARNING("%" PRIu32
  2976. " text relocations in %s section for indirect mode",
  2977. relocation_group->relocation_count,
  2978. relocation_group->section_name);
  2979. }
  2980. relocation_group++;
  2981. }
  2982. LLVMMoveToNextSection(sec_itr);
  2983. }
  2984. LLVMDisposeSectionIterator(sec_itr);
  2985. return true;
  2986. }
  2987. static void
  2988. destroy_relocation_groups(AOTRelocationGroup *relocation_groups,
  2989. uint32 relocation_group_count)
  2990. {
  2991. uint32 i, j;
  2992. AOTRelocationGroup *relocation_group = relocation_groups;
  2993. for (i = 0; i < relocation_group_count; i++, relocation_group++) {
  2994. if (relocation_group->relocations) {
  2995. for (j = 0; j < relocation_group->relocation_count; j++) {
  2996. if (relocation_group->relocations[j].is_symbol_name_allocated)
  2997. wasm_runtime_free(
  2998. relocation_group->relocations[j].symbol_name);
  2999. }
  3000. wasm_runtime_free(relocation_group->relocations);
  3001. }
  3002. if (relocation_group->is_section_name_allocated)
  3003. wasm_runtime_free(relocation_group->section_name);
  3004. }
  3005. wasm_runtime_free(relocation_groups);
  3006. }
  3007. static void
  3008. destroy_relocation_symbol_list(AOTSymbolList *symbol_list)
  3009. {
  3010. AOTSymbolNode *elem;
  3011. elem = symbol_list->head;
  3012. while (elem) {
  3013. AOTSymbolNode *next = elem->next;
  3014. wasm_runtime_free(elem);
  3015. elem = next;
  3016. }
  3017. }
  3018. static void
  3019. aot_obj_data_destroy(AOTObjectData *obj_data)
  3020. {
  3021. if (obj_data->binary)
  3022. LLVMDisposeBinary(obj_data->binary);
  3023. if (obj_data->mem_buf)
  3024. LLVMDisposeMemoryBuffer(obj_data->mem_buf);
  3025. if (obj_data->funcs)
  3026. wasm_runtime_free(obj_data->funcs);
  3027. if (obj_data->data_sections) {
  3028. uint32 i;
  3029. for (i = 0; i < obj_data->data_sections_count; i++) {
  3030. if (obj_data->data_sections[i].name
  3031. && obj_data->data_sections[i].is_name_allocated) {
  3032. wasm_runtime_free(obj_data->data_sections[i].name);
  3033. }
  3034. if (obj_data->data_sections[i].data
  3035. && obj_data->data_sections[i].is_data_allocated) {
  3036. wasm_runtime_free(obj_data->data_sections[i].data);
  3037. }
  3038. }
  3039. wasm_runtime_free(obj_data->data_sections);
  3040. }
  3041. if (obj_data->relocation_groups)
  3042. destroy_relocation_groups(obj_data->relocation_groups,
  3043. obj_data->relocation_group_count);
  3044. if (obj_data->symbol_list.len)
  3045. destroy_relocation_symbol_list(&obj_data->symbol_list);
  3046. if (obj_data->stack_sizes)
  3047. wasm_runtime_free(obj_data->stack_sizes);
  3048. wasm_runtime_free(obj_data);
  3049. }
  3050. static AOTObjectData *
  3051. aot_obj_data_create(AOTCompContext *comp_ctx)
  3052. {
  3053. char *err = NULL;
  3054. AOTObjectData *obj_data;
  3055. LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
  3056. bh_print_time("Begin to emit object file to buffer");
  3057. if (!(obj_data = wasm_runtime_malloc(sizeof(AOTObjectData)))) {
  3058. aot_set_last_error("allocate memory failed.");
  3059. return false;
  3060. }
  3061. memset(obj_data, 0, sizeof(AOTObjectData));
  3062. obj_data->comp_ctx = comp_ctx;
  3063. bh_print_time("Begin to emit object file");
  3064. if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
  3065. #if defined(_WIN32) || defined(_WIN32_)
  3066. aot_set_last_error("external toolchain not supported on Windows");
  3067. goto fail;
  3068. #else
  3069. /* Generate a temp file name */
  3070. int ret;
  3071. char obj_file_name[64];
  3072. if (!aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name,
  3073. sizeof(obj_file_name))) {
  3074. goto fail;
  3075. }
  3076. if (!aot_emit_object_file(comp_ctx, obj_file_name)) {
  3077. goto fail;
  3078. }
  3079. /* create memory buffer from object file */
  3080. ret = LLVMCreateMemoryBufferWithContentsOfFile(
  3081. obj_file_name, &obj_data->mem_buf, &err);
  3082. /* remove temp object file */
  3083. unlink(obj_file_name);
  3084. if (ret != 0) {
  3085. if (err) {
  3086. LLVMDisposeMessage(err);
  3087. err = NULL;
  3088. }
  3089. aot_set_last_error("create mem buffer with file failed.");
  3090. goto fail;
  3091. }
  3092. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3093. }
  3094. else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
  3095. #if defined(_WIN32) || defined(_WIN32_)
  3096. aot_set_last_error("emit object file on Windows is unsupported.");
  3097. goto fail;
  3098. #else
  3099. /* Emit to assmelby file instead for arc target
  3100. as it cannot emit to object file */
  3101. char file_name[] = "wasm-XXXXXX", buf[128];
  3102. int fd, ret;
  3103. if ((fd = mkstemp(file_name)) <= 0) {
  3104. aot_set_last_error("make temp file failed.");
  3105. goto fail;
  3106. }
  3107. /* close and remove temp file */
  3108. close(fd);
  3109. unlink(file_name);
  3110. snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
  3111. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  3112. comp_ctx->module, buf, LLVMAssemblyFile,
  3113. &err)
  3114. != 0) {
  3115. if (err) {
  3116. LLVMDisposeMessage(err);
  3117. err = NULL;
  3118. }
  3119. aot_set_last_error("emit elf to object file failed.");
  3120. goto fail;
  3121. }
  3122. /* call arc gcc to compile assembly file to object file */
  3123. /* TODO: get arc gcc from environment variable firstly
  3124. and check whether the toolchain exists actually */
  3125. snprintf(buf, sizeof(buf), "%s%s%s%s%s%s",
  3126. "/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ",
  3127. "-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
  3128. /* TODO: use try..catch to handle possible exceptions */
  3129. ret = system(buf);
  3130. /* remove temp assembly file */
  3131. snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
  3132. unlink(buf);
  3133. if (ret != 0) {
  3134. aot_set_last_error("failed to compile asm file to obj file "
  3135. "with arc gcc toolchain.");
  3136. goto fail;
  3137. }
  3138. /* create memory buffer from object file */
  3139. snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
  3140. ret = LLVMCreateMemoryBufferWithContentsOfFile(buf, &obj_data->mem_buf,
  3141. &err);
  3142. /* remove temp object file */
  3143. snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
  3144. unlink(buf);
  3145. if (ret != 0) {
  3146. if (err) {
  3147. LLVMDisposeMessage(err);
  3148. err = NULL;
  3149. }
  3150. aot_set_last_error("create mem buffer with file failed.");
  3151. goto fail;
  3152. }
  3153. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3154. }
  3155. else {
  3156. if (LLVMTargetMachineEmitToMemoryBuffer(
  3157. comp_ctx->target_machine, comp_ctx->module, LLVMObjectFile,
  3158. &err, &obj_data->mem_buf)
  3159. != 0) {
  3160. if (err) {
  3161. LLVMDisposeMessage(err);
  3162. err = NULL;
  3163. }
  3164. aot_set_last_error("llvm emit to memory buffer failed.");
  3165. goto fail;
  3166. }
  3167. }
  3168. if (!(obj_data->binary = LLVMCreateBinary(obj_data->mem_buf, NULL, &err))) {
  3169. if (err) {
  3170. LLVMDisposeMessage(err);
  3171. err = NULL;
  3172. }
  3173. aot_set_last_error("llvm create binary failed.");
  3174. goto fail;
  3175. }
  3176. bh_print_time("Begin to resolve object file info");
  3177. /* resolve target info/text/relocations/functions */
  3178. if (!aot_resolve_target_info(comp_ctx, obj_data)
  3179. || !aot_resolve_text(obj_data) || !aot_resolve_literal(obj_data)
  3180. || !aot_resolve_object_data_sections(obj_data)
  3181. || !aot_resolve_functions(comp_ctx, obj_data)
  3182. || !aot_resolve_object_relocation_groups(obj_data))
  3183. goto fail;
  3184. return obj_data;
  3185. fail:
  3186. aot_obj_data_destroy(obj_data);
  3187. return NULL;
  3188. }
  3189. uint8 *
  3190. aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  3191. uint32 *p_aot_file_size)
  3192. {
  3193. AOTObjectData *obj_data = aot_obj_data_create(comp_ctx);
  3194. uint8 *aot_file_buf, *buf, *buf_end;
  3195. uint32 aot_file_size, offset = 0;
  3196. if (!obj_data)
  3197. return NULL;
  3198. aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
  3199. if (aot_file_size == 0) {
  3200. aot_set_last_error("get aot file size failed");
  3201. goto fail1;
  3202. }
  3203. if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
  3204. aot_set_last_error("allocate memory failed.");
  3205. goto fail1;
  3206. }
  3207. memset(aot_file_buf, 0, aot_file_size);
  3208. buf_end = buf + aot_file_size;
  3209. if (!aot_emit_file_header(buf, buf_end, &offset, comp_data, obj_data)
  3210. || !aot_emit_target_info_section(buf, buf_end, &offset, comp_data,
  3211. obj_data)
  3212. || !aot_emit_init_data_section(buf, buf_end, &offset, comp_ctx,
  3213. comp_data, obj_data)
  3214. || !aot_emit_text_section(buf, buf_end, &offset, comp_data, obj_data)
  3215. || !aot_emit_func_section(buf, buf_end, &offset, comp_data, obj_data)
  3216. || !aot_emit_export_section(buf, buf_end, &offset, comp_ctx, comp_data,
  3217. obj_data)
  3218. || !aot_emit_relocation_section(buf, buf_end, &offset, comp_ctx,
  3219. comp_data, obj_data)
  3220. || !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx)
  3221. || !aot_emit_custom_sections(buf, buf_end, &offset, comp_data,
  3222. comp_ctx))
  3223. goto fail2;
  3224. #if 0
  3225. dump_buf(buf, offset, "sections");
  3226. #endif
  3227. if (offset != aot_file_size) {
  3228. aot_set_last_error("emit aot file failed.");
  3229. goto fail2;
  3230. }
  3231. *p_aot_file_size = aot_file_size;
  3232. aot_obj_data_destroy(obj_data);
  3233. return aot_file_buf;
  3234. fail2:
  3235. wasm_runtime_free(aot_file_buf);
  3236. fail1:
  3237. aot_obj_data_destroy(obj_data);
  3238. return NULL;
  3239. }
  3240. bool
  3241. aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  3242. const char *file_name)
  3243. {
  3244. uint8 *aot_file_buf;
  3245. uint32 aot_file_size;
  3246. bool ret = false;
  3247. FILE *file;
  3248. bh_print_time("Begin to emit AOT file");
  3249. if (!(aot_file_buf =
  3250. aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size))) {
  3251. return false;
  3252. }
  3253. /* write buffer to file */
  3254. if (!(file = fopen(file_name, "wb"))) {
  3255. aot_set_last_error("open or create aot file failed.");
  3256. goto fail1;
  3257. }
  3258. if (!fwrite(aot_file_buf, aot_file_size, 1, file)) {
  3259. aot_set_last_error("write to aot file failed.");
  3260. goto fail2;
  3261. }
  3262. ret = true;
  3263. fail2:
  3264. fclose(file);
  3265. fail1:
  3266. wasm_runtime_free(aot_file_buf);
  3267. return ret;
  3268. }