aot_emit_aot_file.c 153 KB

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