| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "aot_compiler.h"
- #include "../aot/aot_runtime.h"
- #define PUT_U64_TO_ADDR(addr, value) \
- do { \
- union { \
- uint64 val; \
- uint32 parts[2]; \
- } u; \
- u.val = (value); \
- ((uint32 *)(addr))[0] = u.parts[0]; \
- ((uint32 *)(addr))[1] = u.parts[1]; \
- } while (0)
- #define CHECK_SIZE(size) \
- do { \
- if (size == (uint32)-1) { \
- aot_set_last_error("get symbol size failed."); \
- return (uint32)-1; \
- } \
- } while (0)
- /* Internal function in object file */
- typedef struct AOTObjectFunc {
- char *func_name;
- /* text offset of aot_func#n */
- uint64 text_offset;
- /* text offset of aot_func_internal#n */
- uint64 text_offset_of_aot_func_internal;
- } AOTObjectFunc;
- /* Symbol table list node */
- typedef struct AOTSymbolNode {
- struct AOTSymbolNode *next;
- uint32 str_len;
- char *symbol;
- } AOTSymbolNode;
- typedef struct AOTSymbolList {
- AOTSymbolNode *head;
- AOTSymbolNode *end;
- uint32 len;
- } AOTSymbolList;
- /* AOT object data */
- typedef struct AOTObjectData {
- AOTCompContext *comp_ctx;
- LLVMMemoryBufferRef mem_buf;
- LLVMBinaryRef binary;
- AOTTargetInfo target_info;
- void *text;
- uint32 text_size;
- void *text_unlikely;
- uint32 text_unlikely_size;
- void *text_hot;
- uint32 text_hot_size;
- /* literal data and size */
- void *literal;
- uint32 literal_size;
- AOTObjectDataSection *data_sections;
- uint32 data_sections_count;
- AOTObjectFunc *funcs;
- uint32 func_count;
- AOTSymbolList symbol_list;
- AOTRelocationGroup *relocation_groups;
- uint32 relocation_group_count;
- const char *stack_sizes_section_name;
- uint32 stack_sizes_offset;
- uint32 *stack_sizes;
- } AOTObjectData;
- #if 0
- static void dump_buf(uint8 *buf, uint32 size, char *title)
- {
- int i;
- printf("------ %s -------", title);
- for (i = 0; i < size; i++) {
- if ((i % 16) == 0)
- printf("\n");
- printf("%02x ", (unsigned char)buf[i]);
- }
- printf("\n\n");
- }
- #endif
- static bool
- is_32bit_binary(const AOTObjectData *obj_data)
- {
- /* bit 1: 0 is 32-bit, 1 is 64-bit */
- return obj_data->target_info.bin_type & 2 ? false : true;
- }
- static bool
- is_little_endian_binary(const AOTObjectData *obj_data)
- {
- /* bit 0: 0 is little-endian, 1 is big-endian */
- return obj_data->target_info.bin_type & 1 ? false : true;
- }
- static bool
- need_call_wrapped_indirect(const AOTObjectData *obj_data)
- {
- const bool need_precheck = obj_data->comp_ctx->enable_stack_bound_check
- || obj_data->comp_ctx->enable_stack_estimation;
- return obj_data->comp_ctx->is_indirect_mode && need_precheck
- && !strncmp(obj_data->comp_ctx->target_arch, "xtensa", 6);
- }
- static bool
- str_starts_with(const char *str, const char *prefix)
- {
- size_t len_pre = strlen(prefix), len_str = strlen(str);
- return (len_str >= len_pre) && !memcmp(str, prefix, len_pre);
- }
- static uint32
- get_file_header_size()
- {
- /* magic number (4 bytes) + version (4 bytes) */
- return sizeof(uint32) + sizeof(uint32);
- }
- static uint32
- get_string_size(AOTCompContext *comp_ctx, const char *s)
- {
- /* string size (2 bytes) + string content + '\0' */
- return (uint32)sizeof(uint16) + (uint32)strlen(s) + 1;
- }
- static uint32
- get_target_info_section_size()
- {
- return sizeof(AOTTargetInfo);
- }
- static uint32
- get_init_expr_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data,
- InitializerExpression *expr);
- static uint32
- get_mem_init_data_size(AOTCompContext *comp_ctx, AOTMemInitData *mem_init_data)
- {
- /* init expr type (4 bytes)
- * + init expr value (4 bytes, valid value can only be i32/get_global)
- * + byte count (4 bytes) + bytes */
- uint32 total_size =
- (uint32)(get_init_expr_size(comp_ctx, comp_ctx->comp_data,
- &mem_init_data->offset)
- + sizeof(uint32) + mem_init_data->byte_count);
- /* bulk_memory enabled:
- is_passive (4 bytes) + memory_index (4 bytes)
- bulk memory disabled:
- placeholder (4 bytes) + placeholder (4 bytes)
- */
- total_size += (sizeof(uint32) + sizeof(uint32));
- return total_size;
- }
- static uint32
- get_mem_init_data_list_size(AOTCompContext *comp_ctx,
- AOTMemInitData **mem_init_data_list,
- uint32 mem_init_data_count)
- {
- AOTMemInitData **mem_init_data = mem_init_data_list;
- uint32 size = 0, i;
- for (i = 0; i < mem_init_data_count; i++, mem_init_data++) {
- size = align_uint(size, 4);
- size += get_mem_init_data_size(comp_ctx, *mem_init_data);
- }
- return size;
- }
- static uint32
- get_import_memory_size(AOTCompData *comp_data)
- {
- /* currently we only emit import_memory_count = 0 */
- return sizeof(uint32);
- }
- static uint32
- get_memory_size(AOTCompData *comp_data)
- {
- /* memory_count + count * (memory_flags + num_bytes_per_page +
- init_page_count + max_page_count) */
- return (uint32)(sizeof(uint32)
- + comp_data->memory_count * sizeof(uint32) * 4);
- }
- static uint32
- get_mem_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /* import_memory_size + memory_size
- + init_data_count + init_data_list */
- return get_import_memory_size(comp_data) + get_memory_size(comp_data)
- + (uint32)sizeof(uint32)
- + get_mem_init_data_list_size(comp_ctx,
- comp_data->mem_init_data_list,
- comp_data->mem_init_data_count);
- }
- static uint32
- get_init_expr_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data,
- InitializerExpression *expr)
- {
- /* init_expr_type */
- uint32 size = sizeof(uint32);
- #if WASM_ENABLE_GC != 0
- WASMModule *module = comp_data->wasm_module;
- #endif
- /* + init value size */
- switch (expr->init_expr_type) {
- case INIT_EXPR_NONE:
- /* no init value, used in table initializer */
- break;
- case INIT_EXPR_TYPE_I32_CONST:
- case INIT_EXPR_TYPE_F32_CONST:
- case INIT_EXPR_TYPE_GET_GLOBAL:
- size += sizeof(uint32);
- break;
- case INIT_EXPR_TYPE_I64_CONST:
- case INIT_EXPR_TYPE_F64_CONST:
- size += sizeof(uint64);
- break;
- case INIT_EXPR_TYPE_V128_CONST:
- size += sizeof(uint64) * 2;
- break;
- case INIT_EXPR_TYPE_FUNCREF_CONST:
- case INIT_EXPR_TYPE_REFNULL_CONST:
- /* ref_index */
- size += sizeof(uint32);
- break;
- #if WASM_ENABLE_GC != 0
- case INIT_EXPR_TYPE_I31_NEW:
- /* i32 */
- size += sizeof(uint32);
- break;
- case INIT_EXPR_TYPE_STRUCT_NEW:
- {
- uint32 i;
- WASMStructNewInitValues *struct_new_init_values =
- (WASMStructNewInitValues *)expr->u.data;
- /* type_index + field_count + fields */
- size += sizeof(uint32) + sizeof(uint32);
- bh_assert(struct_new_init_values->type_idx < module->type_count);
- for (i = 0; i < struct_new_init_values->count; i++) {
- WASMStructType *struct_type =
- (WASMStructType *)
- module->types[struct_new_init_values->type_idx];
- uint32 field_size;
- bh_assert(struct_type);
- bh_assert(struct_type->field_count
- == struct_new_init_values->count);
- field_size = wasm_value_type_size_internal(
- struct_type->fields[i].field_type, comp_ctx->pointer_size);
- if (field_size < sizeof(uint32))
- field_size = sizeof(uint32);
- size += field_size;
- }
- break;
- }
- case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
- /* type_index */
- size += sizeof(uint32);
- break;
- case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
- /* array_elem_type + type_index + len */
- size += sizeof(uint32) * 3;
- break;
- case INIT_EXPR_TYPE_ARRAY_NEW:
- case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
- {
- WASMArrayNewInitValues *array_new_init_values =
- (WASMArrayNewInitValues *)expr->u.data;
- WASMArrayType *array_type = NULL;
- uint32 value_count;
- array_type =
- (WASMArrayType *)module->types[array_new_init_values->type_idx];
- bh_assert(array_type);
- bh_assert(array_new_init_values->type_idx < module->type_count);
- value_count =
- (expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED)
- ? array_new_init_values->length
- : 1;
- /* array_elem_type + type_index + len + elems */
- size += sizeof(uint32) * 3
- + wasm_value_type_size_internal(array_type->elem_type,
- comp_ctx->pointer_size)
- * value_count;
- break;
- }
- #endif /* end of WASM_ENABLE_GC != 0 */
- default:
- bh_assert(0);
- }
- return size;
- }
- static uint32
- get_table_init_data_size(AOTCompContext *comp_ctx,
- AOTTableInitData *table_init_data)
- {
- uint32 size, i;
- /*
- * mode (4 bytes), elem_type (4 bytes)
- *
- * table_index(4 bytes) + init expr type (4 bytes) + init expr value (8
- * bytes)
- */
- size = (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
- + sizeof(uint64))
- /* Size of WasmRefType - inner padding (ref type + nullable +
- heap_type) */
- + 8;
- /* + value count/func index count (4 bytes) + init_values */
- size += sizeof(uint32);
- for (i = 0; i < table_init_data->value_count; i++) {
- size += get_init_expr_size(comp_ctx, comp_ctx->comp_data,
- &table_init_data->init_values[i]);
- }
- return size;
- }
- static uint32
- get_table_init_data_list_size(AOTCompContext *comp_ctx,
- AOTTableInitData **table_init_data_list,
- uint32 table_init_data_count)
- {
- /*
- * ------------------------------
- * | table_init_data_count
- * ------------------------------
- * | | U32 mode
- * | AOTTableInitData[N] | U32 elem_type
- * | | U32 table_index
- * | | U32 offset.init_expr_type
- * | | U64 offset.u.i64
- * | | U32 func_index_count / elem_count
- * | | UINTPTR [func_index_count] / [elem_count]
- * ------------------------------
- */
- AOTTableInitData **table_init_data = table_init_data_list;
- uint32 size = 0, i;
- /* table_init_data_count(4 bytes) */
- size = (uint32)sizeof(uint32);
- for (i = 0; i < table_init_data_count; i++, table_init_data++) {
- size = align_uint(size, 4);
- size += get_table_init_data_size(comp_ctx, *table_init_data);
- }
- return size;
- }
- static uint32
- get_import_table_size(const AOTCompContext *comp_ctx,
- const AOTCompData *comp_data)
- {
- /*
- * ------------------------------
- * | import_table_count
- * ------------------------------
- * | | U8 elem_type
- * | | U8 table_flags
- * | | U8 possible_grow
- * | AOTImportTable[N] | U8 elem_ref_type.nullable (for GC only)
- * | | U32 table_init_size
- * | | U32 table_max_size
- * | | U32 elem_ref_type.heap_type (for GC only)
- * ------------------------------
- */
- uint32 size = 0, i;
- size = (uint32)sizeof(uint32);
- for (i = 0; i < comp_data->import_table_count; i++) {
- size += sizeof(uint32) * 3;
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc && comp_data->import_tables[i].elem_ref_type)
- size += sizeof(uint32);
- #endif
- }
- return size;
- }
- static uint32
- get_table_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data)
- {
- /*
- * ------------------------------
- * | table_count
- * ------------------------------
- * | | U8 elem_type
- * | | U8 table_flags
- * | | U8 possible_grow
- * | AOTTable[N] | U8 elem_ref_type.nullable (for GC only)
- * | | U32 table_init_size
- * | | U32 table_max_size
- * | | U32 elem_ref_type.heap_type (for GC only)
- * | | N init_expr (for GC only)
- * ------------------------------
- */
- uint32 size = 0, i;
- size = (uint32)sizeof(uint32);
- for (i = 0; i < comp_data->table_count; i++) {
- size += sizeof(uint32) * 3;
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc) {
- if (comp_data->tables[i].elem_ref_type) {
- size += sizeof(uint32);
- }
- size += get_init_expr_size(comp_ctx, comp_data,
- &comp_data->tables[i].init_expr);
- }
- #endif
- }
- return size;
- }
- static uint32
- get_table_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /*
- * ------------------------------
- * | import_table_count
- * ------------------------------
- * |
- * | AOTImportTable[import_table_count]
- * |
- * ------------------------------
- * | table_count
- * ------------------------------
- * |
- * | AOTTable[table_count]
- * |
- * ------------------------------
- * | table_init_data_count
- * ------------------------------
- * |
- * | AOTTableInitData*[table_init_data_count]
- * |
- * ------------------------------
- */
- return get_import_table_size(comp_ctx, comp_data)
- + get_table_size(comp_ctx, comp_data)
- + get_table_init_data_list_size(comp_ctx,
- comp_data->table_init_data_list,
- comp_data->table_init_data_count);
- }
- static uint32
- get_func_type_size(AOTCompContext *comp_ctx, AOTFuncType *func_type)
- {
- #if WASM_ENABLE_GC != 0
- /* type flag + equivalence type flag + is_sub_final + parent_type_idx
- + rec_count + rec_idx + param count + result count
- + ref_type_map_count + types + context of ref_type_map */
- if (comp_ctx->enable_gc) {
- uint32 size = 0;
- /* type flag */
- size += sizeof(func_type->base_type.type_flag);
- /* equivalence type flag + is_sub_final */
- size += sizeof(uint16);
- /* parent_type_idx */
- size += sizeof(func_type->base_type.parent_type_idx);
- /* rec_count */
- size += sizeof(func_type->base_type.rec_count);
- /* rec_idx */
- size += sizeof(func_type->base_type.rec_idx);
- /* param count */
- size += sizeof(func_type->param_count);
- /* result count */
- size += sizeof(func_type->result_count);
- /* ref_type_map_count */
- size += sizeof(func_type->ref_type_map_count);
- /* param and result types */
- size += func_type->param_count + func_type->result_count;
- /* align size */
- size = align_uint(size, 4);
- /* ref_type_map */
- size += func_type->ref_type_map_count * 8;
- return size;
- }
- else
- #endif
- {
- /* type flag + param count + result count + types */
- return (uint32)sizeof(uint16) * 3 + func_type->param_count
- + func_type->result_count;
- }
- }
- #if WASM_ENABLE_GC != 0
- static uint32
- get_struct_type_size(AOTCompContext *comp_ctx, AOTStructType *struct_type)
- {
- uint32 size = 0;
- /* type flag + equivalence type flag + is_sub_final + parent_type_idx
- + rec_count + rec_idx + field count + fields */
- /* type flag */
- size += sizeof(struct_type->base_type.type_flag);
- /* equivalence type flag + is_sub_final */
- size += sizeof(uint16);
- /* parent_type_idx */
- size += sizeof(struct_type->base_type.parent_type_idx);
- /* rec_count */
- size += sizeof(struct_type->base_type.rec_count);
- /* rec_idx */
- size += sizeof(struct_type->base_type.rec_idx);
- /* field count */
- size += sizeof(struct_type->field_count);
- /* field types */
- size += struct_type->field_count * 2;
- /* ref_type_map_count */
- size += sizeof(struct_type->ref_type_map_count);
- size = align_uint(size, 4);
- /* ref_type_map */
- size += struct_type->ref_type_map_count * 8;
- return size;
- }
- static uint32
- get_array_type_size(AOTCompContext *comp_ctx, AOTArrayType *array_type)
- {
- uint32 size = 0;
- /* type flag + equivalence type flag + is_sub_final + parent_type_idx
- + rec_count + rec_idx + elem_flags + elem_type + elem_ref_type */
- /* type flag */
- size += sizeof(array_type->base_type.type_flag);
- /* equivalence type flag + is_sub_final */
- size += sizeof(uint16);
- /* parent_type_idx (u32) */
- size += sizeof(array_type->base_type.parent_type_idx);
- /* rec_count */
- size += sizeof(array_type->base_type.rec_count);
- /* rec_idx */
- size += sizeof(array_type->base_type.rec_idx);
- /* elem_flags (u16) */
- size += sizeof(array_type->elem_flags);
- /* elem_type (u8) */
- size += sizeof(array_type->elem_type);
- /* elem_ref_type */
- if (array_type->elem_ref_type) {
- /* nullable (u8) */
- size += sizeof(uint8);
- /* heap type (u32) */
- size += sizeof(uint32);
- }
- return size;
- }
- #endif
- static uint32
- get_type_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /* Initial size with size of type count */
- uint32 size = 4;
- uint32 i;
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc) {
- for (i = 0; i < comp_data->type_count; i++) {
- uint32 j;
- size = align_uint(size, 4);
- /* Emit simple info if there is an equivalence type */
- for (j = 0; j < i; j++) {
- if (comp_data->types[j] == comp_data->types[i]) {
- /* type_flag (2 bytes) + equivalence type flag (1 byte)
- + padding (1 byte) + equivalence type index */
- size += 8;
- break;
- }
- }
- if (j < i)
- continue;
- if (comp_data->types[i]->type_flag == WASM_TYPE_FUNC)
- size += get_func_type_size(comp_ctx,
- (AOTFuncType *)comp_data->types[i]);
- else if (comp_data->types[i]->type_flag == WASM_TYPE_STRUCT)
- size += get_struct_type_size(
- comp_ctx, (AOTStructType *)comp_data->types[i]);
- else if (comp_data->types[i]->type_flag == WASM_TYPE_ARRAY)
- size += get_array_type_size(
- comp_ctx, (AOTArrayType *)comp_data->types[i]);
- else
- bh_assert(0);
- }
- }
- else
- #endif
- {
- for (i = 0; i < comp_data->type_count; i++) {
- size = align_uint(size, 4);
- size += get_func_type_size(comp_ctx,
- (AOTFuncType *)comp_data->types[i]);
- }
- }
- return size;
- }
- static uint32
- get_import_global_size(AOTCompContext *comp_ctx, AOTImportGlobal *import_global)
- {
- /* type (1 byte) + is_mutable (1 byte) + module_name + global_name */
- uint32 size = (uint32)sizeof(uint8) * 2
- + get_string_size(comp_ctx, import_global->module_name);
- size = align_uint(size, 2);
- size += get_string_size(comp_ctx, import_global->global_name);
- return size;
- }
- static uint32
- get_import_globals_size(AOTCompContext *comp_ctx,
- AOTImportGlobal *import_globals,
- uint32 import_global_count)
- {
- AOTImportGlobal *import_global = import_globals;
- uint32 size = 0, i;
- for (i = 0; i < import_global_count; i++, import_global++) {
- size = align_uint(size, 2);
- size += get_import_global_size(comp_ctx, import_global);
- }
- return size;
- }
- static uint32
- get_import_global_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /* import global count + import globals */
- return (uint32)sizeof(uint32)
- + get_import_globals_size(comp_ctx, comp_data->import_globals,
- comp_data->import_global_count);
- }
- static uint32
- get_global_size(AOTCompContext *comp_ctx, AOTGlobal *global)
- {
- /* type (1 byte) + is_mutable (1 byte) + padding (2 bytes)
- + init expr value (include init expr type) */
- return sizeof(uint8) * 2 + sizeof(uint8) * 2
- + get_init_expr_size(comp_ctx, comp_ctx->comp_data,
- &global->init_expr);
- }
- static uint32
- get_globals_size(AOTCompContext *comp_ctx, AOTGlobal *globals,
- uint32 global_count)
- {
- AOTGlobal *global = globals;
- uint32 size = 0, i;
- for (i = 0; i < global_count; i++, global++) {
- size = align_uint(size, 4);
- size += get_global_size(comp_ctx, global);
- }
- return size;
- }
- static uint32
- get_global_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /* global count + globals */
- return (uint32)sizeof(uint32)
- + get_globals_size(comp_ctx, comp_data->globals,
- comp_data->global_count);
- }
- static uint32
- get_import_func_size(AOTCompContext *comp_ctx, AOTImportFunc *import_func)
- {
- /* type index (2 bytes) + module_name + func_name */
- uint32 size = (uint32)sizeof(uint16)
- + get_string_size(comp_ctx, import_func->module_name);
- size = align_uint(size, 2);
- size += get_string_size(comp_ctx, import_func->func_name);
- return size;
- }
- static uint32
- get_import_funcs_size(AOTCompContext *comp_ctx, AOTImportFunc *import_funcs,
- uint32 import_func_count)
- {
- AOTImportFunc *import_func = import_funcs;
- uint32 size = 0, i;
- for (i = 0; i < import_func_count; i++, import_func++) {
- size = align_uint(size, 2);
- size += get_import_func_size(comp_ctx, import_func);
- }
- return size;
- }
- static uint32
- get_import_func_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /* import func count + import funcs */
- return (uint32)sizeof(uint32)
- + get_import_funcs_size(comp_ctx, comp_data->import_funcs,
- comp_data->import_func_count);
- }
- static uint32
- get_object_data_sections_size(AOTCompContext *comp_ctx,
- AOTObjectDataSection *data_sections,
- uint32 data_sections_count)
- {
- AOTObjectDataSection *data_section = data_sections;
- uint32 size = 0, i;
- for (i = 0; i < data_sections_count; i++, data_section++) {
- /* name + size + data */
- size = align_uint(size, 2);
- size += get_string_size(comp_ctx, data_section->name);
- size = align_uint(size, 4);
- size += (uint32)sizeof(uint32);
- size += data_section->size;
- }
- return size;
- }
- static uint32
- get_object_data_section_info_size(AOTCompContext *comp_ctx,
- AOTObjectData *obj_data)
- {
- /* data sections count + data sections */
- return (uint32)sizeof(uint32)
- + get_object_data_sections_size(comp_ctx, obj_data->data_sections,
- obj_data->data_sections_count);
- }
- static uint32
- get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 size = 0;
- size += get_mem_info_size(comp_ctx, comp_data);
- size = align_uint(size, 4);
- size += get_table_info_size(comp_ctx, comp_data);
- size = align_uint(size, 4);
- size += get_type_info_size(comp_ctx, comp_data);
- size = align_uint(size, 4);
- size += get_import_global_info_size(comp_ctx, comp_data);
- size = align_uint(size, 4);
- size += get_global_info_size(comp_ctx, comp_data);
- size = align_uint(size, 4);
- size += get_import_func_info_size(comp_ctx, comp_data);
- /* func count + start func index */
- size = align_uint(size, 4);
- size += (uint32)sizeof(uint32) * 2;
- /* aux data/heap/stack data */
- size += sizeof(uint32) * 10;
- size += get_object_data_section_info_size(comp_ctx, obj_data);
- return size;
- }
- static uint32
- get_text_section_size(AOTObjectData *obj_data)
- {
- return sizeof(uint32) + align_uint(obj_data->literal_size, 4)
- + align_uint(obj_data->text_size, 4)
- + align_uint(obj_data->text_unlikely_size, 4)
- + align_uint(obj_data->text_hot_size, 4);
- }
- static uint32
- get_func_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 size = 0;
- /* text offsets */
- if (is_32bit_binary(obj_data))
- size = (uint32)sizeof(uint32) * comp_data->func_count;
- else
- size = (uint32)sizeof(uint64) * comp_data->func_count;
- /* function type indexes */
- size += (uint32)sizeof(uint32) * comp_data->func_count;
- /* aot_func#xxx + aot_func_internal#xxx in XIP mode for xtensa */
- if (need_call_wrapped_indirect(obj_data))
- size *= 2;
- /* max_local_cell_nums */
- size += (uint32)sizeof(uint32) * comp_data->func_count;
- /* max_stack_cell_nums */
- size += (uint32)sizeof(uint32) * comp_data->func_count;
- #if WASM_ENABLE_GC != 0
- /* func_local_ref_flags */
- if (comp_ctx->enable_gc) {
- AOTFuncType *func_type;
- uint32 i, j, local_ref_flags_cell_num;
- for (i = 0; i < comp_data->import_func_count; i++) {
- func_type = comp_data->import_funcs[i].func_type;
- /* recalculate cell_num based on target pointer size */
- local_ref_flags_cell_num = 0;
- for (j = 0; j < func_type->param_count; j++) {
- local_ref_flags_cell_num += wasm_value_type_cell_num_internal(
- func_type->types[j], comp_ctx->pointer_size);
- }
- local_ref_flags_cell_num =
- local_ref_flags_cell_num > 2 ? local_ref_flags_cell_num : 2;
- size = align_uint(size, 4);
- size += (uint32)sizeof(uint32);
- size += (uint32)sizeof(uint8) * local_ref_flags_cell_num;
- }
- for (i = 0; i < comp_data->func_count; i++) {
- func_type = comp_data->funcs[i]->func_type;
- local_ref_flags_cell_num = comp_data->funcs[i]->param_cell_num
- + comp_data->funcs[i]->local_cell_num;
- size = align_uint(size, 4);
- size += (uint32)sizeof(uint32);
- size += (uint32)sizeof(uint8) * local_ref_flags_cell_num;
- }
- }
- #endif
- return size;
- }
- static uint32
- get_export_size(AOTCompContext *comp_ctx, AOTExport *export)
- {
- /* export index + export kind + 1 byte padding + export name */
- return (uint32)sizeof(uint32) + sizeof(uint8) + 1
- + get_string_size(comp_ctx, export->name);
- }
- static uint32
- get_exports_size(AOTCompContext *comp_ctx, AOTExport *exports,
- uint32 export_count)
- {
- AOTExport *export = exports;
- uint32 size = 0, i;
- for (i = 0; i < export_count; i++, export ++) {
- size = align_uint(size, 4);
- size += get_export_size(comp_ctx, export);
- }
- return size;
- }
- static uint32
- get_export_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- /* export count + exports */
- return (uint32)sizeof(uint32)
- + get_exports_size(comp_ctx, comp_data->wasm_module->exports,
- comp_data->wasm_module->export_count);
- }
- static uint32
- get_relocation_size(AOTRelocation *relocation, bool is_32bin)
- {
- /* offset + addend + relocation type + symbol name */
- uint32 size = 0;
- if (is_32bin)
- size = sizeof(uint32) * 2; /* offset and addend */
- else
- size = sizeof(uint64) * 2; /* offset and addend */
- size += (uint32)sizeof(uint32); /* relocation type */
- size += (uint32)sizeof(uint32); /* symbol name index */
- return size;
- }
- static uint32
- get_relocations_size(AOTObjectData *obj_data,
- AOTRelocationGroup *relocation_group,
- AOTRelocation *relocations, uint32 relocation_count,
- bool is_32bin)
- {
- AOTRelocation *relocation = relocations;
- uint32 size = 0, i;
- for (i = 0; i < relocation_count; i++, relocation++) {
- /* ignore the relocations to aot_func_internal#n in text section
- for windows platform since they will be applied in
- aot_emit_text_section */
- if ((!strcmp(relocation_group->section_name, ".text")
- || !strcmp(relocation_group->section_name, ".ltext"))
- && !strncmp(relocation->symbol_name, AOT_FUNC_INTERNAL_PREFIX,
- strlen(AOT_FUNC_INTERNAL_PREFIX))
- && ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
- /* Windows AOT_COFF64_BIN_TYPE */
- && obj_data->target_info.bin_type == 6
- /* IMAGE_REL_AMD64_REL32 in windows x86_64 */
- && relocation->relocation_type == 4)
- || (!strncmp(obj_data->comp_ctx->target_arch, "i386", 4)
- /* Windows AOT_COFF32_BIN_TYPE */
- && obj_data->target_info.bin_type == 4
- /* IMAGE_REL_I386_REL32 in windows x86_32 */
- && relocation->relocation_type == 20))) {
- continue;
- }
- size = align_uint(size, 4);
- size += get_relocation_size(relocation, is_32bin);
- }
- return size;
- }
- static uint32
- get_relocation_group_size(AOTObjectData *obj_data,
- AOTRelocationGroup *relocation_group, bool is_32bin)
- {
- uint32 size = 0;
- /* section name index + relocation count + relocations */
- size += (uint32)sizeof(uint32);
- size += (uint32)sizeof(uint32);
- size += get_relocations_size(obj_data, relocation_group,
- relocation_group->relocations,
- relocation_group->relocation_count, is_32bin);
- return size;
- }
- static uint32
- get_relocation_groups_size(AOTObjectData *obj_data,
- AOTRelocationGroup *relocation_groups,
- uint32 relocation_group_count, bool is_32bin)
- {
- AOTRelocationGroup *relocation_group = relocation_groups;
- uint32 size = 0, i;
- for (i = 0; i < relocation_group_count; i++, relocation_group++) {
- size = align_uint(size, 4);
- size += get_relocation_group_size(obj_data, relocation_group, is_32bin);
- }
- return size;
- }
- /* return the index (in order of insertion) of the symbol,
- create if not exits, -1 if failed */
- static uint32
- get_relocation_symbol_index(const char *symbol_name, bool *is_new,
- AOTSymbolList *symbol_list)
- {
- AOTSymbolNode *sym;
- uint32 index = 0;
- sym = symbol_list->head;
- while (sym) {
- if (!strcmp(sym->symbol, symbol_name)) {
- if (is_new)
- *is_new = false;
- return index;
- }
- sym = sym->next;
- index++;
- }
- /* Not found in symbol_list, add it */
- sym = wasm_runtime_malloc(sizeof(AOTSymbolNode));
- if (!sym) {
- return (uint32)-1;
- }
- memset(sym, 0, sizeof(AOTSymbolNode));
- sym->symbol = (char *)symbol_name;
- sym->str_len = (uint32)strlen(symbol_name);
- if (!symbol_list->head) {
- symbol_list->head = symbol_list->end = sym;
- }
- else {
- symbol_list->end->next = sym;
- symbol_list->end = sym;
- }
- symbol_list->len++;
- if (is_new)
- *is_new = true;
- return index;
- }
- static uint32
- get_relocation_symbol_size(AOTCompContext *comp_ctx, AOTRelocation *relocation,
- AOTSymbolList *symbol_list)
- {
- uint32 size = 0, index = 0;
- bool is_new = false;
- index = get_relocation_symbol_index(relocation->symbol_name, &is_new,
- symbol_list);
- CHECK_SIZE(index);
- if (is_new) {
- size += get_string_size(comp_ctx, relocation->symbol_name);
- size = align_uint(size, 2);
- }
- relocation->symbol_index = index;
- return size;
- }
- static uint32
- get_relocations_symbol_size(AOTCompContext *comp_ctx,
- AOTRelocation *relocations, uint32 relocation_count,
- AOTSymbolList *symbol_list)
- {
- AOTRelocation *relocation = relocations;
- uint32 size = 0, curr_size, i;
- for (i = 0; i < relocation_count; i++, relocation++) {
- curr_size =
- get_relocation_symbol_size(comp_ctx, relocation, symbol_list);
- CHECK_SIZE(curr_size);
- size += curr_size;
- }
- return size;
- }
- static uint32
- get_relocation_group_symbol_size(AOTCompContext *comp_ctx,
- AOTRelocationGroup *relocation_group,
- AOTSymbolList *symbol_list)
- {
- uint32 size = 0, index = 0, curr_size;
- bool is_new = false;
- index = get_relocation_symbol_index(relocation_group->section_name, &is_new,
- symbol_list);
- CHECK_SIZE(index);
- if (is_new) {
- size += get_string_size(comp_ctx, relocation_group->section_name);
- size = align_uint(size, 2);
- }
- relocation_group->name_index = index;
- curr_size = get_relocations_symbol_size(
- comp_ctx, relocation_group->relocations,
- relocation_group->relocation_count, symbol_list);
- CHECK_SIZE(curr_size);
- size += curr_size;
- return size;
- }
- static uint32
- get_relocation_groups_symbol_size(AOTCompContext *comp_ctx,
- AOTRelocationGroup *relocation_groups,
- uint32 relocation_group_count,
- AOTSymbolList *symbol_list)
- {
- AOTRelocationGroup *relocation_group = relocation_groups;
- uint32 size = 0, curr_size, i;
- for (i = 0; i < relocation_group_count; i++, relocation_group++) {
- curr_size = get_relocation_group_symbol_size(comp_ctx, relocation_group,
- symbol_list);
- CHECK_SIZE(curr_size);
- size += curr_size;
- }
- return size;
- }
- static uint32
- get_symbol_size_from_symbol_list(AOTCompContext *comp_ctx,
- AOTSymbolList *symbol_list)
- {
- AOTSymbolNode *sym;
- uint32 size = 0;
- sym = symbol_list->head;
- while (sym) {
- /* (uint16)str_len + str */
- size += get_string_size(comp_ctx, sym->symbol);
- size = align_uint(size, 2);
- sym = sym->next;
- }
- return size;
- }
- static uint32
- get_relocation_section_symbol_size(AOTCompContext *comp_ctx,
- AOTObjectData *obj_data)
- {
- AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
- uint32 relocation_group_count = obj_data->relocation_group_count;
- uint32 string_count = 0, symbol_table_size = 0;
- /* section size will be calculated twice,
- get symbol size from symbol list directly in the second calculation */
- if (obj_data->symbol_list.len > 0) {
- symbol_table_size =
- get_symbol_size_from_symbol_list(comp_ctx, &obj_data->symbol_list);
- }
- else {
- symbol_table_size = get_relocation_groups_symbol_size(
- comp_ctx, relocation_groups, relocation_group_count,
- &obj_data->symbol_list);
- }
- CHECK_SIZE(symbol_table_size);
- string_count = obj_data->symbol_list.len;
- /* string_count + string_offsets + total_string_len
- + [str (string_len + str)] */
- return (uint32)(sizeof(uint32) + sizeof(uint32) * string_count
- + sizeof(uint32) + symbol_table_size);
- }
- static uint32
- get_relocation_section_size(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
- {
- AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
- uint32 relocation_group_count = obj_data->relocation_group_count;
- uint32 symbol_table_size = 0;
- symbol_table_size = get_relocation_section_symbol_size(comp_ctx, obj_data);
- CHECK_SIZE(symbol_table_size);
- symbol_table_size = align_uint(symbol_table_size, 4);
- /* relocation group count + symbol_table + relocation groups */
- return (uint32)sizeof(uint32) + symbol_table_size
- + get_relocation_groups_size(obj_data, relocation_groups,
- relocation_group_count,
- is_32bit_binary(obj_data));
- }
- static uint32
- get_native_symbol_list_size(AOTCompContext *comp_ctx)
- {
- uint32 len = 0;
- AOTNativeSymbol *sym = NULL;
- sym = bh_list_first_elem(&comp_ctx->native_symbols);
- while (sym) {
- len = align_uint(len, 2);
- len += get_string_size(comp_ctx, sym->symbol);
- sym = bh_list_elem_next(sym);
- }
- return len;
- }
- #if WASM_ENABLE_STRINGREF != 0
- static uint32
- get_string_literal_section_size(AOTCompContext *comp_ctx,
- AOTCompData *comp_data);
- #endif
- static uint32
- get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data);
- static uint32
- get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 size = 0;
- uint32 size_custom_section = 0;
- #if WASM_ENABLE_STRINGREF != 0
- uint32 size_string_literal_section = 0;
- #endif
- /* aot file header */
- size += get_file_header_size();
- /* target info section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_target_info_section_size();
- /* init data section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_init_data_section_size(comp_ctx, comp_data, obj_data);
- /* text section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_text_section_size(obj_data);
- /* function section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_func_section_size(comp_ctx, comp_data, obj_data);
- /* export section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_export_section_size(comp_ctx, comp_data);
- /* relocation section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_relocation_section_size(comp_ctx, obj_data);
- if (get_native_symbol_list_size(comp_ctx) > 0) {
- /* emit only when there are native symbols */
- size = align_uint(size, 4);
- /* section id + section size + sub section id + symbol count */
- size += (uint32)sizeof(uint32) * 4;
- size += get_native_symbol_list_size(comp_ctx);
- }
- size_custom_section = get_custom_sections_size(comp_ctx, comp_data);
- if (size_custom_section > 0) {
- size = align_uint(size, 4);
- size += size_custom_section;
- }
- #if WASM_ENABLE_STRINGREF != 0
- /* string literal section */
- size_string_literal_section =
- get_string_literal_section_size(comp_ctx, comp_data);
- if (size_string_literal_section > 0) {
- size = align_uint(size, 4);
- /* section id + section size + sub section id */
- size += (uint32)sizeof(uint32) * 3;
- size += size_string_literal_section;
- }
- #endif
- return size;
- }
- #define exchange_uint8(p_data) (void)0
- static void
- exchange_uint16(uint8 *p_data)
- {
- uint8 value = *p_data;
- *p_data = *(p_data + 1);
- *(p_data + 1) = value;
- }
- static void
- exchange_uint32(uint8 *p_data)
- {
- uint8 value = *p_data;
- *p_data = *(p_data + 3);
- *(p_data + 3) = value;
- value = *(p_data + 1);
- *(p_data + 1) = *(p_data + 2);
- *(p_data + 2) = value;
- }
- static void
- exchange_uint64(uint8 *p_data)
- {
- uint32 value;
- value = *(uint32 *)p_data;
- *(uint32 *)p_data = *(uint32 *)(p_data + 4);
- *(uint32 *)(p_data + 4) = value;
- exchange_uint32(p_data);
- exchange_uint32(p_data + 4);
- }
- static void
- exchange_uint128(uint8 *p_data)
- {
- /* swap high 64bit and low 64bit */
- uint64 value = *(uint64 *)p_data;
- *(uint64 *)p_data = *(uint64 *)(p_data + 8);
- *(uint64 *)(p_data + 8) = value;
- /* exchange high 64bit */
- exchange_uint64(p_data);
- /* exchange low 64bit */
- exchange_uint64(p_data + 8);
- }
- static union {
- int a;
- char b;
- } __ue = { .a = 1 };
- #define is_little_endian() (__ue.b == 1)
- #define CHECK_BUF(length) \
- do { \
- if (buf + offset + length > buf_end) { \
- aot_set_last_error("buf overflow"); \
- return false; \
- } \
- } while (0)
- #define EMIT_U8(v) \
- do { \
- CHECK_BUF(1); \
- *(uint8 *)(buf + offset) = (uint8)v; \
- offset++; \
- } while (0)
- #define EMIT_U16(v) \
- do { \
- uint16 t = (uint16)v; \
- CHECK_BUF(2); \
- if (!is_little_endian()) \
- exchange_uint16((uint8 *)&t); \
- *(uint16 *)(buf + offset) = t; \
- offset += (uint32)sizeof(uint16); \
- } while (0)
- #define EMIT_U32(v) \
- do { \
- uint32 t = (uint32)v; \
- CHECK_BUF(4); \
- if (!is_little_endian()) \
- exchange_uint32((uint8 *)&t); \
- *(uint32 *)(buf + offset) = t; \
- offset += (uint32)sizeof(uint32); \
- } while (0)
- #define EMIT_U64(v) \
- do { \
- uint64 t = (uint64)v; \
- CHECK_BUF(8); \
- if (!is_little_endian()) \
- exchange_uint64((uint8 *)&t); \
- PUT_U64_TO_ADDR(buf + offset, t); \
- offset += (uint32)sizeof(uint64); \
- } while (0)
- #define EMIT_V128(v) \
- do { \
- uint64 *t = (uint64 *)v.i64x2; \
- CHECK_BUF(16); \
- if (!is_little_endian()) \
- exchange_uint128((uint8 *)t); \
- PUT_U64_TO_ADDR(buf + offset, t[0]); \
- offset += (uint32)sizeof(uint64); \
- PUT_U64_TO_ADDR(buf + offset, t[1]); \
- offset += (uint32)sizeof(uint64); \
- } while (0)
- #define EMIT_BUF(v, len) \
- do { \
- CHECK_BUF(len); \
- memcpy(buf + offset, v, len); \
- offset += len; \
- } while (0)
- /* Emit string with '\0'
- */
- #define EMIT_STR(s) \
- do { \
- uint32 str_len = (uint32)strlen(s) + 1; \
- if (str_len > INT16_MAX) { \
- aot_set_last_error("emit string failed: " \
- "string too long"); \
- return false; \
- } \
- EMIT_U16(str_len); \
- EMIT_BUF(s, str_len); \
- } while (0)
- #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
- static bool
- read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
- uint64 *p_result)
- {
- const uint8 *buf = *p_buf;
- uint64 result = 0;
- uint32 shift = 0;
- uint32 offset = 0, bcnt = 0;
- uint64 byte;
- while (true) {
- /* uN or SN must not exceed ceil(N/7) bytes */
- if (bcnt + 1 > (maxbits + 6) / 7) {
- aot_set_last_error("integer representation too long");
- return false;
- }
- if (buf + offset + 1 > buf_end) {
- aot_set_last_error("unexpected end of section or function");
- return false;
- }
- byte = buf[offset];
- offset += 1;
- result |= ((byte & 0x7f) << shift);
- shift += 7;
- bcnt += 1;
- if ((byte & 0x80) == 0) {
- break;
- }
- }
- if (!sign && maxbits == 32 && shift >= maxbits) {
- /* The top bits set represent values > 32 bits */
- if (((uint8)byte) & 0xf0)
- goto fail_integer_too_large;
- }
- else if (sign && maxbits == 32) {
- if (shift < maxbits) {
- /* Sign extend, second highest bit is the sign bit */
- if ((uint8)byte & 0x40)
- result |= (~((uint64)0)) << shift;
- }
- else {
- /* The top bits should be a sign-extension of the sign bit */
- bool sign_bit_set = ((uint8)byte) & 0x8;
- int top_bits = ((uint8)byte) & 0xf0;
- if ((sign_bit_set && top_bits != 0x70)
- || (!sign_bit_set && top_bits != 0))
- goto fail_integer_too_large;
- }
- }
- else if (sign && maxbits == 64) {
- if (shift < maxbits) {
- /* Sign extend, second highest bit is the sign bit */
- if ((uint8)byte & 0x40)
- result |= (~((uint64)0)) << shift;
- }
- else {
- /* The top bits should be a sign-extension of the sign bit */
- bool sign_bit_set = ((uint8)byte) & 0x1;
- int top_bits = ((uint8)byte) & 0xfe;
- if ((sign_bit_set && top_bits != 0x7e)
- || (!sign_bit_set && top_bits != 0))
- goto fail_integer_too_large;
- }
- }
- *p_buf += offset;
- *p_result = result;
- return true;
- fail_integer_too_large:
- aot_set_last_error("integer too large");
- return false;
- }
- /* NOLINTNEXTLINE */
- #define read_leb_uint32(p, p_end, res) \
- do { \
- uint64 res64; \
- if (!read_leb((uint8 **)&p, p_end, 32, false, &res64)) \
- goto fail; \
- res = (uint32)res64; \
- } while (0)
- /*
- * - transfer .name section in .wasm (comp_data->name_section_buf) to
- * aot buf (comp_data->aot_name_section_buf)
- * - leb128 to u32
- * - add `\0` at the end of every name, and adjust length(+1)
- */
- static uint32
- get_name_section_size(AOTCompData *comp_data)
- {
- /* original name section content in .wasm */
- const uint8 *p = comp_data->name_section_buf,
- *p_end = comp_data->name_section_buf_end;
- uint8 *buf, *buf_end;
- uint32 name_type, subsection_size;
- uint32 previous_name_type = 0;
- uint32 num_func_name;
- uint32 func_index;
- uint32 previous_func_index = ~0U;
- uint32 func_name_len;
- uint32 name_index;
- int i = 0;
- uint32 name_len;
- uint32 offset = 0;
- uint32 max_aot_buf_size = 0;
- if (p >= p_end) {
- aot_set_last_error("unexpected end");
- return 0;
- }
- max_aot_buf_size = 4 * (uint32)(p_end - p);
- if (!(buf = comp_data->aot_name_section_buf =
- wasm_runtime_malloc(max_aot_buf_size))) {
- aot_set_last_error("allocate memory for custom name section failed.");
- return 0;
- }
- memset(buf, 0, (uint32)max_aot_buf_size);
- buf_end = buf + max_aot_buf_size;
- /* the size of "name". it should be 4 */
- read_leb_uint32(p, p_end, name_len);
- offset = align_uint(offset, 4);
- EMIT_U32(name_len);
- if (name_len != 4 || p + name_len > p_end) {
- aot_set_last_error("unexpected end");
- return 0;
- }
- /* "name" */
- if (memcmp(p, "name", 4) != 0) {
- aot_set_last_error("invalid custom name section");
- return 0;
- }
- EMIT_BUF(p, name_len);
- p += name_len;
- while (p < p_end) {
- read_leb_uint32(p, p_end, name_type);
- if (i != 0) {
- if (name_type == previous_name_type) {
- aot_set_last_error("duplicate sub-section");
- return 0;
- }
- if (name_type < previous_name_type) {
- aot_set_last_error("out-of-order sub-section");
- return 0;
- }
- }
- previous_name_type = name_type;
- read_leb_uint32(p, p_end, subsection_size);
- switch (name_type) {
- case SUB_SECTION_TYPE_FUNC:
- if (subsection_size) {
- offset = align_uint(offset, 4);
- EMIT_U32(name_type);
- EMIT_U32(subsection_size);
- read_leb_uint32(p, p_end, num_func_name);
- EMIT_U32(num_func_name);
- for (name_index = 0; name_index < num_func_name;
- name_index++) {
- read_leb_uint32(p, p_end, func_index);
- offset = align_uint(offset, 4);
- EMIT_U32(func_index);
- if (func_index == previous_func_index) {
- aot_set_last_error("duplicate function name");
- return 0;
- }
- if (func_index < previous_func_index
- && previous_func_index != ~0U) {
- aot_set_last_error("out-of-order function index ");
- return 0;
- }
- previous_func_index = func_index;
- read_leb_uint32(p, p_end, func_name_len);
- offset = align_uint(offset, 2);
- /* emit a string ends with `\0` */
- if (func_name_len + 1 > UINT16_MAX) {
- aot_set_last_error(
- "emit string failed: string too long");
- goto fail;
- }
- /* extra 1 byte for \0 */
- EMIT_U16(func_name_len + 1);
- EMIT_BUF(p, func_name_len);
- p += func_name_len;
- EMIT_U8(0);
- }
- }
- break;
- case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
- */
- case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
- default:
- p = p + subsection_size;
- break;
- }
- i++;
- }
- return offset;
- fail:
- return 0;
- }
- #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
- #if WASM_ENABLE_STRINGREF != 0
- static uint32
- get_string_literal_section_size(AOTCompContext *comp_ctx,
- AOTCompData *comp_data)
- {
- uint32 i;
- uint32 size = 0;
- uint32 string_count = comp_data->string_literal_count;
- if (string_count == 0) {
- return 0;
- }
- /* reserved slot + string count + string_lengths */
- size += sizeof(uint32) * (2 + string_count);
- for (i = 0; i < string_count; i++) {
- size += comp_data->string_literal_lengths_wp[i];
- }
- return size;
- }
- #endif /* end of WASM_ENABLE_STRINGREF != 0 */
- static uint32
- get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
- {
- #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
- uint32 size = 0, i;
- for (i = 0; i < comp_ctx->custom_sections_count; i++) {
- const char *section_name = comp_ctx->custom_sections_wp[i];
- const uint8 *content = NULL;
- uint32 length = 0;
- if (strcmp(section_name, "name") == 0) {
- /* custom name section */
- comp_data->aot_name_section_size = get_name_section_size(comp_data);
- if (comp_data->aot_name_section_size == 0) {
- LOG_WARNING("Can't find custom section [name], ignore it");
- continue;
- }
- size = align_uint(size, 4);
- /* section id + section size + sub section id */
- size += (uint32)sizeof(uint32) * 3;
- size += comp_data->aot_name_section_size;
- continue;
- }
- content = wasm_loader_get_custom_section(comp_data->wasm_module,
- section_name, &length);
- if (!content) {
- LOG_WARNING("Can't find custom section [%s], ignore it",
- section_name);
- continue;
- }
- size = align_uint(size, 4);
- /* section id + section size + sub section id */
- size += (uint32)sizeof(uint32) * 3;
- /* section name and len */
- size += get_string_size(comp_ctx, section_name);
- /* section content */
- size += length;
- }
- return size;
- #else
- return 0;
- #endif
- }
- static bool
- aot_emit_file_header(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset;
- uint32 aot_curr_version = AOT_CURRENT_VERSION;
- EMIT_U8('\0');
- EMIT_U8('a');
- EMIT_U8('o');
- EMIT_U8('t');
- EMIT_U32(aot_curr_version);
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_target_info_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset;
- uint32 section_size = get_target_info_section_size();
- AOTTargetInfo *target_info = &obj_data->target_info;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_TARGET_INFO);
- EMIT_U32(section_size);
- EMIT_U16(target_info->bin_type);
- EMIT_U16(target_info->abi_type);
- EMIT_U16(target_info->e_type);
- EMIT_U16(target_info->e_machine);
- EMIT_U32(target_info->e_version);
- EMIT_U32(target_info->e_flags);
- EMIT_U64(target_info->feature_flags);
- EMIT_U64(target_info->reserved);
- EMIT_BUF(target_info->arch, sizeof(target_info->arch));
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit target info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_init_expr(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, InitializerExpression *expr);
- static bool
- aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTMemInitData **init_datas = comp_data->mem_init_data_list;
- *p_offset = offset = align_uint(offset, 4);
- /* Emit import memory count, only emit 0 currently.
- TODO: emit the actual import memory count and
- the full import memory info. */
- EMIT_U32(0);
- /* Emit memory count */
- EMIT_U32(comp_data->memory_count);
- /* Emit memory items */
- for (i = 0; i < comp_data->memory_count; i++) {
- EMIT_U32(comp_data->memories[i].memory_flags);
- EMIT_U32(comp_data->memories[i].num_bytes_per_page);
- EMIT_U32(comp_data->memories[i].mem_init_page_count);
- EMIT_U32(comp_data->memories[i].mem_max_page_count);
- }
- /* Emit mem init data count */
- EMIT_U32(comp_data->mem_init_data_count);
- /* Emit mem init data items */
- for (i = 0; i < comp_data->mem_init_data_count; i++) {
- offset = align_uint(offset, 4);
- #if WASM_ENABLE_BULK_MEMORY != 0
- if (comp_ctx->enable_bulk_memory) {
- EMIT_U32(init_datas[i]->is_passive);
- EMIT_U32(init_datas[i]->memory_index);
- }
- else
- #endif
- {
- /* emit two placeholder to keep the same size */
- EMIT_U32(0);
- EMIT_U32(0);
- }
- if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx,
- &init_datas[i]->offset))
- return false;
- EMIT_U32(init_datas[i]->byte_count);
- EMIT_BUF(init_datas[i]->bytes, init_datas[i]->byte_count);
- }
- if (offset - *p_offset != get_mem_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit memory info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_init_expr(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, InitializerExpression *expr)
- {
- uint32 offset = *p_offset;
- #if WASM_ENABLE_GC != 0
- WASMModule *module = comp_ctx->comp_data->wasm_module;
- #endif
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(expr->init_expr_type);
- switch (expr->init_expr_type) {
- case INIT_EXPR_NONE:
- break;
- case INIT_EXPR_TYPE_I32_CONST:
- case INIT_EXPR_TYPE_F32_CONST:
- EMIT_U32(expr->u.i32);
- break;
- case INIT_EXPR_TYPE_I64_CONST:
- case INIT_EXPR_TYPE_F64_CONST:
- EMIT_U64(expr->u.i64);
- break;
- case INIT_EXPR_TYPE_V128_CONST:
- EMIT_V128(expr->u.v128);
- break;
- case INIT_EXPR_TYPE_GET_GLOBAL:
- EMIT_U32(expr->u.global_index);
- break;
- case INIT_EXPR_TYPE_FUNCREF_CONST:
- case INIT_EXPR_TYPE_REFNULL_CONST:
- EMIT_U32(expr->u.ref_index);
- break;
- #if WASM_ENABLE_GC != 0
- case INIT_EXPR_TYPE_I31_NEW:
- EMIT_U32(expr->u.i32);
- break;
- case INIT_EXPR_TYPE_STRUCT_NEW:
- {
- uint32 i;
- WASMStructNewInitValues *init_values =
- (WASMStructNewInitValues *)expr->u.data;
- WASMStructType *struct_type = NULL;
- EMIT_U32(init_values->type_idx);
- EMIT_U32(init_values->count);
- bh_assert(init_values->type_idx < module->type_count);
- struct_type =
- (WASMStructType *)module->types[init_values->type_idx];
- bh_assert(struct_type);
- bh_assert(struct_type->field_count == init_values->count);
- for (i = 0; i < init_values->count; i++) {
- uint32 field_size = wasm_value_type_size_internal(
- struct_type->fields[i].field_type, comp_ctx->pointer_size);
- if (field_size <= sizeof(uint32))
- EMIT_U32(init_values->fields[i].u32);
- else if (field_size == sizeof(uint64))
- EMIT_U64(init_values->fields[i].u64);
- else if (field_size == sizeof(uint64) * 2)
- EMIT_V128(init_values->fields[i].v128);
- else {
- bh_assert(0);
- }
- }
- break;
- }
- case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
- EMIT_U32(expr->u.type_index);
- break;
- case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
- {
- WASMArrayType *array_type = NULL;
- bh_assert(expr->u.array_new_default.type_index
- < module->type_count);
- array_type =
- (WASMArrayType *)
- module->types[expr->u.array_new_default.type_index];
- EMIT_U32(array_type->elem_type);
- EMIT_U32(expr->u.array_new_default.type_index);
- EMIT_U32(expr->u.array_new_default.length);
- break;
- }
- case INIT_EXPR_TYPE_ARRAY_NEW:
- case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
- {
- uint32 value_count, i, field_size;
- WASMArrayNewInitValues *init_values =
- (WASMArrayNewInitValues *)expr->u.data;
- WASMArrayType *array_type = NULL;
- bh_assert(init_values->type_idx < module->type_count);
- array_type = (WASMArrayType *)module->types[init_values->type_idx];
- EMIT_U32(array_type->elem_type);
- EMIT_U32(init_values->type_idx);
- EMIT_U32(init_values->length);
- value_count =
- (expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED)
- ? init_values->length
- : 1;
- field_size = wasm_value_type_size_internal(array_type->elem_type,
- comp_ctx->pointer_size);
- for (i = 0; i < value_count; i++) {
- if (field_size <= sizeof(uint32))
- EMIT_U32(init_values->elem_data[i].u32);
- else if (field_size == sizeof(uint64))
- EMIT_U64(init_values->elem_data[i].u64);
- else if (field_size == sizeof(uint64) * 2)
- EMIT_V128(init_values->elem_data[i].v128);
- else {
- bh_assert(0);
- }
- }
- break;
- }
- #endif /* end of WASM_ENABLE_GC != 0 */
- default:
- aot_set_last_error("invalid init expr type.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i, j;
- AOTTableInitData **init_datas = comp_data->table_init_data_list;
- *p_offset = offset = align_uint(offset, 4);
- /* Emit import table count */
- EMIT_U32(comp_data->import_table_count);
- /* Emit table items */
- for (i = 0; i < comp_data->import_table_count; i++) {
- /* TODO:
- * EMIT_STR(comp_data->import_tables[i].module_name );
- * EMIT_STR(comp_data->import_tables[i].table_name);
- */
- EMIT_U8(comp_data->import_tables[i].elem_type);
- EMIT_U8(comp_data->import_tables[i].table_flags);
- EMIT_U8(comp_data->import_tables[i].possible_grow);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc && comp_data->import_tables[i].elem_ref_type) {
- EMIT_U8(comp_data->import_tables[i]
- .elem_ref_type->ref_ht_common.nullable);
- }
- else
- #endif
- {
- /* emit one placeholder to keep the same size */
- EMIT_U8(0);
- }
- EMIT_U32(comp_data->import_tables[i].table_init_size);
- EMIT_U32(comp_data->import_tables[i].table_max_size);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc && comp_data->import_tables[i].elem_ref_type) {
- bh_assert(wasm_is_type_multi_byte_type(
- comp_data->import_tables[i].elem_type));
- EMIT_U32(comp_data->import_tables[i]
- .elem_ref_type->ref_ht_common.heap_type);
- }
- #endif
- }
- /* Emit table count */
- EMIT_U32(comp_data->table_count);
- /* Emit table items */
- for (i = 0; i < comp_data->table_count; i++) {
- EMIT_U8(comp_data->tables[i].elem_type);
- EMIT_U8(comp_data->tables[i].table_flags);
- EMIT_U8(comp_data->tables[i].possible_grow);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc && comp_data->tables[i].elem_ref_type) {
- EMIT_U8(comp_data->tables[i].elem_ref_type->ref_ht_common.nullable);
- }
- else
- #endif
- {
- /* emit one placeholder to keep the same size */
- EMIT_U8(0);
- }
- EMIT_U32(comp_data->tables[i].table_init_size);
- EMIT_U32(comp_data->tables[i].table_max_size);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc) {
- if (comp_data->tables[i].elem_ref_type) {
- bh_assert(wasm_is_type_multi_byte_type(
- comp_data->tables[i].elem_type));
- EMIT_U32(comp_data->tables[i]
- .elem_ref_type->ref_ht_common.heap_type);
- }
- if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx,
- &comp_data->tables[i].init_expr)) {
- return false;
- }
- }
- #endif
- }
- /* Emit table init data count */
- EMIT_U32(comp_data->table_init_data_count);
- /* Emit table init data items */
- for (i = 0; i < comp_data->table_init_data_count; i++) {
- offset = align_uint(offset, 4);
- EMIT_U32(init_datas[i]->mode);
- EMIT_U32(init_datas[i]->elem_type);
- EMIT_U32(init_datas[i]->table_index);
- EMIT_U32(init_datas[i]->offset.init_expr_type);
- EMIT_U64(init_datas[i]->offset.u.i64);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc && init_datas[i]->elem_ref_type) {
- EMIT_U16(init_datas[i]->elem_ref_type->ref_ht_common.ref_type);
- EMIT_U16(init_datas[i]->elem_ref_type->ref_ht_common.nullable);
- EMIT_U32(init_datas[i]->elem_ref_type->ref_ht_common.heap_type);
- }
- else
- #endif
- {
- EMIT_U16(init_datas[i]->elem_type);
- EMIT_U16(0);
- EMIT_U32(0);
- }
- EMIT_U32(init_datas[i]->value_count);
- for (j = 0; j < init_datas[i]->value_count; j++) {
- if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx,
- &init_datas[i]->init_values[j]))
- return false;
- }
- }
- if (offset - *p_offset != get_table_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit table info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- #if WASM_ENABLE_GC != 0
- static bool
- aot_emit_reftype_map(uint8 *buf, uint8 *buf_end, uint32 *p_offset, uint32 count,
- WASMRefTypeMap *refmap)
- {
- uint32 offset = *p_offset, i;
- for (i = 0; i < count; i++) {
- EMIT_U16(refmap->index);
- WASMRefType *ref_type = refmap->ref_type;
- /* Note: WASMRefType is a union type */
- EMIT_U8(ref_type->ref_ht_common.ref_type);
- EMIT_U8(ref_type->ref_ht_common.nullable);
- EMIT_U32(ref_type->ref_ht_common.heap_type);
- refmap++;
- }
- *p_offset = offset;
- return true;
- }
- #endif
- static bool
- aot_emit_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->type_count);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc) {
- AOTType **types = comp_data->types;
- int32 idx;
- uint32 j;
- for (i = 0; i < comp_data->type_count; i++) {
- offset = align_uint(offset, 4);
- /* Emit simple info if there is an equivalence type */
- for (j = 0; j < i; j++) {
- if (types[j] == types[i]) {
- EMIT_U16(types[i]->type_flag);
- /* equivalence type flag is true */
- EMIT_U8(1);
- EMIT_U8(0);
- /* equivalence type index */
- EMIT_U32(j);
- break;
- }
- }
- if (j < i)
- continue;
- EMIT_U16(types[i]->type_flag);
- /* equivalence type flag is false */
- EMIT_U8(0);
- EMIT_U8(types[i]->is_sub_final);
- EMIT_U32(types[i]->parent_type_idx);
- EMIT_U16(types[i]->rec_count);
- EMIT_U16(types[i]->rec_idx);
- /* Emit WASM_TYPE_FUNC */
- if (types[i]->type_flag == WASM_TYPE_FUNC) {
- AOTFuncType *func_type = (AOTFuncType *)types[i];
- EMIT_U16(func_type->param_count);
- EMIT_U16(func_type->result_count);
- EMIT_U16(func_type->ref_type_map_count);
- EMIT_BUF(func_type->types,
- func_type->param_count + func_type->result_count);
- offset = align_uint(offset, 4);
- aot_emit_reftype_map(buf, buf_end, &offset,
- func_type->ref_type_map_count,
- func_type->ref_type_maps);
- }
- /* Emit WASM_TYPE_STRUCT */
- else if (types[i]->type_flag == WASM_TYPE_STRUCT) {
- AOTStructType *struct_type = (AOTStructType *)types[i];
- EMIT_U16(struct_type->field_count);
- EMIT_U16(struct_type->ref_type_map_count);
- for (idx = 0; idx < struct_type->field_count; idx++) {
- EMIT_U8(struct_type->fields[idx].field_flags);
- EMIT_U8(struct_type->fields[idx].field_type);
- }
- offset = align_uint(offset, 4);
- aot_emit_reftype_map(buf, buf_end, &offset,
- struct_type->ref_type_map_count,
- struct_type->ref_type_maps);
- }
- /* Emit WASM_TYPE_ARRAY */
- else if (types[i]->type_flag == WASM_TYPE_ARRAY) {
- AOTArrayType *array_type = (AOTArrayType *)types[i];
- EMIT_U16(array_type->elem_flags);
- EMIT_U8(array_type->elem_type);
- if (array_type->elem_ref_type) {
- bh_assert(
- wasm_is_type_multi_byte_type(array_type->elem_type));
- EMIT_U8(array_type->elem_ref_type->ref_ht_common.nullable);
- EMIT_U32(
- array_type->elem_ref_type->ref_ht_common.heap_type);
- }
- }
- else {
- aot_set_last_error("invalid type flag.");
- return false;
- }
- }
- if (offset - *p_offset != get_type_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit function type info failed.");
- return false;
- }
- *p_offset = offset;
- }
- else
- #endif
- {
- AOTFuncType **func_types = (AOTFuncType **)comp_data->types;
- for (i = 0; i < comp_data->type_count; i++) {
- offset = align_uint(offset, 4);
- /* If GC is disabled, only emit function type info */
- EMIT_U16(WASM_TYPE_FUNC);
- /* Omit to emit dummy padding for is_sub_final,
- * parent_type_index, rec_count, rec_idx, 10 bytes in total */
- EMIT_U16(func_types[i]->param_count);
- EMIT_U16(func_types[i]->result_count);
- /* Omit to emit dummy padding for ref_type_map_count, 2 bytes in
- * total */
- EMIT_BUF(func_types[i]->types,
- func_types[i]->param_count + func_types[i]->result_count);
- }
- if (offset - *p_offset != get_type_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit function type info failed.");
- return false;
- }
- *p_offset = offset;
- }
- return true;
- }
- static bool
- aot_emit_import_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTImportGlobal *import_global = comp_data->import_globals;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->import_global_count);
- for (i = 0; i < comp_data->import_global_count; i++, import_global++) {
- offset = align_uint(offset, 2);
- EMIT_U8(import_global->type);
- EMIT_U8(import_global->is_mutable);
- EMIT_STR(import_global->module_name);
- offset = align_uint(offset, 2);
- EMIT_STR(import_global->global_name);
- }
- if (offset - *p_offset
- != get_import_global_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit import global info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTGlobal *global = comp_data->globals;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->global_count);
- for (i = 0; i < comp_data->global_count; i++, global++) {
- offset = align_uint(offset, 4);
- EMIT_U8(global->type);
- EMIT_U8(global->is_mutable);
- offset = align_uint(offset, 4);
- if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx,
- &global->init_expr))
- return false;
- }
- if (offset - *p_offset != get_global_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit global info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTImportFunc *import_func = comp_data->import_funcs;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->import_func_count);
- for (i = 0; i < comp_data->import_func_count; i++, import_func++) {
- offset = align_uint(offset, 2);
- EMIT_U16(import_func->func_type_index);
- EMIT_STR(import_func->module_name);
- offset = align_uint(offset, 2);
- EMIT_STR(import_func->func_name);
- }
- if (offset - *p_offset != get_import_func_info_size(comp_ctx, comp_data)) {
- aot_set_last_error("emit import function info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_object_data_section_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTObjectDataSection *data_section = obj_data->data_sections;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(obj_data->data_sections_count);
- for (i = 0; i < obj_data->data_sections_count; i++, data_section++) {
- offset = align_uint(offset, 2);
- EMIT_STR(data_section->name);
- offset = align_uint(offset, 4);
- EMIT_U32(data_section->size);
- if (obj_data->stack_sizes_section_name != NULL
- && !strcmp(obj_data->stack_sizes_section_name,
- data_section->name)) {
- uint32 ss_offset = obj_data->stack_sizes_offset;
- uint32 ss_size =
- obj_data->func_count * sizeof(*obj_data->stack_sizes);
- LOG_VERBOSE("Replacing stack_sizes in %s section, offset %" PRIu32
- ", size %" PRIu32,
- obj_data->stack_sizes_section_name, ss_offset, ss_size);
- bh_assert(ss_offset + ss_size <= data_section->size);
- /* 0 .. ss_offset */
- if (ss_offset > 0) {
- EMIT_BUF(data_section->data, ss_offset);
- }
- /* ss_offset .. ss_offset+ss_size */
- EMIT_BUF(obj_data->stack_sizes, ss_size);
- /* ss_offset+ss_size .. data_section->size */
- if (data_section->size > ss_offset + ss_size) {
- EMIT_BUF(data_section->data + ss_offset + ss_size,
- data_section->size - (ss_offset + ss_size));
- }
- }
- else {
- EMIT_BUF(data_section->data, data_section->size);
- }
- }
- if (offset - *p_offset
- != get_object_data_section_info_size(comp_ctx, obj_data)) {
- aot_set_last_error("emit object data section info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 section_size =
- get_init_data_section_size(comp_ctx, comp_data, obj_data);
- uint32 offset = *p_offset;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_INIT_DATA);
- EMIT_U32(section_size);
- if (!aot_emit_mem_info(buf, buf_end, &offset, comp_ctx, comp_data, obj_data)
- || !aot_emit_table_info(buf, buf_end, &offset, comp_ctx, comp_data,
- obj_data)
- || !aot_emit_type_info(buf, buf_end, &offset, comp_ctx, comp_data,
- obj_data)
- || !aot_emit_import_global_info(buf, buf_end, &offset, comp_ctx,
- comp_data, obj_data)
- || !aot_emit_global_info(buf, buf_end, &offset, comp_ctx, comp_data,
- obj_data)
- || !aot_emit_import_func_info(buf, buf_end, &offset, comp_ctx,
- comp_data, obj_data))
- return false;
- offset = align_uint(offset, 4);
- EMIT_U32(comp_data->func_count);
- EMIT_U32(comp_data->start_func_index);
- EMIT_U32(comp_data->aux_data_end_global_index);
- EMIT_U64(comp_data->aux_data_end);
- EMIT_U32(comp_data->aux_heap_base_global_index);
- EMIT_U64(comp_data->aux_heap_base);
- EMIT_U32(comp_data->aux_stack_top_global_index);
- EMIT_U64(comp_data->aux_stack_bottom);
- EMIT_U32(comp_data->aux_stack_size);
- if (!aot_emit_object_data_section_info(buf, buf_end, &offset, comp_ctx,
- obj_data))
- return false;
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit init data section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 section_size = get_text_section_size(obj_data);
- uint32 offset = *p_offset;
- uint8 placeholder = 0;
- AOTRelocationGroup *relocation_group;
- AOTRelocation *relocation;
- uint32 i, j, relocation_count;
- uint8 *text;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_TEXT);
- EMIT_U32(section_size);
- EMIT_U32(obj_data->literal_size);
- if (obj_data->literal_size > 0) {
- EMIT_BUF(obj_data->literal, obj_data->literal_size);
- while (offset & 3)
- EMIT_BUF(&placeholder, 1);
- }
- text = buf + offset;
- if (obj_data->text_size > 0) {
- EMIT_BUF(obj_data->text, obj_data->text_size);
- while (offset & 3)
- EMIT_BUF(&placeholder, 1);
- }
- if (obj_data->text_unlikely_size > 0) {
- EMIT_BUF(obj_data->text_unlikely, obj_data->text_unlikely_size);
- while (offset & 3)
- EMIT_BUF(&placeholder, 1);
- }
- if (obj_data->text_hot_size > 0) {
- EMIT_BUF(obj_data->text_hot, obj_data->text_hot_size);
- while (offset & 3)
- EMIT_BUF(&placeholder, 1);
- }
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit text section failed.");
- return false;
- }
- /* apply relocations to aot_func_internal#n in text section for
- windows platform */
- if ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
- /* Windows AOT_COFF64_BIN_TYPE */
- && obj_data->target_info.bin_type == 6)
- || (!strncmp(obj_data->comp_ctx->target_arch, "i386", 4)
- /* Windows AOT_COFF32_BIN_TYPE */
- && obj_data->target_info.bin_type == 4)) {
- relocation_group = obj_data->relocation_groups;
- for (i = 0; i < obj_data->relocation_group_count;
- i++, relocation_group++) {
- /* relocation in text section */
- if ((!strcmp(relocation_group->section_name, ".text")
- || !strcmp(relocation_group->section_name, ".ltext"))) {
- relocation = relocation_group->relocations;
- relocation_count = relocation_group->relocation_count;
- for (j = 0; j < relocation_count; j++) {
- /* relocation to aot_func_internal#n */
- if (str_starts_with(relocation->symbol_name,
- AOT_FUNC_INTERNAL_PREFIX)
- && ((obj_data->target_info.bin_type
- == 6 /* AOT_COFF64_BIN_TYPE */
- && relocation->relocation_type
- == 4 /* IMAGE_REL_AMD64_REL32 */)
- || (obj_data->target_info.bin_type
- == 4 /* AOT_COFF32_BIN_TYPE */
- && relocation->relocation_type
- == 20 /* IMAGE_REL_I386_REL32 */))) {
- uint32 func_idx =
- atoi(relocation->symbol_name
- + strlen(AOT_FUNC_INTERNAL_PREFIX));
- uint64 text_offset, reloc_offset, reloc_addend;
- bh_assert(func_idx < obj_data->func_count);
- text_offset = obj_data->funcs[func_idx]
- .text_offset_of_aot_func_internal;
- reloc_offset = relocation->relocation_offset;
- reloc_addend = relocation->relocation_addend;
- /* S + A - P */
- *(uint32 *)(text + reloc_offset) =
- (uint32)(text_offset + reloc_addend - reloc_offset
- - 4);
- /* remove current relocation as it has been applied */
- if (j < relocation_count - 1) {
- uint32 move_size =
- (uint32)(sizeof(AOTRelocation)
- * (relocation_count - 1 - j));
- bh_memmove_s(relocation, move_size, relocation + 1,
- move_size);
- }
- relocation_group->relocation_count--;
- }
- else {
- relocation++;
- }
- }
- }
- }
- }
- *p_offset = offset;
- return true;
- }
- #if WASM_ENABLE_GC != 0
- static bool
- aot_emit_ref_flag(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- uint8 pointer_size, int8 type)
- {
- uint32 j, offset = *p_offset;
- uint16 value_type_cell_num;
- if (wasm_is_type_reftype(type) && !wasm_is_reftype_i31ref(type)) {
- EMIT_U8(1);
- if (pointer_size == sizeof(uint64))
- EMIT_U8(1);
- }
- else {
- value_type_cell_num = wasm_value_type_cell_num(type);
- for (j = 0; j < value_type_cell_num; j++)
- EMIT_U8(0);
- }
- *p_offset = offset;
- return true;
- }
- #endif
- static bool
- aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 section_size = get_func_section_size(comp_ctx, comp_data, obj_data);
- uint32 i, offset = *p_offset;
- AOTObjectFunc *func = obj_data->funcs;
- AOTFunc **funcs = comp_data->funcs;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_FUNCTION);
- EMIT_U32(section_size);
- for (i = 0; i < obj_data->func_count; i++, func++) {
- if (is_32bit_binary(obj_data))
- EMIT_U32(func->text_offset);
- else
- EMIT_U64(func->text_offset);
- }
- if (need_call_wrapped_indirect(obj_data)) {
- /*
- * Explicitly emit aot_func_internal#xxx for Xtensa XIP, therefore,
- * for aot_func#xxx, func_indexes ranged from 0 ~ func_count,
- * for aot_func_internal#xxxx, from func_count + 1 ~ 2 * func_count.
- */
- for (i = 0, func = obj_data->funcs; i < obj_data->func_count;
- i++, func++) {
- if (is_32bit_binary(obj_data))
- EMIT_U32(func->text_offset_of_aot_func_internal);
- else
- EMIT_U64(func->text_offset_of_aot_func_internal);
- }
- }
- for (i = 0; i < comp_data->func_count; i++)
- EMIT_U32(funcs[i]->func_type_index);
- if (need_call_wrapped_indirect(obj_data)) {
- /* func_type_index for aot_func_internal#xxxx */
- for (i = 0; i < comp_data->func_count; i++)
- EMIT_U32(funcs[i]->func_type_index);
- }
- for (i = 0; i < comp_data->func_count; i++) {
- uint32 max_local_cell_num =
- funcs[i]->param_cell_num + funcs[i]->local_cell_num;
- EMIT_U32(max_local_cell_num);
- }
- for (i = 0; i < comp_data->func_count; i++)
- EMIT_U32(funcs[i]->max_stack_cell_num);
- #if WASM_ENABLE_GC != 0
- if (comp_ctx->enable_gc) {
- /* emit func_local_ref_flag arrays for both import and AOTed funcs */
- AOTFuncType *func_type;
- uint32 j, local_ref_flags_cell_num, paddings;
- for (i = 0; i < comp_data->import_func_count; i++) {
- func_type = comp_data->import_funcs[i].func_type;
- /* recalculate cell_num based on target pointer size */
- local_ref_flags_cell_num = 0;
- for (j = 0; j < func_type->param_count; j++) {
- local_ref_flags_cell_num += wasm_value_type_cell_num_internal(
- func_type->types[j], comp_ctx->pointer_size);
- }
- paddings =
- local_ref_flags_cell_num < 2 ? 2 - local_ref_flags_cell_num : 0;
- local_ref_flags_cell_num =
- local_ref_flags_cell_num > 2 ? local_ref_flags_cell_num : 2;
- offset = align_uint(offset, 4);
- EMIT_U32(local_ref_flags_cell_num);
- for (j = 0; j < func_type->param_count; j++) {
- if (!aot_emit_ref_flag(buf, buf_end, &offset,
- comp_ctx->pointer_size,
- func_type->types[j]))
- return false;
- }
- for (j = 0; j < paddings; j++)
- EMIT_U8(0);
- }
- for (i = 0; i < comp_data->func_count; i++) {
- func_type = funcs[i]->func_type;
- local_ref_flags_cell_num =
- funcs[i]->param_cell_num + funcs[i]->local_cell_num;
- offset = align_uint(offset, 4);
- EMIT_U32(local_ref_flags_cell_num);
- /* emit local_ref_flag for param variables */
- for (j = 0; j < func_type->param_count; j++) {
- if (!aot_emit_ref_flag(buf, buf_end, &offset,
- comp_ctx->pointer_size,
- func_type->types[j]))
- return false;
- }
- /* emit local_ref_flag for local variables */
- for (j = 0; j < funcs[i]->local_count; j++) {
- if (!aot_emit_ref_flag(buf, buf_end, &offset,
- comp_ctx->pointer_size,
- funcs[i]->local_types_wp[j]))
- return false;
- }
- }
- }
- #endif /* end of WASM_ENABLE_GC != 0 */
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit function section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_export_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 section_size = get_export_section_size(comp_ctx, comp_data);
- AOTExport *export = comp_data->wasm_module->exports;
- uint32 export_count = comp_data->wasm_module->export_count;
- uint32 i, offset = *p_offset;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_EXPORT);
- EMIT_U32(section_size);
- EMIT_U32(export_count);
- for (i = 0; i < export_count; i++, export ++) {
- offset = align_uint(offset, 4);
- EMIT_U32(export->index);
- EMIT_U8(export->kind);
- EMIT_U8(0);
- EMIT_STR(export->name);
- }
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit export section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx,
- AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 symbol_offset = 0, total_string_len = 0;
- uint32 offset = *p_offset;
- AOTSymbolNode *sym;
- EMIT_U32(obj_data->symbol_list.len);
- /* emit symbol offsets */
- sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
- while (sym) {
- EMIT_U32(symbol_offset);
- /* string_len + str[0 .. string_len - 1] */
- symbol_offset += get_string_size(comp_ctx, sym->symbol);
- symbol_offset = align_uint(symbol_offset, 2);
- sym = sym->next;
- }
- /* emit total string len */
- total_string_len = symbol_offset;
- EMIT_U32(total_string_len);
- /* emit symbols */
- sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
- while (sym) {
- EMIT_STR(sym->symbol);
- offset = align_uint(offset, 2);
- sym = sym->next;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 section_size = get_relocation_section_size(comp_ctx, obj_data);
- uint32 i, offset = *p_offset;
- AOTRelocationGroup *relocation_group = obj_data->relocation_groups;
- if (section_size == (uint32)-1)
- return false;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_RELOCATION);
- EMIT_U32(section_size);
- aot_emit_relocation_symbol_table(buf, buf_end, &offset, comp_ctx, comp_data,
- obj_data);
- offset = align_uint(offset, 4);
- EMIT_U32(obj_data->relocation_group_count);
- /* emit each relocation group */
- for (i = 0; i < obj_data->relocation_group_count; i++, relocation_group++) {
- AOTRelocation *relocation = relocation_group->relocations;
- uint32 j;
- offset = align_uint(offset, 4);
- EMIT_U32(relocation_group->name_index);
- offset = align_uint(offset, 4);
- EMIT_U32(relocation_group->relocation_count);
- /* emit each relocation */
- for (j = 0; j < relocation_group->relocation_count; j++, relocation++) {
- offset = align_uint(offset, 4);
- if (is_32bit_binary(obj_data)) {
- EMIT_U32(relocation->relocation_offset);
- EMIT_U32(relocation->relocation_addend);
- }
- else {
- EMIT_U64(relocation->relocation_offset);
- EMIT_U64(relocation->relocation_addend);
- }
- EMIT_U32(relocation->relocation_type);
- EMIT_U32(relocation->symbol_index);
- }
- }
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit relocation section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_native_symbol(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx)
- {
- uint32 offset = *p_offset;
- AOTNativeSymbol *sym = NULL;
- if (bh_list_length(&comp_ctx->native_symbols) == 0)
- /* emit only when there are native symbols */
- return true;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
- /* sub section id + symbol count + symbol list */
- EMIT_U32(sizeof(uint32) * 2 + get_native_symbol_list_size(comp_ctx));
- EMIT_U32(AOT_CUSTOM_SECTION_NATIVE_SYMBOL);
- EMIT_U32(bh_list_length(&comp_ctx->native_symbols));
- sym = bh_list_first_elem(&comp_ctx->native_symbols);
- while (sym) {
- offset = align_uint(offset, 2);
- EMIT_STR(sym->symbol);
- sym = bh_list_elem_next(sym);
- }
- *p_offset = offset;
- return true;
- }
- #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
- static bool
- aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTCompContext *comp_ctx)
- {
- uint32 offset = *p_offset;
- if (comp_data->aot_name_section_size == 0)
- return true;
- offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
- /* sub section id + name section size */
- EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size);
- EMIT_U32(AOT_CUSTOM_SECTION_NAME);
- bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
- comp_data->aot_name_section_buf,
- (uint32)comp_data->aot_name_section_size);
- offset += comp_data->aot_name_section_size;
- *p_offset = offset;
- LOG_DEBUG("emit name section");
- return true;
- }
- #endif
- #if WASM_ENABLE_STRINGREF != 0
- static bool
- aot_emit_string_literal_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data,
- AOTCompContext *comp_ctx)
- {
- uint32 string_count = comp_data->string_literal_count;
- if (string_count > 0) {
- uint32 offset = *p_offset;
- uint32 i;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
- /* sub section id + string literal section size */
- EMIT_U32(sizeof(uint32) * 1
- + get_string_literal_section_size(comp_ctx, comp_data));
- EMIT_U32(AOT_CUSTOM_SECTION_STRING_LITERAL);
- /* reserved */
- EMIT_U32(0);
- /* string literal count */
- EMIT_U32(string_count);
- for (i = 0; i < string_count; i++) {
- EMIT_U32(comp_data->string_literal_lengths_wp[i]);
- }
- for (i = 0; i < string_count; i++) {
- uint32 string_length = comp_data->string_literal_lengths_wp[i];
- bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
- comp_data->string_literal_ptrs_wp[i], string_length);
- offset += string_length;
- }
- *p_offset = offset;
- }
- return true;
- }
- #endif /* end of WASM_ENABLE_STRINGREF != 0 */
- static bool
- aot_emit_custom_sections(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTCompContext *comp_ctx)
- {
- #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
- uint32 offset = *p_offset, i;
- for (i = 0; i < comp_ctx->custom_sections_count; i++) {
- const char *section_name = comp_ctx->custom_sections_wp[i];
- const uint8 *content = NULL;
- uint32 length = 0;
- if (strcmp(section_name, "name") == 0) {
- *p_offset = offset;
- if (!aot_emit_name_section(buf, buf_end, p_offset, comp_data,
- comp_ctx))
- return false;
- offset = *p_offset;
- continue;
- }
- content = wasm_loader_get_custom_section(comp_data->wasm_module,
- section_name, &length);
- if (!content) {
- /* Warning has been reported during calculating size */
- continue;
- }
- offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
- /* sub section id + content */
- EMIT_U32(sizeof(uint32) * 1 + get_string_size(comp_ctx, section_name)
- + length);
- EMIT_U32(AOT_CUSTOM_SECTION_RAW);
- EMIT_STR(section_name);
- bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf), content,
- length);
- offset += length;
- }
- *p_offset = offset;
- #endif
- return true;
- }
- typedef uint32 U32;
- typedef int32 I32;
- typedef uint16 U16;
- typedef uint8 U8;
- struct coff_hdr {
- U16 u16Machine;
- U16 u16NumSections;
- U32 u32DateTimeStamp;
- U32 u32SymTblPtr;
- U32 u32NumSymbols;
- U16 u16PeHdrSize;
- U16 u16Characs;
- };
- #define E_TYPE_REL 1
- #define E_TYPE_XIP 4
- #define IMAGE_FILE_MACHINE_AMD64 0x8664
- #define IMAGE_FILE_MACHINE_I386 0x014c
- #define IMAGE_FILE_MACHINE_IA64 0x0200
- #define AOT_COFF32_BIN_TYPE 4 /* 32-bit little endian */
- #define AOT_COFF64_BIN_TYPE 6 /* 64-bit little endian */
- #define EI_NIDENT 16
- typedef uint32 elf32_word;
- typedef int32 elf32_sword;
- typedef uint16 elf32_half;
- typedef uint32 elf32_off;
- typedef uint32 elf32_addr;
- struct elf32_ehdr {
- unsigned char e_ident[EI_NIDENT]; /* ident bytes */
- elf32_half e_type; /* file type */
- elf32_half e_machine; /* target machine */
- elf32_word e_version; /* file version */
- elf32_addr e_entry; /* start address */
- elf32_off e_phoff; /* phdr file offset */
- elf32_off e_shoff; /* shdr file offset */
- elf32_word e_flags; /* file flags */
- elf32_half e_ehsize; /* sizeof ehdr */
- elf32_half e_phentsize; /* sizeof phdr */
- elf32_half e_phnum; /* number phdrs */
- elf32_half e_shentsize; /* sizeof shdr */
- elf32_half e_shnum; /* number shdrs */
- elf32_half e_shstrndx; /* shdr string index */
- };
- struct elf32_rel {
- elf32_addr r_offset;
- elf32_word r_info;
- } elf32_rel;
- struct elf32_rela {
- elf32_addr r_offset;
- elf32_word r_info;
- elf32_sword r_addend;
- } elf32_rela;
- typedef uint32 elf64_word;
- typedef int32 elf64_sword;
- typedef uint64 elf64_xword;
- typedef int64 elf64_sxword;
- typedef uint16 elf64_half;
- typedef uint64 elf64_off;
- typedef uint64 elf64_addr;
- struct elf64_ehdr {
- unsigned char e_ident[EI_NIDENT]; /* ident bytes */
- elf64_half e_type; /* file type */
- elf64_half e_machine; /* target machine */
- elf64_word e_version; /* file version */
- elf64_addr e_entry; /* start address */
- elf64_off e_phoff; /* phdr file offset */
- elf64_off e_shoff; /* shdr file offset */
- elf64_word e_flags; /* file flags */
- elf64_half e_ehsize; /* sizeof ehdr */
- elf64_half e_phentsize; /* sizeof phdr */
- elf64_half e_phnum; /* number phdrs */
- elf64_half e_shentsize; /* sizeof shdr */
- elf64_half e_shnum; /* number shdrs */
- elf64_half e_shstrndx; /* shdr string index */
- };
- typedef struct elf64_rel {
- elf64_addr r_offset;
- elf64_xword r_info;
- } elf64_rel;
- typedef struct elf64_rela {
- elf64_addr r_offset;
- elf64_xword r_info;
- elf64_sxword r_addend;
- } elf64_rela;
- #define SET_TARGET_INFO(f, v, type, little) \
- do { \
- type tmp = elf_header->v; \
- if ((little && !is_little_endian()) \
- || (!little && is_little_endian())) \
- exchange_##type((uint8 *)&tmp); \
- obj_data->target_info.f = tmp; \
- } while (0)
- static bool
- aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
- {
- LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
- const uint8 *elf_buf = (uint8 *)LLVMGetBufferStart(obj_data->mem_buf);
- uint32 elf_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
- if (bin_type != LLVMBinaryTypeCOFF && bin_type != LLVMBinaryTypeELF32L
- && bin_type != LLVMBinaryTypeELF32B && bin_type != LLVMBinaryTypeELF64L
- && bin_type != LLVMBinaryTypeELF64B
- && bin_type != LLVMBinaryTypeMachO32L
- && bin_type != LLVMBinaryTypeMachO32B
- && bin_type != LLVMBinaryTypeMachO64L
- && bin_type != LLVMBinaryTypeMachO64B) {
- aot_set_last_error("invalid llvm binary bin_type.");
- return false;
- }
- obj_data->target_info.bin_type = bin_type - LLVMBinaryTypeELF32L;
- if (bin_type == LLVMBinaryTypeCOFF) {
- struct coff_hdr *coff_header;
- if (!elf_buf || elf_size < sizeof(struct coff_hdr)) {
- aot_set_last_error("invalid coff_hdr buffer.");
- return false;
- }
- coff_header = (struct coff_hdr *)elf_buf;
- /* Emit eXecute In Place file type while in indirect mode */
- if (comp_ctx->is_indirect_mode)
- obj_data->target_info.e_type = E_TYPE_XIP;
- else
- obj_data->target_info.e_type = E_TYPE_REL;
- obj_data->target_info.e_machine = coff_header->u16Machine;
- obj_data->target_info.e_version = 1;
- obj_data->target_info.e_flags = 0;
- if (coff_header->u16Machine == IMAGE_FILE_MACHINE_AMD64
- || coff_header->u16Machine == IMAGE_FILE_MACHINE_IA64)
- obj_data->target_info.bin_type = AOT_COFF64_BIN_TYPE;
- else if (coff_header->u16Machine == IMAGE_FILE_MACHINE_I386)
- obj_data->target_info.bin_type = AOT_COFF32_BIN_TYPE;
- }
- else if (bin_type == LLVMBinaryTypeELF32L
- || bin_type == LLVMBinaryTypeELF32B) {
- struct elf32_ehdr *elf_header;
- bool is_little_bin = bin_type == LLVMBinaryTypeELF32L;
- if (!elf_buf || elf_size < sizeof(struct elf32_ehdr)) {
- aot_set_last_error("invalid elf32 buffer.");
- return false;
- }
- elf_header = (struct elf32_ehdr *)elf_buf;
- /* Emit eXecute In Place file type while in indirect mode */
- if (comp_ctx->is_indirect_mode)
- elf_header->e_type = E_TYPE_XIP;
- SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
- SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
- SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
- SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
- }
- else if (bin_type == LLVMBinaryTypeELF64L
- || bin_type == LLVMBinaryTypeELF64B) {
- struct elf64_ehdr *elf_header;
- bool is_little_bin = bin_type == LLVMBinaryTypeELF64L;
- if (!elf_buf || elf_size < sizeof(struct elf64_ehdr)) {
- aot_set_last_error("invalid elf64 buffer.");
- return false;
- }
- elf_header = (struct elf64_ehdr *)elf_buf;
- /* Emit eXecute In Place file type while in indirect mode */
- if (comp_ctx->is_indirect_mode)
- elf_header->e_type = E_TYPE_XIP;
- SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
- SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
- SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
- SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
- }
- else if (bin_type == LLVMBinaryTypeMachO32L
- || bin_type == LLVMBinaryTypeMachO32B) {
- /* TODO: parse file type of Mach-O 32 */
- aot_set_last_error("invalid llvm binary bin_type.");
- return false;
- }
- else if (bin_type == LLVMBinaryTypeMachO64L
- || bin_type == LLVMBinaryTypeMachO64B) {
- /* TODO: parse file type of Mach-O 64 */
- aot_set_last_error("invalid llvm binary bin_type.");
- return false;
- }
- bh_assert(sizeof(obj_data->target_info.arch)
- == sizeof(comp_ctx->target_arch));
- bh_memcpy_s(obj_data->target_info.arch, sizeof(obj_data->target_info.arch),
- comp_ctx->target_arch, sizeof(comp_ctx->target_arch));
- return true;
- }
- static bool
- aot_resolve_text(AOTObjectData *obj_data)
- {
- #if WASM_ENABLE_DEBUG_AOT != 0
- LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
- if (bin_type == LLVMBinaryTypeELF32L || bin_type == LLVMBinaryTypeELF64L) {
- obj_data->text = (char *)LLVMGetBufferStart(obj_data->mem_buf);
- obj_data->text_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
- }
- else
- #endif
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (
- !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))) {
- if (!strcmp(name, ".text") || !strcmp(name, ".ltext")) {
- obj_data->text = (char *)LLVMGetSectionContents(sec_itr);
- obj_data->text_size = (uint32)LLVMGetSectionSize(sec_itr);
- }
- else if (!strcmp(name, ".text.unlikely.")
- || !strcmp(name, ".ltext.unlikely.")) {
- obj_data->text_unlikely =
- (char *)LLVMGetSectionContents(sec_itr);
- obj_data->text_unlikely_size =
- (uint32)LLVMGetSectionSize(sec_itr);
- }
- else if (!strcmp(name, ".text.hot.")
- || !strcmp(name, ".ltext.hot.")) {
- obj_data->text_hot =
- (char *)LLVMGetSectionContents(sec_itr);
- obj_data->text_hot_size =
- (uint32)LLVMGetSectionSize(sec_itr);
- }
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- }
- return true;
- }
- static bool
- aot_resolve_literal(AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && !strcmp(name, ".literal")) {
- obj_data->literal = (char *)LLVMGetSectionContents(sec_itr);
- obj_data->literal_size = (uint32)LLVMGetSectionSize(sec_itr);
- break;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- return true;
- }
- static bool
- get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count);
- static bool
- is_data_section(AOTObjectData *obj_data, LLVMSectionIteratorRef sec_itr,
- char *section_name)
- {
- uint32 relocation_count = 0;
- return (!strcmp(section_name, ".data") || !strcmp(section_name, ".sdata")
- || !strcmp(section_name, ".rodata")
- /* ".rodata.cst4/8/16/.." */
- || !strncmp(section_name, ".rodata.cst", strlen(".rodata.cst"))
- /* ".rodata.strn.m" */
- || !strncmp(section_name, ".rodata.str", strlen(".rodata.str"))
- || (!strcmp(section_name, ".rdata")
- && get_relocations_count(sec_itr, &relocation_count)
- && relocation_count > 0)
- || !strcmp(section_name, aot_stack_sizes_section_name)
- || (obj_data->comp_ctx->enable_llvm_pgo
- && (!strncmp(section_name, "__llvm_prf_cnts", 15)
- || !strncmp(section_name, "__llvm_prf_data", 15)
- || !strncmp(section_name, "__llvm_prf_names", 16))));
- }
- static bool
- get_object_data_sections_count(AOTObjectData *obj_data, uint32 *p_count)
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- uint32 count = 0;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && (is_data_section(obj_data, sec_itr, name))) {
- count++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- *p_count = count;
- return true;
- }
- static bool
- aot_resolve_object_data_sections(AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- AOTObjectDataSection *data_section;
- uint32 sections_count;
- uint32 size;
- if (!get_object_data_sections_count(obj_data, §ions_count)) {
- return false;
- }
- if (sections_count > 0) {
- uint32 llvm_prf_cnts_idx = 0, llvm_prf_data_idx = 0;
- char buf[32];
- size = (uint32)sizeof(AOTObjectDataSection) * sections_count;
- if (!(data_section = obj_data->data_sections =
- wasm_runtime_malloc(size))) {
- aot_set_last_error("allocate memory for data sections failed.");
- return false;
- }
- memset(obj_data->data_sections, 0, size);
- obj_data->data_sections_count = sections_count;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (
- !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && (is_data_section(obj_data, sec_itr, name))) {
- data_section->name = name;
- if (obj_data->comp_ctx->enable_llvm_pgo
- && !strcmp(name, "__llvm_prf_cnts")) {
- snprintf(buf, sizeof(buf), "%s%u", name,
- llvm_prf_cnts_idx++);
- size = (uint32)(strlen(buf) + 1);
- if (!(data_section->name = wasm_runtime_malloc(size))) {
- aot_set_last_error(
- "allocate memory for data section name failed.");
- return false;
- }
- bh_memcpy_s(data_section->name, size, buf, size);
- data_section->is_name_allocated = true;
- }
- else if (obj_data->comp_ctx->enable_llvm_pgo
- && !strcmp(name, "__llvm_prf_data")) {
- snprintf(buf, sizeof(buf), "%s%u", name,
- llvm_prf_data_idx++);
- size = (uint32)(strlen(buf) + 1);
- if (!(data_section->name = wasm_runtime_malloc(size))) {
- aot_set_last_error(
- "allocate memory for data section name failed.");
- return false;
- }
- bh_memcpy_s(data_section->name, size, buf, size);
- data_section->is_name_allocated = true;
- }
- if (obj_data->comp_ctx->enable_llvm_pgo
- && !strcmp(name, "__llvm_prf_names")) {
- data_section->data = (uint8 *)aot_compress_aot_func_names(
- obj_data->comp_ctx, &data_section->size);
- data_section->is_data_allocated = true;
- }
- else {
- data_section->data =
- (uint8 *)LLVMGetSectionContents(sec_itr);
- data_section->size = (uint32)LLVMGetSectionSize(sec_itr);
- }
- data_section++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- }
- return true;
- }
- static bool
- read_stack_usage_file(const AOTCompContext *comp_ctx, const char *filename,
- uint32 *sizes, uint32 count)
- {
- FILE *fp = NULL;
- if (filename == NULL) {
- aot_set_last_error("no stack usage file is specified.");
- return false;
- }
- fp = fopen(filename, "r");
- if (fp == NULL) {
- LOG_ERROR("failed to open stack usage file: %s", filename);
- goto fail;
- }
- /*
- * the file consists of lines like:
- *
- * WASM Module:aot_func#9 72 static
- */
- const char *aot_func_prefix = AOT_FUNC_PREFIX;
- const char *aot_func_internal_prefix = AOT_FUNC_INTERNAL_PREFIX;
- uint32 precheck_found = 0;
- uint32 precheck_stack_size_max = 0;
- uint32 precheck_stack_size_min = UINT32_MAX;
- uint32 found = 0;
- while (true) {
- const char *prefix;
- char line[100];
- char *cp = fgets(line, sizeof(line), fp);
- char *fn;
- char *colon;
- uintmax_t func_idx;
- uintmax_t sz;
- int ret;
- if (cp == NULL) {
- break;
- }
- /*
- * Note: strrchr (not strchr) because a module name can contain
- * colons.
- */
- colon = strrchr(cp, ':');
- if (colon == NULL) {
- goto fail;
- }
- fn = strstr(colon, aot_func_prefix);
- if (fn != NULL) {
- prefix = aot_func_prefix;
- }
- else {
- fn = strstr(colon, aot_func_internal_prefix);
- if (fn == NULL) {
- LOG_ERROR("failed to parse stack usage line: %s", cp);
- goto fail;
- }
- prefix = aot_func_internal_prefix;
- }
- ret = sscanf(fn + strlen(prefix), "%ju %ju static", &func_idx, &sz);
- if (ret != 2) {
- goto fail;
- }
- if (sz > UINT32_MAX) {
- goto fail;
- }
- if (func_idx > UINT32_MAX) {
- goto fail;
- }
- if (func_idx >= count) {
- goto fail;
- }
- if (prefix == aot_func_prefix) {
- if (sz < precheck_stack_size_min) {
- precheck_stack_size_min = (uint32)sz;
- }
- if (sz > precheck_stack_size_max) {
- precheck_stack_size_max = (uint32)sz;
- }
- precheck_found++;
- continue;
- }
- sizes[func_idx] = (uint32)sz;
- found++;
- }
- fclose(fp);
- if (precheck_found != count) {
- LOG_ERROR("%" PRIu32 " precheck entries found while %" PRIu32
- " entries are expected",
- precheck_found, count);
- return false;
- }
- if (found != count) {
- /*
- * LLVM seems to eliminate calls to an empty function
- * (and eliminate the function) even if it's marked noinline.
- */
- LOG_VERBOSE("%" PRIu32 " entries found while %" PRIu32
- " entries are expected. Maybe LLVM optimization eliminated "
- "some functions.",
- found, count);
- }
- if (precheck_stack_size_min != precheck_stack_size_max) {
- /*
- * Note: this is too strict.
- *
- * actually, the stack consumption of the precheck functions
- * can depend on the type of them.
- * that is, depending on various factors including
- * calling conventions and compilers, a function with many
- * parameters can consume more stack, even if it merely does
- * a tail-call to another function.
- */
- bool musttail = aot_target_precheck_can_use_musttail(comp_ctx);
- if (musttail) {
- LOG_WARNING(
- "precheck functions use variable amount of stack. (%" PRIu32
- " - %" PRIu32 ")",
- precheck_stack_size_min, precheck_stack_size_max);
- }
- else {
- LOG_VERBOSE("precheck functions use %" PRIu32 " - %" PRIu32
- " bytes of stack.",
- precheck_stack_size_min, precheck_stack_size_max);
- }
- }
- else {
- LOG_VERBOSE("precheck functions use %" PRIu32 " bytes of stack.",
- precheck_stack_size_max);
- }
- if (precheck_stack_size_max >= 1024) {
- LOG_WARNING("precheck functions themselves consume relatively large "
- "amount of stack (%" PRIu32
- "). Please ensure the runtime has large enough "
- "WASM_STACK_GUARD_SIZE.",
- precheck_stack_size_max);
- }
- return true;
- fail:
- if (fp != NULL)
- fclose(fp);
- aot_set_last_error("failed to read stack usage file.");
- return false;
- }
- static bool
- aot_resolve_stack_sizes(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr = NULL;
- LLVMSymbolIteratorRef sym_itr;
- const char *name;
- if (!(sym_itr = LLVMObjectFileCopySymbolIterator(obj_data->binary))) {
- aot_set_last_error("llvm get symbol iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
- if ((name = LLVMGetSymbolName(sym_itr))
- && (!strcmp(name, aot_stack_sizes_alias_name)
- /* symbol of COFF32 starts with "_" */
- || (obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
- && !strncmp(name, "_", 1)
- && !strcmp(name + 1, aot_stack_sizes_alias_name)))) {
- #if 0 /* cf. https://github.com/llvm/llvm-project/issues/67765 */
- uint64 sz = LLVMGetSymbolSize(sym_itr);
- if (sz != sizeof(uint32) * obj_data->func_count
- /* sz of COFF64/COFF32 is 0, ignore the check */
- && obj_data->target_info.bin_type != AOT_COFF64_BIN_TYPE
- && obj_data->target_info.bin_type != AOT_COFF32_BIN_TYPE) {
- aot_set_last_error("stack_sizes had unexpected size.");
- goto fail;
- }
- #endif
- uint64 addr = LLVMGetSymbolAddress(sym_itr);
- if (!(sec_itr =
- LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- goto fail;
- }
- LLVMMoveToContainingSection(sec_itr, sym_itr);
- const char *sec_name = LLVMGetSectionName(sec_itr);
- LOG_VERBOSE("stack_sizes found in section %s offset %" PRIu64 ".",
- sec_name, addr);
- if (strcmp(sec_name, aot_stack_sizes_section_name) || addr != 0) {
- aot_set_last_error(
- "stack_sizes found at an unexpected location.");
- goto fail;
- }
- /*
- * Note: We can't always modify stack_sizes in-place.
- * E.g. When WAMRC_LLC_COMPILER is used, LLVM sometimes uses
- * read-only mmap of the temporary file to back
- * LLVMGetSectionContents.
- */
- const uint32 *ro_stack_sizes =
- (const uint32 *)(LLVMGetSectionContents(sec_itr) + addr);
- uint32 i;
- for (i = 0; i < obj_data->func_count; i++) {
- /* Note: -1 == AOT_NEG_ONE from aot_create_stack_sizes */
- if (ro_stack_sizes[i] != (uint32)-1) {
- aot_set_last_error("unexpected data in stack_sizes.");
- goto fail;
- }
- }
- /*
- * Record section/offset and construct a copy of stack_sizes.
- * aot_emit_object_data_section_info will emit this copy.
- */
- obj_data->stack_sizes_section_name = sec_name;
- obj_data->stack_sizes_offset = (uint32)addr;
- obj_data->stack_sizes = wasm_runtime_malloc(
- obj_data->func_count * sizeof(*obj_data->stack_sizes));
- if (obj_data->stack_sizes == NULL) {
- aot_set_last_error("failed to allocate memory.");
- goto fail;
- }
- uint32 *stack_sizes = obj_data->stack_sizes;
- for (i = 0; i < obj_data->func_count; i++) {
- stack_sizes[i] = (uint32)-1;
- }
- if (!read_stack_usage_file(comp_ctx, comp_ctx->stack_usage_file,
- stack_sizes, obj_data->func_count)) {
- goto fail;
- }
- for (i = 0; i < obj_data->func_count; i++) {
- const AOTFuncContext *func_ctx = comp_ctx->func_ctxes[i];
- bool musttail = aot_target_precheck_can_use_musttail(comp_ctx);
- unsigned int stack_consumption_to_call_wrapped_func =
- musttail ? 0
- : aot_estimate_stack_usage_for_function_call(
- comp_ctx, func_ctx->aot_func->func_type);
- /*
- * LLVM seems to eliminate calls to an empty function
- * (and eliminate the function) even if it's marked noinline.
- *
- * Note: -1 == AOT_NEG_ONE from aot_create_stack_sizes
- */
- if (stack_sizes[i] == (uint32)-1) {
- if (func_ctx->stack_consumption_for_func_call != 0) {
- /*
- * This happens if a function calling another
- * function has been optimized out.
- *
- * for example,
- *
- * (func $func
- * (local i32)
- * local.get 0
- * if
- * call $another
- * end
- * )
- */
- LOG_VERBOSE("AOT func#%" PRIu32
- " had call(s) but eliminated?",
- i);
- }
- else {
- LOG_VERBOSE("AOT func#%" PRIu32 " eliminated?", i);
- }
- stack_sizes[i] = 0;
- }
- else {
- LOG_VERBOSE("AOT func#%" PRIu32 " stack_size %u + %" PRIu32
- " + %u",
- i, stack_consumption_to_call_wrapped_func,
- stack_sizes[i],
- func_ctx->stack_consumption_for_func_call);
- if (UINT32_MAX - stack_sizes[i]
- < func_ctx->stack_consumption_for_func_call) {
- aot_set_last_error("stack size overflow.");
- goto fail;
- }
- stack_sizes[i] += func_ctx->stack_consumption_for_func_call;
- if (UINT32_MAX - stack_sizes[i]
- < stack_consumption_to_call_wrapped_func) {
- aot_set_last_error("stack size overflow.");
- goto fail;
- }
- stack_sizes[i] += stack_consumption_to_call_wrapped_func;
- }
- }
- LLVMDisposeSectionIterator(sec_itr);
- LLVMDisposeSymbolIterator(sym_itr);
- return true;
- }
- LLVMMoveToNextSymbol(sym_itr);
- }
- aot_set_last_error("stack_sizes not found.");
- fail:
- if (sec_itr)
- LLVMDisposeSectionIterator(sec_itr);
- LLVMDisposeSymbolIterator(sym_itr);
- return false;
- }
- static bool
- aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
- {
- AOTObjectFunc *func;
- LLVMSymbolIteratorRef sym_itr;
- char *name, *prefix = AOT_FUNC_PREFIX;
- uint32 func_index, total_size;
- /* allocate memory for aot function */
- obj_data->func_count = comp_ctx->comp_data->func_count;
- if (obj_data->func_count) {
- if ((comp_ctx->enable_stack_bound_check
- || comp_ctx->enable_stack_estimation)
- && !aot_resolve_stack_sizes(comp_ctx, obj_data))
- return false;
- total_size = (uint32)sizeof(AOTObjectFunc) * obj_data->func_count;
- if (!(obj_data->funcs = wasm_runtime_malloc(total_size))) {
- aot_set_last_error("allocate memory for functions failed.");
- return false;
- }
- memset(obj_data->funcs, 0, total_size);
- }
- if (!(sym_itr = LLVMObjectFileCopySymbolIterator(obj_data->binary))) {
- aot_set_last_error("llvm get symbol iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
- if ((name = (char *)LLVMGetSymbolName(sym_itr))
- && str_starts_with(name, prefix)) {
- /* symbol aot_func#n */
- func_index = (uint32)atoi(name + strlen(prefix));
- if (func_index < obj_data->func_count) {
- LLVMSectionIteratorRef contain_section;
- char *contain_section_name;
- func = obj_data->funcs + func_index;
- func->func_name = name;
- if (!(contain_section = LLVMObjectFileCopySectionIterator(
- obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- LLVMDisposeSymbolIterator(sym_itr);
- return false;
- }
- LLVMMoveToContainingSection(contain_section, sym_itr);
- contain_section_name =
- (char *)LLVMGetSectionName(contain_section);
- LLVMDisposeSectionIterator(contain_section);
- if (!strcmp(contain_section_name, ".text.unlikely.")
- || !strcmp(contain_section_name, ".ltext.unlikely.")) {
- func->text_offset = align_uint(obj_data->text_size, 4)
- + LLVMGetSymbolAddress(sym_itr);
- }
- else if (!strcmp(contain_section_name, ".text.hot.")
- || !strcmp(contain_section_name, ".ltext.hot.")) {
- func->text_offset =
- align_uint(obj_data->text_size, 4)
- + align_uint(obj_data->text_unlikely_size, 4)
- + LLVMGetSymbolAddress(sym_itr);
- }
- else {
- func->text_offset = LLVMGetSymbolAddress(sym_itr);
- }
- }
- }
- else if ((name = (char *)LLVMGetSymbolName(sym_itr))
- && str_starts_with(name, AOT_FUNC_INTERNAL_PREFIX)) {
- /* symbol aot_func_internal#n */
- func_index = (uint32)atoi(name + strlen(AOT_FUNC_INTERNAL_PREFIX));
- if (func_index < obj_data->func_count) {
- LLVMSectionIteratorRef contain_section;
- char *contain_section_name;
- func = obj_data->funcs + func_index;
- if (!(contain_section = LLVMObjectFileCopySectionIterator(
- obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- LLVMDisposeSymbolIterator(sym_itr);
- return false;
- }
- LLVMMoveToContainingSection(contain_section, sym_itr);
- contain_section_name =
- (char *)LLVMGetSectionName(contain_section);
- LLVMDisposeSectionIterator(contain_section);
- if (!strcmp(contain_section_name, ".text.unlikely.")
- || !strcmp(contain_section_name, ".ltext.unlikely.")) {
- func->text_offset_of_aot_func_internal =
- align_uint(obj_data->text_size, 4)
- + LLVMGetSymbolAddress(sym_itr);
- }
- else if (!strcmp(contain_section_name, ".text.hot.")
- || !strcmp(contain_section_name, ".ltext.hot.")) {
- func->text_offset_of_aot_func_internal =
- align_uint(obj_data->text_size, 4)
- + align_uint(obj_data->text_unlikely_size, 4)
- + LLVMGetSymbolAddress(sym_itr);
- }
- else {
- func->text_offset_of_aot_func_internal =
- LLVMGetSymbolAddress(sym_itr);
- }
- }
- }
- LLVMMoveToNextSymbol(sym_itr);
- }
- LLVMDisposeSymbolIterator(sym_itr);
- return true;
- }
- static bool
- get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count)
- {
- uint32 relocation_count = 0;
- LLVMRelocationIteratorRef rel_itr;
- if (!(rel_itr = LLVMGetRelocations(sec_itr))) {
- aot_set_last_error("llvm get relocations failed.");
- LLVMDisposeSectionIterator(sec_itr);
- return false;
- }
- while (!LLVMIsRelocationIteratorAtEnd(sec_itr, rel_itr)) {
- relocation_count++;
- LLVMMoveToNextRelocation(rel_itr);
- }
- LLVMDisposeRelocationIterator(rel_itr);
- *p_count = relocation_count;
- return true;
- }
- static bool
- aot_resolve_object_relocation_group(AOTObjectData *obj_data,
- AOTRelocationGroup *group,
- LLVMSectionIteratorRef rel_sec)
- {
- LLVMRelocationIteratorRef rel_itr;
- AOTRelocation *relocation = group->relocations;
- uint32 size;
- bool is_binary_32bit = is_32bit_binary(obj_data);
- bool is_binary_little_endian = is_little_endian_binary(obj_data);
- bool has_addend = str_starts_with(group->section_name, ".rela");
- uint8 *rela_content = NULL;
- /* calculate relocations count and allocate memory */
- if (!get_relocations_count(rel_sec, &group->relocation_count))
- return false;
- if (group->relocation_count == 0) {
- aot_set_last_error("invalid relocations count");
- return false;
- }
- size = (uint32)sizeof(AOTRelocation) * group->relocation_count;
- if (!(relocation = group->relocations = wasm_runtime_malloc(size))) {
- aot_set_last_error("allocate memory for relocations failed.");
- return false;
- }
- memset(group->relocations, 0, size);
- if (has_addend) {
- uint64 rela_content_size;
- /* LLVM doesn't provide C API to get relocation addend. So we have to
- * parse it manually. */
- rela_content = (uint8 *)LLVMGetSectionContents(rel_sec);
- rela_content_size = LLVMGetSectionSize(rel_sec);
- if (is_binary_32bit)
- size = (uint32)sizeof(struct elf32_rela) * group->relocation_count;
- else
- size = (uint32)sizeof(struct elf64_rela) * group->relocation_count;
- if (rela_content_size != (uint64)size) {
- aot_set_last_error("invalid relocation section content.");
- return false;
- }
- }
- /* pares each relocation */
- if (!(rel_itr = LLVMGetRelocations(rel_sec))) {
- aot_set_last_error("llvm get relocations failed.");
- return false;
- }
- while (!LLVMIsRelocationIteratorAtEnd(rel_sec, rel_itr)) {
- uint64 offset = LLVMGetRelocationOffset(rel_itr);
- uint64 type = LLVMGetRelocationType(rel_itr);
- LLVMSymbolIteratorRef rel_sym = LLVMGetRelocationSymbol(rel_itr);
- if (!rel_sym) {
- aot_set_last_error("llvm get relocation symbol failed.");
- goto fail;
- }
- /* parse relocation addend from relocation content */
- if (has_addend) {
- if (is_binary_32bit) {
- int32 addend =
- (int32)(((struct elf32_rela *)rela_content)->r_addend);
- if (is_binary_little_endian != is_little_endian())
- exchange_uint32((uint8 *)&addend);
- relocation->relocation_addend = (int64)addend;
- rela_content += sizeof(struct elf32_rela);
- }
- else {
- int64 addend =
- (int64)(((struct elf64_rela *)rela_content)->r_addend);
- if (is_binary_little_endian != is_little_endian())
- exchange_uint64((uint8 *)&addend);
- relocation->relocation_addend = addend;
- rela_content += sizeof(struct elf64_rela);
- }
- }
- /* set relocation fields */
- relocation->relocation_type = (uint32)type;
- relocation->symbol_name = (char *)LLVMGetSymbolName(rel_sym);
- relocation->relocation_offset = offset;
- if (!strcmp(group->section_name, ".rela.text.unlikely.")
- || !strcmp(group->section_name, ".rel.text.unlikely.")) {
- relocation->relocation_offset += align_uint(obj_data->text_size, 4);
- }
- else if (!strcmp(group->section_name, ".rela.text.hot.")
- || !strcmp(group->section_name, ".rel.text.hot.")) {
- relocation->relocation_offset +=
- align_uint(obj_data->text_size, 4)
- + align_uint(obj_data->text_unlikely_size, 4);
- }
- if (!strcmp(relocation->symbol_name, ".text.unlikely.")) {
- relocation->symbol_name = ".text";
- relocation->relocation_addend += align_uint(obj_data->text_size, 4);
- }
- if (!strcmp(relocation->symbol_name, ".text.hot.")) {
- relocation->symbol_name = ".text";
- relocation->relocation_addend +=
- align_uint(obj_data->text_size, 4)
- + align_uint(obj_data->text_unlikely_size, 4);
- }
- /*
- * Note: aot_stack_sizes_section_name section only contains
- * stack_sizes table.
- */
- if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)
- /* in windows 32, the symbol name may start with '_' */
- || (strlen(relocation->symbol_name) > 0
- && relocation->symbol_name[0] == '_'
- && !strcmp(relocation->symbol_name + 1,
- aot_stack_sizes_name))) {
- /* discard const */
- relocation->symbol_name = (char *)aot_stack_sizes_section_name;
- }
- if (obj_data->comp_ctx->enable_llvm_pgo
- && (!strcmp(relocation->symbol_name, "__llvm_prf_cnts")
- || !strcmp(relocation->symbol_name, "__llvm_prf_data"))) {
- LLVMSectionIteratorRef sec_itr;
- char buf[32], *section_name;
- uint32 prof_section_idx = 0;
- if (!(sec_itr =
- LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- LLVMDisposeSymbolIterator(rel_sym);
- goto fail;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary,
- sec_itr)) {
- section_name = (char *)LLVMGetSectionName(sec_itr);
- if (section_name
- && !strcmp(section_name, relocation->symbol_name)) {
- if (LLVMGetSectionContainsSymbol(sec_itr, rel_sym))
- break;
- prof_section_idx++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- if (!strcmp(group->section_name, ".rela.text")
- || !strcmp(group->section_name, ".rel.text")) {
- snprintf(buf, sizeof(buf), "%s%u", relocation->symbol_name,
- prof_section_idx);
- size = (uint32)(strlen(buf) + 1);
- if (!(relocation->symbol_name = wasm_runtime_malloc(size))) {
- aot_set_last_error(
- "allocate memory for relocation symbol name failed.");
- LLVMDisposeSymbolIterator(rel_sym);
- goto fail;
- }
- bh_memcpy_s(relocation->symbol_name, size, buf, size);
- relocation->is_symbol_name_allocated = true;
- }
- else if (!strncmp(group->section_name, ".rela__llvm_prf_data", 20)
- || !strncmp(group->section_name, ".rel__llvm_prf_data",
- 19)) {
- snprintf(buf, sizeof(buf), "%s%u", relocation->symbol_name,
- prof_section_idx);
- size = (uint32)(strlen(buf) + 1);
- if (!(relocation->symbol_name = wasm_runtime_malloc(size))) {
- aot_set_last_error(
- "allocate memory for relocation symbol name failed.");
- LLVMDisposeSymbolIterator(rel_sym);
- goto fail;
- }
- bh_memcpy_s(relocation->symbol_name, size, buf, size);
- relocation->is_symbol_name_allocated = true;
- }
- }
- /* for ".LCPIxxx", ".LJTIxxx", ".LBBxxx" and switch lookup table
- * relocation, transform the symbol name to real section name and set
- * addend to the offset of the symbol in the real section */
- if (relocation->symbol_name
- && (str_starts_with(relocation->symbol_name, ".LCPI")
- || str_starts_with(relocation->symbol_name, ".LJTI")
- || str_starts_with(relocation->symbol_name, ".LBB")
- || str_starts_with(relocation->symbol_name,
- ".Lswitch.table."))) {
- /* change relocation->relocation_addend and
- relocation->symbol_name */
- LLVMSectionIteratorRef contain_section;
- if (!(contain_section =
- LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- goto fail;
- }
- LLVMMoveToContainingSection(contain_section, rel_sym);
- if (LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary,
- contain_section)) {
- LLVMDisposeSectionIterator(contain_section);
- aot_set_last_error("llvm get containing section failed.");
- goto fail;
- }
- relocation->relocation_addend += LLVMGetSymbolAddress(rel_sym);
- relocation->symbol_name =
- (char *)LLVMGetSectionName(contain_section);
- LLVMDisposeSectionIterator(contain_section);
- }
- LLVMDisposeSymbolIterator(rel_sym);
- LLVMMoveToNextRelocation(rel_itr);
- relocation++;
- }
- LLVMDisposeRelocationIterator(rel_itr);
- return true;
- fail:
- LLVMDisposeRelocationIterator(rel_itr);
- return false;
- }
- static bool
- is_relocation_section_name(AOTObjectData *obj_data, char *section_name)
- {
- return (!strcmp(section_name, ".rela.text")
- || !strcmp(section_name, ".rel.text")
- || !strcmp(section_name, ".rela.text.unlikely.")
- || !strcmp(section_name, ".rel.text.unlikely.")
- || !strcmp(section_name, ".rela.text.hot.")
- || !strcmp(section_name, ".rel.text.hot.")
- || !strcmp(section_name, ".rela.ltext")
- || !strcmp(section_name, ".rel.ltext")
- || !strcmp(section_name, ".rela.ltext.unlikely.")
- || !strcmp(section_name, ".rel.ltext.unlikely.")
- || !strcmp(section_name, ".rela.ltext.hot.")
- || !strcmp(section_name, ".rel.ltext.hot.")
- || !strcmp(section_name, ".rela.literal")
- || !strcmp(section_name, ".rela.data")
- || !strcmp(section_name, ".rel.data")
- || !strcmp(section_name, ".rela.sdata")
- || !strcmp(section_name, ".rel.sdata")
- || !strcmp(section_name, ".rela.rodata")
- || !strcmp(section_name, ".rel.rodata")
- || (obj_data->comp_ctx->enable_llvm_pgo
- && (!strcmp(section_name, ".rela__llvm_prf_data")
- || !strcmp(section_name, ".rel__llvm_prf_data")))
- /* ".rela.rodata.cst4/8/16/.." */
- || !strncmp(section_name, ".rela.rodata.cst",
- strlen(".rela.rodata.cst"))
- /* ".rel.rodata.cst4/8/16/.." */
- || !strncmp(section_name, ".rel.rodata.cst",
- strlen(".rel.rodata.cst")));
- }
- static bool
- is_relocation_section(AOTObjectData *obj_data, LLVMSectionIteratorRef sec_itr)
- {
- uint32 count = 0;
- char *name = (char *)LLVMGetSectionName(sec_itr);
- if (name) {
- if (is_relocation_section_name(obj_data, name))
- return true;
- else if ((!strcmp(name, ".text") || !strcmp(name, ".text.unlikely.")
- || !strcmp(name, ".text.hot.") || !strcmp(name, ".rdata"))
- && get_relocations_count(sec_itr, &count) && count > 0)
- return true;
- }
- return false;
- }
- static bool
- is_readonly_section(const char *name)
- {
- return !strcmp(name, ".rel.text") || !strcmp(name, ".rela.text")
- || !strcmp(name, ".rel.ltext") || !strcmp(name, ".rela.ltext")
- || !strcmp(name, ".rela.literal") || !strcmp(name, ".text")
- || !strcmp(name, ".ltext");
- }
- static bool
- get_relocation_groups_count(AOTObjectData *obj_data, uint32 *p_count)
- {
- uint32 count = 0;
- LLVMSectionIteratorRef sec_itr;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if (is_relocation_section(obj_data, sec_itr)) {
- count++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- *p_count = count;
- return true;
- }
- static bool
- aot_resolve_object_relocation_groups(AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr;
- AOTRelocationGroup *relocation_group;
- uint32 group_count, llvm_prf_data_idx = 0;
- char *name;
- uint32 size;
- /* calculate relocation groups count and allocate memory */
- if (!get_relocation_groups_count(obj_data, &group_count))
- return false;
- if (0 == (obj_data->relocation_group_count = group_count))
- return true;
- size = (uint32)sizeof(AOTRelocationGroup) * group_count;
- if (!(relocation_group = obj_data->relocation_groups =
- wasm_runtime_malloc(size))) {
- aot_set_last_error("allocate memory for relocation groups failed.");
- return false;
- }
- memset(obj_data->relocation_groups, 0, size);
- /* resolve each relocation group */
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if (is_relocation_section(obj_data, sec_itr)) {
- name = (char *)LLVMGetSectionName(sec_itr);
- relocation_group->section_name = name;
- if (obj_data->comp_ctx->enable_llvm_pgo
- && (!strcmp(name, ".rela__llvm_prf_data")
- || !strcmp(name, ".rel__llvm_prf_data"))) {
- char buf[32];
- snprintf(buf, sizeof(buf), "%s%u", name, llvm_prf_data_idx);
- size = (uint32)(strlen(buf) + 1);
- if (!(relocation_group->section_name =
- wasm_runtime_malloc(size))) {
- aot_set_last_error(
- "allocate memory for section name failed.");
- LLVMDisposeSectionIterator(sec_itr);
- return false;
- }
- bh_memcpy_s(relocation_group->section_name, size, buf, size);
- relocation_group->is_section_name_allocated = true;
- }
- if (!aot_resolve_object_relocation_group(obj_data, relocation_group,
- sec_itr)) {
- LLVMDisposeSectionIterator(sec_itr);
- return false;
- }
- if (obj_data->comp_ctx->enable_llvm_pgo
- && (!strcmp(name, ".rela__llvm_prf_data")
- || !strcmp(name, ".rel__llvm_prf_data"))) {
- llvm_prf_data_idx++;
- }
- if (!strcmp(relocation_group->section_name, ".rela.text.unlikely.")
- || !strcmp(relocation_group->section_name, ".rela.text.hot.")) {
- relocation_group->section_name = ".rela.text";
- }
- else if (!strcmp(relocation_group->section_name,
- ".rela.ltext.unlikely.")
- || !strcmp(relocation_group->section_name,
- ".rela.ltext.hot.")) {
- relocation_group->section_name = ".rela.ltext";
- }
- else if (!strcmp(relocation_group->section_name,
- ".rel.text.unlikely.")
- || !strcmp(relocation_group->section_name,
- ".rel.text.hot.")) {
- relocation_group->section_name = ".rel.text";
- }
- else if (!strcmp(relocation_group->section_name,
- ".rel.ltext.unlikely.")
- || !strcmp(relocation_group->section_name,
- ".rel.ltext.hot.")) {
- relocation_group->section_name = ".rel.ltext";
- }
- /*
- * Relocations in read-only sections are problematic,
- * especially for XIP on platforms which don't have
- * copy-on-write mappings.
- */
- if (obj_data->comp_ctx->is_indirect_mode
- && is_readonly_section(relocation_group->section_name)) {
- LOG_WARNING("%" PRIu32
- " text relocations in %s section for indirect mode",
- relocation_group->relocation_count,
- relocation_group->section_name);
- }
- relocation_group++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- return true;
- }
- static void
- destroy_relocation_groups(AOTRelocationGroup *relocation_groups,
- uint32 relocation_group_count)
- {
- uint32 i, j;
- AOTRelocationGroup *relocation_group = relocation_groups;
- for (i = 0; i < relocation_group_count; i++, relocation_group++) {
- if (relocation_group->relocations) {
- for (j = 0; j < relocation_group->relocation_count; j++) {
- if (relocation_group->relocations[j].is_symbol_name_allocated)
- wasm_runtime_free(
- relocation_group->relocations[j].symbol_name);
- }
- wasm_runtime_free(relocation_group->relocations);
- }
- if (relocation_group->is_section_name_allocated)
- wasm_runtime_free(relocation_group->section_name);
- }
- wasm_runtime_free(relocation_groups);
- }
- static void
- destroy_relocation_symbol_list(AOTSymbolList *symbol_list)
- {
- AOTSymbolNode *elem;
- elem = symbol_list->head;
- while (elem) {
- AOTSymbolNode *next = elem->next;
- wasm_runtime_free(elem);
- elem = next;
- }
- }
- static void
- aot_obj_data_destroy(AOTObjectData *obj_data)
- {
- if (obj_data->binary)
- LLVMDisposeBinary(obj_data->binary);
- if (obj_data->mem_buf)
- LLVMDisposeMemoryBuffer(obj_data->mem_buf);
- if (obj_data->funcs)
- wasm_runtime_free(obj_data->funcs);
- if (obj_data->data_sections) {
- uint32 i;
- for (i = 0; i < obj_data->data_sections_count; i++) {
- if (obj_data->data_sections[i].name
- && obj_data->data_sections[i].is_name_allocated) {
- wasm_runtime_free(obj_data->data_sections[i].name);
- }
- if (obj_data->data_sections[i].data
- && obj_data->data_sections[i].is_data_allocated) {
- wasm_runtime_free(obj_data->data_sections[i].data);
- }
- }
- wasm_runtime_free(obj_data->data_sections);
- }
- if (obj_data->relocation_groups)
- destroy_relocation_groups(obj_data->relocation_groups,
- obj_data->relocation_group_count);
- if (obj_data->symbol_list.len)
- destroy_relocation_symbol_list(&obj_data->symbol_list);
- if (obj_data->stack_sizes)
- wasm_runtime_free(obj_data->stack_sizes);
- wasm_runtime_free(obj_data);
- }
- static AOTObjectData *
- aot_obj_data_create(AOTCompContext *comp_ctx)
- {
- char *err = NULL;
- AOTObjectData *obj_data;
- LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
- bh_print_time("Begin to emit object file to buffer");
- if (!(obj_data = wasm_runtime_malloc(sizeof(AOTObjectData)))) {
- aot_set_last_error("allocate memory failed.");
- return false;
- }
- memset(obj_data, 0, sizeof(AOTObjectData));
- obj_data->comp_ctx = comp_ctx;
- bh_print_time("Begin to emit object file");
- if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
- #if defined(_WIN32) || defined(_WIN32_)
- aot_set_last_error("external toolchain not supported on Windows");
- goto fail;
- #else
- /* Generate a temp file name */
- int ret;
- char obj_file_name[64];
- if (!aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name,
- sizeof(obj_file_name))) {
- goto fail;
- }
- if (!aot_emit_object_file(comp_ctx, obj_file_name)) {
- goto fail;
- }
- /* create memory buffer from object file */
- ret = LLVMCreateMemoryBufferWithContentsOfFile(
- obj_file_name, &obj_data->mem_buf, &err);
- /* remove temp object file */
- unlink(obj_file_name);
- if (ret != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("create mem buffer with file failed.");
- goto fail;
- }
- #endif /* end of defined(_WIN32) || defined(_WIN32_) */
- }
- else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
- #if defined(_WIN32) || defined(_WIN32_)
- aot_set_last_error("emit object file on Windows is unsupported.");
- goto fail;
- #else
- /* Emit to assembly file instead for arc target
- as it cannot emit to object file */
- char file_name[] = "wasm-XXXXXX", buf[128];
- int fd, ret;
- if ((fd = mkstemp(file_name)) <= 0) {
- aot_set_last_error("make temp file failed.");
- goto fail;
- }
- /* close and remove temp file */
- close(fd);
- unlink(file_name);
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
- if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
- comp_ctx->module, buf, LLVMAssemblyFile,
- &err)
- != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("emit elf to object file failed.");
- goto fail;
- }
- /* call arc gcc to compile assembly file to object file */
- /* TODO: get arc gcc from environment variable firstly
- and check whether the toolchain exists actually */
- snprintf(buf, sizeof(buf), "%s%s%s%s%s%s",
- "/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ",
- "-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
- /* TODO: use try..catch to handle possible exceptions */
- ret = system(buf);
- /* remove temp assembly file */
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
- unlink(buf);
- if (ret != 0) {
- aot_set_last_error("failed to compile asm file to obj file "
- "with arc gcc toolchain.");
- goto fail;
- }
- /* create memory buffer from object file */
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
- ret = LLVMCreateMemoryBufferWithContentsOfFile(buf, &obj_data->mem_buf,
- &err);
- /* remove temp object file */
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
- unlink(buf);
- if (ret != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("create mem buffer with file failed.");
- goto fail;
- }
- #endif /* end of defined(_WIN32) || defined(_WIN32_) */
- }
- else {
- if (LLVMTargetMachineEmitToMemoryBuffer(
- comp_ctx->target_machine, comp_ctx->module, LLVMObjectFile,
- &err, &obj_data->mem_buf)
- != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("llvm emit to memory buffer failed.");
- goto fail;
- }
- }
- if (!(obj_data->binary = LLVMCreateBinary(obj_data->mem_buf, NULL, &err))) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("llvm create binary failed.");
- goto fail;
- }
- /* Create wasm feature flags form compile options */
- obj_data->target_info.feature_flags = 0;
- if (comp_ctx->enable_simd) {
- obj_data->target_info.feature_flags |= WASM_FEATURE_SIMD_128BIT;
- }
- if (comp_ctx->enable_bulk_memory) {
- obj_data->target_info.feature_flags |= WASM_FEATURE_BULK_MEMORY;
- }
- if (comp_ctx->enable_thread_mgr) {
- obj_data->target_info.feature_flags |= WASM_FEATURE_MULTI_THREAD;
- }
- if (comp_ctx->enable_ref_types) {
- obj_data->target_info.feature_flags |= WASM_FEATURE_REF_TYPES;
- }
- if (comp_ctx->enable_gc) {
- obj_data->target_info.feature_flags |= WASM_FEATURE_GARBAGE_COLLECTION;
- }
- bh_print_time("Begin to resolve object file info");
- /* resolve target info/text/relocations/functions */
- if (!aot_resolve_target_info(comp_ctx, obj_data)
- || !aot_resolve_text(obj_data) || !aot_resolve_literal(obj_data)
- || !aot_resolve_object_data_sections(obj_data)
- || !aot_resolve_functions(comp_ctx, obj_data)
- || !aot_resolve_object_relocation_groups(obj_data))
- goto fail;
- return obj_data;
- fail:
- aot_obj_data_destroy(obj_data);
- return NULL;
- }
- uint8 *
- aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- uint32 *p_aot_file_size)
- {
- AOTObjectData *obj_data = aot_obj_data_create(comp_ctx);
- uint8 *aot_file_buf, *buf, *buf_end;
- uint32 aot_file_size, offset = 0;
- if (!obj_data)
- return NULL;
- aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
- if (aot_file_size == 0) {
- aot_set_last_error("get aot file size failed");
- goto fail1;
- }
- if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
- aot_set_last_error("allocate memory failed.");
- goto fail1;
- }
- memset(aot_file_buf, 0, aot_file_size);
- buf_end = buf + aot_file_size;
- if (!aot_emit_file_header(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_target_info_section(buf, buf_end, &offset, comp_data,
- obj_data)
- || !aot_emit_init_data_section(buf, buf_end, &offset, comp_ctx,
- comp_data, obj_data)
- || !aot_emit_text_section(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_func_section(buf, buf_end, &offset, comp_ctx, comp_data,
- obj_data)
- || !aot_emit_export_section(buf, buf_end, &offset, comp_ctx, comp_data,
- obj_data)
- || !aot_emit_relocation_section(buf, buf_end, &offset, comp_ctx,
- comp_data, obj_data)
- || !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx)
- || !aot_emit_custom_sections(buf, buf_end, &offset, comp_data, comp_ctx)
- #if WASM_ENABLE_STRINGREF != 0
- || !aot_emit_string_literal_section(buf, buf_end, &offset, comp_data,
- comp_ctx)
- #endif
- )
- goto fail2;
- #if 0
- dump_buf(buf, offset, "sections");
- #endif
- if (offset != aot_file_size) {
- aot_set_last_error("emit aot file failed.");
- goto fail2;
- }
- *p_aot_file_size = aot_file_size;
- aot_obj_data_destroy(obj_data);
- return aot_file_buf;
- fail2:
- wasm_runtime_free(aot_file_buf);
- fail1:
- aot_obj_data_destroy(obj_data);
- return NULL;
- }
- bool
- aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- const char *file_name)
- {
- uint8 *aot_file_buf;
- uint32 aot_file_size;
- bool ret = false;
- FILE *file;
- bh_print_time("Begin to emit AOT file");
- if (!(aot_file_buf =
- aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size))) {
- return false;
- }
- /* write buffer to file */
- if (!(file = fopen(file_name, "wb"))) {
- aot_set_last_error("open or create aot file failed.");
- goto fail1;
- }
- if (!fwrite(aot_file_buf, aot_file_size, 1, file)) {
- aot_set_last_error("write to aot file failed.");
- goto fail2;
- }
- ret = true;
- fail2:
- fclose(file);
- fail1:
- wasm_runtime_free(aot_file_buf);
- return ret;
- }
|