aot_emit_aot_file.c 123 KB

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