12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515 |
- {
- Copyright (c) 2021 by Nikolay Nikolov
- Contains the WebAssembly binary module format reader and writer
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit ogwasm;
- {$i fpcdefs.inc}
- interface
- uses
- { common }
- cclasses,globtype,
- { target }
- systems,cpubase,
- { assembler }
- aasmbase,assemble,aasmcpu,
- { WebAssembly module format definitions }
- wasmbase,
- { output }
- ogbase,
- owbase;
- type
- TWasmObjSymbolExtraData = class;
- TGlobalInitializer = record
- case typ:TWasmBasicType of
- wbt_i32: (init_i32: Int32);
- wbt_i64: (init_i64: Int64);
- wbt_f32: (init_f32: Single);
- wbt_f64: (init_f64: Double);
- end;
- { TWasmObjSymbolLinkingData }
- TWasmObjSymbolLinkingData = class
- public
- ImportModule: ansistring;
- ImportName: ansistring;
- FuncType: TWasmFuncType;
- ExeFunctionIndex: Integer;
- ExeIndirectFunctionTableIndex: Integer;
- ExeTypeIndex: Integer;
- ExeTagIndex: Integer;
- GlobalType: TWasmBasicType;
- GlobalIsMutable: Boolean;
- GlobalInitializer: TGlobalInitializer;
- IsExported: Boolean;
- ExportName: ansistring;
- constructor Create;
- destructor Destroy;override;
- end;
- { TWasmObjSymbol }
- TWasmObjSymbol = class(TObjSymbol)
- FuncIndex: Integer;
- SymbolIndex: Integer;
- GlobalIndex: Integer;
- TagIndex: Integer;
- AliasOf: ansistring;
- ExtraData: TWasmObjSymbolExtraData;
- LinkingData: TWasmObjSymbolLinkingData;
- TlsGlobalSym: TWasmObjSymbol;
- TlsDataSym: TWasmObjSymbol;
- constructor create(AList:TFPHashObjectList;const AName:string);override;
- destructor Destroy;override;
- function IsAlias: Boolean;
- end;
- { TWasmObjRelocation }
- TWasmObjRelocation = class(TObjRelocation)
- public
- TypeIndex: Integer;
- Addend: LongInt;
- { used during linking }
- FuncType: TWasmFuncType;
- ExeTypeIndex: Integer;
- IsFunctionOffsetI32: Boolean;
- constructor CreateTypeIndex(ADataOffset:TObjSectionOfs; ATypeIndex: Integer);
- constructor CreateFuncType(ADataOffset:TObjSectionOfs; AFuncType: TWasmFuncType);
- destructor Destroy;override;
- end;
- { TWasmObjSymbolExtraData }
- TWasmObjSymbolExtraData = class(TFPHashObject)
- TypeIdx: Integer;
- ExceptionTagTypeIdx: Integer;
- ImportModule: ansistring;
- ImportName: ansistring;
- ExportName: ansistring;
- GlobalType: TWasmBasicType;
- GlobalIsImmutable: Boolean;
- Locals: array of TWasmBasicType;
- EncodedLocals: tdynamicarray;
- constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
- destructor Destroy; override;
- procedure AddLocals(alocals: TWasmLocalsDynArray);
- end;
- { TWasmObjSection }
- TWasmObjSection = class(TObjSection)
- public
- SegIdx: Integer;
- SegSymIdx: Integer;
- SegOfs: qword;
- FileSectionOfs: qword;
- MainFuncSymbol: TWasmObjSymbol;
- CustomSectionIdx: Integer;
- constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);override;
- function IsCode: Boolean;
- function IsData: Boolean;
- function IsDebug: Boolean;
- end;
- { TWasmFuncTypeTable }
- TWasmFuncTypeTable = class
- private
- FFuncTypes: array of TWasmFuncType;
- function GetCount: Integer;
- function GetItem(Index: Integer): TWasmFuncType;
- public
- destructor Destroy; override;
- function AddOrGetFuncType(wft: TWasmFuncType): integer;
- procedure WriteTo(d: tdynamicarray);
- property Count: Integer read GetCount;
- property Items[Index: Integer]: TWasmFuncType read GetItem; default;
- end;
- { TWasmObjData }
- TWasmObjData = class(TObjData)
- private
- FFuncTypes: TWasmFuncTypeTable;
- FObjSymbolsExtraDataList: TFPHashObjectList;
- FLastFuncName: string;
- function is_smart_section(atype:TAsmSectiontype):boolean;
- function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
- public
- constructor create(const n:string);override;
- destructor destroy; override;
- function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
- procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
- function AddOrCreateObjSymbolExtraData(const symname:TSymStr): TWasmObjSymbolExtraData;
- function globalref(asmsym:TAsmSymbol):TObjSymbol;
- function ExceptionTagRef(asmsym:TAsmSymbol):TObjSymbol;
- procedure DeclareGlobalType(gt: tai_globaltype);
- procedure DeclareFuncType_Pass0(ft: tai_functype);
- procedure DeclareFuncType_Pass1(ft: tai_functype);
- procedure DeclareFuncType_Pass2(ft: tai_functype);
- procedure DeclareTagType(tt: tai_tagtype);
- procedure DeclareExportName(en: tai_export_name);
- procedure DeclareImportModule(aim: tai_import_module);
- procedure DeclareImportName(ain: tai_import_name);
- procedure DeclareLocals_Pass0(al: tai_local);
- procedure DeclareLocals_Pass1(al: tai_local);
- procedure WriteLocals_Pass2(al: tai_local);
- procedure symbolpairdefine(akind: TSymbolPairKind;const asym, avalue: string);override;
- property FuncTypes: TWasmFuncTypeTable read FFuncTypes;
- end;
- { TWasmObjOutput }
- TWasmObjOutput = class(tObjOutput)
- private
- FData: TWasmObjData;
- FWasmRelocationCodeTable: tdynamicarray;
- FWasmRelocationCodeTableEntriesCount: Integer;
- FWasmRelocationDataTable: tdynamicarray;
- FWasmRelocationDataTableEntriesCount: Integer;
- FWasmRelocationDebugFrameTable: tdynamicarray;
- FWasmRelocationDebugFrameTableEntriesCount: Integer;
- FWasmRelocationDebugInfoTable: tdynamicarray;
- FWasmRelocationDebugInfoTableEntriesCount: Integer;
- FWasmRelocationDebugLineTable: tdynamicarray;
- FWasmRelocationDebugLineTableEntriesCount: Integer;
- FWasmRelocationDebugAbbrevTable: tdynamicarray;
- FWasmRelocationDebugAbbrevTableEntriesCount: Integer;
- FWasmRelocationDebugArangesTable: tdynamicarray;
- FWasmRelocationDebugArangesTableEntriesCount: Integer;
- FWasmRelocationDebugRangesTable: tdynamicarray;
- FWasmRelocationDebugRangesTableEntriesCount: Integer;
- FWasmRelocationDebugStrTable: tdynamicarray;
- FWasmRelocationDebugStrTableEntriesCount: Integer;
- FWasmSymbolTable: tdynamicarray;
- FWasmSymbolTableEntriesCount: Integer;
- FWasmSections: array [TWasmSectionID] of tdynamicarray;
- FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
- FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of tdynamicarray;
- procedure WriteWasmSection(wsid: TWasmSectionID);
- procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
- function IsExternalFunction(sym: TObjSymbol): Boolean;
- function IsExportedFunction(sym: TWasmObjSymbol): Boolean;
- procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
- procedure WriteSymbolTable;
- procedure WriteRelocationCodeTable(CodeSectionIndex: Integer);
- procedure WriteRelocationDataTable(DataSectionIndex: Integer);
- procedure MaybeWriteRelocationDebugTable(cst: TWasmCustomSectionType; SectionIndex: Integer; EntriesCount: Integer; Table: tdynamicarray);
- procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
- procedure DoRelocations;
- procedure WriteRelocations;
- function FindFunctionSymbol(Symbol: TWasmObjSymbol): TWasmObjSymbol;
- protected
- function writeData(Data:TObjData):boolean;override;
- public
- constructor create(AWriter:TObjectWriter);override;
- destructor destroy;override;
- end;
- { TWasmObjInput }
- TWasmObjInput = class(TObjInput)
- private
- FFuncTypes: array of TWasmFuncType;
- public
- constructor create;override;
- destructor Destroy;override;
- class function CanReadObjData(AReader:TObjectreader):boolean;override;
- function ReadObjData(AReader:TObjectreader;out ObjData:TObjData):boolean;override;
- end;
- { TWasmExeOutput }
- TWasmExeOutput = class(TExeOutput)
- private
- const
- DataSections: array [1..3] of string = (
- '.rodata',
- '.data',
- 'fpc.resources');
- WasmPageSize = 65536;
- type
- TCustomSectionNameMapEntry = record
- idx: UInt32;
- name: string;
- end;
- TCustomSectionNameMap = array of TCustomSectionNameMapEntry;
- private
- FImports: TFPHashObjectList;
- FFuncTypes: TWasmFuncTypeTable;
- FFunctionImports: array of record
- ModName: ansistring;
- Name: ansistring;
- TypeIdx: uint32;
- end;
- FTagImports: array of record
- end;
- FIndirectFunctionTable: array of record
- FuncIdx: Integer;
- end;
- FImportedMemories: array of record
- ModName: ansistring;
- Name: ansistring;
- MemType: TWasmMemoryType;
- end;
- FMemories: array of TWasmMemoryType;
- FRelocationPass: Integer;
- FWasmSections: array [TWasmSectionID] of tdynamicarray;
- FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
- FWasmNameSubsections: array [TWasmNameSubsectionType] of tdynamicarray;
- FStackPointerSym: TWasmObjSymbol;
- FTlsBaseSym: TWasmObjSymbol;
- FTlsSizeSym: TWasmObjSymbol;
- FTlsAlignSym: TWasmObjSymbol;
- FInitTlsFunctionSym: TWasmObjSymbol;
- FInitSharedMemoryFunctionSym: TWasmObjSymbol;
- FMinMemoryPages,
- FMaxMemoryPages: Integer;
- { use for the Name section }
- FFunctionNameMap: TCustomSectionNameMap;
- FGlobalNameMap: TCustomSectionNameMap;
- FDataNameMap: TCustomSectionNameMap;
- FTagNameMap: TCustomSectionNameMap;
- procedure AddToNameMap(var nm: TCustomSectionNameMap; aidx: UInt32; const aname: string);
- procedure AddToFunctionNameMap(aidx: UInt32; const aname: string);
- procedure AddToGlobalNameMap(aidx: UInt32; const aname: string);
- procedure AddToDataNameMap(aidx: UInt32; const aname: string);
- procedure AddToTagNameMap(aidx: UInt32; const aname: string);
- procedure WriteWasmSection(wsid: TWasmSectionID);
- procedure WriteWasmSectionIfNotEmpty(wsid: TWasmSectionID);
- procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
- procedure PrepareImports;
- procedure PrepareFunctions;
- procedure PrepareTags;
- function AddOrGetIndirectFunctionTableIndex(FuncIdx: Integer): integer;
- procedure SetStackPointer;
- procedure SetTlsSizeAlignAndBase;
- procedure SetThreadVarGlobalsInitValues;
- procedure GenerateCode_InitTls;
- procedure GenerateCode_InitSharedMemory;
- procedure GenerateCode_InvokeHelper;
- procedure WriteExeSectionToDynArray(exesec: TExeSection; dynarr: tdynamicarray);
- procedure WriteMemoryTo(dest: tdynamicarray;const MemType:TWasmMemoryType);
- function Memory2String(const MemType:TWasmMemoryType):string;
- procedure WriteMap_TypeSection;
- procedure WriteMap_IndirectFunctionTable;
- protected
- function writeData:boolean;override;
- procedure DoRelocationFixup(objsec:TObjSection);override;
- public
- constructor create;override;
- destructor destroy;override;
- procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
- procedure AfterUnusedSectionRemoval;override;
- procedure MemPos_ExeSection(const aname:string);override;
- procedure Load_Symbol(const aname: string);override;
- end;
- { TWasmAssembler }
- TWasmAssembler = class(tinternalassembler)
- constructor create(info: pasminfo; smart:boolean);override;
- end;
- implementation
- uses
- cutils,verbose,version,globals,fmodule,ogmap;
- const
- StackPointerSymStr='__stack_pointer';
- procedure WriteUleb5(d: tdynamicarray; v: uint64);
- var
- b: byte;
- i: Integer;
- begin
- for i:=1 to 5 do
- begin
- b:=byte(v) and 127;
- v:=v shr 7;
- if i<>5 then
- b:=b or 128;
- d.write(b,1);
- end;
- end;
- procedure WriteUleb5(d: tobjsection; v: uint64);
- var
- b: byte;
- i: Integer;
- begin
- for i:=1 to 5 do
- begin
- b:=byte(v) and 127;
- v:=v shr 7;
- if i<>5 then
- b:=b or 128;
- d.write(b,1);
- end;
- end;
- procedure WriteSleb5(d: tdynamicarray; v: int64);
- var
- b: byte;
- i: Integer;
- begin
- for i:=1 to 5 do
- begin
- b:=byte(v) and 127;
- v:=SarInt64(v,7);
- if i<>5 then
- b:=b or 128;
- d.write(b,1);
- end;
- end;
- procedure WriteSleb5(d: tobjsection; v: int64);
- var
- b: byte;
- i: Integer;
- begin
- for i:=1 to 5 do
- begin
- b:=byte(v) and 127;
- v:=SarInt64(v,7);
- if i<>5 then
- b:=b or 128;
- d.write(b,1);
- end;
- end;
- procedure WriteUleb(d: tdynamicarray; v: uint64);
- var
- b: byte;
- begin
- repeat
- b:=byte(v) and 127;
- v:=v shr 7;
- if v<>0 then
- b:=b or 128;
- d.write(b,1);
- until v=0;
- end;
- procedure WriteUleb(d: tobjsection; v: uint64);
- var
- b: byte;
- begin
- repeat
- b:=byte(v) and 127;
- v:=v shr 7;
- if v<>0 then
- b:=b or 128;
- d.write(b,1);
- until v=0;
- end;
- procedure WriteUleb(w: TObjectWriter; v: uint64);
- var
- b: byte;
- begin
- repeat
- b:=byte(v) and 127;
- v:=v shr 7;
- if v<>0 then
- b:=b or 128;
- w.write(b,1);
- until v=0;
- end;
- procedure WriteSleb(d: tdynamicarray; v: int64);
- var
- b: byte;
- Done: Boolean=false;
- begin
- repeat
- b:=byte(v) and 127;
- v:=SarInt64(v,7);
- if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
- Done:=true
- else
- b:=b or 128;
- d.write(b,1);
- until Done;
- end;
- procedure WriteSleb(d: tobjsection; v: int64);
- var
- b: byte;
- Done: Boolean=false;
- begin
- repeat
- b:=byte(v) and 127;
- v:=SarInt64(v,7);
- if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
- Done:=true
- else
- b:=b or 128;
- d.write(b,1);
- until Done;
- end;
- function UlebEncodingSize(v: uint64): Integer;
- var
- b: byte;
- begin
- Result:=0;
- repeat
- b:=byte(v) and 127;
- v:=v shr 7;
- if v<>0 then
- b:=b or 128;
- Inc(Result);
- until v=0;
- end;
- {$ifdef FPC_LITTLE_ENDIAN}
- procedure WriteF32LE(d: tdynamicarray; v: Single);
- begin
- d.write(v,4);
- end;
- procedure WriteF64LE(d: tdynamicarray; v: Double);
- begin
- d.write(v,8);
- end;
- {$else FPC_LITTLE_ENDIAN}
- procedure WriteF32LE(d: tdynamicarray; v: Single);
- var
- tmpI: UInt32;
- begin
- Move(v,tmpI,4);
- tmpI:=SwapEndian(tmpI);
- d.write(tmpI,4);
- end;
- procedure WriteF64LE(d: tdynamicarray; v: Double);
- var
- tmpI: UInt64;
- begin
- Move(v,tmpI,8);
- tmpI:=SwapEndian(tmpI);
- d.write(tmpI,8);
- end;
- {$endif FPC_LITTLE_ENDIAN}
- procedure WriteByte(d: tdynamicarray; b: byte);
- begin
- d.write(b,1);
- end;
- procedure WriteName(d: tdynamicarray; const s: string);
- begin
- WriteUleb(d,Length(s));
- d.writestr(s);
- end;
- procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
- begin
- WriteByte(dest,encode_wasm_basic_type(wbt));
- end;
- procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
- var
- i: Integer;
- begin
- WriteUleb(dest,Length(wrt));
- for i:=low(wrt) to high(wrt) do
- WriteWasmBasicType(dest,wrt[i]);
- end;
- function ReadUleb(d: tdynamicarray): uint64;
- var
- b: byte;
- shift:integer;
- begin
- b:=0;
- result:=0;
- shift:=0;
- repeat
- d.read(b,1);
- result:=result or (uint64(b and 127) shl shift);
- inc(shift,7);
- until (b and 128)=0;
- end;
- function ReadSleb(d: tdynamicarray): int64;
- var
- b: byte;
- shift:integer;
- begin
- b:=0;
- result:=0;
- shift:=0;
- repeat
- d.read(b,1);
- result:=result or (uint64(b and 127) shl shift);
- inc(shift,7);
- until (b and 128)=0;
- {$ifopt Q+}
- {$define overflowon}
- {$Q-}
- {$endif}
- {$ifopt R+}
- {$define rangeon}
- {$R-}
- {$endif}
- if (b and 64)<>0 then
- result:=result or (high(uint64) shl shift);
- end;
- {$ifdef overflowon}
- {$Q+}
- {$undef overflowon}
- {$endif}
- {$ifdef rangeon}
- {$R+}
- {$undef rangeon}
- {$endif}
- procedure AddSleb5(d: tdynamicarray; v: int64);
- var
- q: Int64;
- p: LongWord;
- begin
- p:=d.Pos;
- q:=ReadSleb(d);
- q:=q+v;
- d.seek(p);
- WriteSleb5(d,q);
- end;
- procedure AddUleb5(d: tdynamicarray; v: int64);
- var
- q: UInt64;
- p: LongWord;
- begin
- p:=d.Pos;
- q:=ReadUleb(d);
- q:=q+v;
- d.seek(p);
- WriteUleb5(d,q);
- end;
- procedure AddInt32(d: tdynamicarray; v: int32);
- var
- q: int32;
- p: LongWord;
- begin
- p:=d.Pos;
- d.read(q,4);
- {$ifdef FPC_BIG_ENDIAN}
- q:=SwapEndian(q);
- {$endif FPC_BIG_ENDIAN}
- q:=q+v;
- {$ifdef FPC_BIG_ENDIAN}
- q:=SwapEndian(q);
- {$endif FPC_BIG_ENDIAN}
- d.seek(p);
- d.write(q,4);
- end;
- procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
- var
- buf: array [0..4095] of byte;
- bs: Integer;
- begin
- while size>0 do
- begin
- if size<SizeOf(buf) then
- bs:=Integer(size)
- else
- bs:=SizeOf(buf);
- src.read(buf,bs);
- dest.write(buf,bs);
- dec(size,bs);
- end;
- end;
- procedure WriteZeros(dest: tdynamicarray; size: QWord);
- var
- buf : array[0..1023] of byte;
- bs: Integer;
- begin
- fillchar(buf,sizeof(buf),0);
- while size>0 do
- begin
- if size<SizeOf(buf) then
- bs:=Integer(size)
- else
- bs:=SizeOf(buf);
- dest.write(buf,bs);
- dec(size,bs);
- end;
- end;
- {****************************************************************************
- TWasmObjSymbolLinkingData
- ****************************************************************************}
- constructor TWasmObjSymbolLinkingData.Create;
- begin
- ExeFunctionIndex:=-1;
- ExeIndirectFunctionTableIndex:=-1;
- ExeTypeIndex:=-1;
- ExeTagIndex:=-1;
- end;
- destructor TWasmObjSymbolLinkingData.Destroy;
- begin
- FuncType.Free;
- inherited Destroy;
- end;
- {****************************************************************************
- TWasmObjRelocation
- ****************************************************************************}
- constructor TWasmObjRelocation.CreateTypeIndex(ADataOffset: TObjSectionOfs; ATypeIndex: Integer);
- begin
- DataOffset:=ADataOffset;
- Symbol:=nil;
- OrgSize:=0;
- Group:=nil;
- ObjSection:=nil;
- ftype:=ord(RELOC_TYPE_INDEX_LEB);
- TypeIndex:=ATypeIndex;
- FuncType:=nil;
- ExeTypeIndex:=-1;
- end;
- constructor TWasmObjRelocation.CreateFuncType(ADataOffset: TObjSectionOfs; AFuncType: TWasmFuncType);
- begin
- DataOffset:=ADataOffset;
- Symbol:=nil;
- OrgSize:=0;
- Group:=nil;
- ObjSection:=nil;
- ftype:=ord(RELOC_TYPE_INDEX_LEB);
- TypeIndex:=-1;
- ExeTypeIndex:=-1;
- FuncType:=TWasmFuncType.Create(AFuncType);
- end;
- destructor TWasmObjRelocation.Destroy;
- begin
- FuncType.Free;
- inherited Destroy;
- end;
- {****************************************************************************
- TWasmObjSymbol
- ****************************************************************************}
- constructor TWasmObjSymbol.create(AList: TFPHashObjectList; const AName: string);
- begin
- inherited create(AList,AName);
- FuncIndex:=-1;
- SymbolIndex:=-1;
- GlobalIndex:=-1;
- TagIndex:=-1;
- AliasOf:='';
- ExtraData:=nil;
- LinkingData:=TWasmObjSymbolLinkingData.Create;
- end;
- destructor TWasmObjSymbol.Destroy;
- begin
- LinkingData.Free;
- inherited Destroy;
- end;
- function TWasmObjSymbol.IsAlias: Boolean;
- begin
- result:=AliasOf<>'';
- end;
- {****************************************************************************
- TWasmObjSymbolExtraData
- ****************************************************************************}
- constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
- begin
- EncodedLocals:=nil;
- inherited Create(HashObjectList,s);
- TypeIdx:=-1;
- ExceptionTagTypeIdx:=-1;
- end;
- destructor TWasmObjSymbolExtraData.Destroy;
- begin
- EncodedLocals.Free;
- inherited Destroy;
- end;
- procedure TWasmObjSymbolExtraData.AddLocals(alocals: TWasmLocalsDynArray);
- var
- i,
- rle_entries,
- cnt: Integer;
- lasttype: TWasmBasicType;
- begin
- Locals:=alocals;
- if Assigned(EncodedLocals) then
- internalerror(2024081502);
- EncodedLocals:=tdynamicarray.Create(64);
- if Length(Locals)=0 then
- begin
- WriteUleb(EncodedLocals,0);
- exit;
- end;
- rle_entries:=1;
- for i:=low(Locals)+1 to high(Locals) do
- if Locals[i]<>Locals[i-1] then
- inc(rle_entries);
- WriteUleb(EncodedLocals,rle_entries);
- lasttype:=Locals[Low(Locals)];
- cnt:=1;
- for i:=low(Locals)+1 to high(Locals) do
- if Locals[i]=Locals[i-1] then
- inc(cnt)
- else
- begin
- WriteUleb(EncodedLocals,cnt);
- WriteWasmBasicType(EncodedLocals,lasttype);
- lasttype:=Locals[i];
- cnt:=1;
- end;
- WriteUleb(EncodedLocals,cnt);
- WriteWasmBasicType(EncodedLocals,lasttype);
- end;
- {****************************************************************************
- TWasmObjSection
- ****************************************************************************}
- constructor TWasmObjSection.create(AList: TFPHashObjectList; const Aname: string; Aalign: longint; Aoptions: TObjSectionOptions);
- begin
- inherited create(AList, Aname, Aalign, Aoptions);
- SegIdx:=-1;
- SegSymIdx:=-1;
- CustomSectionIdx:=-1;
- MainFuncSymbol:=nil;
- end;
- function TWasmObjSection.IsCode: Boolean;
- const
- CodePrefix = '.text';
- begin
- result:=(Length(Name)>=Length(CodePrefix)) and
- (Copy(Name,1,Length(CodePrefix))=CodePrefix);
- end;
- function TWasmObjSection.IsData: Boolean;
- begin
- result:=not (IsCode or IsDebug);
- end;
- function TWasmObjSection.IsDebug: Boolean;
- const
- DebugPrefix = '.debug';
- begin
- result:=(Length(Name)>=Length(DebugPrefix)) and
- (Copy(Name,1,Length(DebugPrefix))=DebugPrefix);
- end;
- {****************************************************************************
- TWasmFuncTypeTable
- ****************************************************************************}
- function TWasmFuncTypeTable.GetCount: Integer;
- begin
- Result:=Length(FFuncTypes);
- end;
- function TWasmFuncTypeTable.GetItem(Index: Integer): TWasmFuncType;
- begin
- if (Index<Low(FFuncTypes)) or (Index>High(FFuncTypes)) then
- internalerror(2023123101);
- Result:=FFuncTypes[Index];
- end;
- destructor TWasmFuncTypeTable.Destroy;
- var
- i: Integer;
- begin
- for i:=low(FFuncTypes) to high(FFuncTypes) do
- begin
- FFuncTypes[i].free;
- FFuncTypes[i]:=nil;
- end;
- end;
- function TWasmFuncTypeTable.AddOrGetFuncType(wft: TWasmFuncType): integer;
- var
- i: Integer;
- begin
- for i:=low(FFuncTypes) to high(FFuncTypes) do
- if wft.Equals(FFuncTypes[i]) then
- exit(i);
- result:=Length(FFuncTypes);
- SetLength(FFuncTypes,result+1);
- FFuncTypes[result]:=TWasmFuncType.Create(wft);
- end;
- procedure TWasmFuncTypeTable.WriteTo(d: tdynamicarray);
- var
- types_count, i: Integer;
- begin
- types_count:=Count;
- WriteUleb(d,types_count);
- for i:=0 to types_count-1 do
- with Items[i] do
- begin
- WriteByte(d,$60);
- WriteWasmResultType(d,params);
- WriteWasmResultType(d,results);
- end;
- end;
- {****************************************************************************
- TWasmObjData
- ****************************************************************************}
- function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
- begin
- { For bss we need to set some flags that are target dependent,
- it is easier to disable it for smartlinking. It doesn't take up
- filespace }
- result:=not(target_info.system in systems_darwin) and
- create_smartlink_sections and
- (atype<>sec_toc) and
- (atype<>sec_user) and
- { on embedded systems every byte counts, so smartlink bss too }
- ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
- end;
- function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
- const aname: string; aorder: TAsmSectionOrder): string;
- const
- secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
- '.text',
- '.data',
- { why doesn't .rodata work? (FK) }
- { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
- { vtables (and anything else containing relocations), otherwise those are }
- { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
- { vtable for a class called Window: }
- { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
- { TODO: .data.ro not yet working}
- {$if defined(arm) or defined(riscv64) or defined(powerpc)}
- '.rodata',
- {$else defined(arm) or defined(riscv64) or defined(powerpc)}
- '.data',
- {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
- '.rodata',
- '.bss',
- '.tbss',
- '.pdata',
- '', { stubs }
- '__DATA,__nl_symbol_ptr',
- '__DATA,__la_symbol_ptr',
- '__DATA,__mod_init_func',
- '__DATA,__mod_term_func',
- '.stab',
- '.stabstr',
- '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
- '.eh_frame',
- '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges','.debug_loc','.debug_loclists',
- '.fpc',
- '.toc',
- '.init',
- '.fini',
- '.objc_class',
- '.objc_meta_class',
- '.objc_cat_cls_meth',
- '.objc_cat_inst_meth',
- '.objc_protocol',
- '.objc_string_object',
- '.objc_cls_meth',
- '.objc_inst_meth',
- '.objc_cls_refs',
- '.objc_message_refs',
- '.objc_symbols',
- '.objc_category',
- '.objc_class_vars',
- '.objc_instance_vars',
- '.objc_module_info',
- '.objc_class_names',
- '.objc_meth_var_types',
- '.objc_meth_var_names',
- '.objc_selector_strs',
- '.objc_protocol_ext',
- '.objc_class_ext',
- '.objc_property',
- '.objc_image_info',
- '.objc_cstring_object',
- '.objc_sel_fixup',
- '__DATA,__objc_data',
- '__DATA,__objc_const',
- '.objc_superrefs',
- '__DATA, __datacoal_nt,coalesced',
- '.objc_classlist',
- '.objc_nlclasslist',
- '.objc_catlist',
- '.obcj_nlcatlist',
- '.objc_protolist',
- '.stack',
- '.heap',
- '.gcc_except_table',
- '.ARM.attributes'
- );
- var
- sep : string[3];
- secname : string;
- begin
- secname:=secnames[atype];
- if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
- begin
- result:=secname+'.'+aname;
- exit;
- end;
- { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
- Thus, data which normally goes into .rodata and .rodata_norel sections must
- end up in .data section }
- if (atype in [sec_rodata,sec_rodata_norel]) and
- (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
- secname:='.data';
- { Windows correctly handles reallocations in readonly sections }
- if (atype=sec_rodata) and
- (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
- secname:='.rodata';
- { section type user gives the user full controll on the section name }
- if atype=sec_user then
- secname:=aname;
- if is_smart_section(atype) and (aname<>'') then
- begin
- case aorder of
- secorder_begin :
- sep:='.b_';
- secorder_end :
- sep:='.z_';
- else
- sep:='.n_';
- end;
- result:=secname+sep+aname
- end
- else
- result:=secname;
- end;
- constructor TWasmObjData.create(const n: string);
- begin
- inherited;
- CObjSection:=TWasmObjSection;
- CObjSymbol:=TWasmObjSymbol;
- FObjSymbolsExtraDataList:=TFPHashObjectList.Create;
- FFuncTypes:=TWasmFuncTypeTable.Create;
- end;
- destructor TWasmObjData.destroy;
- var
- i: Integer;
- begin
- FObjSymbolsExtraDataList.Free;
- FFuncTypes.Free;
- inherited destroy;
- end;
- function TWasmObjData.sectionname(atype: TAsmSectiontype;
- const aname: string; aorder: TAsmSectionOrder): string;
- begin
- if (atype=sec_fpc) or
- ((atype=sec_threadvar) and not (ts_wasm_threads in current_settings.targetswitches)) then
- atype:=sec_data;
- Result:=sectionname_gas(atype, aname, aorder);
- end;
- procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
- p: TObjSymbol; Reloctype: TObjRelocationType);
- const
- leb_zero: array[0..4] of byte=($80,$80,$80,$80,$00);
- var
- objreloc: TWasmObjRelocation;
- begin
- if CurrObjSec=nil then
- internalerror(200403072);
- { workaround crash, when generating debug info for threadvars, when multithreading is turned off.
- todo: ensure the debug info for threadvars is actually correct, once we've got WebAssembly debug info working in general }
- if (Reloctype=RELOC_DTPOFF) and not (ts_wasm_threads in current_settings.targetswitches) then
- Reloctype:=RELOC_ABSOLUTE;
- objreloc:=nil;
- case Reloctype of
- RELOC_FUNCTION_INDEX_LEB:
- begin
- if Data<>0 then
- internalerror(2021092502);
- if len<>5 then
- internalerror(2021092503);
- if not assigned(p) then
- internalerror(2021092504);
- objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
- CurrObjSec.ObjRelocations.Add(objreloc);
- writebytes(leb_zero,5);
- end;
- RELOC_MEMORY_ADDR_LEB,
- RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
- begin
- if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then
- internalerror(2021092602);
- if len<>5 then
- internalerror(2021092503);
- if not assigned(p) then
- internalerror(2021092504);
- objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
- objreloc.Addend:=Data;
- CurrObjSec.ObjRelocations.Add(objreloc);
- if RelocType=RELOC_MEMORY_ADDR_LEB then
- WriteUleb5(CurrObjSec,Data)
- else
- WriteSleb5(CurrObjSec,Data);
- end;
- RELOC_ABSOLUTE:
- begin
- if len<>4 then
- internalerror(2021092607);
- if not assigned(p) then
- internalerror(2021092608);
- if (p.objsection<>nil) and TWasmObjSection(p.objsection).IsDebug and
- (p.bind<>AB_COMMON) and (p.bind<>AB_EXTERNAL) then
- begin
- objreloc:=TWasmObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,RELOC_ABSOLUTE);
- objreloc.Addend:=Data+p.Address;
- CurrObjSec.ObjRelocations.Add(objreloc);
- {inc(data,p.address);}
- data:=0;
- Data:=NtoLE(Data);
- writebytes(Data,4);
- end
- else
- begin
- objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
- objreloc.Addend:=Data;
- CurrObjSec.ObjRelocations.Add(objreloc);
- Data:=NtoLE(Data);
- writebytes(Data,4);
- end;
- end;
- RELOC_TYPE_INDEX_LEB:
- begin
- if len<>5 then
- internalerror(2021092612);
- if assigned(p) then
- internalerror(2021092613);
- objreloc:=TWasmObjRelocation.CreateTypeIndex(CurrObjSec.Size,Data);
- CurrObjSec.ObjRelocations.Add(objreloc);
- WriteUleb5(CurrObjSec,Data);
- end;
- RELOC_GLOBAL_INDEX_LEB:
- begin
- if len<>5 then
- internalerror(2021092701);
- if Data<>0 then
- internalerror(2021092702);
- if not assigned(p) then
- internalerror(2021092703);
- objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
- CurrObjSec.ObjRelocations.Add(objreloc);
- WriteUleb5(CurrObjSec,0);
- end;
- RELOC_TAG_INDEX_LEB:
- begin
- if len<>5 then
- internalerror(2021092712);
- if Data<>0 then
- internalerror(2021092713);
- if not assigned(p) then
- internalerror(2021092714);
- objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
- CurrObjSec.ObjRelocations.Add(objreloc);
- WriteSleb5(CurrObjSec,0);
- end;
- else
- internalerror(2021092501);
- end;
- end;
- function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
- begin
- result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
- if not assigned(result) then
- result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
- end;
- function TWasmObjData.globalref(asmsym: TAsmSymbol): TObjSymbol;
- begin
- if assigned(asmsym) then
- begin
- if (asmsym.typ<>AT_WASM_GLOBAL) and (asmsym.typ<>AT_TLS) then
- internalerror(2021092706);
- result:=symbolref(asmsym);
- result.typ:=asmsym.typ;
- end
- else
- result:=nil;
- end;
- function TWasmObjData.ExceptionTagRef(asmsym: TAsmSymbol): TObjSymbol;
- begin
- if assigned(asmsym) then
- begin
- if asmsym.typ<>AT_WASM_EXCEPTION_TAG then
- internalerror(2021092707);
- result:=symbolref(asmsym);
- result.typ:=AT_WASM_EXCEPTION_TAG;
- end
- else
- result:=nil;
- end;
- procedure TWasmObjData.DeclareGlobalType(gt: tai_globaltype);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- ObjSym: TObjSymbol;
- begin
- if not gt.is_external then
- begin
- ObjSym:=symboldefine(gt.sym);
- ObjSym.typ:=AT_WASM_GLOBAL;
- end;
- ObjSymExtraData:=AddOrCreateObjSymbolExtraData(gt.globalname);
- ObjSymExtraData.GlobalType:=gt.gtype;
- ObjSymExtraData.GlobalIsImmutable:=gt.immutable;
- end;
- procedure TWasmObjData.DeclareFuncType_Pass0(ft: tai_functype);
- var
- i: Integer;
- ObjSymExtraData: TWasmObjSymbolExtraData;
- begin
- FLastFuncName:=ft.funcname;
- i:=FFuncTypes.AddOrGetFuncType(ft.functype);
- ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
- ObjSymExtraData.TypeIdx:=i;
- end;
- procedure TWasmObjData.DeclareFuncType_Pass1(ft: tai_functype);
- begin
- FLastFuncName:=ft.funcname;
- end;
- procedure TWasmObjData.DeclareFuncType_Pass2(ft: tai_functype);
- begin
- FLastFuncName:=ft.funcname;
- end;
- procedure TWasmObjData.DeclareTagType(tt: tai_tagtype);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- ft: TWasmFuncType;
- i: Integer;
- begin
- ObjSymExtraData:=AddOrCreateObjSymbolExtraData(tt.tagname);
- ft:=TWasmFuncType.Create([],tt.params);
- i:=FFuncTypes.AddOrGetFuncType(ft);
- ft.free;
- ObjSymExtraData.ExceptionTagTypeIdx:=i;
- end;
- procedure TWasmObjData.DeclareExportName(en: tai_export_name);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- begin
- ObjSymExtraData:=AddOrCreateObjSymbolExtraData(en.intname);
- ObjSymExtraData.ExportName:=en.extname;
- end;
- procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- begin
- ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
- ObjSymExtraData.ImportModule:=aim.importmodule;
- end;
- procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- begin
- ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
- ObjSymExtraData.ImportName:=ain.importname;
- end;
- procedure TWasmObjData.DeclareLocals_Pass0(al: tai_local);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- begin
- ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
- ObjSymExtraData.AddLocals(al.locals);
- alloc(ObjSymExtraData.EncodedLocals.size);
- end;
- procedure TWasmObjData.DeclareLocals_Pass1(al: tai_local);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- begin
- ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
- alloc(ObjSymExtraData.EncodedLocals.size);
- end;
- procedure TWasmObjData.WriteLocals_Pass2(al: tai_local);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- d: tdynamicarray;
- buf: array [0..4095] of byte;
- bs,size: Integer;
- begin
- ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
- d:=ObjSymExtraData.EncodedLocals;
- d.seek(0);
- size:=d.size;
- while size>0 do
- begin
- if size<SizeOf(buf) then
- bs:=Integer(size)
- else
- bs:=SizeOf(buf);
- d.read(buf,bs);
- writebytes(buf,bs);
- dec(size,bs);
- end;
- end;
- procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
- var
- valsym: TObjSymbol;
- aliassym: TWasmObjSymbol;
- begin
- valsym:=CreateSymbol(avalue);
- aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
- aliassym.AliasOf:=valsym.Name;
- end;
- {****************************************************************************
- TWasmObjOutput
- ****************************************************************************}
- procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
- var
- b: byte;
- begin
- b:=ord(wsid);
- Writer.write(b,1);
- WriteUleb(Writer,FWasmSections[wsid].size);
- Writer.writearray(FWasmSections[wsid]);
- end;
- procedure TWasmObjOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
- var
- b: byte;
- begin
- b:=0;
- Writer.write(b,1);
- WriteUleb(Writer,FWasmCustomSections[wcst].size);
- Writer.writearray(FWasmCustomSections[wcst]);
- end;
- function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
- var
- ExtraData: TWasmObjSymbolExtraData;
- begin
- if sym.bind=AB_EXTERNAL then
- begin
- ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
- result:=(ExtraData<>nil) and (ExtraData.TypeIdx<>-1);
- end
- else
- result:=false;
- end;
- function TWasmObjOutput.IsExportedFunction(sym: TWasmObjSymbol): Boolean;
- var
- ExtraData: TWasmObjSymbolExtraData;
- begin
- if (sym.typ=AT_FUNCTION) and not sym.IsAlias then
- begin
- ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
- result:=(ExtraData<>nil) and (ExtraData.ExportName<>'');
- end
- else
- result:=false;
- end;
- procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
- var
- ObjSymExtraData: TWasmObjSymbolExtraData;
- ObjSection: TWasmObjSection;
- codelen: QWord;
- begin
- ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
- ObjSection:=TWasmObjSection(objsym.objsection);
- ObjSection.Data.seek(objsym.address);
- codelen:=objsym.size;
- WriteUleb(dest,codelen);
- ObjSection.FileSectionOfs:=dest.size-objsym.offset;
- CopyDynamicArray(ObjSection.Data,dest,codelen);
- end;
- procedure TWasmObjOutput.WriteSymbolTable;
- begin
- WriteUleb(FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTableEntriesCount);
- FWasmSymbolTable.seek(0);
- CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
- end;
- procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
- begin
- WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
- WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
- FWasmRelocationCodeTable.seek(0);
- CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
- end;
- procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
- begin
- WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
- WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
- FWasmRelocationDataTable.seek(0);
- CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
- end;
- procedure TWasmObjOutput.MaybeWriteRelocationDebugTable(cst: TWasmCustomSectionType; SectionIndex: Integer; EntriesCount: Integer; Table: tdynamicarray);
- begin
- if EntriesCount>0 then
- begin
- WriteUleb(FWasmCustomSections[cst],SectionIndex);
- WriteUleb(FWasmCustomSections[cst],EntriesCount);
- Table.seek(0);
- CopyDynamicArray(Table,FWasmCustomSections[cst],Table.size);
- WriteWasmCustomSection(cst);
- end;
- end;
- procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
- begin
- if FWasmLinkingSubsections[wlst].size>0 then
- begin
- WriteByte(FWasmCustomSections[wcstLinking],Ord(wlst));
- WriteUleb(FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
- FWasmLinkingSubsections[wlst].seek(0);
- CopyDynamicArray(FWasmLinkingSubsections[wlst],FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
- end;
- end;
- procedure TWasmObjOutput.DoRelocations;
- var
- si, ri: Integer;
- objsec: TWasmObjSection;
- objrel: TWasmObjRelocation;
- begin
- for si:=0 to FData.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(FData.ObjSectionList[si]);
- for ri:=0 to objsec.ObjRelocations.Count-1 do
- begin
- objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
- case objrel.typ of
- RELOC_FUNCTION_INDEX_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092509);
- objsec.Data.seek(objrel.DataOffset);
- if TWasmObjSymbol(objrel.symbol).FuncIndex<0 then
- message1(asmw_e_illegal_unset_index,objrel.symbol.name)
- else
- WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).FuncIndex);
- end;
- RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092605);
- if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
- begin
- objsec.Data.seek(objrel.DataOffset);
- AddSleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
- end;
- end;
- RELOC_MEMORY_ADDR_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092606);
- if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
- internalerror(2021092628);
- if objrel.symbol.bind<>AB_EXTERNAL then
- begin
- objsec.Data.seek(objrel.DataOffset);
- AddUleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
- end;
- end;
- RELOC_ABSOLUTE:
- begin
- if assigned(objrel.ObjSection) then
- begin
- { todo: should we do something here? }
- //Writeln('todo: section relocation');
- end
- else if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
- begin
- objsec.Data.seek(objrel.DataOffset);
- AddInt32(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
- end;
- end;
- RELOC_TYPE_INDEX_LEB:
- ;
- RELOC_GLOBAL_INDEX_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092509);
- objsec.Data.seek(objrel.DataOffset);
- if TWasmObjSymbol(objrel.symbol).GlobalIndex<0 then
- message1(asmw_e_illegal_unset_index,objrel.symbol.name)
- else
- WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).GlobalIndex);
- end;
- RELOC_TAG_INDEX_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092716);
- objsec.Data.seek(objrel.DataOffset);
- if TWasmObjSymbol(objrel.symbol).TagIndex<0 then
- message1(asmw_e_illegal_unset_index,objrel.symbol.name)
- else
- WriteSleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).TagIndex);
- end;
- else
- internalerror(2021092510);
- end;
- end;
- end;
- end;
- procedure TWasmObjOutput.WriteRelocations;
- var
- si, ri: Integer;
- objsec: TWasmObjSection;
- objrel: TWasmObjRelocation;
- relout: tdynamicarray;
- relcount: PInteger;
- FuncSym: TWasmObjSymbol;
- begin
- for si:=0 to FData.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(FData.ObjSectionList[si]);
- if objsec.IsCode then
- begin
- relout:=FWasmRelocationCodeTable;
- relcount:=@FWasmRelocationCodeTableEntriesCount;
- end
- else if objsec.IsData then
- begin
- relout:=FWasmRelocationDataTable;
- relcount:=@FWasmRelocationDataTableEntriesCount;
- end
- else if objsec.IsDebug then
- begin
- case objsec.Name of
- '.debug_frame':
- begin
- relout:=FWasmRelocationDebugFrameTable;
- relcount:=@FWasmRelocationDebugFrameTableEntriesCount;
- end;
- '.debug_info':
- begin
- relout:=FWasmRelocationDebugInfoTable;
- relcount:=@FWasmRelocationDebugInfoTableEntriesCount;
- end;
- '.debug_line':
- begin
- relout:=FWasmRelocationDebugLineTable;
- relcount:=@FWasmRelocationDebugLineTableEntriesCount;
- end;
- '.debug_abbrev':
- begin
- relout:=FWasmRelocationDebugAbbrevTable;
- relcount:=@FWasmRelocationDebugAbbrevTableEntriesCount;
- end;
- '.debug_aranges':
- begin
- relout:=FWasmRelocationDebugArangesTable;
- relcount:=@FWasmRelocationDebugArangesTableEntriesCount;
- end;
- '.debug_ranges':
- begin
- relout:=FWasmRelocationDebugRangesTable;
- relcount:=@FWasmRelocationDebugRangesTableEntriesCount;
- end;
- '.debug_str':
- begin
- relout:=FWasmRelocationDebugStrTable;
- relcount:=@FWasmRelocationDebugStrTableEntriesCount;
- end;
- else
- internalerror(2022071601);
- end;
- end
- else
- continue;
- for ri:=0 to objsec.ObjRelocations.Count-1 do
- begin
- objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
- case objrel.typ of
- RELOC_FUNCTION_INDEX_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092508);
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_FUNCTION_INDEX_LEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- end;
- RELOC_MEMORY_ADDR_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092603);
- Inc(relcount^);
- if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
- internalerror(2021092628);
- WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_LEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- WriteSleb(relout,objrel.Addend); { addend to add to the address }
- end;
- RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092604);
- Inc(relcount^);
- if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
- begin
- WriteByte(relout,Ord(R_WASM_TABLE_INDEX_SLEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- end
- else
- begin
- WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- WriteSleb(relout,objrel.Addend); { addend to add to the address }
- end;
- end;
- RELOC_ABSOLUTE:
- begin
- // todo: figure this out, why do these exist?
- //if assigned(objrel.symbol) and not assigned(objrel.symbol.objsection) then
- // Writeln('!!! ', objrel.symbol.name);
- if assigned(objrel.objsection) then
- begin
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_SECTION_OFFSET_I32));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- if (TWasmObjSection(objrel.objsection).SegSymIdx<0) then
- message1(asmw_e_illegal_unset_index,objrel.objsection.name)
- else
- WriteUleb(relout,TWasmObjSection(objrel.objsection).SegSymIdx);
- WriteSleb(relout,objrel.Addend); { addend to add to the address }
- end
- else if (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION)) and not objsec.IsDebug then
- begin
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_TABLE_INDEX_I32));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- end
- else if assigned(objrel.symbol) and assigned(objrel.symbol.objsection) and TWasmObjSection(objrel.symbol.objsection).IsCode then
- begin
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_FUNCTION_OFFSET_I32));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- FuncSym:=FindFunctionSymbol(TWasmObjSymbol(objrel.Symbol));
- if FuncSym.SymbolIndex<0 then
- message1(asmw_e_illegal_unset_index,FuncSym.Name)
- else
- WriteUleb(relout,FuncSym.SymbolIndex);
- WriteSleb(relout,objrel.Addend+objrel.symbol.address) { addend to add to the address }
- end
- else if assigned(objrel.symbol) and (objrel.symbol.typ=AT_WASM_GLOBAL) then
- begin
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_GLOBAL_INDEX_I32));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
- message1(asmw_e_illegal_unset_index,objrel.symbol.name)
- else
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- end
- else
- begin
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_I32));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
- begin
- Writeln(objrel.symbol.objsection.Name, ' ', objrel.symbol.name, ' ', objsec.Name);
- message1(asmw_e_illegal_unset_index,objrel.symbol.name);
- end
- else
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- WriteSleb(relout,objrel.Addend); { addend to add to the address }
- end;
- end;
- RELOC_TYPE_INDEX_LEB:
- begin
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_TYPE_INDEX_LEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- WriteUleb(relout,objrel.TypeIndex);
- end;
- RELOC_GLOBAL_INDEX_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092704);
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_GLOBAL_INDEX_LEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
- message1(asmw_e_illegal_unset_index,objrel.symbol.name)
- else
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- end;
- RELOC_TAG_INDEX_LEB:
- begin
- if not assigned(objrel.symbol) then
- internalerror(2021092717);
- Inc(relcount^);
- WriteByte(relout,Ord(R_WASM_TAG_INDEX_LEB));
- WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
- if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
- message1(asmw_e_illegal_unset_index,objrel.symbol.name)
- else
- WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
- end;
- else
- internalerror(2021092507);
- end;
- end;
- end;
- end;
- function TWasmObjOutput.FindFunctionSymbol(Symbol: TWasmObjSymbol): TWasmObjSymbol;
- begin
- Result:=TWasmObjSection(Symbol.objsection).MainFuncSymbol;
- end;
- function TWasmObjOutput.writeData(Data:TObjData):boolean;
- var
- section_nr: Integer;
- procedure MaybeAddDebugSectionToSymbolTable(st: TWasmCustomDebugSectionType; var debug_section_nr: Integer);
- var
- objsec: TWasmObjSection;
- begin
- objsec:=TWasmObjSection(Data.ObjSectionList.Find(WasmCustomSectionName[st]));
- if Assigned(objsec) then
- begin
- debug_section_nr:=section_nr;
- Inc(section_nr);
- objsec.SegSymIdx:=FWasmSymbolTableEntriesCount;
- objsec.CustomSectionIdx:=debug_section_nr;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
- WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
- WriteUleb(FWasmSymbolTable,debug_section_nr);
- end;
- end;
- procedure MaybeWriteDebugSection(st: TWasmCustomDebugSectionType);
- var
- objsec: TWasmObjSection;
- begin
- objsec:=TWasmObjSection(Data.ObjSectionList.Find(WasmCustomSectionName[st]));
- if Assigned(objsec) then
- begin
- if oso_Data in objsec.SecOptions then
- begin
- objsec.Data.seek(0);
- CopyDynamicArray(objsec.Data,FWasmCustomSections[st],objsec.Size);
- end
- else
- WriteZeros(FWasmCustomSections[st],objsec.Size);
- WriteWasmCustomSection(st);
- end;
- end;
- var
- i: Integer;
- objsec: TWasmObjSection;
- segment_count: Integer = 0;
- cur_seg_ofs: qword = 0;
- imports_count, NextImportFunctionIndex, NextFunctionIndex,
- code_section_nr, data_section_nr,
- debug_abbrev_section_nr,debug_info_section_nr,debug_str_section_nr,
- debug_line_section_nr,debug_frame_section_nr,debug_aranges_section_nr,
- debug_ranges_section_nr,
- NextGlobalIndex, NextTagIndex: Integer;
- import_globals_count: Integer = 0;
- globals_count: Integer = 0;
- import_functions_count: Integer = 0;
- export_functions_count: Integer = 0;
- functions_count: Integer = 0;
- import_exception_tags_count: Integer = 0;
- exception_tags_count: Integer = 0;
- objsym, ObjSymAlias: TWasmObjSymbol;
- cust_sec: TWasmCustomSectionType;
- SegmentFlags, SymbolFlags: UInt64;
- begin
- FData:=TWasmObjData(Data);
- { each custom sections starts with its name }
- for cust_sec in TWasmCustomSectionType do
- WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
- WriteUleb(FWasmCustomSections[wcstLinking],2); { linking metadata version }
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if objsym.typ=AT_WASM_EXCEPTION_TAG then
- if objsym.bind=AB_EXTERNAL then
- Inc(import_exception_tags_count)
- else
- Inc(exception_tags_count);
- if objsym.typ=AT_WASM_GLOBAL then
- if objsym.bind=AB_EXTERNAL then
- Inc(import_globals_count)
- else
- Inc(globals_count);
- if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
- Inc(import_globals_count);
- if IsExternalFunction(objsym) then
- Inc(import_functions_count);
- if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
- begin
- TWasmObjSection(objsym.objsection).MainFuncSymbol:=objsym;
- Inc(functions_count);
- end;
- if IsExportedFunction(objsym) then
- Inc(export_functions_count);
- end;
- FData.FFuncTypes.WriteTo(FWasmSections[wsiType]);
- for i:=0 to Data.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(Data.ObjSectionList[i]);
- if objsec.IsCode then
- objsec.SegIdx:=-1
- else if objsec.IsData then
- begin
- objsec.SegIdx:=segment_count;
- objsec.SegOfs:=cur_seg_ofs;
- Inc(segment_count);
- Inc(cur_seg_ofs,objsec.Size);
- end;
- end;
- imports_count:=2+import_globals_count+import_functions_count+import_exception_tags_count;
- WriteUleb(FWasmSections[wsiImport],imports_count);
- { import memories }
- WriteName(FWasmSections[wsiImport],'env');
- WriteName(FWasmSections[wsiImport],'__linear_memory');
- WriteByte(FWasmSections[wsiImport],$02); { mem }
- WriteByte(FWasmSections[wsiImport],$00); { min }
- WriteUleb(FWasmSections[wsiImport],1); { 1 page }
- { import globals }
- NextGlobalIndex:=0;
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if (objsym.bind=AB_EXTERNAL) and (objsym.typ=AT_WASM_GLOBAL) then
- begin
- objsym.GlobalIndex:=NextGlobalIndex;
- Inc(NextGlobalIndex);
- objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
- if objsym.ExtraData.ImportModule<>'' then
- WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
- else
- WriteName(FWasmSections[wsiImport],'env');
- WriteName(FWasmSections[wsiImport],objsym.Name);
- WriteByte(FWasmSections[wsiImport],$03); { global }
- WriteWasmBasicType(FWasmSections[wsiImport],objsym.ExtraData.GlobalType);
- if objsym.ExtraData.GlobalIsImmutable then
- WriteByte(FWasmSections[wsiImport],$00) { const }
- else
- WriteByte(FWasmSections[wsiImport],$01); { var }
- end
- else if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
- begin
- objsym.GlobalIndex:=NextGlobalIndex;
- Inc(NextGlobalIndex);
- objsym.ExtraData:=nil;
- WriteName(FWasmSections[wsiImport],'GOT.mem');
- WriteName(FWasmSections[wsiImport],objsym.Name);
- WriteByte(FWasmSections[wsiImport],$03); { global }
- WriteWasmBasicType(FWasmSections[wsiImport],wbt_i32); { i32 }
- WriteByte(FWasmSections[wsiImport],$01); { var }
- end;
- end;
- { import functions }
- NextImportFunctionIndex:=0;
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if IsExternalFunction(objsym) then
- begin
- objsym.FuncIndex:=NextImportFunctionIndex;
- Inc(NextImportFunctionIndex);
- objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
- if objsym.ExtraData.ImportModule<>'' then
- WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
- else
- WriteName(FWasmSections[wsiImport],'env');
- WriteName(FWasmSections[wsiImport],objsym.Name);
- WriteByte(FWasmSections[wsiImport],$00); { func }
- WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
- end;
- end;
- { import tables }
- WriteName(FWasmSections[wsiImport],'env');
- WriteName(FWasmSections[wsiImport],'__indirect_function_table');
- WriteByte(FWasmSections[wsiImport],$01); { table }
- WriteByte(FWasmSections[wsiImport],$70); { funcref }
- WriteByte(FWasmSections[wsiImport],$00); { min }
- WriteUleb(FWasmSections[wsiImport],1); { 1 }
- { import tags }
- NextTagIndex:=0;
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind=AB_EXTERNAL) then
- begin
- objsym.TagIndex:=NextTagIndex;
- Inc(NextTagIndex);
- objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
- if objsym.ExtraData.ImportModule<>'' then
- WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
- else
- WriteName(FWasmSections[wsiImport],'env');
- WriteName(FWasmSections[wsiImport],objsym.Name);
- WriteByte(FWasmSections[wsiImport],$04); { tag }
- WriteByte(FWasmSections[wsiImport],$00); { exception }
- WriteUleb(FWasmSections[wsiImport],objsym.ExtraData.ExceptionTagTypeIdx);
- end;
- end;
- WriteUleb(FWasmSections[wsiFunction],functions_count);
- NextFunctionIndex:=NextImportFunctionIndex;
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
- begin
- objsym.FuncIndex:=NextFunctionIndex;
- Inc(NextFunctionIndex);
- WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
- end;
- end;
- if exception_tags_count>0 then
- begin
- WriteUleb(FWasmSections[wsiTag],exception_tags_count);
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind<>AB_EXTERNAL) then
- begin
- objsym.TagIndex:=NextTagIndex;
- Inc(NextTagIndex);
- objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
- WriteByte(FWasmSections[wsiTag],$00); { exception }
- WriteUleb(FWasmSections[wsiTag],objsym.ExtraData.ExceptionTagTypeIdx);
- end;
- end;
- end;
- if globals_count>0 then
- begin
- WriteUleb(FWasmSections[wsiGlobal],globals_count);
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if (objsym.typ=AT_WASM_GLOBAL) and (objsym.bind<>AB_EXTERNAL) then
- begin
- objsym.GlobalIndex:=NextGlobalIndex;
- Inc(NextGlobalIndex);
- objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
- WriteWasmBasicType(FWasmSections[wsiGlobal],objsym.ExtraData.GlobalType);
- if objsym.ExtraData.GlobalIsImmutable then
- WriteByte(FWasmSections[wsiGlobal],$00) { const }
- else
- WriteByte(FWasmSections[wsiGlobal],$01); { var }
- { init expr }
- case objsym.ExtraData.GlobalType of
- wbt_i32:
- begin
- WriteByte(FWasmSections[wsiGlobal],$41); { i32.const }
- WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- end;
- wbt_i64:
- begin
- WriteByte(FWasmSections[wsiGlobal],$42); { i64.const }
- WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- end;
- wbt_f32:
- begin
- WriteByte(FWasmSections[wsiGlobal],$43); { f32.const }
- WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE single precision floating point format) }
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- end;
- wbt_f64:
- begin
- WriteByte(FWasmSections[wsiGlobal],$44); { f64.const }
- WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE double precision floating point format) }
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$00);
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- end;
- wbt_externref:
- begin
- WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null extern }
- WriteByte(FWasmSections[wsiGlobal],$6F);
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- end;
- wbt_funcref:
- begin
- WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null func }
- WriteByte(FWasmSections[wsiGlobal],$70);
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- end;
- else
- internalerror(2022052801);
- end;
- end;
- end;
- end;
- if export_functions_count>0 then
- begin
- WriteUleb(FWasmSections[wsiExport],export_functions_count);
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if IsExportedFunction(objsym) then
- begin
- WriteName(FWasmSections[wsiExport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).ExportName);
- WriteByte(FWasmSections[wsiExport],0); { func }
- if (objsym.FuncIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSections[wsiExport],objsym.FuncIndex);
- end;
- end;
- end;
- Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
- Writer.write(WasmVersion,SizeOf(WasmVersion));
- if ts_wasm_threads in current_settings.targetswitches then
- begin
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],4);
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'atomics');
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
- end
- else
- begin
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],3);
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
- WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
- WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
- end;
- { Write the producers section:
- https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md }
- WriteUleb(FWasmCustomSections[wcstProducers],2);
- WriteName(FWasmCustomSections[wcstProducers],'language');
- WriteUleb(FWasmCustomSections[wcstProducers],1);
- WriteName(FWasmCustomSections[wcstProducers],'Pascal');
- WriteName(FWasmCustomSections[wcstProducers],'');
- WriteName(FWasmCustomSections[wcstProducers],'processed-by');
- WriteUleb(FWasmCustomSections[wcstProducers],1);
- WriteName(FWasmCustomSections[wcstProducers],'Free Pascal Compiler (FPC)');
- WriteName(FWasmCustomSections[wcstProducers],full_version_string+' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname);
- code_section_nr:=-1;
- data_section_nr:=-1;
- debug_abbrev_section_nr:=-1;
- debug_info_section_nr:=-1;
- debug_str_section_nr:=-1;
- debug_line_section_nr:=-1;
- debug_frame_section_nr:=-1;
- debug_aranges_section_nr:=-1;
- debug_ranges_section_nr:=-1;
- section_nr:=0;
- WriteWasmSection(wsiType);
- Inc(section_nr);
- WriteWasmSection(wsiImport);
- Inc(section_nr);
- WriteWasmSection(wsiFunction);
- Inc(section_nr);
- if exception_tags_count>0 then
- begin
- WriteWasmSection(wsiTag);
- Inc(section_nr);
- end;
- if globals_count>0 then
- begin
- WriteWasmSection(wsiGlobal);
- Inc(section_nr);
- end;
- if export_functions_count>0 then
- begin
- WriteWasmSection(wsiExport);
- Inc(section_nr);
- end;
- { determine the section numbers for the datacount, code, data and debug sections ahead of time }
- if segment_count>0 then
- Inc(section_nr); { the DataCount section }
- code_section_nr:=section_nr; { the Code section }
- Inc(section_nr);
- if segment_count>0 then
- begin
- data_section_nr:=section_nr; { the Data section }
- Inc(section_nr);
- end;
- { the debug sections }
- MaybeAddDebugSectionToSymbolTable(wcstDebugAbbrev,debug_abbrev_section_nr);
- MaybeAddDebugSectionToSymbolTable(wcstDebugInfo,debug_info_section_nr);
- MaybeAddDebugSectionToSymbolTable(wcstDebugStr,debug_str_section_nr);
- MaybeAddDebugSectionToSymbolTable(wcstDebugLine,debug_line_section_nr);
- MaybeAddDebugSectionToSymbolTable(wcstDebugFrame,debug_frame_section_nr);
- MaybeAddDebugSectionToSymbolTable(wcstDebugAranges,debug_aranges_section_nr);
- MaybeAddDebugSectionToSymbolTable(wcstDebugRanges,debug_ranges_section_nr);
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if objsym.typ=AT_WASM_EXCEPTION_TAG then
- begin
- objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_EVENT));
- if objsym.bind=AB_GLOBAL then
- WriteUleb(FWasmSymbolTable,0)
- else if objsym.bind=AB_LOCAL then
- WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
- else if objsym.bind=AB_EXTERNAL then
- WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
- else if objsym.bind=AB_WEAK then
- WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_WEAK)
- else
- internalerror(2021092715);
- if (objsym.TagIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSymbolTable,objsym.TagIndex);
- if objsym.bind<>AB_EXTERNAL then
- WriteName(FWasmSymbolTable,objsym.Name);
- end
- else if objsym.typ=AT_WASM_GLOBAL then
- begin
- objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_GLOBAL));
- if objsym.bind=AB_EXTERNAL then
- begin
- WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
- if (objsym.GlobalIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
- end
- else
- begin
- WriteUleb(FWasmSymbolTable,0);
- if (objsym.GlobalIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
- WriteName(FWasmSymbolTable,objsym.Name);
- end;
- end
- else if IsExternalFunction(objsym) then
- begin
- objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
- if objsym.ExtraData.ImportModule<>'' then
- begin
- WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
- if (objsym.FuncIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
- WriteName(FWasmSymbolTable,objsym.Name);
- end
- else
- begin
- WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
- if (objsym.FuncIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
- end;
- end
- else if objsym.typ=AT_FUNCTION then
- begin
- objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
- if objsym.IsAlias then
- begin
- ObjSymAlias:=TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf));
- ObjSym.FuncIndex:=ObjSymAlias.FuncIndex;
- WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
- WriteUleb(FWasmSymbolTable,ObjSymAlias.FuncIndex);
- end
- else
- begin
- if IsExportedFunction(objsym) then
- WriteUleb(FWasmSymbolTable,WASM_SYM_EXPORTED)
- else
- WriteUleb(FWasmSymbolTable,0);
- if (objsym.FuncIndex<0) then
- message1(asmw_e_illegal_unset_index,objsym.name)
- else
- WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
- end;
- WriteName(FWasmSymbolTable,objsym.Name);
- end
- else if (objsym.typ in [AT_DATA,AT_TLS,AT_METADATA]) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
- begin
- if (objsym.bind<>AB_EXTERNAL) and TWasmObjSection(objsym.objsection).IsDebug then
- begin
- objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_FPC_CUSTOM));
- if objsym.bind=AB_GLOBAL then
- SymbolFlags:=0
- else if objsym.bind=AB_LOCAL then
- SymbolFlags:=WASM_SYM_BINDING_LOCAL
- else if objsym.bind=AB_EXTERNAL then
- SymbolFlags:=WASM_SYM_UNDEFINED
- else
- internalerror(2024090701);
- WriteUleb(FWasmSymbolTable,SymbolFlags);
- WriteName(FWasmSymbolTable,objsym.Name);
- WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).CustomSectionIdx);
- WriteUleb(FWasmSymbolTable,objsym.offset);
- WriteUleb(FWasmSymbolTable,objsym.size);
- end
- else
- begin
- objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
- Inc(FWasmSymbolTableEntriesCount);
- WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
- if objsym.bind=AB_GLOBAL then
- SymbolFlags:=0
- else if objsym.bind=AB_LOCAL then
- SymbolFlags:=WASM_SYM_BINDING_LOCAL
- else if objsym.bind=AB_EXTERNAL then
- SymbolFlags:=WASM_SYM_UNDEFINED
- else
- internalerror(2021092506);
- if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
- SymbolFlags:=(SymbolFlags and not WASM_SYM_BINDING_LOCAL) or WASM_SYM_TLS;
- WriteUleb(FWasmSymbolTable,SymbolFlags);
- WriteName(FWasmSymbolTable,objsym.Name);
- if objsym.bind<>AB_EXTERNAL then
- begin
- WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
- WriteUleb(FWasmSymbolTable,objsym.offset);
- WriteUleb(FWasmSymbolTable,objsym.size);
- end;
- end;
- end;
- end;
- DoRelocations;
- if segment_count>0 then
- begin
- WriteUleb(FWasmSections[wsiData],segment_count);
- WriteUleb(FWasmSections[wsiDataCount],segment_count);
- WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],segment_count);
- for i:=0 to Data.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(Data.ObjSectionList[i]);
- if objsec.IsData then
- begin
- WriteName(FWasmLinkingSubsections[WASM_SEGMENT_INFO],objsec.Name);
- WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],BsrQWord(objsec.SecAlign));
- SegmentFlags:=0;
- if (ts_wasm_threads in current_settings.targetswitches) and
- (oso_threadvar in objsec.SecOptions) then
- SegmentFlags:=SegmentFlags or WASM_SEG_FLAG_TLS;
- WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],SegmentFlags); { flags }
- WriteByte(FWasmSections[wsiData],0);
- WriteByte(FWasmSections[wsiData],$41);
- WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
- WriteByte(FWasmSections[wsiData],$0b);
- WriteUleb(FWasmSections[wsiData],objsec.Size);
- objsec.FileSectionOfs:=FWasmSections[wsiData].size;
- if oso_Data in objsec.SecOptions then
- begin
- objsec.Data.seek(0);
- CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
- end
- else
- begin
- WriteZeros(FWasmSections[wsiData],objsec.Size);
- end;
- end;
- end;
- end;
- WriteUleb(FWasmSections[wsiCode],functions_count);
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
- if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
- WriteFunctionCode(FWasmSections[wsiCode],objsym);
- end;
- if segment_count>0 then
- WriteWasmSection(wsiDataCount);
- WriteWasmSection(wsiCode);
- if segment_count>0 then
- WriteWasmSection(wsiData);
- MaybeWriteDebugSection(wcstDebugAbbrev);
- MaybeWriteDebugSection(wcstDebugInfo);
- MaybeWriteDebugSection(wcstDebugStr);
- MaybeWriteDebugSection(wcstDebugLine);
- MaybeWriteDebugSection(wcstDebugFrame);
- MaybeWriteDebugSection(wcstDebugAranges);
- MaybeWriteDebugSection(wcstDebugRanges);
- WriteRelocations;
- WriteSymbolTable;
- WriteLinkingSubsection(WASM_SYMBOL_TABLE);
- if segment_count>0 then
- WriteLinkingSubsection(WASM_SEGMENT_INFO);
- WriteRelocationCodeTable(code_section_nr);
- if segment_count>0 then
- WriteRelocationDataTable(data_section_nr);
- WriteWasmCustomSection(wcstLinking);
- Inc(section_nr);
- WriteWasmCustomSection(wcstRelocCode);
- Inc(section_nr);
- if segment_count>0 then
- begin
- WriteWasmCustomSection(wcstRelocData);
- Inc(section_nr);
- end;
- MaybeWriteRelocationDebugTable(wcstRelocDebugAbbrev,debug_abbrev_section_nr,FWasmRelocationDebugAbbrevTableEntriesCount,FWasmRelocationDebugAbbrevTable);
- MaybeWriteRelocationDebugTable(wcstRelocDebugInfo,debug_info_section_nr,FWasmRelocationDebugInfoTableEntriesCount,FWasmRelocationDebugInfoTable);
- MaybeWriteRelocationDebugTable(wcstRelocDebugStr,debug_str_section_nr,FWasmRelocationDebugStrTableEntriesCount,FWasmRelocationDebugStrTable);
- MaybeWriteRelocationDebugTable(wcstRelocDebugLine,debug_line_section_nr,FWasmRelocationDebugLineTableEntriesCount,FWasmRelocationDebugLineTable);
- MaybeWriteRelocationDebugTable(wcstRelocDebugFrame,debug_frame_section_nr,FWasmRelocationDebugFrameTableEntriesCount,FWasmRelocationDebugFrameTable);
- MaybeWriteRelocationDebugTable(wcstRelocDebugAranges,debug_aranges_section_nr,FWasmRelocationDebugArangesTableEntriesCount,FWasmRelocationDebugArangesTable);
- MaybeWriteRelocationDebugTable(wcstRelocDebugRanges,debug_ranges_section_nr,FWasmRelocationDebugRangesTableEntriesCount,FWasmRelocationDebugRangesTable);
- WriteWasmCustomSection(wcstProducers);
- Inc(section_nr);
- WriteWasmCustomSection(wcstTargetFeatures);
- Inc(section_nr);
- result:=true;
- end;
- constructor TWasmObjOutput.create(AWriter: TObjectWriter);
- var
- i: TWasmSectionID;
- j: TWasmCustomSectionType;
- k: TWasmLinkingSubsectionType;
- begin
- inherited;
- cobjdata:=TWasmObjData;
- for i in TWasmSectionID do
- FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
- for j in TWasmCustomSectionType do
- FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
- for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
- FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
- FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmSymbolTableEntriesCount:=0;
- FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationCodeTableEntriesCount:=0;
- FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDataTableEntriesCount:=0;
- FWasmRelocationDebugFrameTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugFrameTableEntriesCount:=0;
- FWasmRelocationDebugInfoTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugInfoTableEntriesCount:=0;
- FWasmRelocationDebugLineTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugLineTableEntriesCount:=0;
- FWasmRelocationDebugAbbrevTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugAbbrevTableEntriesCount:=0;
- FWasmRelocationDebugArangesTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugArangesTableEntriesCount:=0;
- FWasmRelocationDebugRangesTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugRangesTableEntriesCount:=0;
- FWasmRelocationDebugStrTable:=tdynamicarray.create(SectionDataMaxGrow);
- FWasmRelocationDebugStrTableEntriesCount:=0;
- end;
- destructor TWasmObjOutput.destroy;
- var
- i: TWasmSectionID;
- j: TWasmCustomSectionType;
- k: TWasmLinkingSubsectionType;
- begin
- for i in TWasmSectionID do
- FWasmSections[i].Free;
- for j in TWasmCustomSectionType do
- FWasmCustomSections[j].Free;
- for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
- FWasmLinkingSubsections[k].Free;
- FWasmSymbolTable.Free;
- FWasmRelocationCodeTable.Free;
- FWasmRelocationDataTable.Free;
- FWasmRelocationDebugFrameTable.Free;
- FWasmRelocationDebugInfoTable.Free;
- FWasmRelocationDebugLineTable.Free;
- FWasmRelocationDebugAbbrevTable.Free;
- FWasmRelocationDebugArangesTable.Free;
- FWasmRelocationDebugRangesTable.Free;
- FWasmRelocationDebugStrTable.Free;
- inherited destroy;
- end;
- {****************************************************************************
- TWasmObjInput
- ****************************************************************************}
- constructor TWasmObjInput.create;
- begin
- inherited create;
- cobjdata:=TWasmObjData;
- end;
- destructor TWasmObjInput.Destroy;
- var
- i: Integer;
- begin
- for i:=low(FFuncTypes) to high(FFuncTypes) do
- begin
- FFuncTypes[i].free;
- FFuncTypes[i]:=nil;
- end;
- inherited Destroy;
- end;
- class function TWasmObjInput.CanReadObjData(AReader: TObjectreader): boolean;
- var
- ModuleMagic: array [0..3] of Byte;
- ModuleVersion: array [0..3] of Byte;
- i: Integer;
- begin
- result:=false;
- if not AReader.read(ModuleMagic,4) then
- exit;
- for i:=0 to 3 do
- if ModuleMagic[i]<>WasmModuleMagic[i] then
- exit;
- if not AReader.read(ModuleVersion,4) then
- exit;
- for i:=0 to 3 do
- if ModuleVersion[i]<>WasmVersion[i] then
- exit;
- result:=true;
- end;
- function TWasmObjInput.ReadObjData(AReader: TObjectreader; out ObjData: TObjData): boolean;
- type
- TLimits = record
- Min, Max: uint32;
- HasMax: Boolean;
- end;
- var
- SectionIndex: Integer = -1;
- SectionId: Byte;
- SectionSize: uint32;
- SectionStart: LongInt;
- CheckSectionBounds: Boolean;
- TypeSectionRead: Boolean = false;
- ImportSectionRead: Boolean = false;
- FunctionSectionRead: Boolean = false;
- GlobalSectionRead: Boolean = false;
- ExportSectionRead: Boolean = false;
- ElementSectionRead: Boolean = false;
- TagSectionRead: Boolean = false;
- CodeSectionRead: Boolean = false;
- DataSectionRead: Boolean = false;
- DataCountSectionRead: Boolean = false;
- SegmentInfoSectionRead: Boolean = false;
- SymbolTableSectionRead: Boolean = false;
- CodeSectionIndex: Integer = -1;
- DataSectionIndex: Integer = -1;
- DebugSectionIndex: array [TWasmCustomDebugSectionType] of Integer = (-1,-1,-1,-1,-1,-1,-1);
- FuncTypes: array of record
- IsImport: Boolean;
- ImportName: ansistring;
- ImportModName: ansistring;
- typidx: uint32;
- IsExported: Boolean;
- ExportName: ansistring;
- end;
- FuncTypeImportsCount: uint32;
- TableTypes: array of record
- IsImport: Boolean;
- ImportName: ansistring;
- ImportModName: ansistring;
- reftype: TWasmBAsicType;
- limits: TLimits;
- IsExported: Boolean;
- ExportName: ansistring;
- end;
- TableTypeImportsCount: uint32;
- MemTypes: array of record
- IsImport: Boolean;
- ImportName: ansistring;
- ImportModName: ansistring;
- limits: TLimits;
- IsExported: Boolean;
- ExportName: ansistring;
- end;
- MemTypeImportsCount: uint32;
- GlobalTypes: array of record
- IsImport: Boolean;
- ImportName: ansistring;
- ImportModName: ansistring;
- valtype: TWasmBasicType;
- IsMutable: Boolean;
- IsExported: Boolean;
- ExportName: ansistring;
- GlobalInit: TGlobalInitializer;
- end;
- GlobalTypeImportsCount: uint32;
- TagTypes: array of record
- IsImport: Boolean;
- ImportName: ansistring;
- ImportModName: ansistring;
- TagAttr: Byte;
- TagTypeIdx: uint32;
- IsExported: Boolean;
- ExportName: ansistring;
- end;
- TagTypeImportsCount: uint32;
- CodeSegments: array of record
- CodeSectionOffset: uint32;
- CodeSize: uint32;
- DataPos: LongInt;
- SegName: ansistring;
- SegIsExported: Boolean;
- end;
- DataSegments: array of record
- DataSectionOffset: uint32;
- Active: Boolean;
- MemIdx: uint32;
- Len: uint32;
- Offset: int32;
- DataPos: LongInt;
- SegName: ansistring;
- SegAlignment: uint32;
- SegFlags: uint32;
- end;
- SymbolTable: array of record
- SymFlags: uint32;
- TargetSection: uint32;
- SymIndex: uint32;
- SymOffset: uint32;
- SymSize: uint32;
- SymCustomSectionIndex: uint32;
- SymCustomSectionType: TWasmCustomDebugSectionType;
- SymKind: TWasmSymbolType;
- SymName: ansistring;
- ObjSym: TWasmObjSymbol;
- ObjSec: TWasmObjSection;
- end;
- { meaning of first index: }
- { table 0 is code relocs }
- { table 1 is data relocs }
- { tables 2.. are custom section relocs for debug sections }
- RelocationTable: array of array of record
- RelocType: TWasmRelocationType;
- RelocOffset: uint32;
- RelocIndex: uint32;
- RelocAddend: int32;
- end;
- function FindDebugSectionByIndex(SectionIndex: Integer; out res: TWasmCustomDebugSectionType): Boolean;
- var
- ds: TWasmCustomDebugSectionType;
- begin
- for ds in TWasmCustomDebugSectionType do
- if DebugSectionIndex[ds]=SectionIndex then
- begin
- Res:=ds;
- Result:=True;
- exit;
- end;
- Res:=low(TWasmCustomDebugSectionType);
- Result:=False;
- end;
- function ReadSection: Boolean;
- function read(out b;len:longint):boolean;
- begin
- result:=false;
- if not CheckSectionBounds or ((AReader.Pos+len)<=(SectionStart+SectionSize)) then
- result:=AReader.read(b,len)
- else
- begin
- { trying to read beyond the end of the section }
- AReader.read(b,SectionStart+SectionSize-AReader.Pos);
- result:=false;
- end;
- end;
- function ReadUleb(out v: uint64): boolean;
- var
- b: byte;
- shift:integer;
- begin
- result:=false;
- b:=0;
- v:=0;
- shift:=0;
- repeat
- if not read(b,1) then
- exit;
- v:=v or (uint64(b and 127) shl shift);
- inc(shift,7);
- until (b and 128)=0;
- result:=true;
- end;
- function ReadUleb32(out v: uint32): boolean;
- var
- vv: uint64;
- begin
- result:=false;
- v:=default(uint32);
- if not ReadUleb(vv) then
- exit;
- if vv>high(uint32) then
- exit;
- v:=vv;
- result:=true;
- end;
- function ReadSleb(out v: int64): boolean;
- var
- b: byte;
- shift:integer;
- begin
- result:=false;
- b:=0;
- v:=0;
- shift:=0;
- repeat
- if not read(b,1) then
- exit;
- v:=v or (uint64(b and 127) shl shift);
- inc(shift,7);
- until (b and 128)=0;
- {$ifopt Q+}
- {$define overflowon}
- {$Q-}
- {$endif}
- {$ifopt R+}
- {$define rangeon}
- {$R-}
- {$endif}
- if (b and 64)<>0 then
- v:=v or (high(uint64) shl shift);
- result:=true;
- end;
- {$ifdef overflowon}
- {$Q+}
- {$undef overflowon}
- {$endif}
- {$ifdef rangeon}
- {$R+}
- {$undef rangeon}
- {$endif}
- function ReadSleb32(out v: int32): boolean;
- var
- vv: int64;
- begin
- result:=false;
- v:=default(int32);
- if not ReadSleb(vv) then
- exit;
- if (vv>high(int32)) or (vv<low(int32)) then
- exit;
- v:=vv;
- result:=true;
- end;
- function ReadName(out v: ansistring): boolean;
- var
- len: uint32;
- begin
- result:=false;
- if not ReadUleb32(len) then
- exit;
- SetLength(v,len);
- if len>0 then
- result:=read(v[1],len)
- else
- result:=true;
- end;
- function ReadCustomSection: Boolean;
- function ReadRelocationSection: Boolean;
- var
- TargetSection, RelocCount: uint32;
- i: Integer;
- RelocTableIndex: Integer;
- ds: TWasmCustomDebugSectionType;
- begin
- Result:=False;
- if not ReadUleb32(TargetSection) then
- begin
- InputError('Error reading the index of the target section of a relocation section');
- exit;
- end;
- if TargetSection=CodeSectionIndex then
- RelocTableIndex:=0
- else if TargetSection=DataSectionIndex then
- RelocTableIndex:=1
- else
- begin
- RelocTableIndex:=-1;
- for ds:=Low(DebugSectionIndex) to High(DebugSectionIndex) do
- if DebugSectionIndex[ds]=TargetSection then
- begin
- RelocTableIndex:=2+(Ord(ds)-Ord(Low(TWasmCustomDebugSectionType)));
- break;
- end;
- if RelocTableIndex=-1 then
- begin
- InputError('Relocation found for a custom section, that is not supported');
- exit;
- end;
- end;
- if not ReadUleb32(RelocCount) then
- begin
- InputError('Error reading the relocation entries count from a relocation section');
- exit;
- end;
- SetLength(RelocationTable[RelocTableIndex],RelocCount);
- for i:=0 to RelocCount-1 do
- with RelocationTable[RelocTableIndex,i] do
- begin
- if not Read(RelocType,1) then
- begin
- InputError('Error reading the relocation type of a relocation entry');
- exit;
- end;
- if not (RelocType in [R_WASM_FUNCTION_INDEX_LEB,
- R_WASM_MEMORY_ADDR_LEB,
- R_WASM_TABLE_INDEX_SLEB,
- R_WASM_MEMORY_ADDR_SLEB,
- R_WASM_SECTION_OFFSET_I32,
- R_WASM_TABLE_INDEX_I32,
- R_WASM_FUNCTION_OFFSET_I32,
- R_WASM_MEMORY_ADDR_I32,
- R_WASM_TYPE_INDEX_LEB,
- R_WASM_GLOBAL_INDEX_LEB,
- R_WASM_TAG_INDEX_LEB,
- R_WASM_GLOBAL_INDEX_I32]) then
- begin
- InputError('Unsupported relocation type: ' + tostr(Ord(RelocType)));
- exit;
- end;
- if not ReadUleb32(RelocOffset) then
- begin
- InputError('Error reading the relocation offset of a relocation entry');
- exit;
- end;
- if not ReadUleb32(RelocIndex) then
- begin
- InputError('Error reading the relocation index of a relocation entry');
- exit;
- end;
- if RelocType in [R_WASM_FUNCTION_OFFSET_I32,R_WASM_SECTION_OFFSET_I32,R_WASM_MEMORY_ADDR_LEB,R_WASM_MEMORY_ADDR_SLEB,R_WASM_MEMORY_ADDR_I32] then
- begin
- if not ReadSleb32(RelocAddend) then
- begin
- InputError('Error reading the relocation addend of a relocation entry');
- exit;
- end;
- end;
- if (RelocType in [
- R_WASM_SECTION_OFFSET_I32,
- R_WASM_FUNCTION_INDEX_LEB,
- R_WASM_TABLE_INDEX_SLEB,
- R_WASM_TABLE_INDEX_I32,
- R_WASM_MEMORY_ADDR_LEB,
- R_WASM_MEMORY_ADDR_SLEB,
- R_WASM_MEMORY_ADDR_I32,
- R_WASM_FUNCTION_OFFSET_I32,
- R_WASM_GLOBAL_INDEX_LEB,
- R_WASM_GLOBAL_INDEX_I32]) and (RelocIndex>High(SymbolTable)) then
- begin
- InputError('Relocation index outside the bounds of the symbol table');
- exit;
- end;
- if (RelocType=R_WASM_TYPE_INDEX_LEB) and (RelocIndex>High(FFuncTypes)) then
- begin
- InputError('Relocation index of R_WASM_TYPE_INDEX_LEB outside the bounds of the func types, defined in the func section of the module');
- exit;
- end;
- if (RelocType=R_WASM_SECTION_OFFSET_I32) and (SymbolTable[RelocIndex].SymKind<>SYMTAB_SECTION) then
- begin
- InputError('R_WASM_SECTION_OFFSET_I32 must point to a SYMTAB_SECTION symbol');
- exit;
- end;
- if (RelocType in [R_WASM_GLOBAL_INDEX_LEB,R_WASM_GLOBAL_INDEX_I32]) and
- not ((SymbolTable[RelocIndex].SymKind=SYMTAB_GLOBAL) or
- ((ts_wasm_threads in current_settings.targetswitches) and
- (SymbolTable[RelocIndex].SymKind=SYMTAB_DATA) and
- ((SymbolTable[RelocIndex].SymFlags and WASM_SYM_TLS)<>0))) then
- begin
- if ts_wasm_threads in current_settings.targetswitches then
- InputError('Relocation must point to a SYMTAB_GLOBAL symbol or a SYMTAB_DATA symbol with the WASM_SYM_TLS flag set')
- else
- InputError('Relocation must point to a SYMTAB_GLOBAL symbol');
- exit;
- end;
- if (RelocType=R_WASM_TAG_INDEX_LEB) and (SymbolTable[RelocIndex].SymKind<>SYMTAB_EVENT) then
- begin
- InputError('Relocation must point to a SYMTAB_EVENT symbol');
- exit;
- end;
- if (RelocType in [
- R_WASM_FUNCTION_INDEX_LEB,
- R_WASM_TABLE_INDEX_SLEB,
- R_WASM_TABLE_INDEX_I32,
- R_WASM_FUNCTION_OFFSET_I32]) and (SymbolTable[RelocIndex].SymKind<>SYMTAB_FUNCTION) then
- begin
- InputError('Relocation must point to a SYMTAB_FUNCTION symbol');
- exit;
- end;
- if (RelocType in [
- R_WASM_MEMORY_ADDR_LEB,
- R_WASM_MEMORY_ADDR_SLEB,
- R_WASM_MEMORY_ADDR_I32]) and (SymbolTable[RelocIndex].SymKind<>SYMTAB_DATA) then
- begin
- InputError('Relocation must point to a SYMTAB_DATA symbol');
- exit;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected relocation section size');
- exit;
- end;
- Result:=True;
- end;
- function ReadLinkingSection: Boolean;
- function ReadSegmentInfo: Boolean;
- var
- SegmentCount: uint32;
- i: Integer;
- begin
- Result:=False;
- if SegmentInfoSectionRead then
- begin
- InputError('The WASM_SEGMENT_INFO subsection is duplicated');
- exit;
- end;
- SegmentInfoSectionRead:=True;
- if not ReadUleb32(SegmentCount) then
- begin
- InputError('Error reading the segment count from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
- exit;
- end;
- if SegmentCount<>Length(DataSegments) then
- begin
- InputError('Segment count in the WASM_SEGMENT_INFO subsection does not match the data count in the data section');
- exit;
- end;
- for i:=0 to SegmentCount-1 do
- with DataSegments[i] do
- begin
- if not ReadName(SegName) then
- begin
- InputError('Error reading segment name from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
- exit;
- end;
- if not ReadUleb32(SegAlignment) then
- begin
- InputError('Error reading segment alignment from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
- exit;
- end;
- if not ReadUleb32(SegFlags) then
- begin
- InputError('Error reading segment flags from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
- exit;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected WASM_SEGMENT_INFO section size');
- exit;
- end;
- Result:=True;
- end;
- function ReadSymbolTable: Boolean;
- var
- SymCount: uint32;
- i: Integer;
- SymKindName: string;
- SymKindB: Byte;
- begin
- Result:=False;
- if SymbolTableSectionRead then
- begin
- InputError('The WASM_SYMBOL_TABLE subsection is duplicated');
- exit;
- end;
- SymbolTableSectionRead:=True;
- if not ReadUleb32(SymCount) then
- begin
- InputError('Error reading the symbol count from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
- exit;
- end;
- SetLength(SymbolTable,SymCount);
- for i:=0 to SymCount-1 do
- with SymbolTable[i] do
- begin
- if not Read(SymKindB,1) then
- begin
- InputError('Error reading symbol type from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
- exit;
- end;
- if SymKindB>Ord(High(TWasmSymbolType)) then
- begin
- InputError('Unsupported symbol type from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
- exit;
- end;
- SymKind:=TWasmSymbolType(SymKindB);
- if not ReadUleb32(SymFlags) then
- begin
- InputError('Error reading symbol flags from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
- exit;
- end;
- case SymKind of
- SYMTAB_FUNCTION,
- SYMTAB_GLOBAL,
- SYMTAB_EVENT,
- SYMTAB_TABLE:
- begin
- WriteStr(SymKindName, SymKind);
- if not ReadUleb32(SymIndex) then
- begin
- InputError('Error reading the index of a ' + SymKindName + ' symbol');
- exit;
- end;
- if ((SymKind=SYMTAB_FUNCTION) and (SymIndex>high(FuncTypes))) or
- ((SymKind=SYMTAB_EVENT) and (SymIndex>high(TagTypes))) then
- begin
- InputError('Symbol index too high');
- exit;
- end;
- if ((SymFlags and WASM_SYM_EXPLICIT_NAME)<>0) or
- ((SymFlags and WASM_SYM_UNDEFINED)=0) then
- begin
- if not ReadName(SymName) then
- begin
- InputError('Error reading symbol name of a ' + SymKindName + ' symbol');
- exit;
- end;
- end;
- end;
- SYMTAB_DATA:
- begin
- if not ReadName(SymName) then
- begin
- InputError('Error reading symbol name of a SYMTAB_DATA symbol');
- exit;
- end;
- if (SymFlags and WASM_SYM_UNDEFINED)=0 then
- begin
- if not ReadUleb32(SymIndex) then
- begin
- InputError('Error reading the data segment index of a SYMTAB_DATA symbol');
- exit;
- end;
- if SymIndex>high(DataSegments) then
- begin
- InputError('Data segment index of SYMTAB_DATA symbol out of bounds');
- exit;
- end;
- if not ReadUleb32(SymOffset) then
- begin
- InputError('Error reading the offset of a SYMTAB_DATA symbol');
- exit;
- end;
- if not ReadUleb32(SymSize) then
- begin
- InputError('Error reading the size of a SYMTAB_DATA symbol');
- exit;
- end;
- end;
- end;
- SYMTAB_FPC_CUSTOM:
- begin
- if not ReadName(SymName) then
- begin
- InputError('Error reading symbol name of a SYMTAB_FPC_CUSTOM symbol');
- exit;
- end;
- if (SymFlags and WASM_SYM_UNDEFINED)=0 then
- begin
- if not ReadUleb32(SymCustomSectionIndex) then
- begin
- InputError('Error reading the custom section index of a SYMTAB_FPC_CUSTOM symbol');
- exit;
- end;
- if not FindDebugSectionByIndex(SymCustomSectionIndex,SymCustomSectionType) then
- begin
- InputError('Custom section index of SYMTAB_FPC_CUSTOM symbol not pointing to a debug section');
- exit;
- end;
- if not ReadUleb32(SymOffset) then
- begin
- InputError('Error reading the offset of a SYMTAB_FPC_CUSTOM symbol');
- exit;
- end;
- if not ReadUleb32(SymSize) then
- begin
- InputError('Error reading the size of a SYMTAB_FPC_CUSTOM symbol');
- exit;
- end;
- end;
- end;
- SYMTAB_SECTION:
- begin
- if not ReadUleb32(TargetSection) then
- begin
- InputError('Error reading the target section of a SYMTAB_SECTION symbol');
- exit;
- end;
- end;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected WASM_SYMBOL_TABLE section size');
- exit;
- end;
- Result:=True;
- end;
- const
- ExpectedVersion = 2;
- var
- Version, SubsectionSize, SaveSectionSize: uint32;
- SubsectionType: Byte;
- SaveSectionStart: LongInt;
- begin
- Result:=False;
- if not ReadUleb32(Version) then
- begin
- InputError('Error reading the version of the ''linking'' section');
- exit;
- end;
- if Version<>ExpectedVersion then
- begin
- InputError('The ''linking'' section has an unsupported version (expected version ' + tostr(ExpectedVersion) + ', got version ' + tostr(Version) + ')');
- exit;
- end;
- while AReader.Pos<(SectionStart+SectionSize) do
- begin
- if not read(SubsectionType, 1) then
- begin
- InputError('Error reading subsection type in the ''linking'' section');
- exit;
- end;
- if not ReadUleb32(SubsectionSize) then
- begin
- InputError('Error reading subsection size in the ''linking'' section');
- exit;
- end;
- if (AReader.Pos+SubsectionSize)>(SectionStart+SectionSize) then
- begin
- InputError('Subsection size exceeds bounds of its parent ''linking'' section');
- exit;
- end;
- SaveSectionStart:=SectionStart;
- SaveSectionSize:=SectionSize;
- SectionStart:=AReader.Pos;
- SectionSize:=SubsectionSize;
- case SubsectionType of
- Byte(WASM_SEGMENT_INFO):
- if not ReadSegmentInfo then
- begin
- InputError('Error reading the WASM_SEGMENT_INFO subsection of the ''linking'' section');
- exit;
- end;
- Byte(WASM_SYMBOL_TABLE):
- if not ReadSymbolTable then
- begin
- InputError('Error reading the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
- exit;
- end;
- else
- begin
- InputError('Unsupported ''linking'' section subsection type ' + tostr(SubsectionType));
- exit;
- end;
- end;
- AReader.Seek(SectionStart+SectionSize);
- SectionStart:=SaveSectionStart;
- SectionSize:=SaveSectionSize;
- end;
- result:=True;
- end;
- function ReadProducersSection: Boolean;
- begin
- Result:=False;
- end;
- function ReadTargetFeaturesSection: Boolean;
- begin
- Result:=False;
- end;
- function ReadDebugSection(const SectionName: string; SectionType: TWasmCustomDebugSectionType): Boolean;
- var
- ObjSec: TObjSection;
- begin
- Result:=False;
- if DebugSectionIndex[SectionType]<>-1 then
- begin
- InputError('Duplicated debug section: ' + SectionName);
- exit;
- end;
- DebugSectionIndex[SectionType]:=SectionIndex;
- ObjSec:=ObjData.createsection(SectionName,1,[oso_Data,oso_debug],false);
- ObjSec.DataPos:=AReader.Pos;
- ObjSec.Size:=SectionStart+SectionSize-AReader.Pos;
- Result:=True;
- end;
- const
- RelocationSectionPrefix = 'reloc.';
- var
- SectionName: ansistring;
- begin
- Result:=False;
- ReadName(SectionName);
- if Copy(SectionName,1,Length(RelocationSectionPrefix)) = RelocationSectionPrefix then
- begin
- if not ReadRelocationSection then
- begin
- InputError('Error reading the relocation section ''' + SectionName + '''');
- exit;
- end;
- end
- else
- case SectionName of
- 'linking':
- if not ReadLinkingSection then
- begin
- InputError('Error reading the ''linking'' section');
- exit;
- end;
- 'producers':
- Result:=ReadProducersSection;
- 'target_features':
- Result:=ReadTargetFeaturesSection;
- '.debug_frame':
- if not ReadDebugSection(SectionName, wcstDebugFrame) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- '.debug_info':
- if not ReadDebugSection(SectionName, wcstDebugInfo) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- '.debug_line':
- if not ReadDebugSection(SectionName, wcstDebugLine) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- '.debug_abbrev':
- if not ReadDebugSection(SectionName, wcstDebugAbbrev) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- '.debug_aranges':
- if not ReadDebugSection(SectionName, wcstDebugAranges) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- '.debug_ranges':
- if not ReadDebugSection(SectionName, wcstDebugRanges) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- '.debug_str':
- if not ReadDebugSection(SectionName, wcstDebugStr) then
- begin
- InputError('Error reading section ' + SectionName);
- exit;
- end;
- else
- InputError('Unsupported custom section: ''' + SectionName + '''');
- end;
- Result:=True;
- end;
- function ReadTypeSection: Boolean;
- var
- FuncTypesCount, ParamsCount, ResultsCount: uint32;
- FuncTypeId, WasmTypeId: Byte;
- i, j: Integer;
- wbt: TWasmBasicType;
- begin
- Result:=False;
- if TypeSectionRead then
- begin
- InputError('Type section is duplicated');
- exit;
- end;
- TypeSectionRead:=True;
- if not ReadUleb32(FuncTypesCount) then
- begin
- InputError('Error reading the func types count');
- exit;
- end;
- SetLength(FFuncTypes,FuncTypesCount);
- for i:=0 to FuncTypesCount - 1 do
- begin
- FFuncTypes[i]:=TWasmFuncType.Create([],[]);
- if not AReader.read(FuncTypeId,1) then
- begin
- InputError('Error reading the function type identifier');
- exit;
- end;
- if FuncTypeId<>$60 then
- begin
- InputError('Incorrect function type identifier (expected $60, got $' + HexStr(FuncTypeId,2) + ')');
- exit;
- end;
- if not ReadUleb32(ParamsCount) then
- begin
- InputError('Error reading the function parameters count');
- exit;
- end;
- for j:=0 to ParamsCount-1 do
- begin
- if not AReader.read(WasmTypeId,1) then
- begin
- InputError('Error reading a function parameter basic type');
- exit;
- end;
- if not decode_wasm_basic_type(WasmTypeId,wbt) then
- begin
- InputError('Unknown function parameter basic type: $' + HexStr(WasmTypeId,2));
- exit;
- end;
- FFuncTypes[i].add_param(wbt);
- end;
- if not ReadUleb32(ResultsCount) then
- begin
- InputError('Error reading the function results count');
- exit;
- end;
- for j:=0 to ResultsCount-1 do
- begin
- if not AReader.read(WasmTypeId,1) then
- begin
- InputError('Error reading a function result basic type');
- exit;
- end;
- if not decode_wasm_basic_type(WasmTypeId,wbt) then
- begin
- InputError('Unknown function result basic type: $' + HexStr(WasmTypeId,2));
- exit;
- end;
- FFuncTypes[i].add_result(wbt);
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected type section size');
- exit;
- end;
- Result:=true;
- end;
- function ReadImportSection: Boolean;
- var
- ImportsCount: uint32;
- i: Integer;
- ModName, Name: ansistring;
- ImportType, TableElemTyp, TableLimitsKind, MemoryLimitsKind,
- GlobalType, GlobalMutabilityType: Byte;
- begin
- Result:=False;
- if ImportSectionRead then
- begin
- InputError('Import section is duplicated');
- exit;
- end;
- ImportSectionRead:=True;
- if not ReadUleb32(ImportsCount) then
- begin
- InputError('Error reading the imports count');
- exit;
- end;
- for i:=0 to ImportsCount-1 do
- begin
- if not ReadName(ModName) then
- begin
- InputError('Error reading import module name');
- exit;
- end;
- if not ReadName(Name) then
- begin
- InputError('Error import name');
- exit;
- end;
- if not AReader.Read(ImportType,1) then
- begin
- InputError('Error reading import type');
- exit;
- end;
- case ImportType of
- $00: { func }
- begin
- Inc(FuncTypeImportsCount);
- SetLength(FuncTypes,FuncTypeImportsCount);
- with FuncTypes[FuncTypeImportsCount-1] do
- begin
- IsImport:=True;
- ImportName:=Name;
- ImportModName:=ModName;
- if not ReadUleb32(typidx) then
- begin
- InputError('Error reading type index for func import');
- exit;
- end;
- if typidx>high(FFuncTypes) then
- begin
- InputError('Type index in func import exceeds bounds of the types table');
- exit;
- end;
- end;
- end;
- $01: { table }
- begin
- Inc(TableTypeImportsCount);
- SetLength(TableTypes,TableTypeImportsCount);
- with TableTypes[TableTypeImportsCount-1] do
- begin
- IsImport:=True;
- ImportName:=Name;
- ImportModName:=ModName;
- if not AReader.read(TableElemTyp,1) then
- begin
- InputError('Error reading table element type for table import');
- exit;
- end;
- if not decode_wasm_basic_type(TableElemTyp,reftype) then
- begin
- InputError('Invalid table element type for table import: $' + HexStr(TableElemTyp,2));
- exit;
- end;
- if not (reftype in WasmReferenceTypes) then
- begin
- InputError('Table element type for table import must be a reference type');
- exit;
- end;
- if not AReader.read(TableLimitsKind,1) then
- begin
- InputError('Error reading table limits kind for table import');
- exit;
- end;
- case TableLimitsKind of
- $00:
- begin
- limits.HasMax:=False;
- limits.Max:=high(limits.Max);
- if not ReadUleb32(limits.min) then
- begin
- InputError('Error reading table limits min for table import');
- exit;
- end;
- end;
- $01:
- begin
- limits.HasMax:=True;
- if not ReadUleb32(limits.min) then
- begin
- InputError('Error reading table limits min for table import');
- exit;
- end;
- if not ReadUleb32(limits.max) then
- begin
- InputError('Error reading table limits max for table import');
- exit;
- end;
- if limits.min>limits.max then
- begin
- InputError('Table limits min exceed table limits max in table import');
- exit;
- end;
- end;
- else
- begin
- InputError('Unsupported table limits kind for table import: $' + HexStr(TableLimitsKind,2));
- exit;
- end;
- end;
- end;
- end;
- $02: { mem }
- begin
- Inc(MemTypeImportsCount);
- SetLength(MemTypes,MemTypeImportsCount);
- with MemTypes[MemTypeImportsCount-1] do
- begin
- IsImport:=True;
- ImportName:=Name;
- ImportModName:=ModName;
- if not AReader.read(MemoryLimitsKind,1) then
- begin
- InputError('Error reading memory limits kind for memory import');
- exit;
- end;
- case MemoryLimitsKind of
- $00:
- begin
- limits.HasMax:=False;
- limits.Max:=high(limits.Max);
- if not ReadUleb32(limits.min) then
- begin
- InputError('Error reading memory limits min for memory import');
- exit;
- end;
- end;
- $01:
- begin
- limits.HasMax:=True;
- if not ReadUleb32(limits.min) then
- begin
- InputError('Error reading memory limits min for memory import');
- exit;
- end;
- if not ReadUleb32(limits.max) then
- begin
- InputError('Error reading memory limits max for memory import');
- exit;
- end;
- if limits.Min>limits.Max then
- begin
- InputError('Memory limits min exceed memory limits max in memory import');
- exit;
- end;
- end;
- else
- begin
- InputError('Unsupported memory limits kind for memory import: $' + HexStr(MemoryLimitsKind,2));
- exit;
- end;
- end;
- end;
- end;
- $03: { global }
- begin
- Inc(GlobalTypeImportsCount);
- SetLength(GlobalTypes,GlobalTypeImportsCount);
- with GlobalTypes[GlobalTypeImportsCount-1] do
- begin
- IsImport:=True;
- ImportName:=Name;
- ImportModName:=ModName;
- if not AReader.read(GlobalType,1) then
- begin
- InputError('Error reading global type for global import');
- exit;
- end;
- if not decode_wasm_basic_type(GlobalType,valtype) then
- begin
- InputError('Unsupported global type for global import: ' + HexStr(GlobalType,2));
- exit;
- end;
- if not AReader.read(GlobalMutabilityType,1) then
- begin
- InputError('Error reading global mutability flag for global import');
- exit;
- end;
- case GlobalMutabilityType of
- $00:
- IsMutable:=False;
- $01:
- IsMutable:=True;
- else
- begin
- InputError('Unknown global mutability flag for global import: $' + HexStr(GlobalMutabilityType,2));
- exit;
- end;
- end;
- end;
- end;
- $04: { tag }
- begin
- Inc(TagTypeImportsCount);
- SetLength(TagTypes,TagTypeImportsCount);
- with TagTypes[TagTypeImportsCount-1] do
- begin
- IsImport:=True;
- ImportName:=Name;
- ImportModName:=ModName;
- if not Read(TagAttr,1) then
- begin
- InputError('Error reading import tag attribute');
- exit;
- end;
- if not ReadUleb32(TagTypeIdx) then
- begin
- InputError('Error reading import tag type index');
- exit;
- end;
- if TagTypeIdx>high(FFuncTypes) then
- begin
- InputError('Type index in tag import exceeds bounds of the types table');
- exit;
- end;
- end;
- end;
- else
- begin
- InputError('Unknown import type: $' + HexStr(ImportType,2));
- exit;
- end;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected import section size');
- exit;
- end;
- Result:=true;
- end;
- function ReadFunctionSection: Boolean;
- var
- FunctionsCount: uint32;
- i: Integer;
- begin
- Result:=False;
- if FunctionSectionRead then
- begin
- InputError('Function section is duplicated');
- exit;
- end;
- FunctionSectionRead:=True;
- if not ReadUleb32(FunctionsCount) then
- begin
- InputError('Error reading the functions count');
- exit;
- end;
- SetLength(FuncTypes, FuncTypeImportsCount + FunctionsCount);
- for i:=0 to FunctionsCount-1 do
- with FuncTypes[i + FuncTypeImportsCount] do
- begin
- IsImport:=False;
- if not ReadUleb32(typidx) then
- begin
- InputError('Error reading type index for function');
- exit;
- end;
- if typidx>high(FFuncTypes) then
- begin
- InputError('Type index in the function section exceeds bounds of the types table');
- exit;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected function section size');
- exit;
- end;
- Result:=true;
- end;
- function ReadGlobalSection: Boolean;
- function ParseExpr(out Init: TGlobalInitializer): Boolean;
- var
- B, B2: Byte;
- tmpU32: UInt32;
- tmpU64: UInt64;
- begin
- Result:=False;
- repeat
- if not Read(B, 1) then
- exit;
- case B of
- $0B: { end }
- ;
- $41: { i32.const }
- begin
- Init.typ:=wbt_i32;
- if not ReadSleb32(Init.init_i32) then
- exit;
- end;
- $42: { i64.const }
- begin
- Init.typ:=wbt_i64;
- if not ReadSleb(Init.init_i64) then
- exit;
- end;
- $43: { f32.const }
- begin
- Init.typ:=wbt_f32;
- if not Read(tmpU32, 4) then
- exit;
- {$ifdef FPC_BIG_ENDIAN}
- tmpU32:=SwapEndian(tmpU32);
- {$endif FPC_BIG_ENDIAN}
- Move(tmpU32,Init.init_f32,4);
- end;
- $44: { f64.const }
- begin
- Init.typ:=wbt_f64;
- if not Read(tmpU64, 8) then
- exit;
- {$ifdef FPC_BIG_ENDIAN}
- tmpU64:=SwapEndian(tmpU64);
- {$endif FPC_BIG_ENDIAN}
- Move(tmpU64,Init.init_f64,8);
- end;
- $D0: { ref.null }
- begin
- if not Read(B2, 1) then
- exit;
- if not decode_wasm_basic_type(B2, Init.typ) then
- exit;
- if not (Init.typ in WasmReferenceTypes) then
- exit;
- end;
- else
- begin
- InputError('Unsupported opcode in global initializer');
- exit;
- end;
- end;
- until b = $0B;
- Result:=True;
- end;
- var
- GlobalsCount: uint32;
- i: Integer;
- vt: Byte;
- mut: Byte;
- begin
- Result:=False;
- if GlobalSectionRead then
- begin
- InputError('Global section is duplicated');
- exit;
- end;
- GlobalSectionRead:=True;
- if not ReadUleb32(GlobalsCount) then
- begin
- InputError('Error reading the globals count from the global section');
- exit;
- end;
- SetLength(GlobalTypes,Length(GlobalTypes)+GlobalsCount);
- for i:=0 to GlobalsCount-1 do
- with GlobalTypes[i + GlobalTypeImportsCount] do
- begin
- if not read(vt,1) then
- begin
- InputError('Error reading the type of a global from the global section');
- exit;
- end;
- if not decode_wasm_basic_type(vt,valtype) then
- begin
- InputError('Unsupported type of global in the global section');
- exit;
- end;
- if not read(mut,1) then
- begin
- InputError('Error reading the mutability flag of a global in the global section');
- exit;
- end;
- case mut of
- $00:
- IsMutable:=False;
- $01:
- IsMutable:=True;
- else
- begin
- InputError('Unsupported value (' + tostr(mut) + ') for the mutability flag of a global in the global section');
- exit;
- end;
- end;
- if not ParseExpr(GlobalInit) then
- begin
- InputError('Error parsing the global initializer expression in the global section');
- exit;
- end;
- if GlobalInit.typ<>valtype then
- begin
- InputError('Initializer expression for global produces a type, which does not match the type of the global');
- exit;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected global section size');
- exit;
- end;
- Result:=True;
- end;
- function ReadExportSection: Boolean;
- var
- ExportsCount, FuncIdx, TableIdx, MemIdx, GlobalIdx, TagIdx: uint32;
- i: Integer;
- Name: ansistring;
- ExportType: Byte;
- begin
- Result:=False;
- if ExportSectionRead then
- begin
- InputError('Export section is duplicated');
- exit;
- end;
- ExportSectionRead:=True;
- if not ReadUleb32(ExportsCount) then
- begin
- InputError('Error reading the exports count from the export section');
- exit;
- end;
- for i:=0 to ExportsCount-1 do
- begin
- if not ReadName(Name) then
- begin
- InputError('Error reading an export name from the export section');
- exit;
- end;
- if not Read(ExportType,1) then
- begin
- InputError('Error reading an export type from the export section');
- exit;
- end;
- case ExportType of
- $00: { func }
- begin
- if not ReadUleb32(FuncIdx) then
- begin
- InputError('Error reading a func index from the export section');
- exit;
- end;
- if FuncIdx>high(FuncTypes) then
- begin
- InputError('Func index too high in the export section');
- exit;
- end;
- with FuncTypes[FuncIdx] do
- begin
- IsExported:=True;
- ExportName:=Name;
- end;
- end;
- $01: { table }
- begin
- if not ReadUleb32(TableIdx) then
- begin
- InputError('Error reading a table index from the export section');
- exit;
- end;
- if TableIdx>high(TableTypes) then
- begin
- InputError('Table index too high in the export section');
- exit;
- end;
- with TableTypes[TableIdx] do
- begin
- IsExported:=True;
- ExportName:=Name;
- end;
- end;
- $02: { mem }
- begin
- if not ReadUleb32(MemIdx) then
- begin
- InputError('Error reading a mem index from the export section');
- exit;
- end;
- if MemIdx>high(MemTypes) then
- begin
- InputError('Mem index too high in the export section');
- exit;
- end;
- with MemTypes[MemIdx] do
- begin
- IsExported:=True;
- ExportName:=Name;
- end;
- end;
- $03: { global }
- begin
- if not ReadUleb32(GlobalIdx) then
- begin
- InputError('Error reading a global index from the export section');
- exit;
- end;
- if GlobalIdx>high(GlobalTypes) then
- begin
- InputError('Global index too high in the export section');
- exit;
- end;
- with GlobalTypes[GlobalIdx] do
- begin
- IsExported:=True;
- ExportName:=Name;
- end;
- end;
- $04: { tag }
- begin
- if not ReadUleb32(TagIdx) then
- begin
- InputError('Error reading a tag index from the export section');
- exit;
- end;
- if TagIdx>high(TagTypes) then
- begin
- InputError('Tag index too high in the export section');
- exit;
- end;
- with TagTypes[TagIdx] do
- begin
- IsExported:=True;
- ExportName:=Name;
- end;
- end;
- else
- begin
- InputError('Unsupported export type in the export section: ' + tostr(ExportType));
- exit;
- end;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected export section size');
- exit;
- end;
- Result:=True;
- end;
- function ReadElementSection: Boolean;
- begin
- Result:=False;
- if ElementSectionRead then
- begin
- InputError('Element section is duplicated');
- exit;
- end;
- ElementSectionRead:=True;
- { We skip the element section for now }
- { TODO: implement reading it (and linking of tables) }
- Result:=True;
- end;
- function ReadTagSection: Boolean;
- var
- TagCount: uint32;
- i: Integer;
- begin
- Result:=False;
- if TagSectionRead then
- begin
- InputError('Tag section is duplicated');
- exit;
- end;
- TagSectionRead:=True;
- if not ReadUleb32(TagCount) then
- begin
- InputError('Error reading the tag count from the tag section');
- exit;
- end;
- SetLength(TagTypes,Length(TagTypes)+TagCount);
- for i:=0 to TagCount-1 do
- with TagTypes[i + TagTypeImportsCount] do
- begin
- if not Read(TagAttr,1) then
- begin
- InputError('Error reading tag attribute');
- exit;
- end;
- if not ReadUleb32(TagTypeIdx) then
- begin
- InputError('Error reading tag type index');
- exit;
- end;
- if TagTypeIdx>high(FFuncTypes) then
- begin
- InputError('Type index in tag import exceeds bounds of the types table');
- exit;
- end;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected tag section size');
- exit;
- end;
- Result:=True;
- end;
- function ReadCodeSection: Boolean;
- var
- CodeEntriesCount: uint32;
- i: Integer;
- begin
- Result:=False;
- if CodeSectionRead then
- begin
- InputError('Code section is duplicated');
- exit;
- end;
- CodeSectionRead:=True;
- CodeSectionIndex:=SectionIndex;
- if not ReadUleb32(CodeEntriesCount) then
- begin
- InputError('Error reading the code entries cound from the code section');
- exit;
- end;
- if CodeEntriesCount <> (Length(FuncTypes) - FuncTypeImportsCount) then
- begin
- InputError('Code segment count in the code section does not match the function definition count in the function section');
- exit;
- end;
- SetLength(CodeSegments,CodeEntriesCount);
- for i:=0 to CodeEntriesCount-1 do
- with CodeSegments[i] do
- begin
- if not ReadUleb32(CodeSize) then
- begin
- InputError('Error reading the code size of an entry in the code section');
- exit;
- end;
- if (AReader.Pos+CodeSize)>(SectionStart+SectionSize) then
- begin
- InputError('Code segment exceeds the bounds of the code section');
- exit;
- end;
- DataPos:=AReader.Pos;
- CodeSectionOffset:=AReader.Pos-SectionStart;
- AReader.Seek(AReader.Pos+CodeSize);
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected code section size');
- exit;
- end;
- Result:=true;
- end;
- function ReadDataSection: Boolean;
- function ReadExpr(out ExprV: int32): Boolean;
- var
- b: Byte;
- begin
- Result:=False;
- if not Read(b,1) then
- exit;
- if b<>$41 then
- begin
- InputError('Only i32.const expressions supported');
- exit;
- end;
- if not ReadSleb32(ExprV) then
- exit;
- if not Read(b,1) then
- exit;
- if b<>$0B then
- begin
- InputError('Only single const expressions supported');
- exit;
- end;
- Result:=True;
- end;
- var
- DataCount: uint32;
- DataType: Byte;
- i: Integer;
- begin
- Result:=False;
- if DataSectionRead then
- begin
- InputError('Data section is duplicated');
- exit;
- end;
- DataSectionRead:=True;
- DataSectionIndex:=SectionIndex;
- if not ReadUleb32(DataCount) then
- begin
- InputError('Error reading the data entries count from the data section');
- exit;
- end;
- if DataCountSectionRead then
- begin
- if Length(DataSegments)<>DataCount then
- begin
- InputError('Data entries count in the data section do not match the number, specified in the data count section');
- exit;
- end;
- end
- else
- SetLength(DataSegments,DataCount);
- for i:=0 to DataCount-1 do
- with DataSegments[i] do
- begin
- if not read(DataType, 1) then
- begin
- InputError('Error reading data type of segment from the data section');
- exit;
- end;
- case DataType of
- 0:
- begin
- Active:=True;
- MemIdx:=0;
- if not ReadExpr(Offset) then
- begin
- InputError('Error reading memory offset of segment from the data section');
- exit;
- end;
- end;
- 1:
- Active:=False;
- 2:
- begin
- Active:=True;
- if not ReadUleb32(MemIdx) then
- begin
- InputError('Error reading MemIdx of segment from the data section');
- exit;
- end;
- if not ReadExpr(Offset) then
- begin
- InputError('Error reading memory offset of segment from the data section');
- exit;
- end;
- end;
- else
- begin
- InputError('Unsupported data type of segment in the data section: ' + tostr(DataType));
- exit;
- end;
- end;
- if MemIdx<>0 then
- begin
- InputError('Memory index other than 0 not supported (got ' + tostr(MemIdx) + ')');
- exit;
- end;
- if not Active then
- begin
- InputError('Passive memory segments not supported');
- exit;
- end;
- if not ReadUleb32(Len) then
- begin
- InputError('Error reading data segment length');
- exit;
- end;
- if (AReader.Pos+Len)>(SectionStart+SectionSize) then
- begin
- InputError('Data segment exceeds the bounds of the data section');
- exit;
- end;
- DataPos:=AReader.Pos;
- DataSectionOffset:=AReader.Pos-SectionStart;
- AReader.Seek(AReader.Pos+Len);
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected data section size');
- exit;
- end;
- Result:=true;
- end;
- function ReadDataCountSection: Boolean;
- var
- DataCount: uint32;
- begin
- Result:=False;
- if DataCountSectionRead then
- begin
- InputError('Data count section is duplicated');
- exit;
- end;
- DataCountSectionRead:=True;
- if DataSectionRead then
- begin
- InputError('The data count section must occur before the data section');
- exit;
- end;
- if not ReadUleb32(DataCount) then
- begin
- InputError('Error reading the data count from the data count section');
- exit;
- end;
- if AReader.Pos<>(SectionStart+SectionSize) then
- begin
- InputError('Unexpected data count section size');
- exit;
- end;
- SetLength(DataSegments, DataCount);
- Result:=true;
- end;
- begin
- Result:=False;
- Inc(SectionIndex);
- if not AReader.read(SectionId,1) then
- begin
- InputError('Error reading section ID');
- exit;
- end;
- CheckSectionBounds:=false;
- if not ReadUleb32(SectionSize) then
- begin
- InputError('Error reading section size');
- exit;
- end;
- if (AReader.Pos+SectionSize)>AReader.size then
- begin
- InputError('Section exceeds beyond the end of file');
- exit;
- end;
- SectionStart:=AReader.Pos;
- CheckSectionBounds:=true;
- case SectionId of
- Byte(wsiCustom):
- if not ReadCustomSection then
- begin
- InputError('Error encountered, while reading a custom section');
- exit;
- end;
- Byte(wsiType):
- if not ReadTypeSection then
- begin
- InputError('Error reading the type section');
- exit;
- end;
- Byte(wsiImport):
- if not ReadImportSection then
- begin
- InputError('Error reading the import section');
- exit;
- end;
- Byte(wsiFunction):
- if not ReadFunctionSection then
- begin
- InputError('Error reading the function section');
- exit;
- end;
- Byte(wsiGlobal):
- if not ReadGlobalSection then
- begin
- InputError('Error reading the global section');
- exit;
- end;
- Byte(wsiExport):
- if not ReadExportSection then
- begin
- InputError('Error reading the export section');
- exit;
- end;
- Byte(wsiElement):
- if not ReadElementSection then
- begin
- InputError('Error reading the element section');
- exit;
- end;
- Byte(wsiTag):
- if not ReadTagSection then
- begin
- InputError('Error reading the tag section');
- exit;
- end;
- Byte(wsiCode):
- if not ReadCodeSection then
- begin
- InputError('Error reading the code section');
- exit;
- end;
- Byte(wsiData):
- if not ReadDataSection then
- begin
- InputError('Error reading the data section');
- exit;
- end;
- Byte(wsiDataCount):
- begin
- if not ReadDataCountSection then
- begin
- InputError('Error reading the data count section');
- exit;
- end;
- end
- else
- begin
- InputError('Unknown section: ' + ToStr(SectionId));
- exit;
- end;
- end;
- if SectionSize>0 then
- AReader.seek(SectionStart+SectionSize);
- Result:=True;
- end;
- function FindCodeSegment(Ofs: uint32): Integer;
- var
- L, R, M: Integer;
- begin
- L:=Low(CodeSegments);
- R:=High(CodeSegments);
- while L<=R do
- begin
- M:=(L+R) div 2;
- if (CodeSegments[M].CodeSectionOffset+CodeSegments[M].CodeSize-1) < Ofs then
- L:=M+1
- else if CodeSegments[M].CodeSectionOffset > Ofs then
- R:=M-1
- else
- begin
- Result:=M;
- exit;
- end;
- end;
- Result:=-1;
- end;
- function FindDataSegment(Ofs: uint32): Integer;
- var
- L, R, M: Integer;
- begin
- L:=Low(DataSegments);
- R:=High(DataSegments);
- while L<=R do
- begin
- M:=(L+R) div 2;
- if (DataSegments[M].DataSectionOffset+DataSegments[M].Len-1) < Ofs then
- L:=M+1
- else if DataSegments[M].DataSectionOffset > Ofs then
- R:=M-1
- else
- begin
- Result:=M;
- exit;
- end;
- end;
- Result:=-1;
- end;
- var
- ModuleMagic: array [0..3] of Byte;
- ModuleVersion: array [0..3] of Byte;
- i, j, FirstCodeSegmentIdx, FirstDataSegmentIdx, SegI: Integer;
- CurrSec, ObjSec: TObjSection;
- BaseSectionOffset: UInt32;
- ObjReloc: TWasmObjRelocation;
- ds: TWasmCustomDebugSectionType;
- begin
- FReader:=AReader;
- InputFileName:=AReader.FileName;
- objdata:=CObjData.Create(InputFileName);
- result:=false;
- CodeSegments:=nil;
- DataSegments:=nil;
- SymbolTable:=nil;
- RelocationTable:=nil;
- SetLength(RelocationTable,2+(Ord(High(TWasmCustomDebugSectionType))-Ord(Low(TWasmCustomDebugSectionType))+1));
- FuncTypes:=nil;
- FuncTypeImportsCount:=0;
- TableTypes:=nil;
- TableTypeImportsCount:=0;
- MemTypes:=nil;
- MemTypeImportsCount:=0;
- GlobalTypes:=nil;
- GlobalTypeImportsCount:=0;
- TagTypes:=nil;
- TagTypeImportsCount:=0;
- if not AReader.read(ModuleMagic,4) then
- exit;
- for i:=0 to 3 do
- if ModuleMagic[i]<>WasmModuleMagic[i] then
- exit;
- if not AReader.read(ModuleVersion,4) then
- exit;
- for i:=0 to 3 do
- if ModuleVersion[i]<>WasmVersion[i] then
- exit;
- while AReader.Pos<AReader.size do
- if not ReadSection then
- exit;
- { fill the code segment names }
- for i:=low(SymbolTable) to high(SymbolTable) do
- with SymbolTable[i] do
- if (SymKind=SYMTAB_FUNCTION) and ((SymFlags and WASM_SYM_UNDEFINED)=0) then
- begin
- if FuncTypes[SymIndex].IsImport then
- begin
- InputError('WASM_SYM_UNDEFINED not set on a SYMTAB_FUNCTION symbol, that is an import');
- exit;
- end;
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)=0 then
- begin
- with CodeSegments[SymIndex-FuncTypeImportsCount] do
- begin
- SegName:='.text.n_'+SymName;
- SegIsExported:=FuncTypes[SymIndex].IsExported;
- end;
- end;
- end;
- { create segments }
- FirstCodeSegmentIdx:=ObjData.ObjSectionList.Count;
- for i:=low(CodeSegments) to high(CodeSegments) do
- with CodeSegments[i] do
- begin
- if SegName='' then
- begin
- InputError('Code section ' + tostr(i) + ' does not have a main symbol defined in the symbol table');
- exit;
- end;
- if SegIsExported or not (cs_link_smart in current_settings.globalswitches) then
- CurrSec:=ObjData.createsection(SegName,1,[oso_executable,oso_Data,oso_load,oso_keep],false)
- else
- CurrSec:=ObjData.createsection(SegName,1,[oso_executable,oso_Data,oso_load],false);
- CurrSec.DataPos:=DataPos;
- CurrSec.Size:=CodeSize;
- end;
- FirstDataSegmentIdx:=ObjData.ObjSectionList.Count;
- for i:=low(DataSegments) to high(DataSegments) do
- with DataSegments[i] do
- if Active then
- begin
- if not (cs_link_smart in current_settings.globalswitches) then
- CurrSec:=ObjData.createsection(SegName,1 shl SegAlignment,[oso_Data,oso_load,oso_write,oso_keep],false)
- else
- CurrSec:=ObjData.createsection(SegName,1 shl SegAlignment,[oso_Data,oso_load,oso_write],false);
- CurrSec.DataPos:=DataPos;
- CurrSec.MemPos:=Offset;
- CurrSec.Size:=Len;
- end;
- ReadSectionContent(ObjData);
- for i:=low(SymbolTable) to high(SymbolTable) do
- with SymbolTable[i] do
- case SymKind of
- SYMTAB_DATA:
- if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_EXTERNAL;
- if (SymFlags and WASM_SYM_TLS)<>0 then
- begin
- objsym.typ:=AT_TLS;
- objsym.TlsGlobalSym:=TWasmObjSymbol(ObjData.CreateSymbol('GOT.mem.'+SymName));
- objsym.TlsGlobalSym.TlsDataSym:=objsym;
- objsym.TlsGlobalSym.bind:=AB_EXTERNAL;
- objsym.TlsGlobalSym.typ:=AT_WASM_GLOBAL;
- objsym.TlsGlobalSym.objsection:=nil;
- objsym.TlsGlobalSym.offset:=0;
- objsym.TlsGlobalSym.size:=1;
- objsym.TlsGlobalSym.LinkingData.GlobalType:=wbt_i32;
- objsym.TlsGlobalSym.LinkingData.GlobalIsMutable:=true;
- end
- else
- objsym.typ:=AT_DATA;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=0;
- end
- else
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- if (SymFlags and WASM_SYM_BINDING_LOCAL)<> 0 then
- objsym.bind:=AB_LOCAL
- else
- objsym.bind:=AB_GLOBAL;
- if (SymFlags and WASM_SYM_TLS)<>0 then
- begin
- objsym.typ:=AT_TLS;
- objsym.TlsGlobalSym:=TWasmObjSymbol(ObjData.CreateSymbol('GOT.mem.'+SymName));
- objsym.TlsGlobalSym.TlsDataSym:=objsym;
- objsym.TlsGlobalSym.bind:=objsym.bind;
- objsym.TlsGlobalSym.typ:=AT_WASM_GLOBAL;
- objsym.TlsGlobalSym.objsection:=ObjData.createsection('.wasm_globals.n_'+objsym.TlsGlobalSym.Name,1,[oso_Data,oso_load],true);
- if objsym.TlsGlobalSym.objsection.Size=0 then
- objsym.TlsGlobalSym.objsection.WriteZeros(1);
- TWasmObjSection(objsym.TlsGlobalSym.objsection).MainFuncSymbol:=objsym.TlsGlobalSym;
- objsym.TlsGlobalSym.offset:=0;
- objsym.TlsGlobalSym.size:=1;
- objsym.TlsGlobalSym.LinkingData.GlobalType:=wbt_i32;
- objsym.TlsGlobalSym.LinkingData.GlobalIsMutable:=true;
- end
- else
- objsym.typ:=AT_DATA;
- objsym.objsection:=TObjSection(ObjData.ObjSectionList[FirstDataSegmentIdx+SymIndex]);
- objsym.offset:=SymOffset;
- objsym.size:=SymSize;
- end;
- SYMTAB_FPC_CUSTOM:
- if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_EXTERNAL;
- if (SymFlags and WASM_SYM_TLS)<>0 then
- internalerror(2024080702);
- objsym.typ:=AT_DATA;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=0;
- end
- else
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- if (SymFlags and WASM_SYM_BINDING_LOCAL)<> 0 then
- objsym.bind:=AB_LOCAL
- else
- objsym.bind:=AB_GLOBAL;
- if (SymFlags and WASM_SYM_TLS)<>0 then
- internalerror(2024080703);
- objsym.typ:=AT_DATA;
- objsym.objsection:=TObjSection(ObjData.ObjSectionList.Find(WasmCustomSectionName[SymCustomSectionType]));
- objsym.offset:=SymOffset;
- objsym.size:=SymSize;
- end;
- SYMTAB_FUNCTION:
- begin
- if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
- begin
- if not FuncTypes[SymIndex].IsImport then
- begin
- InputError('WASM_SYM_UNDEFINED set on a SYMTAB_FUNCTION symbol, that is not an import');
- exit;
- end;
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)<>0 then
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_FUNCTION;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=0;
- objsym.LinkingData.ImportModule:=FuncTypes[SymIndex].ImportModName;
- objsym.LinkingData.ImportName:=FuncTypes[SymIndex].ImportName;
- end
- else
- begin
- if FuncTypes[SymIndex].ImportModName = 'env' then
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(FuncTypes[SymIndex].ImportName))
- else
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(FuncTypes[SymIndex].ImportModName + '.' + FuncTypes[SymIndex].ImportName));
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_FUNCTION;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=0;
- end;
- end
- else
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_GLOBAL;
- objsym.typ:=AT_FUNCTION;
- objsym.objsection:=TObjSection(ObjData.ObjSectionList[FirstCodeSegmentIdx+SymIndex-FuncTypeImportsCount]);
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)=0 then
- TWasmObjSection(ObjData.ObjSectionList[FirstCodeSegmentIdx+SymIndex-FuncTypeImportsCount]).MainFuncSymbol:=objsym;
- objsym.offset:=0;
- objsym.size:=objsym.objsection.Size;
- end;
- objsym.LinkingData.FuncType:=TWasmFuncType.Create(FFuncTypes[FuncTypes[SymIndex].typidx]);
- objsym.LinkingData.IsExported:=FuncTypes[SymIndex].IsExported;
- objsym.LinkingData.ExportName:=FuncTypes[SymIndex].ExportName;
- end;
- SYMTAB_GLOBAL:
- begin
- if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
- begin
- if not GlobalTypes[SymIndex].IsImport then
- begin
- InputError('WASM_SYM_UNDEFINED set on a SYMTAB_GLOBAL symbol, that is not an import');
- exit;
- end;
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)<>0 then
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_WASM_GLOBAL;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=1;
- objsym.LinkingData.ImportModule:=GlobalTypes[SymIndex].ImportModName;
- objsym.LinkingData.ImportName:=GlobalTypes[SymIndex].ImportName;
- end
- else
- begin
- if GlobalTypes[SymIndex].ImportModName = 'env' then
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(GlobalTypes[SymIndex].ImportName))
- else
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(GlobalTypes[SymIndex].ImportModName + '.' + GlobalTypes[SymIndex].ImportName));
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_WASM_GLOBAL;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=1;
- end;
- end
- else
- begin
- if GlobalTypes[SymIndex].IsImport then
- begin
- InputError('WASM_SYM_UNDEFINED not set on a SYMTAB_GLOBAL symbol, that is an import');
- exit;
- end;
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_GLOBAL;
- objsym.typ:=AT_WASM_GLOBAL;
- objsym.objsection:=ObjData.createsection('.wasm_globals.n_'+SymName,1,[oso_Data,oso_load],true);
- if objsym.objsection.Size=0 then
- objsym.objsection.WriteZeros(1);
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)=0 then
- TWasmObjSection(objsym.objsection).MainFuncSymbol:=objsym;
- objsym.offset:=0;
- objsym.size:=1;
- objsym.LinkingData.GlobalInitializer:=GlobalTypes[SymIndex].GlobalInit;
- end;
- objsym.LinkingData.GlobalType:=GlobalTypes[SymIndex].valtype;
- objsym.LinkingData.GlobalIsMutable:=GlobalTypes[SymIndex].IsMutable;
- objsym.LinkingData.IsExported:=GlobalTypes[SymIndex].IsExported;
- objsym.LinkingData.ExportName:=GlobalTypes[SymIndex].ExportName;
- end;
- SYMTAB_SECTION:
- begin
- for ds:=Low(DebugSectionIndex) to High(DebugSectionIndex) do
- if DebugSectionIndex[ds]=TargetSection then
- begin
- ObjSec:=TWasmObjSection(ObjData.findsection(WasmCustomSectionName[ds]));
- break;
- end;
- if ObjSec=nil then
- begin
- InputError('SYMTAB_SECTION entry points to an unsupported section');
- exit;
- end;
- end;
- SYMTAB_EVENT:
- begin
- if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
- begin
- if not TagTypes[SymIndex].IsImport then
- begin
- InputError('WASM_SYM_UNDEFINED set on a SYMTAB_EVENT symbol, that is not an import');
- exit;
- end;
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)<>0 then
- begin
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_WASM_EXCEPTION_TAG;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=1;
- objsym.LinkingData.ImportModule:=TagTypes[SymIndex].ImportModName;
- objsym.LinkingData.ImportName:=TagTypes[SymIndex].ImportName;
- end
- else
- begin
- if GlobalTypes[SymIndex].ImportModName = 'env' then
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(GlobalTypes[SymIndex].ImportName))
- else
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(GlobalTypes[SymIndex].ImportModName + '.' + GlobalTypes[SymIndex].ImportName));
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_WASM_EXCEPTION_TAG;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=1;
- end;
- end
- else
- begin
- if TagTypes[SymIndex].IsImport then
- begin
- InputError('WASM_SYM_UNDEFINED not set on a SYMTAB_EVENT symbol, that is an import');
- exit;
- end;
- objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
- if (symflags and WASM_SYM_BINDING_WEAK) <> 0 then
- objsym.bind:=AB_WEAK_EXTERNAL
- else if (symflags and WASM_SYM_BINDING_LOCAL) <> 0 then
- objsym.bind:=AB_LOCAL
- else
- objsym.bind:=AB_GLOBAL;
- objsym.typ:=AT_WASM_EXCEPTION_TAG;
- objsym.objsection:=ObjData.createsection('.wasm_tags.n_'+SymName,1,[oso_Data,oso_load],true);
- if objsym.objsection.Size=0 then
- objsym.objsection.WriteZeros(1);
- if (SymFlags and WASM_SYM_EXPLICIT_NAME)=0 then
- TWasmObjSection(objsym.objsection).MainFuncSymbol:=objsym;
- objsym.offset:=0;
- objsym.size:=1;
- end;
- objsym.LinkingData.FuncType:=TWasmFuncType.Create(FFuncTypes[TagTypes[SymIndex].TagTypeIdx]);
- objsym.LinkingData.IsExported:=TagTypes[SymIndex].IsExported;
- objsym.LinkingData.ExportName:=TagTypes[SymIndex].ExportName;
- end;
- SYMTAB_TABLE:
- {TODO};
- end;
- for j:=0 to high(RelocationTable) do
- for i:=0 to high(RelocationTable[j]) do
- with RelocationTable[j,i] do
- begin
- case j of
- 0:
- begin
- SegI:=FindCodeSegment(RelocOffset);
- if SegI=-1 then
- begin
- InputError('Relocation offset not found in code segment');
- Exit;
- end;
- BaseSectionOffset:=CodeSegments[SegI].CodeSectionOffset;
- ObjSec:=TObjSection(ObjData.ObjSectionList[FirstCodeSegmentIdx+SegI]);
- end;
- 1:
- begin
- SegI:=FindDataSegment(RelocOffset);
- if SegI=-1 then
- begin
- InputError('Relocation offset not found in data segment');
- Exit;
- end;
- BaseSectionOffset:=DataSegments[SegI].DataSectionOffset;
- ObjSec:=TObjSection(ObjData.ObjSectionList[FirstDataSegmentIdx+SegI]);
- end;
- 2..2+(Ord(High(TWasmCustomDebugSectionType))-Ord(Low(TWasmCustomDebugSectionType))):
- begin
- BaseSectionOffset:=0;
- ObjSec:=ObjData.findsection(WasmCustomSectionName[TWasmCustomSectionType((j-2)+Ord(Low(TWasmCustomDebugSectionType)))]);
- end;
- else
- internalerror(2023122801);
- end;
- case RelocType of
- R_WASM_FUNCTION_INDEX_LEB:
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_FUNCTION_INDEX_LEB));
- R_WASM_TABLE_INDEX_SLEB:
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB));
- R_WASM_TABLE_INDEX_I32:
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE));
- R_WASM_MEMORY_ADDR_LEB:
- begin
- ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_MEMORY_ADDR_LEB);
- ObjReloc.Addend:=RelocAddend;
- ObjSec.ObjRelocations.Add(ObjReloc);
- end;
- R_WASM_MEMORY_ADDR_SLEB:
- begin
- ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB);
- ObjReloc.Addend:=RelocAddend;
- ObjSec.ObjRelocations.Add(ObjReloc);
- end;
- R_WASM_MEMORY_ADDR_I32:
- begin
- ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE);
- ObjReloc.Addend:=RelocAddend;
- ObjSec.ObjRelocations.Add(ObjReloc);
- end;
- R_WASM_TYPE_INDEX_LEB:
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateFuncType(RelocOffset-BaseSectionOffset,FFuncTypes[RelocIndex]));
- R_WASM_FUNCTION_OFFSET_I32:
- begin
- ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE);
- ObjReloc.Addend:=RelocAddend;
- ObjReloc.IsFunctionOffsetI32:=True;
- ObjSec.ObjRelocations.Add(ObjReloc);
- end;
- R_WASM_SECTION_OFFSET_I32:
- begin
- ObjReloc:=TWasmObjRelocation.CreateSection(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSec,RELOC_ABSOLUTE);
- ObjReloc.Addend:=RelocAddend;
- ObjSec.ObjRelocations.Add(ObjReloc);
- end;
- R_WASM_GLOBAL_INDEX_LEB:
- begin
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_GLOBAL_INDEX_LEB));
- if Assigned(SymbolTable[RelocIndex].ObjSym.TlsGlobalSym) then
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym.TlsGlobalSym,RELOC_GLOBAL_INDEX_LEB));
- end;
- R_WASM_GLOBAL_INDEX_I32:
- begin
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE));
- if Assigned(SymbolTable[RelocIndex].ObjSym.TlsGlobalSym) then
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym.TlsGlobalSym,RELOC_ABSOLUTE));
- end;
- R_WASM_TAG_INDEX_LEB:
- ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_TAG_INDEX_LEB));
- else
- internalerror(2023122802);
- end;
- end;
- Result:=True;
- end;
- {****************************************************************************
- TWasmExeOutput
- ****************************************************************************}
- procedure TWasmExeOutput.AddToNameMap(var nm: TCustomSectionNameMap; aidx: UInt32; const aname: string);
- begin
- SetLength(nm,Length(nm)+1);
- with nm[High(nm)] do
- begin
- idx:=aidx;
- name:=aname;
- end;
- end;
- procedure TWasmExeOutput.AddToFunctionNameMap(aidx: UInt32; const aname: string);
- begin
- AddToNameMap(FFunctionNameMap,aidx,aname);
- end;
- procedure TWasmExeOutput.AddToGlobalNameMap(aidx: UInt32; const aname: string);
- begin
- AddToNameMap(FGlobalNameMap,aidx,aname);
- end;
- procedure TWasmExeOutput.AddToDataNameMap(aidx: UInt32; const aname: string);
- begin
- AddToNameMap(FDataNameMap,aidx,aname);
- end;
- procedure TWasmExeOutput.AddToTagNameMap(aidx: UInt32; const aname: string);
- begin
- AddToNameMap(FTagNameMap,aidx,aname);
- end;
- procedure TWasmExeOutput.WriteWasmSection(wsid: TWasmSectionID);
- var
- b: byte;
- begin
- b:=ord(wsid);
- Writer.write(b,1);
- WriteUleb(Writer,FWasmSections[wsid].size);
- Writer.writearray(FWasmSections[wsid]);
- end;
- procedure TWasmExeOutput.WriteWasmSectionIfNotEmpty(wsid: TWasmSectionID);
- begin
- if FWasmSections[wsid].size>0 then
- WriteWasmSection(wsid);
- end;
- procedure TWasmExeOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
- var
- b: byte;
- begin
- b:=0;
- Writer.write(b,1);
- WriteUleb(Writer,FWasmCustomSections[wcst].size);
- Writer.writearray(FWasmCustomSections[wcst]);
- end;
- function TWasmExeOutput.writeData: boolean;
- procedure WriteImportSection;
- var
- imports_count,
- i: Integer;
- begin
- if assigned(exemap) then
- exemap.AddHeader('Import section');
- imports_count:=Length(FImportedMemories)+Length(FFunctionImports);
- WriteUleb(FWasmSections[wsiImport],imports_count);
- for i:=0 to Length(FImportedMemories)-1 do
- with FImportedMemories[i] do
- begin
- WriteName(FWasmSections[wsiImport],ModName);
- WriteName(FWasmSections[wsiImport],Name);
- WriteByte(FWasmSections[wsiImport],$02); { mem }
- WriteMemoryTo(FWasmSections[wsiImport],MemType);
- if assigned(exemap) then
- exemap.Add(' Memory['+tostr(i)+'] '+Memory2String(MemType)+' <- '+ModName+'.'+Name);
- end;
- for i:=0 to Length(FFunctionImports)-1 do
- with FFunctionImports[i] do
- begin
- WriteName(FWasmSections[wsiImport],ModName);
- WriteName(FWasmSections[wsiImport],Name);
- WriteByte(FWasmSections[wsiImport],$00); { func }
- WriteUleb(FWasmSections[wsiImport],TypeIdx);
- if assigned(exemap) then
- exemap.Add(' Function['+tostr(i)+'] sig='+tostr(TypeIdx)+' <- '+ModName+'.'+Name);
- end;
- end;
- procedure WriteCodeSegments;
- var
- i: Integer;
- exesec: TExeSection;
- objsec: TWasmObjSection;
- begin
- exesec:=FindExeSection('.text');
- if not assigned(exesec) then
- internalerror(2023123102);
- if not (oso_Data in exesec.SecOptions) then
- internalerror(2023123103);
- WriteUleb(FWasmSections[wsiFunction],exesec.ObjSectionList.Count);
- WriteUleb(FWasmSections[wsiCode],exesec.ObjSectionList.Count);
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(exesec.ObjSectionList[i]);
- if not (oso_data in objsec.secoptions) then
- internalerror(2023123104);
- if not assigned(objsec.data) then
- internalerror(2023123105);
- if objsec.MainFuncSymbol.LinkingData.ExeFunctionIndex<>(i+Length(FFunctionImports)) then
- internalerror(2024010101);
- WriteUleb(FWasmSections[wsiFunction],objsec.MainFuncSymbol.LinkingData.ExeTypeIndex);
- WriteUleb(FWasmSections[wsiCode],objsec.Data.size);
- objsec.Data.seek(0);
- CopyDynamicArray(objsec.Data,FWasmSections[wsiCode],objsec.Data.size);
- end;
- end;
- procedure WriteDataSegments;
- procedure WriteExeSection(exesec: TExeSection);
- var
- i: Integer;
- objsec: TObjSection;
- exesecdatapos: LongWord;
- dpos, pad: QWord;
- begin
- AddToDataNameMap(Length(FDataNameMap),exesec.Name);
- if ts_wasm_threads in current_settings.targetswitches then
- WriteByte(FWasmSections[wsiData],1) { mode passive }
- else
- begin
- WriteByte(FWasmSections[wsiData],0); { mode active, memory 0, offset e }
- WriteByte(FWasmSections[wsiData],$41); { i32.const }
- WriteSleb(FWasmSections[wsiData],longint(exesec.MemPos));
- WriteByte(FWasmSections[wsiData],$0B); { end }
- end;
- WriteUleb(FWasmSections[wsiData],exesec.Size);
- exesecdatapos:=FWasmSections[wsiData].size;
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TObjSection(exesec.ObjSectionList[i]);
- if not (oso_data in objsec.secoptions) then
- internalerror(2024010104);
- if not assigned(objsec.data) then
- internalerror(2024010105);
- dpos:=objsec.MemPos-exesec.MemPos+exesecdatapos;
- pad:=dpos-FWasmSections[wsiData].size;
- { objsection must be within SecAlign bytes from the previous one }
- if (dpos<FWasmSections[wsiData].Size) or
- (pad>=max(objsec.SecAlign,1)) then
- internalerror(2024010106);
- writeZeros(FWasmSections[wsiData],pad);
- objsec.data.seek(0);
- CopyDynamicArray(objsec.data,FWasmSections[wsiData],objsec.data.size);
- end;
- if (FWasmSections[wsiData].size-exesecdatapos)<>exesec.Size then
- internalerror(2024010107);
- end;
- var
- DataCount: Integer;
- DataSecName: string;
- ExeSec: TExeSection;
- begin
- DataCount:=0;
- for DataSecName in DataSections do
- begin
- ExeSec:=FindExeSection(DataSecName);
- if Assigned(ExeSec) and (ExeSec.Size>0) then
- Inc(DataCount);
- end;
- WriteUleb(FWasmSections[wsiDataCount],DataCount);
- WriteUleb(FWasmSections[wsiData],DataCount);
- for DataSecName in DataSections do
- begin
- ExeSec:=FindExeSection(DataSecName);
- if Assigned(ExeSec) and (ExeSec.Size>0) then
- WriteExeSection(ExeSec);
- end;
- end;
- procedure WriteTableAndElemSections;
- const
- TableCount=1;
- var
- i: Integer;
- begin
- { Table section }
- WriteUleb(FWasmSections[wsiTable],TableCount);
- { table 0 }
- { table type }
- WriteByte(FWasmSections[wsiTable],encode_wasm_basic_type(wbt_funcref));
- { table limits }
- WriteByte(FWasmSections[wsiTable],$01); { has min & max }
- WriteUleb(FWasmSections[wsiTable],Length(FIndirectFunctionTable)); { min }
- WriteUleb(FWasmSections[wsiTable],Length(FIndirectFunctionTable)); { max }
- { Elem section }
- WriteUleb(FWasmSections[wsiElement],1); { 1 element segment }
- { element segment 0 }
- WriteByte(FWasmSections[wsiElement],0); { type funcref, init((ref.func y) end)*, mode active <table 0, offset e> }
- { e:expr }
- WriteByte(FWasmSections[wsiElement],$41); { i32.const }
- WriteSleb(FWasmSections[wsiElement],1); { starting from 1 (table entry 0 is ref.null) }
- WriteByte(FWasmSections[wsiElement],$0B); { end }
- { y*:vec(funcidx) }
- WriteUleb(FWasmSections[wsiElement],Length(FIndirectFunctionTable)-1);
- for i:=1 to Length(FIndirectFunctionTable)-1 do
- WriteUleb(FWasmSections[wsiElement],FIndirectFunctionTable[i].FuncIdx);
- end;
- procedure WriteGlobalSection;
- var
- exesec: TExeSection;
- globals_count, i: Integer;
- objsec: TWasmObjSection;
- mapstr: string='';
- begin
- if assigned(exemap) then
- exemap.AddHeader('Global section');
- exesec:=FindExeSection('.wasm_globals');
- if not assigned(exesec) then
- internalerror(2024010112);
- globals_count:=exesec.ObjSectionList.Count;
- if globals_count<>exesec.Size then
- internalerror(2024010113);
- WriteUleb(FWasmSections[wsiGlobal],globals_count);
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(exesec.ObjSectionList[i]);
- WriteByte(FWasmSections[wsiGlobal],encode_wasm_basic_type(objsec.MainFuncSymbol.LinkingData.GlobalType));
- if objsec.MainFuncSymbol.LinkingData.GlobalIsMutable then
- WriteByte(FWasmSections[wsiGlobal],1)
- else
- WriteByte(FWasmSections[wsiGlobal],0);
- if assigned(exemap) then
- WriteStr(mapstr,' Global[',i,'] ',wasm_basic_type_str[objsec.MainFuncSymbol.LinkingData.GlobalType],' mutable=',objsec.MainFuncSymbol.LinkingData.GlobalIsMutable,' <',objsec.MainFuncSymbol.Name,'> - init ');
- { initializer expr }
- with objsec.MainFuncSymbol.LinkingData.GlobalInitializer do
- case typ of
- wbt_i32:
- begin
- WriteByte(FWasmSections[wsiGlobal],$41); { i32.const }
- WriteSleb(FWasmSections[wsiGlobal],init_i32);
- if assigned(exemap) then
- mapstr:=mapstr+'i32='+tostr(init_i32);
- end;
- wbt_i64:
- begin
- WriteByte(FWasmSections[wsiGlobal],$42); { i64.const }
- WriteSleb(FWasmSections[wsiGlobal],init_i64);
- if assigned(exemap) then
- mapstr:=mapstr+'i64='+tostr(init_i64);
- end;
- wbt_f32:
- begin
- WriteByte(FWasmSections[wsiGlobal],$43); { f32.const }
- WriteF32LE(FWasmSections[wsiGlobal],init_f32);
- if assigned(exemap) then
- WriteStr(mapstr,mapstr+'f32=',init_f32);
- end;
- wbt_f64:
- begin
- WriteByte(FWasmSections[wsiGlobal],$44); { f64.const }
- WriteF64LE(FWasmSections[wsiGlobal],init_f64);
- if assigned(exemap) then
- WriteStr(mapstr,mapstr+'f64=',init_f64);
- end;
- wbt_funcref,
- wbt_externref:
- begin
- WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null }
- WriteByte(FWasmSections[wsiGlobal],encode_wasm_basic_type(typ));
- if assigned(exemap) then
- mapstr:=mapstr+'ref.null '+wasm_basic_type_str[typ];
- end;
- else
- internalerror(2024010114);
- end;
- WriteByte(FWasmSections[wsiGlobal],$0B); { end }
- { add entry for the name section }
- AddToGlobalNameMap(i,objsec.MainFuncSymbol.Name);
- if assigned(exemap) then
- exemap.Add(mapstr);
- end;
- end;
- procedure WriteTagSection;
- var
- exesec: TExeSection;
- tags_count, i: Integer;
- objsec: TWasmObjSection;
- begin
- exesec:=FindExeSection('.wasm_tags');
- if not assigned(exesec) then
- exit;
- tags_count:=exesec.ObjSectionList.Count;
- if tags_count<>exesec.Size then
- internalerror(2024010702);
- if tags_count=0 then
- exit;
- WriteUleb(FWasmSections[wsiTag],tags_count);
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(exesec.ObjSectionList[i]);
- WriteByte(FWasmSections[wsiTag],0);
- WriteUleb(FWasmSections[wsiTag],objsec.MainFuncSymbol.LinkingData.ExeTypeIndex);
- AddToTagNameMap(i,objsec.MainFuncSymbol.Name);
- end;
- end;
- procedure WriteExportSection;
- const
- MemoryExportsCount=1;
- var
- FunctionExportsCount: Integer;
- ExportsCount: Integer;
- textsec: TExeSection;
- i: Integer;
- objsec: TWasmObjSection;
- begin
- if assigned(exemap) then
- exemap.AddHeader('Export section');
- FunctionExportsCount:=0;
- textsec:=FindExeSection('.text');
- if not assigned(textsec) then
- internalerror(2024010115);
- for i:=0 to textsec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(textsec.ObjSectionList[i]);
- if objsec.MainFuncSymbol.LinkingData.IsExported then
- Inc(FunctionExportsCount)
- end;
- ExportsCount:=MemoryExportsCount+FunctionExportsCount;
- WriteUleb(FWasmSections[wsiExport],ExportsCount);
- { export 0 }
- WriteName(FWasmSections[wsiExport],'memory');
- WriteByte(FWasmSections[wsiExport],$02); { mem }
- WriteUleb(FWasmSections[wsiExport],0); { memidx = 0 }
- if assigned(exemap) then
- exemap.Add(' Memory[0] -> "memory"');
- for i:=0 to textsec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(textsec.ObjSectionList[i]);
- if objsec.MainFuncSymbol.LinkingData.IsExported then
- begin
- WriteName(FWasmSections[wsiExport],objsec.MainFuncSymbol.LinkingData.ExportName);
- WriteByte(FWasmSections[wsiExport],$00); { func }
- WriteUleb(FWasmSections[wsiExport],objsec.MainFuncSymbol.LinkingData.ExeFunctionIndex); { funcidx }
- if assigned(exemap) then
- exemap.Add(' Function['+tostr(objsec.MainFuncSymbol.LinkingData.ExeFunctionIndex)+'] -> "'+objsec.MainFuncSymbol.LinkingData.ExportName+'"');
- end;
- end;
- end;
- procedure MaybeWriteDebugSection(st: TWasmCustomDebugSectionType);
- var
- exesec: TExeSection;
- begin
- exesec:=FindExeSection(WasmCustomSectionName[st]);
- if assigned(exesec) then
- begin
- WriteExeSectionToDynArray(exesec,FWasmCustomSections[st]);
- WriteWasmCustomSection(st);
- end;
- end;
- procedure WriteNameMap(const nm: TCustomSectionNameMap; dest: tdynamicarray);
- var
- i: Integer;
- begin
- WriteUleb(dest,Length(nm));
- for i:=low(nm) to high(nm) do
- with nm[i] do
- begin
- WriteUleb(dest,idx);
- WriteName(dest,name);
- end;
- end;
- procedure WriteNameSubsection(wnst: TWasmNameSubsectionType);
- begin
- if FWasmNameSubsections[wnst].size>0 then
- begin
- WriteByte(FWasmCustomSections[wcstName],Ord(wnst));
- WriteUleb(FWasmCustomSections[wcstName],FWasmNameSubsections[wnst].size);
- FWasmNameSubsections[wnst].seek(0);
- CopyDynamicArray(FWasmNameSubsections[wnst],FWasmCustomSections[wcstName],FWasmNameSubsections[wnst].size);
- end;
- end;
- procedure WriteNameSection;
- begin
- WriteName(FWasmNameSubsections[wnstModuleName],current_module.exefilename);
- WriteNameSubsection(wnstModuleName);
- WriteNameMap(FFunctionNameMap,FWasmNameSubsections[wnstFunctionNames]);
- WriteNameSubsection(wnstFunctionNames);
- WriteNameMap(FGlobalNameMap,FWasmNameSubsections[wnstGlobalNames]);
- WriteNameSubsection(wnstGlobalNames);
- WriteNameMap(FDataNameMap,FWasmNameSubsections[wnstDataNames]);
- WriteNameSubsection(wnstDataNames);
- if Length(FTagNameMap)>0 then
- begin
- WriteNameMap(FTagNameMap,FWasmNameSubsections[wnstTagNames]);
- WriteNameSubsection(wnstTagNames);
- end;
- end;
- procedure WriteMemorySection;
- var
- i: Integer;
- begin
- if assigned(exemap) then
- exemap.AddHeader('Memory section');
- WriteUleb(FWasmSections[wsiMemory],Length(FMemories));
- for i:=low(FMemories) to high(FMemories) do
- begin
- WriteMemoryTo(FWasmSections[wsiMemory],FMemories[i]);
- if assigned(exemap) then
- exemap.Add(' Memory['+tostr(i+Length(FImportedMemories))+'] '+Memory2String(FMemories[i]));
- end;
- end;
- var
- cust_sec: TWasmCustomSectionType;
- begin
- result:=false;
- FMaxMemoryPages:=align(maxheapsize,WasmPageSize) div WasmPageSize;
- { each custom sections starts with its name }
- for cust_sec in TWasmCustomSectionType do
- WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
- SetStackPointer;
- SetTlsSizeAlignAndBase;
- SetThreadVarGlobalsInitValues;
- GenerateCode_InitTls;
- GenerateCode_InitSharedMemory;
- if ts_wasm_threads in current_settings.targetswitches then
- begin
- SetLength(FImportedMemories,1);
- with FImportedMemories[0] do
- begin
- ModName:='env';
- Name:='memory';
- with MemType do
- begin
- Flags:=[wmfShared,wmfHasMaximumBound];
- MinPages:=FMinMemoryPages;
- MaxPages:=Max(FMinMemoryPages,FMaxMemoryPages);
- end;
- end;
- end
- else
- begin
- SetLength(FMemories,1);
- with FMemories[0] do
- begin
- Flags:=[];
- MinPages:=FMinMemoryPages;
- if FMaxMemoryPages>=FMinMemoryPages then
- begin
- Include(Flags,wmfHasMaximumBound);
- MaxPages:=FMaxMemoryPages;
- end;
- end;
- end;
- FFuncTypes.WriteTo(FWasmSections[wsiType]);
- WriteImportSection;
- WriteCodeSegments;
- WriteDataSegments;
- WriteTableAndElemSections;
- WriteGlobalSection;
- WriteTagSection;
- if Length(FMemories)>0 then
- WriteMemorySection;
- WriteExportSection;
- if ts_wasm_threads in current_settings.targetswitches then
- WriteUleb(FWasmSections[wsiStart],FInitSharedMemoryFunctionSym.LinkingData.ExeFunctionIndex);
- WriteNameSection;
- Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
- Writer.write(WasmVersion,SizeOf(WasmVersion));
- WriteWasmSection(wsiType);
- WriteWasmSection(wsiImport);
- WriteWasmSection(wsiFunction);
- WriteWasmSection(wsiTable);
- if not (ts_wasm_threads in current_settings.targetswitches) then
- WriteWasmSection(wsiMemory);
- WriteWasmSectionIfNotEmpty(wsiTag);
- WriteWasmSection(wsiGlobal);
- WriteWasmSection(wsiExport);
- if ts_wasm_threads in current_settings.targetswitches then
- WriteWasmSection(wsiStart);
- WriteWasmSection(wsiElement);
- WriteWasmSection(wsiDataCount);
- WriteWasmSection(wsiCode);
- WriteWasmSection(wsiData);
- MaybeWriteDebugSection(wcstDebugAbbrev);
- MaybeWriteDebugSection(wcstDebugInfo);
- MaybeWriteDebugSection(wcstDebugStr);
- MaybeWriteDebugSection(wcstDebugLine);
- MaybeWriteDebugSection(wcstDebugFrame);
- MaybeWriteDebugSection(wcstDebugAranges);
- MaybeWriteDebugSection(wcstDebugRanges);
- WriteWasmCustomSection(wcstName);
- result := true;
- end;
- procedure TWasmExeOutput.DoRelocationFixup(objsec: TObjSection);
- procedure writeUInt32LE(v: uint32);
- begin
- {$ifdef FPC_BIG_ENDIAN}
- v:=SwapEndian(v);
- {$endif FPC_BIG_ENDIAN}
- objsec.data.write(v,4);
- end;
- var
- i: Integer;
- objreloc: TWasmObjRelocation;
- objsym: TWasmObjSymbol;
- begin
- for i:=0 to objsec.ObjRelocations.Count-1 do
- begin
- objreloc:=TWasmObjRelocation(objsec.ObjRelocations[i]);
- if assigned(objreloc.symbol) then
- begin
- objsym:=TWasmObjSymbol(objreloc.symbol);
- case objreloc.typ of
- RELOC_FUNCTION_INDEX_LEB:
- begin
- if objsym.LinkingData.ExeFunctionIndex=-1 then
- internalerror(2024010103);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteUleb5(objsec.Data,objsym.LinkingData.ExeFunctionIndex);
- end;
- end;
- RELOC_ABSOLUTE:
- begin
- case objsym.typ of
- AT_FUNCTION:
- begin
- if objreloc.IsFunctionOffsetI32 then
- begin
- { R_WASM_FUNCTION_OFFSET_I32 }
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- writeUInt32LE(UInt32(objsym.objsection.MemPos+objreloc.Addend));
- end;
- end
- else
- begin
- { R_WASM_TABLE_INDEX_I32 }
- if objsym.LinkingData.ExeFunctionIndex=-1 then
- internalerror(2024010103);
- case FRelocationPass of
- 1:
- if objsym.LinkingData.ExeIndirectFunctionTableIndex=-1 then
- objsym.LinkingData.ExeIndirectFunctionTableIndex:=AddOrGetIndirectFunctionTableIndex(objsym.LinkingData.ExeFunctionIndex);
- 2:
- begin
- objsec.Data.seek(objreloc.DataOffset);
- writeUInt32LE(UInt32(objsym.LinkingData.ExeIndirectFunctionTableIndex));
- end;
- end;
- end;
- end;
- AT_DATA:
- begin
- if objreloc.IsFunctionOffsetI32 then
- internalerror(2024010602);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- writeUInt32LE(UInt32((objsym.offset+objsym.objsection.MemPos)+objreloc.Addend));
- end;
- end;
- AT_TLS:
- begin
- if objreloc.IsFunctionOffsetI32 then
- internalerror(2024010602);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- writeUInt32LE(UInt32((objsym.offset+objsym.objsection.MemPos-objsym.objsection.ExeSection.MemPos)+objreloc.Addend));
- end;
- end;
- AT_WASM_GLOBAL:
- begin
- if objreloc.IsFunctionOffsetI32 then
- internalerror(2024010602);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- writeUInt32LE(UInt32(objsym.offset+objsym.objsection.MemPos));
- end;
- end;
- else
- internalerror(2024010108);
- end;
- end;
- RELOC_MEMORY_ADDR_LEB:
- begin
- if objsym.typ<>AT_DATA then
- internalerror(2024010109);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteUleb5(objsec.Data,UInt32((objsym.offset+objsym.objsection.MemPos)+objreloc.Addend));
- end;
- end;
- RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
- begin
- case objsym.typ of
- AT_FUNCTION:
- begin
- if objsym.LinkingData.ExeFunctionIndex=-1 then
- internalerror(2024010103);
- case FRelocationPass of
- 1:
- if objsym.LinkingData.ExeIndirectFunctionTableIndex=-1 then
- objsym.LinkingData.ExeIndirectFunctionTableIndex:=AddOrGetIndirectFunctionTableIndex(objsym.LinkingData.ExeFunctionIndex);
- 2:
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteSleb5(objsec.Data,Int32(objsym.LinkingData.ExeIndirectFunctionTableIndex));
- end;
- end;
- end;
- AT_DATA:
- begin
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteSleb5(objsec.Data,Int32((objsym.offset+objsym.objsection.MemPos)+objreloc.Addend));
- end;
- end;
- else
- internalerror(2024010110);
- end;
- end;
- RELOC_GLOBAL_INDEX_LEB:
- if objsym.typ=AT_WASM_GLOBAL then
- begin
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteUleb5(objsec.Data,UInt32(objsym.offset+objsym.objsection.MemPos));
- end;
- end
- else if (ts_wasm_threads in current_settings.targetswitches) and
- (objsym.typ=AT_TLS) then
- begin
- { Nothing to do here. A second RELOC_GLOBAL_INDEX_LEB
- relocation, overlaid on top of this one, pointing to
- an AT_WASM_GLOBAL should have already done the job. }
- end
- else
- internalerror(2024010111);
- RELOC_TAG_INDEX_LEB:
- begin
- if objsym.typ<>AT_WASM_EXCEPTION_TAG then
- internalerror(2024010708);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteUleb5(objsec.Data,UInt32(objsym.offset+objsym.objsection.MemPos));
- end;
- end;
- else
- internalerror(2024010109);
- end;
- end
- else if assigned(objreloc.objsection) then
- begin
- if objreloc.typ<>RELOC_ABSOLUTE then
- internalerror(2024010601);
- if FRelocationPass=2 then
- begin
- objsec.Data.seek(objreloc.DataOffset);
- writeUInt32LE(UInt32((objreloc.objsection.MemPos)+objreloc.Addend));
- end;
- end
- else if objreloc.typ=RELOC_TYPE_INDEX_LEB then
- begin
- case FRelocationPass of
- 1:
- objreloc.ExeTypeIndex:=FFuncTypes.AddOrGetFuncType(objreloc.FuncType);
- 2:
- begin
- objsec.Data.seek(objreloc.DataOffset);
- WriteUleb5(objsec.Data,objreloc.ExeTypeIndex);
- end;
- end;
- end
- else
- internalerror(2024010110);
- end;
- end;
- constructor TWasmExeOutput.create;
- var
- i: TWasmSectionID;
- j: TWasmCustomSectionType;
- k: TWasmNameSubsectionType;
- begin
- inherited create;
- CObjData:=TWasmObjData;
- SectionMemAlign:=16;
- MaxMemPos:=$FFFFFFFF;
- FFuncTypes:=TWasmFuncTypeTable.Create;
- for i in TWasmSectionID do
- FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
- for j in TWasmCustomSectionType do
- FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
- for k:=low(FWasmNameSubsections) to high(FWasmNameSubsections) do
- FWasmNameSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
- SetLength(FIndirectFunctionTable,1);
- FIndirectFunctionTable[0].FuncIdx:=-1;
- end;
- destructor TWasmExeOutput.destroy;
- var
- i: TWasmSectionID;
- j: TWasmCustomSectionType;
- k: TWasmNameSubsectionType;
- begin
- for i in TWasmSectionID do
- FWasmSections[i].Free;
- for j in TWasmCustomSectionType do
- FWasmCustomSections[j].Free;
- for k:=low(FWasmNameSubsections) to high(FWasmNameSubsections) do
- FWasmNameSubsections[k].Free;
- FFuncTypes.Free;
- inherited destroy;
- end;
- procedure TWasmExeOutput.GenerateLibraryImports(ImportLibraryList: TFPHashObjectList);
- var
- i, j: Integer;
- ImportLibrary: TImportLibrary;
- ImportSymbol: TImportSymbol;
- exesym: TExeSymbol;
- begin
- { Here map import symbols to exe symbols and create necessary sections.
- Actual import generation is done after unused sections (and symbols) are removed. }
- FImports:=ImportLibraryList;
- for i:=0 to ImportLibraryList.Count-1 do
- begin
- ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
- for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
- begin
- ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
- exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
- if assigned(exesym) and
- (exesym.State<>symstate_defined) then
- begin
- ImportSymbol.CachedExeSymbol:=exesym;
- exesym.State:=symstate_defined;
- end;
- end;
- end;
- PackUnresolvedExeSymbols('after module imports');
- end;
- procedure TWasmExeOutput.AfterUnusedSectionRemoval;
- begin
- PrepareImports;
- PrepareFunctions;
- PrepareTags;
- if Assigned(exemap) then
- WriteMap_TypeSection;
- { we do an extra preliminary relocation pass, in order to prepare the
- indices for the Type section and the Table section. This is required
- by GenerateCode_InvokeHelper. }
- FRelocationPass:=1;
- FixupRelocations;
- { in pass 2, we do the actual relocation fixups. No need to call
- FixupRelocations here, since it'll be called in
- TInternalLinker.RunLinkScript, after this method finishes. We only
- set the FRelocationPass variable here, so DoRelocationFixup knows
- which pass it is. }
- FRelocationPass:=2;
- { This needs to be done before pass 2 of the relocation fixups, because
- it'll generate code, thus it'll move the offsets of the functions that
- follow it in the Code section, and we want our DWARF debug info to
- contain correct code offsets. }
- GenerateCode_InvokeHelper;
- if Assigned(exemap) then
- WriteMap_IndirectFunctionTable;
- end;
- procedure TWasmExeOutput.MemPos_ExeSection(const aname: string);
- const
- DebugPrefix = '.debug_';
- var
- ExeSec: TExeSection;
- i: Integer;
- objsec: TObjSection;
- firstdatasec: string;
- begin
- { WebAssembly is a Harvard architecture.
- Data lives in a separate address space, so start addressing back from 0
- (the LLVM leaves the first 1024 bytes in the data segment empty, so we
- start at 1024). }
- if ts_wasm_threads in current_settings.targetswitches then
- firstdatasec:='.tbss'
- else
- firstdatasec:='.rodata';
- if aname=firstdatasec then
- begin
- CurrMemPos:=1024;
- inherited;
- end
- else if aname='.text' then
- begin
- CurrMemPos:=0;
- ExeSec:=FindExeSection(aname);
- if not assigned(ExeSec) then
- exit;
- exesec.MemPos:=CurrMemPos;
- CurrMemPos:=CurrMemPos+UlebEncodingSize(exesec.ObjSectionList.Count);
- { set position of object ObjSections }
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TObjSection(exesec.ObjSectionList[i]);
- CurrMemPos:=CurrMemPos+UlebEncodingSize(objsec.Size);
- CurrMemPos:=objsec.setmempos(CurrMemPos);
- end;
- { calculate size of the section }
- exesec.Size:=CurrMemPos-exesec.MemPos;
- end
- else if (aname='.wasm_globals') or (aname='.wasm_tags') or
- (Copy(aname,1,Length(DebugPrefix))=DebugPrefix) then
- begin
- CurrMemPos:=0;
- inherited;
- end
- else
- inherited;
- end;
- procedure TWasmExeOutput.Load_Symbol(const aname: string);
- begin
- if aname=StackPointerSymStr then
- begin
- internalObjData.createsection('*'+aname,1,[oso_Data,oso_load]);
- FStackPointerSym:=TWasmObjSymbol(internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_WASM_GLOBAL));
- FStackPointerSym.size:=1;
- FStackPointerSym.ObjSection.WriteZeros(1);
- TWasmObjSection(FStackPointerSym.ObjSection).MainFuncSymbol:=FStackPointerSym;
- FStackPointerSym.LinkingData.GlobalType:=wbt_i32;
- FStackPointerSym.LinkingData.GlobalIsMutable:=True;
- FStackPointerSym.LinkingData.GlobalInitializer.typ:=wbt_i32;
- FStackPointerSym.LinkingData.GlobalInitializer.init_i32:=0;
- end
- else if (ts_wasm_threads in current_settings.targetswitches) and (aname='__tls_base') then
- begin
- internalObjData.createsection('*'+aname,1,[oso_Data,oso_load]);
- FTlsBaseSym:=TWasmObjSymbol(internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_WASM_GLOBAL));
- FTlsBaseSym.size:=1;
- FTlsBaseSym.ObjSection.WriteZeros(1);
- TWasmObjSection(FTlsBaseSym.ObjSection).MainFuncSymbol:=FTlsBaseSym;
- FTlsBaseSym.LinkingData.GlobalType:=wbt_i32;
- FTlsBaseSym.LinkingData.GlobalIsMutable:=True;
- FTlsBaseSym.LinkingData.GlobalInitializer.typ:=wbt_i32;
- FTlsBaseSym.LinkingData.GlobalInitializer.init_i32:=0;
- end
- else if (ts_wasm_threads in current_settings.targetswitches) and (aname='__tls_size') then
- begin
- internalObjData.createsection('*'+aname,1,[oso_Data,oso_load]);
- FTlsSizeSym:=TWasmObjSymbol(internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_WASM_GLOBAL));
- FTlsSizeSym.size:=1;
- FTlsSizeSym.ObjSection.WriteZeros(1);
- TWasmObjSection(FTlsSizeSym.ObjSection).MainFuncSymbol:=FTlsSizeSym;
- FTlsSizeSym.LinkingData.GlobalType:=wbt_i32;
- FTlsSizeSym.LinkingData.GlobalIsMutable:=False;
- FTlsSizeSym.LinkingData.GlobalInitializer.typ:=wbt_i32;
- FTlsSizeSym.LinkingData.GlobalInitializer.init_i32:=0;
- end
- else if (ts_wasm_threads in current_settings.targetswitches) and (aname='__tls_align') then
- begin
- internalObjData.createsection('*'+aname,1,[oso_Data,oso_load]);
- FTlsAlignSym:=TWasmObjSymbol(internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_WASM_GLOBAL));
- FTlsAlignSym.size:=1;
- FTlsAlignSym.ObjSection.WriteZeros(1);
- TWasmObjSection(FTlsAlignSym.ObjSection).MainFuncSymbol:=FTlsAlignSym;
- FTlsAlignSym.LinkingData.GlobalType:=wbt_i32;
- FTlsAlignSym.LinkingData.GlobalIsMutable:=False;
- FTlsAlignSym.LinkingData.GlobalInitializer.typ:=wbt_i32;
- FTlsAlignSym.LinkingData.GlobalInitializer.init_i32:=0;
- end
- else if (ts_wasm_threads in current_settings.targetswitches) and (aname='__wasm_init_tls') then
- begin
- internalObjData.createsection('*'+aname,0,[]);
- FInitTlsFunctionSym:=TWasmObjSymbol(internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION));
- TWasmObjSection(FInitTlsFunctionSym.ObjSection).MainFuncSymbol:=FInitTlsFunctionSym;
- FInitTlsFunctionSym.LinkingData.FuncType:=TWasmFuncType.Create([wbt_i32],[]);
- end
- else if (ts_wasm_threads in current_settings.targetswitches) and (aname='__fpc_wasm_init_shared_memory') then
- begin
- internalObjData.createsection('*'+aname,0,[]);
- FInitSharedMemoryFunctionSym:=TWasmObjSymbol(internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION));
- TWasmObjSection(FInitSharedMemoryFunctionSym.ObjSection).MainFuncSymbol:=FInitSharedMemoryFunctionSym;
- FInitSharedMemoryFunctionSym.ObjSection.SecOptions:=FInitSharedMemoryFunctionSym.ObjSection.SecOptions+[oso_keep];
- FInitSharedMemoryFunctionSym.LinkingData.FuncType:=TWasmFuncType.Create([],[]);
- end
- else
- inherited;
- end;
- procedure TWasmExeOutput.PrepareImports;
- function AddFunctionImport(const libname,symname:TCmdStr; functype: TWasmFuncType): Integer;
- begin
- SetLength(FFunctionImports,Length(FFunctionImports)+1);
- Result:=High(FFunctionImports);
- if assigned(exemap) then
- exemap.Add(' Importing Function[' + tostr(Result) + '] ' + symname + functype.ToString);
- with FFunctionImports[Result] do
- begin
- ModName:=libname;
- Name:=symname;
- TypeIdx:=FFuncTypes.AddOrGetFuncType(functype);
- end;
- end;
- var
- i, j: Integer;
- ImportLibrary: TImportLibrary;
- ImportSymbol: TImportSymbol;
- exesym: TExeSymbol;
- newdll: Boolean;
- fsym: TWasmObjSymbol;
- objdata: TObjData;
- begin
- for i:=0 to FImports.Count-1 do
- begin
- ImportLibrary:=TImportLibrary(FImports[i]);
- newdll:=False;
- for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
- begin
- ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
- exesym:=ImportSymbol.CachedExeSymbol;
- if assigned(exesym) and
- exesym.Used then
- begin
- if (not newdll) and assigned(exemap) then
- begin
- exemap.Add('');
- exemap.Add('Importing from module '+ImportLibrary.Name);
- end;
- newdll:=True;
- TWasmObjSymbol(exesym.ObjSymbol).LinkingData.ExeFunctionIndex:=
- AddFunctionImport(ImportLibrary.Name,ImportSymbol.Name,TWasmObjSymbol(exesym.ObjSymbol).LinkingData.FuncType);
- AddToFunctionNameMap(TWasmObjSymbol(exesym.ObjSymbol).LinkingData.ExeFunctionIndex,ImportSymbol.MangledName);
- end;
- end;
- end;
- { set ExeFunctionIndex to the alias symbols as well }
- for i:=0 to ObjDataList.Count-1 do
- begin
- objdata:=TObjData(ObjDataList[i]);
- for j:=0 to objdata.ObjSymbolList.Count-1 do
- begin
- fsym:=TWasmObjSymbol(objdata.ObjSymbolList[j]);
- if (fsym.LinkingData.ExeFunctionIndex=-1) and assigned(fsym.exesymbol) and (TWasmObjSymbol(fsym.exesymbol.ObjSymbol).LinkingData.ExeFunctionIndex<>-1) then
- fsym.LinkingData.ExeFunctionIndex:=TWasmObjSymbol(fsym.exesymbol.ObjSymbol).LinkingData.ExeFunctionIndex;
- end;
- end;
- end;
- procedure TWasmExeOutput.PrepareFunctions;
- var
- i, j: Integer;
- exesec: TExeSection;
- objsec: TWasmObjSection;
- fsym: TWasmObjSymbol;
- objdata: TObjData;
- begin
- if assigned(exemap) then
- begin
- exemap.Add('');
- exemap.Add('Functions, defined in this module:');
- end;
- exesec:=FindExeSection('.text');
- if not assigned(exesec) then
- internalerror(2023123106);
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(exesec.ObjSectionList[i]);
- fsym:=objsec.MainFuncSymbol;
- if not assigned(fsym) then
- internalerror(2023123107);
- if not assigned(fsym.LinkingData.FuncType) then
- internalerror(2023123108);
- if fsym.LinkingData.ExeFunctionIndex<>-1 then
- internalerror(2023123109);
- if fsym.LinkingData.ExeTypeIndex<>-1 then
- internalerror(2023123109);
- fsym.LinkingData.ExeTypeIndex:=FFuncTypes.AddOrGetFuncType(fsym.LinkingData.FuncType);
- fsym.LinkingData.ExeFunctionIndex:=i+Length(FFunctionImports);
- if assigned(exemap) then
- begin
- exemap.Add(' Function[' + tostr(fsym.LinkingData.ExeFunctionIndex) + '] ' + fsym.Name + fsym.LinkingData.FuncType.ToString);
- end;
- AddToFunctionNameMap(fsym.LinkingData.ExeFunctionIndex,fsym.Name);
- end;
- { set ExeFunctionIndex to the alias symbols as well }
- for i:=0 to ObjDataList.Count-1 do
- begin
- objdata:=TObjData(ObjDataList[i]);
- for j:=0 to objdata.ObjSymbolList.Count-1 do
- begin
- fsym:=TWasmObjSymbol(objdata.ObjSymbolList[j]);
- if assigned(fsym.objsection) and fsym.objsection.USed and (fsym.typ=AT_FUNCTION) and (fsym.LinkingData.ExeFunctionIndex=-1) then
- begin
- fsym.LinkingData.ExeFunctionIndex:=TWasmObjSection(fsym.objsection).MainFuncSymbol.LinkingData.ExeFunctionIndex;
- if fsym.LinkingData.ExeFunctionIndex=-1 then
- internalerror(2024010102);
- end;
- end;
- end;
- end;
- procedure TWasmExeOutput.PrepareTags;
- var
- exesec: TExeSection;
- i, j: Integer;
- objsec: TWasmObjSection;
- fsym: TWasmObjSymbol;
- objdata: TObjData;
- begin
- exesec:=FindExeSection('.wasm_tags');
- if not assigned(exesec) then
- exit;
- if assigned(exemap) then
- begin
- exemap.Add('');
- exemap.Add('Tags, defined in this module:');
- end;
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(exesec.ObjSectionList[i]);
- fsym:=objsec.MainFuncSymbol;
- if not assigned(fsym) then
- internalerror(2024010703);
- if not assigned(fsym.LinkingData.FuncType) then
- internalerror(2024010704);
- if fsym.LinkingData.ExeTagIndex<>-1 then
- internalerror(2024010705);
- if fsym.LinkingData.ExeTypeIndex<>-1 then
- internalerror(2024010706);
- fsym.LinkingData.ExeTypeIndex:=FFuncTypes.AddOrGetFuncType(fsym.LinkingData.FuncType);
- fsym.LinkingData.ExeTagIndex:=i+Length(FTagImports);
- if assigned(exemap) then
- begin
- exemap.Add(' Tag[' + tostr(fsym.LinkingData.ExeTagIndex) + '] ' + fsym.Name + fsym.LinkingData.FuncType.ToString);
- end;
- end;
- { set ExeTagIndex to the alias symbols as well }
- for i:=0 to ObjDataList.Count-1 do
- begin
- objdata:=TObjData(ObjDataList[i]);
- for j:=0 to objdata.ObjSymbolList.Count-1 do
- begin
- fsym:=TWasmObjSymbol(objdata.ObjSymbolList[j]);
- if assigned(fsym.objsection) and fsym.objsection.USed and (fsym.typ=AT_WASM_EXCEPTION_TAG) and (fsym.LinkingData.ExeTagIndex=-1) then
- begin
- fsym.LinkingData.ExeTagIndex:=TWasmObjSection(fsym.objsection).MainFuncSymbol.LinkingData.ExeTagIndex;
- if fsym.LinkingData.ExeTagIndex=-1 then
- internalerror(2024010707);
- end;
- end;
- end;
- end;
- function TWasmExeOutput.AddOrGetIndirectFunctionTableIndex(FuncIdx: Integer): integer;
- var
- i: Integer;
- begin
- for i:=1 to length(FIndirectFunctionTable)-1 do
- if FIndirectFunctionTable[i].FuncIdx=FuncIdx then
- begin
- Result:=i;
- exit;
- end;
- SetLength(FIndirectFunctionTable,Length(FIndirectFunctionTable)+1);
- Result:=High(FIndirectFunctionTable);
- FIndirectFunctionTable[Result].FuncIdx:=FuncIdx;
- end;
- procedure TWasmExeOutput.SetStackPointer;
- var
- BssSec: TExeSection;
- StackStart, InitialStackPtrAddr: QWord;
- begin
- BssSec:=FindExeSection('.bss');
- InitialStackPtrAddr := (BssSec.MemPos+BssSec.Size+stacksize+15) and (not 15);
- FMinMemoryPages := Max(
- QWord(Align(QWord(InitialStackPtrAddr),QWord(WasmPageSize)) div WasmPageSize),
- QWord(Align(QWord(heapsize),QWord(WasmPageSize)) div WasmPageSize));
- FStackPointerSym.LinkingData.GlobalInitializer.init_i32:=Int32(InitialStackPtrAddr);
- end;
- procedure TWasmExeOutput.SetTlsSizeAlignAndBase;
- var
- TBssSec: TExeSection;
- begin
- if not (ts_wasm_threads in current_settings.targetswitches) then
- exit;
- TBssSec:=FindExeSection('.tbss');
- FTlsSizeSym.LinkingData.GlobalInitializer.init_i32:=Int32(TBssSec.Size);
- FTlsAlignSym.LinkingData.GlobalInitializer.init_i32:=Int32(TBssSec.SecAlign);
- FTlsBaseSym.LinkingData.GlobalInitializer.init_i32:=Int32(TBssSec.MemPos);
- end;
- procedure TWasmExeOutput.SetThreadVarGlobalsInitValues;
- var
- exesec: TExeSection;
- i: Integer;
- objsec: TWasmObjSection;
- objsym: TWasmObjSymbol;
- begin
- if not (ts_wasm_threads in current_settings.targetswitches) then
- exit;
- exesec:=FindExeSection('.wasm_globals');
- if not assigned(exesec) then
- internalerror(2024010112);
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TWasmObjSection(exesec.ObjSectionList[i]);
- objsym:=objsec.MainFuncSymbol;
- if Assigned(objsym.TlsDataSym) then
- begin
- objsym.LinkingData.GlobalInitializer.typ:=wbt_i32;
- objsym.LinkingData.GlobalInitializer.init_i32:=objsym.TlsDataSym.offset+objsym.TlsDataSym.objsection.MemPos;
- end;
- end;
- end;
- procedure TWasmExeOutput.GenerateCode_InitTls;
- var
- Sec: TObjSection;
- globalexesec: TExeSection;
- i: Integer;
- globalobjsec: TWasmObjSection;
- globalobjsym: TWasmObjSymbol;
- OffsetInTls: QWord;
- begin
- if not (ts_wasm_threads in current_settings.targetswitches) then
- exit;
- globalexesec:=FindExeSection('.wasm_globals');
- if not assigned(globalexesec) then
- internalerror(2024010112);
- Sec:=FInitTlsFunctionSym.objsection;
- Sec.SecOptions:=Sec.SecOptions+[oso_Data];
- { locals }
- Sec.writeUInt8($00);
- { local.get 0 }
- Sec.writeUInt16BE($2000);
- { global.set $__tls_base }
- Sec.writeUInt8($24);
- WriteUleb(sec,FTlsBaseSym.offset+FTlsBaseSym.objsection.MemPos);
- for i:=0 to globalexesec.ObjSectionList.Count-1 do
- begin
- globalobjsec:=TWasmObjSection(globalexesec.ObjSectionList[i]);
- globalobjsym:=globalobjsec.MainFuncSymbol;
- if Assigned(globalobjsym.TlsDataSym) then
- begin
- OffsetInTls:=globalobjsym.TlsDataSym.offset+globalobjsym.TlsDataSym.objsection.MemPos-globalobjsym.TlsDataSym.objsection.ExeSection.MemPos;
- { local.get 0 }
- Sec.writeUInt16BE($2000);
- if OffsetInTls<>0 then
- begin
- { i32.const $OffsetInTls }
- Sec.writeUInt8($41);
- WriteSleb(Sec,Int32(OffsetInTls));
- { i32.add }
- Sec.writeUInt8($6A);
- end;
- { global.set y }
- Sec.writeUInt8($24);
- WriteUleb(sec,globalobjsym.offset+globalobjsym.objsection.MemPos);
- end;
- end;
- Sec.writeUInt8($0B); { end }
- end;
- procedure TWasmExeOutput.GenerateCode_InitSharedMemory;
- const
- InitFlagOfs=256;
- var
- Sec: TObjSection;
- DataSecName: string;
- DataSecIdx: Integer;
- ExeSec: TExeSection;
- begin
- if not (ts_wasm_threads in current_settings.targetswitches) then
- exit;
- Sec:=FInitSharedMemoryFunctionSym.objsection;
- Sec.SecOptions:=Sec.SecOptions+[oso_Data];
- { locals }
- Sec.writeUInt8($00);
- { block }
- Sec.writeUInt16BE($0240);
- { block }
- Sec.writeUInt16BE($0240);
- { block }
- Sec.writeUInt16BE($0240);
- { i32.const $InitFlag }
- Sec.writeUInt8($41);
- WriteSleb(sec,InitFlagOfs);
- { i32.const 0 }
- Sec.writeUInt16BE($4100);
- { i32.const 1 }
- Sec.writeUInt16BE($4101);
- { i32.atomic.rmw.cmpxchg 2 0 }
- Sec.writeUInt32BE($fe480200);
- { br_table 0 1 2 }
- Sec.writebytes(#$0e#$02#$00#$01#$02);
- { end }
- Sec.writeUInt8($0B);
- DataSecIdx:=-1;
- for DataSecName in DataSections do
- begin
- ExeSec:=FindExeSection(DataSecName);
- if Assigned(ExeSec) and (ExeSec.Size>0) then
- begin
- Inc(DataSecIdx);
- { i32.const $memPos }
- Sec.writeUInt8($41);
- WriteSleb(sec,Int32(ExeSec.MemPos));
- { i32.const 0 }
- Sec.writeUInt16BE($4100);
- { i32.const size }
- Sec.writeUInt8($41);
- WriteSleb(sec,Int32(ExeSec.Size));
- { memory.init $DataSecIdx 0 }
- Sec.writeUInt16BE($fc08);
- WriteUleb(sec,DataSecIdx);
- Sec.writeUInt8(0);
- end;
- end;
- { i32.const $InitFlag }
- Sec.writeUInt8($41);
- WriteSleb(sec,InitFlagOfs);
- { i32.const 2 }
- Sec.writeUInt16BE($4102);
- { i32.atomic.store 2 0 }
- Sec.writeUInt32BE($fe170200);
- { i32.const $InitFlag }
- Sec.writeUInt8($41);
- WriteSleb(sec,InitFlagOfs);
- { i32.const 4294967295 }
- Sec.writeUInt16BE($417f);
- { memory.atomic.notify 2 0 }
- Sec.writeUInt32BE($fe000200);
- { drop }
- Sec.writeUInt8($1A);
- { br 1 }
- Sec.writeUInt16BE($0C01);
- { end }
- Sec.writeUInt8($0B);
- { i32.const $InitFlag }
- Sec.writeUInt8($41);
- WriteSleb(sec,InitFlagOfs);
- { i32.const 1 }
- Sec.writeUInt16BE($4101);
- { i64.const -1 }
- Sec.writeUInt16BE($427f);
- { memory.atomic.wait32 2 0 }
- Sec.writeUInt32BE($fe010200);
- { drop }
- Sec.writeUInt8($1A);
- { end }
- Sec.writeUInt8($0B);
- DataSecIdx:=-1;
- for DataSecName in DataSections do
- begin
- ExeSec:=FindExeSection(DataSecName);
- if Assigned(ExeSec) and (ExeSec.Size>0) then
- begin
- Inc(DataSecIdx);
- { data.drop $DataSecIdx }
- Sec.writeUInt16BE($fc09);
- WriteUleb(sec,DataSecIdx);
- end;
- end;
- { end }
- Sec.writeUInt8($0B);
- end;
- procedure TWasmExeOutput.GenerateCode_InvokeHelper;
- var
- Sec: TObjSection;
- IndirectFunctionTableMap: array of Integer;
- procedure InvokeFuncType(typidx: Integer; islast: Boolean);
- var
- ft: TWasmFuncType;
- i, nextofs: Integer;
- begin
- ft:=FFuncTypes[typidx];
- for i:=0 to Length(ft.results)-1 do
- { local.get 2 }
- Sec.writeUInt16BE($2002);
- nextofs:=0;
- for i:=0 to Length(ft.params)-1 do
- begin
- { local.get 1 }
- Sec.writeUInt16BE($2001);
- case ft.params[i] of
- wbt_i32:
- begin
- { i32.load nextofs }
- Sec.writeUInt16BE($2802);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,4);
- end;
- wbt_i64:
- begin
- { i64.load nextofs }
- Sec.writeUInt16BE($2902);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,8);
- end;
- wbt_f32:
- begin
- { f32.load nextofs }
- Sec.writeUInt16BE($2A02);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,4);
- end;
- wbt_f64:
- begin
- { f64.load nextofs }
- Sec.writeUInt16BE($2B02);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,8);
- end;
- wbt_v128:
- begin
- { v128.load nextofs }
- Sec.writeUInt16BE($FD00);
- Sec.writeUInt8($02); { align: 4 bytes }
- WriteUleb(Sec, nextofs);
- Inc(nextofs,16);
- end;
- wbt_externref,
- wbt_funcref:
- begin
- { unreachable }
- Sec.writeUInt8($00);
- end;
- else
- internalerror(2025012501);
- end;
- end;
- { local.get 0 }
- Sec.writeUInt16BE($2000);
- { call_indirect }
- Sec.writeUInt8($11);
- WriteUleb(Sec,typidx);
- Sec.writeUInt8($0); { table index 0 }
- nextofs:=0;
- for i:=0 to Length(ft.results)-1 do
- begin
- case ft.results[i] of
- wbt_i32:
- begin
- { i32.store nextofs }
- Sec.writeUInt16BE($3602);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,4);
- end;
- wbt_i64:
- begin
- { i64.store nextofs }
- Sec.writeUInt16BE($3702);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,8);
- end;
- wbt_f32:
- begin
- { f32.store nextofs }
- Sec.writeUInt16BE($3802);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,4);
- end;
- wbt_f64:
- begin
- { f64.store nextofs }
- Sec.writeUInt16BE($3902);
- WriteUleb(Sec, nextofs);
- Inc(nextofs,8);
- end;
- wbt_v128:
- begin
- { v128.store nextofs }
- Sec.writeUInt16BE($FD0B);
- Sec.writeUInt8($02); { align: 4 bytes }
- WriteUleb(Sec, nextofs);
- Inc(nextofs,16);
- end;
- wbt_externref,
- wbt_funcref:
- begin
- { unreachable }
- Sec.writeUInt8($00);
- end;
- else
- internalerror(2025012501);
- end;
- end;
- if not islast then
- { return }
- Sec.writeUInt8($0F);
- end;
- function FuncIdx2TypeIdx(fi: Integer): Integer;
- var
- exesec: TExeSection;
- objsec: TWasmObjSection;
- fsym: TWasmObjSymbol;
- begin
- if fi<Length(FFunctionImports) then
- Result:=FFunctionImports[fi].TypeIdx
- else
- begin
- exesec:=FindExeSection('.text');
- if not assigned(exesec) then
- internalerror(2023123106);
- objsec:=TWasmObjSection(exesec.ObjSectionList[fi-Length(FFunctionImports)]);
- fsym:=objsec.MainFuncSymbol;
- Result:=fsym.LinkingData.ExeTypeIndex;
- end;
- end;
- procedure WriteBrTable(l,h: Integer; addend: Integer=0);
- const
- { max len of br_table instruction }
- MaxLen=1000;
- var
- i, len, m: Integer;
- begin
- { local.get 0 }
- Sec.writeUInt16BE($2000);
- len:=h-l+1;
- if len<=MaxLen then
- begin
- if l>0 then
- begin
- { i32.const l }
- Sec.writeUInt8($41);
- WriteSleb(sec,l);
- { i32.sub }
- Sec.writeUInt8($6B);
- end;
- { br_table }
- Sec.writeUInt8($0E);
- if h=high(IndirectFunctionTableMap) then
- begin
- WriteUleb(Sec,len);
- for i:=l to h do
- WriteUleb(Sec,IndirectFunctionTableMap[i]+addend);
- WriteUleb(Sec,addend);
- end
- else
- begin
- WriteUleb(Sec,len-1);
- for i:=l to h do
- WriteUleb(Sec,IndirectFunctionTableMap[i]+addend);
- end;
- end
- else
- begin
- m:=(l+h) div 2;
- { i32.const m }
- Sec.writeUInt8($41);
- WriteSleb(sec,m);
- { i32.lt_u }
- Sec.writeUInt8($49);
- { if }
- Sec.writeUInt16BE($0440);
- WriteBrTable(l,m-1,addend+1);
- { else }
- Sec.writeUInt8($05);
- WriteBrTable(m,h,addend+1);
- { end }
- Sec.writeUInt8($0B);
- end;
- end;
- var
- exesym: TExeSymbol;
- objsym: TObjSymbol;
- i, j, TypIdx: Integer;
- InvokableTypeIndices: array of Integer;
- begin
- exesym:=TExeSymbol(ExeSymbolList.Find('fpc_wasm_invoke_helper'));
- if not Assigned(exesym) then
- exit;
- SetLength(IndirectFunctionTableMap, Length(FIndirectFunctionTable));
- SetLength(InvokableTypeIndices, 1);
- InvokableTypeIndices[0] := -1;
- for i:=1 to Length(FIndirectFunctionTable)-1 do
- begin
- IndirectFunctionTableMap[i]:=0;
- TypIdx := FuncIdx2TypeIdx(FIndirectFunctionTable[i].FuncIdx);
- for j := 1 to Length(InvokableTypeIndices)-1 do
- if InvokableTypeIndices[j]=TypIdx then
- begin
- IndirectFunctionTableMap[i]:=j;
- break;
- end;
- if IndirectFunctionTableMap[i]=0 then
- begin
- SetLength(InvokableTypeIndices,Length(InvokableTypeIndices)+1);
- InvokableTypeIndices[High(InvokableTypeIndices)]:=TypIdx;
- IndirectFunctionTableMap[i]:=High(InvokableTypeIndices);
- end;
- end;
- objsym:=exesym.ObjSymbol;
- Sec:=objsym.objsection;
- Sec.Size:=0;
- Sec.Data.reset;
- { locals }
- Sec.writeUInt8($00);
- for i:=1 to Length(InvokableTypeIndices)-1 do
- { block }
- Sec.writeUInt16BE($0240);
- { block }
- Sec.writeUInt16BE($0240);
- { local.get 0 + br_table }
- WriteBrTable(low(IndirectFunctionTableMap),high(IndirectFunctionTableMap));
- { end }
- Sec.writeUInt8($0B);
- { unreachable }
- Sec.writeUInt8($00);
- for i:=1 to Length(InvokableTypeIndices)-1 do
- begin
- { end }
- Sec.writeUInt8($0B);
- InvokeFuncType(InvokableTypeIndices[i],i=(Length(InvokableTypeIndices)-1));
- end;
- { end }
- Sec.writeUInt8($0B);
- end;
- procedure TWasmExeOutput.WriteExeSectionToDynArray(exesec: TExeSection; dynarr: tdynamicarray);
- var
- exesecdatapos: LongWord;
- i: Integer;
- objsec: TObjSection;
- dpos, pad: QWord;
- begin
- exesecdatapos:=dynarr.size;
- for i:=0 to exesec.ObjSectionList.Count-1 do
- begin
- objsec:=TObjSection(exesec.ObjSectionList[i]);
- if not (oso_data in objsec.secoptions) then
- internalerror(2024010104);
- if not assigned(objsec.data) then
- internalerror(2024010105);
- dpos:=objsec.MemPos-exesec.MemPos+exesecdatapos;
- pad:=dpos-dynarr.size;
- { objsection must be within SecAlign bytes from the previous one }
- if (dpos<dynarr.Size) or
- (pad>=max(objsec.SecAlign,1)) then
- internalerror(2024010106);
- writeZeros(dynarr,pad);
- objsec.data.seek(0);
- CopyDynamicArray(objsec.data,dynarr,objsec.data.size);
- end;
- if (dynarr.size-exesecdatapos)<>exesec.Size then
- internalerror(2024010107);
- end;
- procedure TWasmExeOutput.WriteMemoryTo(dest: tdynamicarray; const MemType: TWasmMemoryType);
- begin
- WriteByte(dest,Byte(MemType.Flags));
- WriteUleb(dest,MemType.MinPages);
- if wmfHasMaximumBound in MemType.Flags then
- WriteUleb(dest,MemType.MaxPages);
- { todo: wmfCustomPageSize }
- end;
- function TWasmExeOutput.Memory2String(const MemType: TWasmMemoryType): string;
- begin
- Result:='index type: ';
- if wmfMemory64 in MemType.Flags then
- Result:=Result+'i64'
- else
- Result:=Result+'i32';
- Result:=Result+', pages: initial='+tostr(MemType.MinPages);
- if wmfHasMaximumBound in MemType.Flags then
- Result:=Result+' max='+tostr(MemType.MaxPages);
- if wmfShared in MemType.Flags then
- Result:=Result+', shared'
- else
- Result:=Result+', unshared';
- { todo: wmfCustomPageSize }
- end;
- procedure TWasmExeOutput.WriteMap_TypeSection;
- var
- i: Integer;
- begin
- exemap.AddHeader('Type section');
- for i:=0 to FFuncTypes.Count-1 do
- exemap.Add(' Type[' + tostr(i) + '] ' + FFuncTypes.Items[i].ToString);
- end;
- procedure TWasmExeOutput.WriteMap_IndirectFunctionTable;
- var
- i: Integer;
- begin
- exemap.AddHeader('Indirect function table');
- for i:=1 to High(FIndirectFunctionTable) do
- exemap.Add(' Elem[' + tostr(i) + '] = Function[' + tostr(FIndirectFunctionTable[i].FuncIdx) + ']');
- end;
- {****************************************************************************
- TWasmAssembler
- ****************************************************************************}
- constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
- begin
- inherited;
- CObjOutput:=TWasmObjOutput;
- end;
- {*****************************************************************************
- Initialize
- *****************************************************************************}
- {$ifdef wasm32}
- const
- as_wasm32_wasm_info : tasminfo =
- (
- id : as_wasm32_wasm;
- idtxt : 'WASM';
- asmbin : '';
- asmcmd : '';
- supported_targets : [system_wasm32_embedded,system_wasm32_wasip1,system_wasm32_wasip1threads,
- system_wasm32_wasip2];
- flags : [af_outputbinary,af_smartlink_sections];
- labelprefix : '..@';
- labelmaxlen : -1;
- comment : '; ';
- dollarsign: '$';
- );
- {$endif wasm32}
- initialization
- {$ifdef wasm32}
- RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
- {$endif wasm32}
- finalization
- end.
|