aot_emit_aot_file.c 125 KB

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