aot_emit_aot_file.c 155 KB

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