aot_emit_aot_file.c 123 KB

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