ogwasm.pas 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002
  1. {
  2. Copyright (c) 2021 by Nikolay Nikolov
  3. Contains the WebAssembly binary module format reader and writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogwasm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,cpubase,
  25. { assembler }
  26. aasmbase,assemble,aasmcpu,
  27. { WebAssembly module format definitions }
  28. wasmbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. TWasmObjSymbolExtraData = class;
  34. { TWasmObjSymbol }
  35. TWasmObjSymbol = class(TObjSymbol)
  36. FuncIndex: Integer;
  37. SymbolIndex: Integer;
  38. GlobalIndex: Integer;
  39. TagIndex: Integer;
  40. AliasOf: string;
  41. ExtraData: TWasmObjSymbolExtraData;
  42. constructor create(AList:TFPHashObjectList;const AName:string);override;
  43. function IsAlias: Boolean;
  44. end;
  45. { TWasmObjRelocation }
  46. TWasmObjRelocation = class(TObjRelocation)
  47. public
  48. TypeIndex: Integer;
  49. Addend: LongInt;
  50. constructor CreateTypeIndex(ADataOffset:TObjSectionOfs; ATypeIndex: Integer);
  51. end;
  52. { TWasmObjSymbolExtraData }
  53. TWasmObjSymbolExtraData = class(TFPHashObject)
  54. TypeIdx: Integer;
  55. ExceptionTagTypeIdx: Integer;
  56. ImportModule: string;
  57. ImportName: string;
  58. ExportName: string;
  59. GlobalType: TWasmBasicType;
  60. GlobalIsImmutable: Boolean;
  61. Locals: array of TWasmBasicType;
  62. constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  63. procedure AddLocal(bastyp: TWasmBasicType);
  64. end;
  65. { TWasmObjSection }
  66. TWasmObjSection = class(TObjSection)
  67. public
  68. SegIdx: Integer;
  69. SegSymIdx: Integer;
  70. SegOfs: qword;
  71. FileSectionOfs: qword;
  72. MainFuncSymbol: TWasmObjSymbol;
  73. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);override;
  74. function IsCode: Boolean;
  75. function IsData: Boolean;
  76. function IsDebug: Boolean;
  77. end;
  78. { TWasmObjData }
  79. TWasmObjData = class(TObjData)
  80. private
  81. FFuncTypes: array of TWasmFuncType;
  82. FObjSymbolsExtraDataList: TFPHashObjectList;
  83. FLastFuncName: string;
  84. function is_smart_section(atype:TAsmSectiontype):boolean;
  85. function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  86. public
  87. constructor create(const n:string);override;
  88. destructor destroy; override;
  89. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  90. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  91. function AddOrCreateObjSymbolExtraData(const symname:TSymStr): TWasmObjSymbolExtraData;
  92. function AddFuncType(wft: TWasmFuncType): integer;
  93. function globalref(asmsym:TAsmSymbol):TObjSymbol;
  94. function ExceptionTagRef(asmsym:TAsmSymbol):TObjSymbol;
  95. procedure DeclareGlobalType(gt: tai_globaltype);
  96. procedure DeclareFuncType(ft: tai_functype);
  97. procedure DeclareTagType(tt: tai_tagtype);
  98. procedure DeclareExportName(en: tai_export_name);
  99. procedure DeclareImportModule(aim: tai_import_module);
  100. procedure DeclareImportName(ain: tai_import_name);
  101. procedure DeclareLocal(al: tai_local);
  102. procedure symbolpairdefine(akind: TSymbolPairKind;const asym, avalue: string);override;
  103. end;
  104. { TWasmObjOutput }
  105. TWasmObjOutput = class(tObjOutput)
  106. private
  107. FData: TWasmObjData;
  108. FWasmRelocationCodeTable: tdynamicarray;
  109. FWasmRelocationCodeTableEntriesCount: Integer;
  110. FWasmRelocationDataTable: tdynamicarray;
  111. FWasmRelocationDataTableEntriesCount: Integer;
  112. FWasmRelocationDebugFrameTable: tdynamicarray;
  113. FWasmRelocationDebugFrameTableEntriesCount: Integer;
  114. FWasmRelocationDebugInfoTable: tdynamicarray;
  115. FWasmRelocationDebugInfoTableEntriesCount: Integer;
  116. FWasmRelocationDebugLineTable: tdynamicarray;
  117. FWasmRelocationDebugLineTableEntriesCount: Integer;
  118. FWasmRelocationDebugAbbrevTable: tdynamicarray;
  119. FWasmRelocationDebugAbbrevTableEntriesCount: Integer;
  120. FWasmRelocationDebugArangesTable: tdynamicarray;
  121. FWasmRelocationDebugArangesTableEntriesCount: Integer;
  122. FWasmRelocationDebugRangesTable: tdynamicarray;
  123. FWasmRelocationDebugRangesTableEntriesCount: Integer;
  124. FWasmRelocationDebugStrTable: tdynamicarray;
  125. FWasmRelocationDebugStrTableEntriesCount: Integer;
  126. FWasmSymbolTable: tdynamicarray;
  127. FWasmSymbolTableEntriesCount: Integer;
  128. FWasmSections: array [TWasmSectionID] of tdynamicarray;
  129. FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
  130. FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of tdynamicarray;
  131. procedure WriteUleb(d: tdynamicarray; v: uint64);
  132. procedure WriteUleb(w: TObjectWriter; v: uint64);
  133. procedure WriteSleb(d: tdynamicarray; v: int64);
  134. procedure WriteByte(d: tdynamicarray; b: byte);
  135. procedure WriteName(d: tdynamicarray; const s: string);
  136. procedure WriteWasmSection(wsid: TWasmSectionID);
  137. procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  138. procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  139. procedure WriteZeros(dest: tdynamicarray; size: QWord);
  140. procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  141. procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  142. function IsExternalFunction(sym: TObjSymbol): Boolean;
  143. function IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  144. procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  145. procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  146. procedure WriteSymbolTable;
  147. procedure WriteRelocationCodeTable(CodeSectionIndex: Integer);
  148. procedure WriteRelocationDataTable(DataSectionIndex: Integer);
  149. procedure MaybeWriteRelocationDebugTable(cst: TWasmCustomSectionType; SectionIndex: Integer; EntriesCount: Integer; Table: tdynamicarray);
  150. procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  151. procedure DoRelocations;
  152. procedure WriteRelocations;
  153. function FindFunctionSymbol(Symbol: TWasmObjSymbol): TWasmObjSymbol;
  154. protected
  155. function writeData(Data:TObjData):boolean;override;
  156. public
  157. constructor create(AWriter:TObjectWriter);override;
  158. destructor destroy;override;
  159. end;
  160. { TWasmObjInput }
  161. TWasmObjInput = class(TObjInput)
  162. private
  163. FFuncTypes: array of TWasmFuncType;
  164. public
  165. constructor create;override;
  166. destructor Destroy;override;
  167. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  168. function ReadObjData(AReader:TObjectreader;out ObjData:TObjData):boolean;override;
  169. end;
  170. { TWasmExeOutput }
  171. TWasmExeOutput = class(TExeOutput)
  172. protected
  173. function writeData:boolean;override;
  174. procedure DoRelocationFixup(objsec:TObjSection);override;
  175. public
  176. constructor create;override;
  177. end;
  178. { TWasmAssembler }
  179. TWasmAssembler = class(tinternalassembler)
  180. constructor create(info: pasminfo; smart:boolean);override;
  181. end;
  182. implementation
  183. uses
  184. cutils,verbose,version,globals;
  185. procedure WriteUleb5(d: tdynamicarray; v: uint64);
  186. var
  187. b: byte;
  188. i: Integer;
  189. begin
  190. for i:=1 to 5 do
  191. begin
  192. b:=byte(v) and 127;
  193. v:=v shr 7;
  194. if i<>5 then
  195. b:=b or 128;
  196. d.write(b,1);
  197. end;
  198. end;
  199. procedure WriteUleb5(d: tobjsection; v: uint64);
  200. var
  201. b: byte;
  202. i: Integer;
  203. begin
  204. for i:=1 to 5 do
  205. begin
  206. b:=byte(v) and 127;
  207. v:=v shr 7;
  208. if i<>5 then
  209. b:=b or 128;
  210. d.write(b,1);
  211. end;
  212. end;
  213. procedure WriteSleb5(d: tdynamicarray; v: int64);
  214. var
  215. b: byte;
  216. i: Integer;
  217. begin
  218. for i:=1 to 5 do
  219. begin
  220. b:=byte(v) and 127;
  221. v:=SarInt64(v,7);
  222. if i<>5 then
  223. b:=b or 128;
  224. d.write(b,1);
  225. end;
  226. end;
  227. procedure WriteSleb5(d: tobjsection; v: int64);
  228. var
  229. b: byte;
  230. i: Integer;
  231. begin
  232. for i:=1 to 5 do
  233. begin
  234. b:=byte(v) and 127;
  235. v:=SarInt64(v,7);
  236. if i<>5 then
  237. b:=b or 128;
  238. d.write(b,1);
  239. end;
  240. end;
  241. function ReadUleb(d: tdynamicarray): uint64;
  242. var
  243. b: byte;
  244. shift:integer;
  245. begin
  246. b:=0;
  247. result:=0;
  248. shift:=0;
  249. repeat
  250. d.read(b,1);
  251. result:=result or (uint64(b and 127) shl shift);
  252. inc(shift,7);
  253. until (b and 128)=0;
  254. end;
  255. function ReadSleb(d: tdynamicarray): int64;
  256. var
  257. b: byte;
  258. shift:integer;
  259. begin
  260. b:=0;
  261. result:=0;
  262. shift:=0;
  263. repeat
  264. d.read(b,1);
  265. result:=result or (uint64(b and 127) shl shift);
  266. inc(shift,7);
  267. until (b and 128)=0;
  268. {$ifopt Q+}
  269. {$define overflowon}
  270. {$Q-}
  271. {$endif}
  272. {$ifopt R+}
  273. {$define rangeon}
  274. {$R-}
  275. {$endif}
  276. if (b and 64)<>0 then
  277. result:=result or (high(uint64) shl shift);
  278. end;
  279. {$ifdef overflowon}
  280. {$Q+}
  281. {$undef overflowon}
  282. {$endif}
  283. {$ifdef rangeon}
  284. {$R+}
  285. {$undef rangeon}
  286. {$endif}
  287. procedure AddSleb5(d: tdynamicarray; v: int64);
  288. var
  289. q: Int64;
  290. p: LongWord;
  291. begin
  292. p:=d.Pos;
  293. q:=ReadSleb(d);
  294. q:=q+v;
  295. d.seek(p);
  296. WriteSleb5(d,q);
  297. end;
  298. procedure AddUleb5(d: tdynamicarray; v: int64);
  299. var
  300. q: UInt64;
  301. p: LongWord;
  302. begin
  303. p:=d.Pos;
  304. q:=ReadUleb(d);
  305. q:=q+v;
  306. d.seek(p);
  307. WriteUleb5(d,q);
  308. end;
  309. procedure AddInt32(d: tdynamicarray; v: int32);
  310. var
  311. q: int32;
  312. p: LongWord;
  313. begin
  314. p:=d.Pos;
  315. d.read(q,4);
  316. {$ifdef FPC_BIG_ENDIAN}
  317. q:=SwapEndian(q);
  318. {$endif FPC_BIG_ENDIAN}
  319. q:=q+v;
  320. {$ifdef FPC_BIG_ENDIAN}
  321. q:=SwapEndian(q);
  322. {$endif FPC_BIG_ENDIAN}
  323. d.seek(p);
  324. d.write(q,4);
  325. end;
  326. {****************************************************************************
  327. TWasmObjRelocation
  328. ****************************************************************************}
  329. constructor TWasmObjRelocation.CreateTypeIndex(ADataOffset: TObjSectionOfs; ATypeIndex: Integer);
  330. begin
  331. DataOffset:=ADataOffset;
  332. Symbol:=nil;
  333. OrgSize:=0;
  334. Group:=nil;
  335. ObjSection:=nil;
  336. ftype:=ord(RELOC_TYPE_INDEX_LEB);
  337. TypeIndex:=ATypeIndex;
  338. end;
  339. {****************************************************************************
  340. TWasmObjSymbol
  341. ****************************************************************************}
  342. constructor TWasmObjSymbol.create(AList: TFPHashObjectList; const AName: string);
  343. begin
  344. inherited create(AList,AName);
  345. FuncIndex:=-1;
  346. SymbolIndex:=-1;
  347. GlobalIndex:=-1;
  348. TagIndex:=-1;
  349. AliasOf:='';
  350. ExtraData:=nil;
  351. end;
  352. function TWasmObjSymbol.IsAlias: Boolean;
  353. begin
  354. result:=AliasOf<>'';
  355. end;
  356. {****************************************************************************
  357. TWasmObjSymbolExtraData
  358. ****************************************************************************}
  359. constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  360. begin
  361. inherited Create(HashObjectList,s);
  362. TypeIdx:=-1;
  363. ExceptionTagTypeIdx:=-1;
  364. end;
  365. procedure TWasmObjSymbolExtraData.AddLocal(bastyp: TWasmBasicType);
  366. begin
  367. SetLength(Locals,Length(Locals)+1);
  368. Locals[High(Locals)]:=bastyp;
  369. end;
  370. {****************************************************************************
  371. TWasmObjSection
  372. ****************************************************************************}
  373. constructor TWasmObjSection.create(AList: TFPHashObjectList; const Aname: string; Aalign: longint; Aoptions: TObjSectionOptions);
  374. begin
  375. inherited create(AList, Aname, Aalign, Aoptions);
  376. SegIdx:=-1;
  377. SegSymIdx:=-1;
  378. MainFuncSymbol:=nil;
  379. end;
  380. function TWasmObjSection.IsCode: Boolean;
  381. const
  382. CodePrefix = '.text';
  383. begin
  384. result:=(Length(Name)>=Length(CodePrefix)) and
  385. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  386. end;
  387. function TWasmObjSection.IsData: Boolean;
  388. begin
  389. result:=not (IsCode or IsDebug);
  390. end;
  391. function TWasmObjSection.IsDebug: Boolean;
  392. const
  393. DebugPrefix = '.debug';
  394. begin
  395. result:=(Length(Name)>=Length(DebugPrefix)) and
  396. (Copy(Name,1,Length(DebugPrefix))=DebugPrefix);
  397. end;
  398. {****************************************************************************
  399. TWasmObjData
  400. ****************************************************************************}
  401. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  402. begin
  403. { For bss we need to set some flags that are target dependent,
  404. it is easier to disable it for smartlinking. It doesn't take up
  405. filespace }
  406. result:=not(target_info.system in systems_darwin) and
  407. create_smartlink_sections and
  408. (atype<>sec_toc) and
  409. (atype<>sec_user) and
  410. { on embedded systems every byte counts, so smartlink bss too }
  411. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  412. end;
  413. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  414. const aname: string; aorder: TAsmSectionOrder): string;
  415. const
  416. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  417. '.text',
  418. '.data',
  419. { why doesn't .rodata work? (FK) }
  420. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  421. { vtables (and anything else containing relocations), otherwise those are }
  422. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  423. { vtable for a class called Window: }
  424. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  425. { TODO: .data.ro not yet working}
  426. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  427. '.rodata',
  428. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  429. '.data',
  430. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  431. '.rodata',
  432. '.bss',
  433. '.tbss',
  434. '.pdata',
  435. '', { stubs }
  436. '__DATA,__nl_symbol_ptr',
  437. '__DATA,__la_symbol_ptr',
  438. '__DATA,__mod_init_func',
  439. '__DATA,__mod_term_func',
  440. '.stab',
  441. '.stabstr',
  442. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  443. '.eh_frame',
  444. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  445. '.fpc',
  446. '.toc',
  447. '.init',
  448. '.fini',
  449. '.objc_class',
  450. '.objc_meta_class',
  451. '.objc_cat_cls_meth',
  452. '.objc_cat_inst_meth',
  453. '.objc_protocol',
  454. '.objc_string_object',
  455. '.objc_cls_meth',
  456. '.objc_inst_meth',
  457. '.objc_cls_refs',
  458. '.objc_message_refs',
  459. '.objc_symbols',
  460. '.objc_category',
  461. '.objc_class_vars',
  462. '.objc_instance_vars',
  463. '.objc_module_info',
  464. '.objc_class_names',
  465. '.objc_meth_var_types',
  466. '.objc_meth_var_names',
  467. '.objc_selector_strs',
  468. '.objc_protocol_ext',
  469. '.objc_class_ext',
  470. '.objc_property',
  471. '.objc_image_info',
  472. '.objc_cstring_object',
  473. '.objc_sel_fixup',
  474. '__DATA,__objc_data',
  475. '__DATA,__objc_const',
  476. '.objc_superrefs',
  477. '__DATA, __datacoal_nt,coalesced',
  478. '.objc_classlist',
  479. '.objc_nlclasslist',
  480. '.objc_catlist',
  481. '.obcj_nlcatlist',
  482. '.objc_protolist',
  483. '.stack',
  484. '.heap',
  485. '.gcc_except_table',
  486. '.ARM.attributes'
  487. );
  488. var
  489. sep : string[3];
  490. secname : string;
  491. begin
  492. secname:=secnames[atype];
  493. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  494. begin
  495. result:=secname+'.'+aname;
  496. exit;
  497. end;
  498. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  499. Thus, data which normally goes into .rodata and .rodata_norel sections must
  500. end up in .data section }
  501. if (atype in [sec_rodata,sec_rodata_norel]) and
  502. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  503. secname:='.data';
  504. { Windows correctly handles reallocations in readonly sections }
  505. if (atype=sec_rodata) and
  506. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  507. secname:='.rodata';
  508. { section type user gives the user full controll on the section name }
  509. if atype=sec_user then
  510. secname:=aname;
  511. if is_smart_section(atype) and (aname<>'') then
  512. begin
  513. case aorder of
  514. secorder_begin :
  515. sep:='.b_';
  516. secorder_end :
  517. sep:='.z_';
  518. else
  519. sep:='.n_';
  520. end;
  521. result:=secname+sep+aname
  522. end
  523. else
  524. result:=secname;
  525. end;
  526. constructor TWasmObjData.create(const n: string);
  527. begin
  528. inherited;
  529. CObjSection:=TWasmObjSection;
  530. CObjSymbol:=TWasmObjSymbol;
  531. FObjSymbolsExtraDataList:=TFPHashObjectList.Create;
  532. end;
  533. destructor TWasmObjData.destroy;
  534. var
  535. i: Integer;
  536. begin
  537. FObjSymbolsExtraDataList.Free;
  538. for i:=low(FFuncTypes) to high(FFuncTypes) do
  539. begin
  540. FFuncTypes[i].free;
  541. FFuncTypes[i]:=nil;
  542. end;
  543. inherited destroy;
  544. end;
  545. function TWasmObjData.sectionname(atype: TAsmSectiontype;
  546. const aname: string; aorder: TAsmSectionOrder): string;
  547. begin
  548. if (atype=sec_fpc) or
  549. ((atype=sec_threadvar) and not (ts_wasm_threads in current_settings.targetswitches)) then
  550. atype:=sec_data;
  551. Result:=sectionname_gas(atype, aname, aorder);
  552. end;
  553. procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
  554. p: TObjSymbol; Reloctype: TObjRelocationType);
  555. const
  556. leb_zero: array[0..4] of byte=($80,$80,$80,$80,$00);
  557. var
  558. objreloc: TWasmObjRelocation;
  559. begin
  560. if CurrObjSec=nil then
  561. internalerror(200403072);
  562. { workaround crash, when generating debug info for threadvars, when multithreading is turned off.
  563. todo: ensure the debug info for threadvars is actually correct, once we've got WebAssembly debug info working in general }
  564. if (Reloctype=RELOC_DTPOFF) and not (ts_wasm_threads in current_settings.targetswitches) then
  565. Reloctype:=RELOC_ABSOLUTE;
  566. objreloc:=nil;
  567. case Reloctype of
  568. RELOC_FUNCTION_INDEX_LEB:
  569. begin
  570. if Data<>0 then
  571. internalerror(2021092502);
  572. if len<>5 then
  573. internalerror(2021092503);
  574. if not assigned(p) then
  575. internalerror(2021092504);
  576. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  577. CurrObjSec.ObjRelocations.Add(objreloc);
  578. writebytes(leb_zero,5);
  579. end;
  580. RELOC_MEMORY_ADDR_LEB,
  581. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  582. begin
  583. if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then
  584. internalerror(2021092602);
  585. if len<>5 then
  586. internalerror(2021092503);
  587. if not assigned(p) then
  588. internalerror(2021092504);
  589. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  590. objreloc.Addend:=Data;
  591. CurrObjSec.ObjRelocations.Add(objreloc);
  592. if RelocType=RELOC_MEMORY_ADDR_LEB then
  593. WriteUleb5(CurrObjSec,Data)
  594. else
  595. WriteSleb5(CurrObjSec,Data);
  596. end;
  597. RELOC_ABSOLUTE:
  598. begin
  599. if len<>4 then
  600. internalerror(2021092607);
  601. if not assigned(p) then
  602. internalerror(2021092608);
  603. if (p.objsection<>nil) and TWasmObjSection(p.objsection).IsDebug and
  604. (p.bind<>AB_COMMON) and (p.bind<>AB_EXTERNAL) then
  605. begin
  606. objreloc:=TWasmObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,RELOC_ABSOLUTE);
  607. objreloc.Addend:=Data+p.Address;
  608. CurrObjSec.ObjRelocations.Add(objreloc);
  609. {inc(data,p.address);}
  610. data:=0;
  611. Data:=NtoLE(Data);
  612. writebytes(Data,4);
  613. end
  614. else
  615. begin
  616. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  617. objreloc.Addend:=Data;
  618. CurrObjSec.ObjRelocations.Add(objreloc);
  619. Data:=NtoLE(Data);
  620. writebytes(Data,4);
  621. end;
  622. end;
  623. RELOC_TYPE_INDEX_LEB:
  624. begin
  625. if len<>5 then
  626. internalerror(2021092612);
  627. if assigned(p) then
  628. internalerror(2021092613);
  629. objreloc:=TWasmObjRelocation.CreateTypeIndex(CurrObjSec.Size,Data);
  630. CurrObjSec.ObjRelocations.Add(objreloc);
  631. WriteUleb5(CurrObjSec,Data);
  632. end;
  633. RELOC_GLOBAL_INDEX_LEB:
  634. begin
  635. if len<>5 then
  636. internalerror(2021092701);
  637. if Data<>0 then
  638. internalerror(2021092702);
  639. if not assigned(p) then
  640. internalerror(2021092703);
  641. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  642. CurrObjSec.ObjRelocations.Add(objreloc);
  643. WriteUleb5(CurrObjSec,0);
  644. end;
  645. RELOC_TAG_INDEX_LEB:
  646. begin
  647. if len<>5 then
  648. internalerror(2021092712);
  649. if Data<>0 then
  650. internalerror(2021092713);
  651. if not assigned(p) then
  652. internalerror(2021092714);
  653. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  654. CurrObjSec.ObjRelocations.Add(objreloc);
  655. WriteSleb5(CurrObjSec,0);
  656. end;
  657. else
  658. internalerror(2021092501);
  659. end;
  660. end;
  661. function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
  662. begin
  663. result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
  664. if not assigned(result) then
  665. result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
  666. end;
  667. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  668. var
  669. i: Integer;
  670. begin
  671. for i:=low(FFuncTypes) to high(FFuncTypes) do
  672. if wft.Equals(FFuncTypes[i]) then
  673. exit(i);
  674. result:=Length(FFuncTypes);
  675. SetLength(FFuncTypes,result+1);
  676. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  677. end;
  678. function TWasmObjData.globalref(asmsym: TAsmSymbol): TObjSymbol;
  679. begin
  680. if assigned(asmsym) then
  681. begin
  682. if (asmsym.typ<>AT_WASM_GLOBAL) and (asmsym.typ<>AT_TLS) then
  683. internalerror(2021092706);
  684. result:=symbolref(asmsym);
  685. result.typ:=asmsym.typ;
  686. end
  687. else
  688. result:=nil;
  689. end;
  690. function TWasmObjData.ExceptionTagRef(asmsym: TAsmSymbol): TObjSymbol;
  691. begin
  692. if assigned(asmsym) then
  693. begin
  694. if asmsym.typ<>AT_WASM_EXCEPTION_TAG then
  695. internalerror(2021092707);
  696. result:=symbolref(asmsym);
  697. result.typ:=AT_WASM_EXCEPTION_TAG;
  698. end
  699. else
  700. result:=nil;
  701. end;
  702. procedure TWasmObjData.DeclareGlobalType(gt: tai_globaltype);
  703. var
  704. ObjSymExtraData: TWasmObjSymbolExtraData;
  705. ObjSym: TObjSymbol;
  706. begin
  707. if not gt.is_external then
  708. begin
  709. ObjSym:=symboldefine(gt.sym);
  710. ObjSym.typ:=AT_WASM_GLOBAL;
  711. end;
  712. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(gt.globalname);
  713. ObjSymExtraData.GlobalType:=gt.gtype;
  714. ObjSymExtraData.GlobalIsImmutable:=gt.immutable;
  715. end;
  716. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  717. var
  718. i: Integer;
  719. ObjSymExtraData: TWasmObjSymbolExtraData;
  720. begin
  721. FLastFuncName:=ft.funcname;
  722. i:=AddFuncType(ft.functype);
  723. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
  724. ObjSymExtraData.TypeIdx:=i;
  725. end;
  726. procedure TWasmObjData.DeclareTagType(tt: tai_tagtype);
  727. var
  728. ObjSymExtraData: TWasmObjSymbolExtraData;
  729. ft: TWasmFuncType;
  730. i: Integer;
  731. begin
  732. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(tt.tagname);
  733. ft:=TWasmFuncType.Create([],tt.params);
  734. i:=AddFuncType(ft);
  735. ft.free;
  736. ObjSymExtraData.ExceptionTagTypeIdx:=i;
  737. end;
  738. procedure TWasmObjData.DeclareExportName(en: tai_export_name);
  739. var
  740. ObjSymExtraData: TWasmObjSymbolExtraData;
  741. begin
  742. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(en.intname);
  743. ObjSymExtraData.ExportName:=en.extname;
  744. end;
  745. procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
  746. var
  747. ObjSymExtraData: TWasmObjSymbolExtraData;
  748. begin
  749. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
  750. ObjSymExtraData.ImportModule:=aim.importmodule;
  751. end;
  752. procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
  753. var
  754. ObjSymExtraData: TWasmObjSymbolExtraData;
  755. begin
  756. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
  757. ObjSymExtraData.ImportName:=ain.importname;
  758. end;
  759. procedure TWasmObjData.DeclareLocal(al: tai_local);
  760. var
  761. ObjSymExtraData: TWasmObjSymbolExtraData;
  762. begin
  763. ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
  764. ObjSymExtraData.AddLocal(al.bastyp);
  765. end;
  766. procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
  767. var
  768. valsym: TObjSymbol;
  769. aliassym: TWasmObjSymbol;
  770. begin
  771. valsym:=CreateSymbol(avalue);
  772. aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
  773. aliassym.AliasOf:=valsym.Name;
  774. end;
  775. {****************************************************************************
  776. TWasmObjOutput
  777. ****************************************************************************}
  778. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  779. var
  780. b: byte;
  781. begin
  782. repeat
  783. b:=byte(v) and 127;
  784. v:=v shr 7;
  785. if v<>0 then
  786. b:=b or 128;
  787. d.write(b,1);
  788. until v=0;
  789. end;
  790. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  791. var
  792. b: byte;
  793. begin
  794. repeat
  795. b:=byte(v) and 127;
  796. v:=v shr 7;
  797. if v<>0 then
  798. b:=b or 128;
  799. w.write(b,1);
  800. until v=0;
  801. end;
  802. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  803. var
  804. b: byte;
  805. Done: Boolean=false;
  806. begin
  807. repeat
  808. b:=byte(v) and 127;
  809. v:=SarInt64(v,7);
  810. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  811. Done:=true
  812. else
  813. b:=b or 128;
  814. d.write(b,1);
  815. until Done;
  816. end;
  817. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  818. begin
  819. d.write(b,1);
  820. end;
  821. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  822. begin
  823. WriteUleb(d,Length(s));
  824. d.writestr(s);
  825. end;
  826. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  827. var
  828. b: byte;
  829. begin
  830. b:=ord(wsid);
  831. Writer.write(b,1);
  832. WriteUleb(Writer,FWasmSections[wsid].size);
  833. Writer.writearray(FWasmSections[wsid]);
  834. end;
  835. procedure TWasmObjOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  836. var
  837. b: byte;
  838. begin
  839. b:=0;
  840. Writer.write(b,1);
  841. WriteUleb(Writer,FWasmCustomSections[wcst].size);
  842. Writer.writearray(FWasmCustomSections[wcst]);
  843. end;
  844. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  845. var
  846. buf: array [0..4095] of byte;
  847. bs: Integer;
  848. begin
  849. while size>0 do
  850. begin
  851. if size<SizeOf(buf) then
  852. bs:=Integer(size)
  853. else
  854. bs:=SizeOf(buf);
  855. src.read(buf,bs);
  856. dest.write(buf,bs);
  857. dec(size,bs);
  858. end;
  859. end;
  860. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  861. var
  862. buf : array[0..1023] of byte;
  863. bs: Integer;
  864. begin
  865. fillchar(buf,sizeof(buf),0);
  866. while size>0 do
  867. begin
  868. if size<SizeOf(buf) then
  869. bs:=Integer(size)
  870. else
  871. bs:=SizeOf(buf);
  872. dest.write(buf,bs);
  873. dec(size,bs);
  874. end;
  875. end;
  876. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  877. var
  878. i: Integer;
  879. begin
  880. WriteUleb(dest,Length(wrt));
  881. for i:=low(wrt) to high(wrt) do
  882. WriteWasmBasicType(dest,wrt[i]);
  883. end;
  884. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  885. begin
  886. WriteByte(dest,encode_wasm_basic_type(wbt));
  887. end;
  888. function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
  889. var
  890. ExtraData: TWasmObjSymbolExtraData;
  891. begin
  892. if sym.bind=AB_EXTERNAL then
  893. begin
  894. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  895. result:=(ExtraData<>nil) and (ExtraData.TypeIdx<>-1);
  896. end
  897. else
  898. result:=false;
  899. end;
  900. function TWasmObjOutput.IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  901. var
  902. ExtraData: TWasmObjSymbolExtraData;
  903. begin
  904. if (sym.typ=AT_FUNCTION) and not sym.IsAlias then
  905. begin
  906. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  907. result:=(ExtraData<>nil) and (ExtraData.ExportName<>'');
  908. end
  909. else
  910. result:=false;
  911. end;
  912. procedure TWasmObjOutput.WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  913. var
  914. i,
  915. rle_entries,
  916. cnt: Integer;
  917. lasttype: TWasmBasicType;
  918. begin
  919. if Length(ed.Locals)=0 then
  920. begin
  921. WriteUleb(dest,0);
  922. exit;
  923. end;
  924. rle_entries:=1;
  925. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  926. if ed.Locals[i]<>ed.Locals[i-1] then
  927. inc(rle_entries);
  928. WriteUleb(dest,rle_entries);
  929. lasttype:=ed.Locals[Low(ed.Locals)];
  930. cnt:=1;
  931. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  932. if ed.Locals[i]=ed.Locals[i-1] then
  933. inc(cnt)
  934. else
  935. begin
  936. WriteUleb(dest,cnt);
  937. WriteWasmBasicType(dest,lasttype);
  938. lasttype:=ed.Locals[i];
  939. cnt:=1;
  940. end;
  941. WriteUleb(dest,cnt);
  942. WriteWasmBasicType(dest,lasttype);
  943. end;
  944. procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  945. var
  946. encoded_locals: tdynamicarray;
  947. ObjSymExtraData: TWasmObjSymbolExtraData;
  948. codelen: LongWord;
  949. ObjSection: TWasmObjSection;
  950. codeexprlen: QWord;
  951. begin
  952. ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  953. ObjSection:=TWasmObjSection(objsym.objsection);
  954. ObjSection.Data.seek(objsym.address);
  955. codeexprlen:=objsym.size;
  956. encoded_locals:=tdynamicarray.Create(64);
  957. WriteFunctionLocals(encoded_locals,ObjSymExtraData);
  958. codelen:=encoded_locals.size+codeexprlen+1;
  959. WriteUleb(dest,codelen);
  960. encoded_locals.seek(0);
  961. CopyDynamicArray(encoded_locals,dest,encoded_locals.size);
  962. ObjSection.FileSectionOfs:=dest.size-objsym.offset;
  963. CopyDynamicArray(ObjSection.Data,dest,codeexprlen);
  964. WriteByte(dest,$0B);
  965. encoded_locals.Free;
  966. end;
  967. procedure TWasmObjOutput.WriteSymbolTable;
  968. begin
  969. WriteUleb(FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTableEntriesCount);
  970. FWasmSymbolTable.seek(0);
  971. CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
  972. end;
  973. procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
  974. begin
  975. WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
  976. WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
  977. FWasmRelocationCodeTable.seek(0);
  978. CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
  979. end;
  980. procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
  981. begin
  982. WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
  983. WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
  984. FWasmRelocationDataTable.seek(0);
  985. CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
  986. end;
  987. procedure TWasmObjOutput.MaybeWriteRelocationDebugTable(cst: TWasmCustomSectionType; SectionIndex: Integer; EntriesCount: Integer; Table: tdynamicarray);
  988. begin
  989. if EntriesCount>0 then
  990. begin
  991. WriteUleb(FWasmCustomSections[cst],SectionIndex);
  992. WriteUleb(FWasmCustomSections[cst],EntriesCount);
  993. Table.seek(0);
  994. CopyDynamicArray(Table,FWasmCustomSections[cst],Table.size);
  995. WriteWasmCustomSection(cst);
  996. end;
  997. end;
  998. procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  999. begin
  1000. if FWasmLinkingSubsections[wlst].size>0 then
  1001. begin
  1002. WriteByte(FWasmCustomSections[wcstLinking],Ord(wlst));
  1003. WriteUleb(FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  1004. FWasmLinkingSubsections[wlst].seek(0);
  1005. CopyDynamicArray(FWasmLinkingSubsections[wlst],FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  1006. end;
  1007. end;
  1008. procedure TWasmObjOutput.DoRelocations;
  1009. var
  1010. si, ri: Integer;
  1011. objsec: TWasmObjSection;
  1012. objrel: TWasmObjRelocation;
  1013. begin
  1014. for si:=0 to FData.ObjSectionList.Count-1 do
  1015. begin
  1016. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  1017. for ri:=0 to objsec.ObjRelocations.Count-1 do
  1018. begin
  1019. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  1020. case objrel.typ of
  1021. RELOC_FUNCTION_INDEX_LEB:
  1022. begin
  1023. if not assigned(objrel.symbol) then
  1024. internalerror(2021092509);
  1025. objsec.Data.seek(objrel.DataOffset);
  1026. if TWasmObjSymbol(objrel.symbol).FuncIndex<0 then
  1027. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1028. else
  1029. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).FuncIndex);
  1030. end;
  1031. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1032. begin
  1033. if not assigned(objrel.symbol) then
  1034. internalerror(2021092605);
  1035. if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  1036. begin
  1037. objsec.Data.seek(objrel.DataOffset);
  1038. AddSleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1039. end;
  1040. end;
  1041. RELOC_MEMORY_ADDR_LEB:
  1042. begin
  1043. if not assigned(objrel.symbol) then
  1044. internalerror(2021092606);
  1045. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1046. internalerror(2021092628);
  1047. if objrel.symbol.bind<>AB_EXTERNAL then
  1048. begin
  1049. objsec.Data.seek(objrel.DataOffset);
  1050. AddUleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1051. end;
  1052. end;
  1053. RELOC_ABSOLUTE:
  1054. begin
  1055. if assigned(objrel.ObjSection) then
  1056. begin
  1057. { todo: should we do something here? }
  1058. //Writeln('todo: section relocation');
  1059. end
  1060. else if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  1061. begin
  1062. objsec.Data.seek(objrel.DataOffset);
  1063. AddInt32(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1064. end;
  1065. end;
  1066. RELOC_TYPE_INDEX_LEB:
  1067. ;
  1068. RELOC_GLOBAL_INDEX_LEB:
  1069. begin
  1070. if not assigned(objrel.symbol) then
  1071. internalerror(2021092509);
  1072. objsec.Data.seek(objrel.DataOffset);
  1073. if TWasmObjSymbol(objrel.symbol).GlobalIndex<0 then
  1074. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1075. else
  1076. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).GlobalIndex);
  1077. end;
  1078. RELOC_TAG_INDEX_LEB:
  1079. begin
  1080. if not assigned(objrel.symbol) then
  1081. internalerror(2021092716);
  1082. objsec.Data.seek(objrel.DataOffset);
  1083. if TWasmObjSymbol(objrel.symbol).TagIndex<0 then
  1084. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1085. else
  1086. WriteSleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).TagIndex);
  1087. end;
  1088. else
  1089. internalerror(2021092510);
  1090. end;
  1091. end;
  1092. end;
  1093. end;
  1094. procedure TWasmObjOutput.WriteRelocations;
  1095. var
  1096. si, ri: Integer;
  1097. objsec: TWasmObjSection;
  1098. objrel: TWasmObjRelocation;
  1099. relout: tdynamicarray;
  1100. relcount: PInteger;
  1101. FuncSym: TWasmObjSymbol;
  1102. begin
  1103. for si:=0 to FData.ObjSectionList.Count-1 do
  1104. begin
  1105. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  1106. if objsec.IsCode then
  1107. begin
  1108. relout:=FWasmRelocationCodeTable;
  1109. relcount:=@FWasmRelocationCodeTableEntriesCount;
  1110. end
  1111. else if objsec.IsData then
  1112. begin
  1113. relout:=FWasmRelocationDataTable;
  1114. relcount:=@FWasmRelocationDataTableEntriesCount;
  1115. end
  1116. else if objsec.IsDebug then
  1117. begin
  1118. case objsec.Name of
  1119. '.debug_frame':
  1120. begin
  1121. relout:=FWasmRelocationDebugFrameTable;
  1122. relcount:=@FWasmRelocationDebugFrameTableEntriesCount;
  1123. end;
  1124. '.debug_info':
  1125. begin
  1126. relout:=FWasmRelocationDebugInfoTable;
  1127. relcount:=@FWasmRelocationDebugInfoTableEntriesCount;
  1128. end;
  1129. '.debug_line':
  1130. begin
  1131. relout:=FWasmRelocationDebugLineTable;
  1132. relcount:=@FWasmRelocationDebugLineTableEntriesCount;
  1133. end;
  1134. '.debug_abbrev':
  1135. begin
  1136. relout:=FWasmRelocationDebugAbbrevTable;
  1137. relcount:=@FWasmRelocationDebugAbbrevTableEntriesCount;
  1138. end;
  1139. '.debug_aranges':
  1140. begin
  1141. relout:=FWasmRelocationDebugArangesTable;
  1142. relcount:=@FWasmRelocationDebugArangesTableEntriesCount;
  1143. end;
  1144. '.debug_ranges':
  1145. begin
  1146. relout:=FWasmRelocationDebugRangesTable;
  1147. relcount:=@FWasmRelocationDebugRangesTableEntriesCount;
  1148. end;
  1149. '.debug_str':
  1150. begin
  1151. relout:=FWasmRelocationDebugStrTable;
  1152. relcount:=@FWasmRelocationDebugStrTableEntriesCount;
  1153. end;
  1154. else
  1155. internalerror(2022071601);
  1156. end;
  1157. end
  1158. else
  1159. continue;
  1160. for ri:=0 to objsec.ObjRelocations.Count-1 do
  1161. begin
  1162. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  1163. case objrel.typ of
  1164. RELOC_FUNCTION_INDEX_LEB:
  1165. begin
  1166. if not assigned(objrel.symbol) then
  1167. internalerror(2021092508);
  1168. Inc(relcount^);
  1169. WriteByte(relout,Ord(R_WASM_FUNCTION_INDEX_LEB));
  1170. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1171. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1172. end;
  1173. RELOC_MEMORY_ADDR_LEB:
  1174. begin
  1175. if not assigned(objrel.symbol) then
  1176. internalerror(2021092603);
  1177. Inc(relcount^);
  1178. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1179. internalerror(2021092628);
  1180. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_LEB));
  1181. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1182. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1183. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1184. end;
  1185. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1186. begin
  1187. if not assigned(objrel.symbol) then
  1188. internalerror(2021092604);
  1189. Inc(relcount^);
  1190. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1191. begin
  1192. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_SLEB));
  1193. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1194. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1195. end
  1196. else
  1197. begin
  1198. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB));
  1199. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1200. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1201. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1202. end;
  1203. end;
  1204. RELOC_ABSOLUTE:
  1205. begin
  1206. // todo: figure this out, why do these exist?
  1207. //if assigned(objrel.symbol) and not assigned(objrel.symbol.objsection) then
  1208. // Writeln('!!! ', objrel.symbol.name);
  1209. if assigned(objrel.objsection) then
  1210. begin
  1211. Inc(relcount^);
  1212. WriteByte(relout,Ord(R_WASM_SECTION_OFFSET_I32));
  1213. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1214. if (TWasmObjSection(objrel.objsection).SegSymIdx<0) then
  1215. message1(asmw_e_illegal_unset_index,objrel.objsection.name)
  1216. else
  1217. WriteUleb(relout,TWasmObjSection(objrel.objsection).SegSymIdx);
  1218. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1219. end
  1220. else if (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION)) and not objsec.IsDebug then
  1221. begin
  1222. Inc(relcount^);
  1223. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_I32));
  1224. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1225. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1226. end
  1227. else if assigned(objrel.symbol) and assigned(objrel.symbol.objsection) and TWasmObjSection(objrel.symbol.objsection).IsCode then
  1228. begin
  1229. Inc(relcount^);
  1230. WriteByte(relout,Ord(R_WASM_FUNCTION_OFFSET_I32));
  1231. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1232. FuncSym:=FindFunctionSymbol(TWasmObjSymbol(objrel.Symbol));
  1233. if FuncSym.SymbolIndex<0 then
  1234. message1(asmw_e_illegal_unset_index,FuncSym.Name)
  1235. else
  1236. WriteUleb(relout,FuncSym.SymbolIndex);
  1237. WriteSleb(relout,objrel.Addend+objrel.symbol.address); { addend to add to the address }
  1238. end
  1239. else
  1240. begin
  1241. Inc(relcount^);
  1242. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_I32));
  1243. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1244. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1245. begin
  1246. Writeln(objrel.symbol.objsection.Name, ' ', objrel.symbol.name, ' ', objsec.Name);
  1247. message1(asmw_e_illegal_unset_index,objrel.symbol.name);
  1248. end
  1249. else
  1250. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1251. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1252. end;
  1253. end;
  1254. RELOC_TYPE_INDEX_LEB:
  1255. begin
  1256. Inc(relcount^);
  1257. WriteByte(relout,Ord(R_WASM_TYPE_INDEX_LEB));
  1258. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1259. WriteUleb(relout,objrel.TypeIndex);
  1260. end;
  1261. RELOC_GLOBAL_INDEX_LEB:
  1262. begin
  1263. if not assigned(objrel.symbol) then
  1264. internalerror(2021092704);
  1265. Inc(relcount^);
  1266. WriteByte(relout,Ord(R_WASM_GLOBAL_INDEX_LEB));
  1267. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1268. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1269. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1270. else
  1271. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1272. end;
  1273. RELOC_TAG_INDEX_LEB:
  1274. begin
  1275. if not assigned(objrel.symbol) then
  1276. internalerror(2021092717);
  1277. Inc(relcount^);
  1278. WriteByte(relout,Ord(R_WASM_TAG_INDEX_LEB));
  1279. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1280. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1281. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1282. else
  1283. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1284. end;
  1285. else
  1286. internalerror(2021092507);
  1287. end;
  1288. end;
  1289. end;
  1290. end;
  1291. function TWasmObjOutput.FindFunctionSymbol(Symbol: TWasmObjSymbol): TWasmObjSymbol;
  1292. begin
  1293. Result:=TWasmObjSection(Symbol.objsection).MainFuncSymbol;
  1294. end;
  1295. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  1296. var
  1297. section_nr: Integer;
  1298. procedure MaybeAddDebugSectionToSymbolTable(st: TWasmCustomDebugSectionType; var debug_section_nr: Integer);
  1299. var
  1300. objsec: TWasmObjSection;
  1301. begin
  1302. objsec:=TWasmObjSection(Data.ObjSectionList.Find(WasmCustomSectionName[st]));
  1303. if Assigned(objsec) then
  1304. begin
  1305. debug_section_nr:=section_nr;
  1306. Inc(section_nr);
  1307. objsec.SegSymIdx:=FWasmSymbolTableEntriesCount;
  1308. Inc(FWasmSymbolTableEntriesCount);
  1309. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1310. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1311. WriteUleb(FWasmSymbolTable,debug_section_nr);
  1312. end;
  1313. end;
  1314. procedure MaybeWriteDebugSection(st: TWasmCustomDebugSectionType);
  1315. var
  1316. objsec: TWasmObjSection;
  1317. begin
  1318. objsec:=TWasmObjSection(Data.ObjSectionList.Find(WasmCustomSectionName[st]));
  1319. if Assigned(objsec) then
  1320. begin
  1321. if oso_Data in objsec.SecOptions then
  1322. begin
  1323. objsec.Data.seek(0);
  1324. CopyDynamicArray(objsec.Data,FWasmCustomSections[st],objsec.Size);
  1325. end
  1326. else
  1327. WriteZeros(FWasmCustomSections[st],objsec.Size);
  1328. WriteWasmCustomSection(st);
  1329. end;
  1330. end;
  1331. var
  1332. i: Integer;
  1333. objsec: TWasmObjSection;
  1334. segment_count: Integer = 0;
  1335. cur_seg_ofs: qword = 0;
  1336. types_count,
  1337. imports_count, NextImportFunctionIndex, NextFunctionIndex,
  1338. code_section_nr, data_section_nr,
  1339. debug_abbrev_section_nr,debug_info_section_nr,debug_str_section_nr,
  1340. debug_line_section_nr,debug_frame_section_nr,debug_aranges_section_nr,
  1341. debug_ranges_section_nr,
  1342. NextGlobalIndex, NextTagIndex: Integer;
  1343. import_globals_count: Integer = 0;
  1344. globals_count: Integer = 0;
  1345. import_functions_count: Integer = 0;
  1346. export_functions_count: Integer = 0;
  1347. functions_count: Integer = 0;
  1348. import_exception_tags_count: Integer = 0;
  1349. exception_tags_count: Integer = 0;
  1350. objsym, ObjSymAlias: TWasmObjSymbol;
  1351. cust_sec: TWasmCustomSectionType;
  1352. SegmentFlags, SymbolFlags: UInt64;
  1353. begin
  1354. FData:=TWasmObjData(Data);
  1355. { each custom sections starts with its name }
  1356. for cust_sec in TWasmCustomSectionType do
  1357. WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
  1358. WriteUleb(FWasmCustomSections[wcstLinking],2); { linking metadata version }
  1359. for i:=0 to Data.ObjSymbolList.Count-1 do
  1360. begin
  1361. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1362. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1363. if objsym.bind=AB_EXTERNAL then
  1364. Inc(import_exception_tags_count)
  1365. else
  1366. Inc(exception_tags_count);
  1367. if objsym.typ=AT_WASM_GLOBAL then
  1368. if objsym.bind=AB_EXTERNAL then
  1369. Inc(import_globals_count)
  1370. else
  1371. Inc(globals_count);
  1372. if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1373. Inc(import_globals_count);
  1374. if IsExternalFunction(objsym) then
  1375. Inc(import_functions_count);
  1376. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1377. begin
  1378. TWasmObjSection(objsym.objsection).MainFuncSymbol:=objsym;
  1379. Inc(functions_count);
  1380. end;
  1381. if IsExportedFunction(objsym) then
  1382. Inc(export_functions_count);
  1383. end;
  1384. types_count:=Length(FData.FFuncTypes);
  1385. WriteUleb(FWasmSections[wsiType],types_count);
  1386. for i:=0 to types_count-1 do
  1387. with FData.FFuncTypes[i] do
  1388. begin
  1389. WriteByte(FWasmSections[wsiType],$60);
  1390. WriteWasmResultType(FWasmSections[wsiType],params);
  1391. WriteWasmResultType(FWasmSections[wsiType],results);
  1392. end;
  1393. for i:=0 to Data.ObjSectionList.Count-1 do
  1394. begin
  1395. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1396. if objsec.IsCode then
  1397. objsec.SegIdx:=-1
  1398. else if objsec.IsData then
  1399. begin
  1400. objsec.SegIdx:=segment_count;
  1401. objsec.SegOfs:=cur_seg_ofs;
  1402. Inc(segment_count);
  1403. Inc(cur_seg_ofs,objsec.Size);
  1404. end;
  1405. end;
  1406. imports_count:=2+import_globals_count+import_functions_count+import_exception_tags_count;
  1407. WriteUleb(FWasmSections[wsiImport],imports_count);
  1408. { import memories }
  1409. WriteName(FWasmSections[wsiImport],'env');
  1410. WriteName(FWasmSections[wsiImport],'__linear_memory');
  1411. WriteByte(FWasmSections[wsiImport],$02); { mem }
  1412. WriteByte(FWasmSections[wsiImport],$00); { min }
  1413. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  1414. { import globals }
  1415. NextGlobalIndex:=0;
  1416. for i:=0 to Data.ObjSymbolList.Count-1 do
  1417. begin
  1418. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1419. if (objsym.bind=AB_EXTERNAL) and (objsym.typ=AT_WASM_GLOBAL) then
  1420. begin
  1421. objsym.GlobalIndex:=NextGlobalIndex;
  1422. Inc(NextGlobalIndex);
  1423. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1424. if objsym.ExtraData.ImportModule<>'' then
  1425. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1426. else
  1427. WriteName(FWasmSections[wsiImport],'env');
  1428. WriteName(FWasmSections[wsiImport],objsym.Name);
  1429. WriteByte(FWasmSections[wsiImport],$03); { global }
  1430. WriteWasmBasicType(FWasmSections[wsiImport],objsym.ExtraData.GlobalType);
  1431. if objsym.ExtraData.GlobalIsImmutable then
  1432. WriteByte(FWasmSections[wsiImport],$00) { const }
  1433. else
  1434. WriteByte(FWasmSections[wsiImport],$01); { var }
  1435. end
  1436. else if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1437. begin
  1438. objsym.GlobalIndex:=NextGlobalIndex;
  1439. Inc(NextGlobalIndex);
  1440. objsym.ExtraData:=nil;
  1441. WriteName(FWasmSections[wsiImport],'GOT.mem');
  1442. WriteName(FWasmSections[wsiImport],objsym.Name);
  1443. WriteByte(FWasmSections[wsiImport],$03); { global }
  1444. WriteWasmBasicType(FWasmSections[wsiImport],wbt_i32); { i32 }
  1445. WriteByte(FWasmSections[wsiImport],$01); { var }
  1446. end;
  1447. end;
  1448. { import functions }
  1449. NextImportFunctionIndex:=0;
  1450. for i:=0 to Data.ObjSymbolList.Count-1 do
  1451. begin
  1452. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1453. if IsExternalFunction(objsym) then
  1454. begin
  1455. objsym.FuncIndex:=NextImportFunctionIndex;
  1456. Inc(NextImportFunctionIndex);
  1457. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1458. if objsym.ExtraData.ImportModule<>'' then
  1459. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1460. else
  1461. WriteName(FWasmSections[wsiImport],'env');
  1462. WriteName(FWasmSections[wsiImport],objsym.Name);
  1463. WriteByte(FWasmSections[wsiImport],$00); { func }
  1464. WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1465. end;
  1466. end;
  1467. { import tables }
  1468. WriteName(FWasmSections[wsiImport],'env');
  1469. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  1470. WriteByte(FWasmSections[wsiImport],$01); { table }
  1471. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  1472. WriteByte(FWasmSections[wsiImport],$00); { min }
  1473. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  1474. { import tags }
  1475. NextTagIndex:=0;
  1476. for i:=0 to Data.ObjSymbolList.Count-1 do
  1477. begin
  1478. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1479. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind=AB_EXTERNAL) then
  1480. begin
  1481. objsym.TagIndex:=NextTagIndex;
  1482. Inc(NextTagIndex);
  1483. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1484. if objsym.ExtraData.ImportModule<>'' then
  1485. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1486. else
  1487. WriteName(FWasmSections[wsiImport],'env');
  1488. WriteName(FWasmSections[wsiImport],objsym.Name);
  1489. WriteByte(FWasmSections[wsiImport],$04); { tag }
  1490. WriteByte(FWasmSections[wsiImport],$00); { exception }
  1491. WriteUleb(FWasmSections[wsiImport],objsym.ExtraData.ExceptionTagTypeIdx);
  1492. end;
  1493. end;
  1494. WriteUleb(FWasmSections[wsiFunction],functions_count);
  1495. NextFunctionIndex:=NextImportFunctionIndex;
  1496. for i:=0 to Data.ObjSymbolList.Count-1 do
  1497. begin
  1498. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1499. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1500. begin
  1501. objsym.FuncIndex:=NextFunctionIndex;
  1502. Inc(NextFunctionIndex);
  1503. WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1504. end;
  1505. end;
  1506. if exception_tags_count>0 then
  1507. begin
  1508. WriteUleb(FWasmSections[wsiTag],exception_tags_count);
  1509. for i:=0 to Data.ObjSymbolList.Count-1 do
  1510. begin
  1511. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1512. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind<>AB_EXTERNAL) then
  1513. begin
  1514. objsym.TagIndex:=NextTagIndex;
  1515. Inc(NextTagIndex);
  1516. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1517. WriteByte(FWasmSections[wsiTag],$00); { exception }
  1518. WriteUleb(FWasmSections[wsiTag],objsym.ExtraData.ExceptionTagTypeIdx);
  1519. end;
  1520. end;
  1521. end;
  1522. if globals_count>0 then
  1523. begin
  1524. WriteUleb(FWasmSections[wsiGlobal],globals_count);
  1525. for i:=0 to Data.ObjSymbolList.Count-1 do
  1526. begin
  1527. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1528. if (objsym.typ=AT_WASM_GLOBAL) and (objsym.bind<>AB_EXTERNAL) then
  1529. begin
  1530. objsym.GlobalIndex:=NextGlobalIndex;
  1531. Inc(NextGlobalIndex);
  1532. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1533. WriteWasmBasicType(FWasmSections[wsiGlobal],objsym.ExtraData.GlobalType);
  1534. if objsym.ExtraData.GlobalIsImmutable then
  1535. WriteByte(FWasmSections[wsiGlobal],$00) { const }
  1536. else
  1537. WriteByte(FWasmSections[wsiGlobal],$01); { var }
  1538. { init expr }
  1539. case objsym.ExtraData.GlobalType of
  1540. wbt_i32:
  1541. begin
  1542. WriteByte(FWasmSections[wsiGlobal],$41); { i32.const }
  1543. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1544. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1545. end;
  1546. wbt_i64:
  1547. begin
  1548. WriteByte(FWasmSections[wsiGlobal],$42); { i64.const }
  1549. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1550. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1551. end;
  1552. wbt_f32:
  1553. begin
  1554. WriteByte(FWasmSections[wsiGlobal],$43); { f32.const }
  1555. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE single precision floating point format) }
  1556. WriteByte(FWasmSections[wsiGlobal],$00);
  1557. WriteByte(FWasmSections[wsiGlobal],$00);
  1558. WriteByte(FWasmSections[wsiGlobal],$00);
  1559. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1560. end;
  1561. wbt_f64:
  1562. begin
  1563. WriteByte(FWasmSections[wsiGlobal],$44); { f64.const }
  1564. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE double precision floating point format) }
  1565. WriteByte(FWasmSections[wsiGlobal],$00);
  1566. WriteByte(FWasmSections[wsiGlobal],$00);
  1567. WriteByte(FWasmSections[wsiGlobal],$00);
  1568. WriteByte(FWasmSections[wsiGlobal],$00);
  1569. WriteByte(FWasmSections[wsiGlobal],$00);
  1570. WriteByte(FWasmSections[wsiGlobal],$00);
  1571. WriteByte(FWasmSections[wsiGlobal],$00);
  1572. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1573. end;
  1574. wbt_externref:
  1575. begin
  1576. WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null extern }
  1577. WriteByte(FWasmSections[wsiGlobal],$6F);
  1578. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1579. end;
  1580. wbt_funcref:
  1581. begin
  1582. WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null func }
  1583. WriteByte(FWasmSections[wsiGlobal],$70);
  1584. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1585. end;
  1586. else
  1587. internalerror(2022052801);
  1588. end;
  1589. end;
  1590. end;
  1591. end;
  1592. if export_functions_count>0 then
  1593. begin
  1594. WriteUleb(FWasmSections[wsiExport],export_functions_count);
  1595. for i:=0 to Data.ObjSymbolList.Count-1 do
  1596. begin
  1597. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1598. if IsExportedFunction(objsym) then
  1599. begin
  1600. WriteName(FWasmSections[wsiExport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).ExportName);
  1601. WriteByte(FWasmSections[wsiExport],0); { func }
  1602. if (objsym.FuncIndex<0) then
  1603. message1(asmw_e_illegal_unset_index,objsym.name)
  1604. else
  1605. WriteUleb(FWasmSections[wsiExport],objsym.FuncIndex);
  1606. end;
  1607. end;
  1608. end;
  1609. for i:=0 to Data.ObjSymbolList.Count-1 do
  1610. begin
  1611. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1612. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1613. begin
  1614. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1615. Inc(FWasmSymbolTableEntriesCount);
  1616. WriteByte(FWasmSymbolTable,Ord(SYMTAB_EVENT));
  1617. if objsym.bind=AB_GLOBAL then
  1618. WriteUleb(FWasmSymbolTable,0)
  1619. else if objsym.bind=AB_LOCAL then
  1620. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1621. else if objsym.bind=AB_EXTERNAL then
  1622. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1623. else if objsym.bind=AB_WEAK then
  1624. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_WEAK)
  1625. else
  1626. internalerror(2021092715);
  1627. if (objsym.TagIndex<0) then
  1628. message1(asmw_e_illegal_unset_index,objsym.name)
  1629. else
  1630. WriteUleb(FWasmSymbolTable,objsym.TagIndex);
  1631. if objsym.bind<>AB_EXTERNAL then
  1632. WriteName(FWasmSymbolTable,objsym.Name);
  1633. end
  1634. else if objsym.typ=AT_WASM_GLOBAL then
  1635. begin
  1636. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1637. Inc(FWasmSymbolTableEntriesCount);
  1638. WriteByte(FWasmSymbolTable,Ord(SYMTAB_GLOBAL));
  1639. if objsym.bind=AB_EXTERNAL then
  1640. begin
  1641. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1642. if (objsym.GlobalIndex<0) then
  1643. message1(asmw_e_illegal_unset_index,objsym.name)
  1644. else
  1645. WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
  1646. end
  1647. else
  1648. begin
  1649. WriteUleb(FWasmSymbolTable,0);
  1650. if (objsym.GlobalIndex<0) then
  1651. message1(asmw_e_illegal_unset_index,objsym.name)
  1652. else
  1653. WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
  1654. WriteName(FWasmSymbolTable,objsym.Name);
  1655. end;
  1656. end
  1657. else if IsExternalFunction(objsym) then
  1658. begin
  1659. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1660. Inc(FWasmSymbolTableEntriesCount);
  1661. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1662. if objsym.ExtraData.ImportModule<>'' then
  1663. begin
  1664. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
  1665. if (objsym.FuncIndex<0) then
  1666. message1(asmw_e_illegal_unset_index,objsym.name)
  1667. else
  1668. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1669. WriteName(FWasmSymbolTable,objsym.Name);
  1670. end
  1671. else
  1672. begin
  1673. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1674. if (objsym.FuncIndex<0) then
  1675. message1(asmw_e_illegal_unset_index,objsym.name)
  1676. else
  1677. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1678. end;
  1679. end
  1680. else if objsym.typ=AT_FUNCTION then
  1681. begin
  1682. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1683. Inc(FWasmSymbolTableEntriesCount);
  1684. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1685. if objsym.IsAlias then
  1686. begin
  1687. ObjSymAlias:=TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf));
  1688. ObjSym.FuncIndex:=ObjSymAlias.FuncIndex;
  1689. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
  1690. WriteUleb(FWasmSymbolTable,ObjSymAlias.FuncIndex);
  1691. end
  1692. else
  1693. begin
  1694. if IsExportedFunction(objsym) then
  1695. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPORTED)
  1696. else
  1697. WriteUleb(FWasmSymbolTable,0);
  1698. if (objsym.FuncIndex<0) then
  1699. message1(asmw_e_illegal_unset_index,objsym.name)
  1700. else
  1701. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1702. end;
  1703. WriteName(FWasmSymbolTable,objsym.Name);
  1704. end
  1705. else if (objsym.typ in [AT_DATA,AT_TLS]) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
  1706. begin
  1707. if (objsym.bind<>AB_EXTERNAL) and TWasmObjSection(objsym.objsection).IsDebug then
  1708. begin
  1709. {todo: debug symbols}
  1710. end
  1711. else
  1712. begin
  1713. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1714. Inc(FWasmSymbolTableEntriesCount);
  1715. WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
  1716. if objsym.bind=AB_GLOBAL then
  1717. SymbolFlags:=0
  1718. else if objsym.bind=AB_LOCAL then
  1719. SymbolFlags:=WASM_SYM_BINDING_LOCAL
  1720. else if objsym.bind=AB_EXTERNAL then
  1721. SymbolFlags:=WASM_SYM_UNDEFINED
  1722. else
  1723. internalerror(2021092506);
  1724. if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1725. SymbolFlags:=(SymbolFlags and not WASM_SYM_BINDING_LOCAL) or WASM_SYM_TLS;
  1726. WriteUleb(FWasmSymbolTable,SymbolFlags);
  1727. WriteName(FWasmSymbolTable,objsym.Name);
  1728. if objsym.bind<>AB_EXTERNAL then
  1729. begin
  1730. WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
  1731. WriteUleb(FWasmSymbolTable,objsym.offset);
  1732. WriteUleb(FWasmSymbolTable,objsym.size);
  1733. end;
  1734. end;
  1735. end;
  1736. end;
  1737. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  1738. Writer.write(WasmVersion,SizeOf(WasmVersion));
  1739. if ts_wasm_threads in current_settings.targetswitches then
  1740. begin
  1741. WriteUleb(FWasmCustomSections[wcstTargetFeatures],4);
  1742. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1743. WriteName(FWasmCustomSections[wcstTargetFeatures],'atomics');
  1744. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1745. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1746. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1747. WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
  1748. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1749. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1750. end
  1751. else
  1752. begin
  1753. WriteUleb(FWasmCustomSections[wcstTargetFeatures],3);
  1754. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1755. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1756. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1757. WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
  1758. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1759. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1760. end;
  1761. { Write the producers section:
  1762. https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md }
  1763. WriteUleb(FWasmCustomSections[wcstProducers],2);
  1764. WriteName(FWasmCustomSections[wcstProducers],'language');
  1765. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1766. WriteName(FWasmCustomSections[wcstProducers],'Pascal');
  1767. WriteName(FWasmCustomSections[wcstProducers],'');
  1768. WriteName(FWasmCustomSections[wcstProducers],'processed-by');
  1769. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1770. WriteName(FWasmCustomSections[wcstProducers],'Free Pascal Compiler (FPC)');
  1771. WriteName(FWasmCustomSections[wcstProducers],full_version_string+' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname);
  1772. code_section_nr:=-1;
  1773. data_section_nr:=-1;
  1774. debug_abbrev_section_nr:=-1;
  1775. debug_info_section_nr:=-1;
  1776. debug_str_section_nr:=-1;
  1777. debug_line_section_nr:=-1;
  1778. debug_frame_section_nr:=-1;
  1779. debug_aranges_section_nr:=-1;
  1780. debug_ranges_section_nr:=-1;
  1781. section_nr:=0;
  1782. WriteWasmSection(wsiType);
  1783. Inc(section_nr);
  1784. WriteWasmSection(wsiImport);
  1785. Inc(section_nr);
  1786. WriteWasmSection(wsiFunction);
  1787. Inc(section_nr);
  1788. if exception_tags_count>0 then
  1789. begin
  1790. WriteWasmSection(wsiTag);
  1791. Inc(section_nr);
  1792. end;
  1793. if globals_count>0 then
  1794. begin
  1795. WriteWasmSection(wsiGlobal);
  1796. Inc(section_nr);
  1797. end;
  1798. if export_functions_count>0 then
  1799. begin
  1800. WriteWasmSection(wsiExport);
  1801. Inc(section_nr);
  1802. end;
  1803. { determine the section numbers for the datacount, code, data and debug sections ahead of time }
  1804. if segment_count>0 then
  1805. Inc(section_nr); { the DataCount section }
  1806. code_section_nr:=section_nr; { the Code section }
  1807. Inc(section_nr);
  1808. if segment_count>0 then
  1809. begin
  1810. data_section_nr:=section_nr; { the Data section }
  1811. Inc(section_nr);
  1812. end;
  1813. { the debug sections }
  1814. MaybeAddDebugSectionToSymbolTable(wcstDebugAbbrev,debug_abbrev_section_nr);
  1815. MaybeAddDebugSectionToSymbolTable(wcstDebugInfo,debug_info_section_nr);
  1816. MaybeAddDebugSectionToSymbolTable(wcstDebugStr,debug_str_section_nr);
  1817. MaybeAddDebugSectionToSymbolTable(wcstDebugLine,debug_line_section_nr);
  1818. MaybeAddDebugSectionToSymbolTable(wcstDebugFrame,debug_frame_section_nr);
  1819. MaybeAddDebugSectionToSymbolTable(wcstDebugAranges,debug_aranges_section_nr);
  1820. MaybeAddDebugSectionToSymbolTable(wcstDebugRanges,debug_ranges_section_nr);
  1821. DoRelocations;
  1822. if segment_count>0 then
  1823. begin
  1824. WriteUleb(FWasmSections[wsiData],segment_count);
  1825. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  1826. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],segment_count);
  1827. for i:=0 to Data.ObjSectionList.Count-1 do
  1828. begin
  1829. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1830. if objsec.IsData then
  1831. begin
  1832. WriteName(FWasmLinkingSubsections[WASM_SEGMENT_INFO],objsec.Name);
  1833. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],BsrQWord(objsec.SecAlign));
  1834. SegmentFlags:=0;
  1835. if (ts_wasm_threads in current_settings.targetswitches) and
  1836. (oso_threadvar in objsec.SecOptions) then
  1837. SegmentFlags:=SegmentFlags or WASM_SEG_FLAG_TLS;
  1838. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],SegmentFlags); { flags }
  1839. WriteByte(FWasmSections[wsiData],0);
  1840. WriteByte(FWasmSections[wsiData],$41);
  1841. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  1842. WriteByte(FWasmSections[wsiData],$0b);
  1843. WriteUleb(FWasmSections[wsiData],objsec.Size);
  1844. objsec.FileSectionOfs:=FWasmSections[wsiData].size;
  1845. if oso_Data in objsec.SecOptions then
  1846. begin
  1847. objsec.Data.seek(0);
  1848. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  1849. end
  1850. else
  1851. begin
  1852. WriteZeros(FWasmSections[wsiData],objsec.Size);
  1853. end;
  1854. end;
  1855. end;
  1856. end;
  1857. WriteUleb(FWasmSections[wsiCode],functions_count);
  1858. for i:=0 to Data.ObjSymbolList.Count-1 do
  1859. begin
  1860. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1861. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1862. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  1863. end;
  1864. if segment_count>0 then
  1865. WriteWasmSection(wsiDataCount);
  1866. WriteWasmSection(wsiCode);
  1867. if segment_count>0 then
  1868. WriteWasmSection(wsiData);
  1869. MaybeWriteDebugSection(wcstDebugAbbrev);
  1870. MaybeWriteDebugSection(wcstDebugInfo);
  1871. MaybeWriteDebugSection(wcstDebugStr);
  1872. MaybeWriteDebugSection(wcstDebugLine);
  1873. MaybeWriteDebugSection(wcstDebugFrame);
  1874. MaybeWriteDebugSection(wcstDebugAranges);
  1875. MaybeWriteDebugSection(wcstDebugRanges);
  1876. WriteRelocations;
  1877. WriteSymbolTable;
  1878. WriteLinkingSubsection(WASM_SYMBOL_TABLE);
  1879. if segment_count>0 then
  1880. WriteLinkingSubsection(WASM_SEGMENT_INFO);
  1881. WriteRelocationCodeTable(code_section_nr);
  1882. if segment_count>0 then
  1883. WriteRelocationDataTable(data_section_nr);
  1884. WriteWasmCustomSection(wcstLinking);
  1885. Inc(section_nr);
  1886. WriteWasmCustomSection(wcstRelocCode);
  1887. Inc(section_nr);
  1888. if segment_count>0 then
  1889. begin
  1890. WriteWasmCustomSection(wcstRelocData);
  1891. Inc(section_nr);
  1892. end;
  1893. MaybeWriteRelocationDebugTable(wcstRelocDebugAbbrev,debug_abbrev_section_nr,FWasmRelocationDebugAbbrevTableEntriesCount,FWasmRelocationDebugAbbrevTable);
  1894. MaybeWriteRelocationDebugTable(wcstRelocDebugInfo,debug_info_section_nr,FWasmRelocationDebugInfoTableEntriesCount,FWasmRelocationDebugInfoTable);
  1895. MaybeWriteRelocationDebugTable(wcstRelocDebugStr,debug_str_section_nr,FWasmRelocationDebugStrTableEntriesCount,FWasmRelocationDebugStrTable);
  1896. MaybeWriteRelocationDebugTable(wcstRelocDebugLine,debug_line_section_nr,FWasmRelocationDebugLineTableEntriesCount,FWasmRelocationDebugLineTable);
  1897. MaybeWriteRelocationDebugTable(wcstRelocDebugFrame,debug_frame_section_nr,FWasmRelocationDebugFrameTableEntriesCount,FWasmRelocationDebugFrameTable);
  1898. MaybeWriteRelocationDebugTable(wcstRelocDebugAranges,debug_aranges_section_nr,FWasmRelocationDebugArangesTableEntriesCount,FWasmRelocationDebugArangesTable);
  1899. MaybeWriteRelocationDebugTable(wcstRelocDebugRanges,debug_ranges_section_nr,FWasmRelocationDebugRangesTableEntriesCount,FWasmRelocationDebugRangesTable);
  1900. WriteWasmCustomSection(wcstProducers);
  1901. Inc(section_nr);
  1902. WriteWasmCustomSection(wcstTargetFeatures);
  1903. Inc(section_nr);
  1904. result:=true;
  1905. end;
  1906. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  1907. var
  1908. i: TWasmSectionID;
  1909. j: TWasmCustomSectionType;
  1910. k: TWasmLinkingSubsectionType;
  1911. begin
  1912. inherited;
  1913. cobjdata:=TWasmObjData;
  1914. for i in TWasmSectionID do
  1915. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  1916. for j in TWasmCustomSectionType do
  1917. FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
  1918. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1919. FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
  1920. FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
  1921. FWasmSymbolTableEntriesCount:=0;
  1922. FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
  1923. FWasmRelocationCodeTableEntriesCount:=0;
  1924. FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
  1925. FWasmRelocationDataTableEntriesCount:=0;
  1926. FWasmRelocationDebugFrameTable:=tdynamicarray.create(SectionDataMaxGrow);
  1927. FWasmRelocationDebugFrameTableEntriesCount:=0;
  1928. FWasmRelocationDebugInfoTable:=tdynamicarray.create(SectionDataMaxGrow);
  1929. FWasmRelocationDebugInfoTableEntriesCount:=0;
  1930. FWasmRelocationDebugLineTable:=tdynamicarray.create(SectionDataMaxGrow);
  1931. FWasmRelocationDebugLineTableEntriesCount:=0;
  1932. FWasmRelocationDebugAbbrevTable:=tdynamicarray.create(SectionDataMaxGrow);
  1933. FWasmRelocationDebugAbbrevTableEntriesCount:=0;
  1934. FWasmRelocationDebugArangesTable:=tdynamicarray.create(SectionDataMaxGrow);
  1935. FWasmRelocationDebugArangesTableEntriesCount:=0;
  1936. FWasmRelocationDebugRangesTable:=tdynamicarray.create(SectionDataMaxGrow);
  1937. FWasmRelocationDebugRangesTableEntriesCount:=0;
  1938. FWasmRelocationDebugStrTable:=tdynamicarray.create(SectionDataMaxGrow);
  1939. FWasmRelocationDebugStrTableEntriesCount:=0;
  1940. end;
  1941. destructor TWasmObjOutput.destroy;
  1942. var
  1943. i: TWasmSectionID;
  1944. j: TWasmCustomSectionType;
  1945. k: TWasmLinkingSubsectionType;
  1946. begin
  1947. for i in TWasmSectionID do
  1948. FWasmSections[i].Free;
  1949. for j in TWasmCustomSectionType do
  1950. FWasmCustomSections[j].Free;
  1951. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1952. FWasmLinkingSubsections[k].Free;
  1953. FWasmSymbolTable.Free;
  1954. FWasmRelocationCodeTable.Free;
  1955. FWasmRelocationDataTable.Free;
  1956. FWasmRelocationDebugFrameTable.Free;
  1957. FWasmRelocationDebugInfoTable.Free;
  1958. FWasmRelocationDebugLineTable.Free;
  1959. FWasmRelocationDebugAbbrevTable.Free;
  1960. FWasmRelocationDebugArangesTable.Free;
  1961. FWasmRelocationDebugRangesTable.Free;
  1962. FWasmRelocationDebugStrTable.Free;
  1963. inherited destroy;
  1964. end;
  1965. {****************************************************************************
  1966. TWasmObjInput
  1967. ****************************************************************************}
  1968. constructor TWasmObjInput.create;
  1969. begin
  1970. inherited create;
  1971. cobjdata:=TWasmObjData;
  1972. end;
  1973. destructor TWasmObjInput.Destroy;
  1974. var
  1975. i: Integer;
  1976. begin
  1977. for i:=low(FFuncTypes) to high(FFuncTypes) do
  1978. begin
  1979. FFuncTypes[i].free;
  1980. FFuncTypes[i]:=nil;
  1981. end;
  1982. inherited Destroy;
  1983. end;
  1984. class function TWasmObjInput.CanReadObjData(AReader: TObjectreader): boolean;
  1985. var
  1986. ModuleMagic: array [0..3] of Byte;
  1987. ModuleVersion: array [0..3] of Byte;
  1988. i: Integer;
  1989. begin
  1990. result:=false;
  1991. if not AReader.read(ModuleMagic,4) then
  1992. exit;
  1993. for i:=0 to 3 do
  1994. if ModuleMagic[i]<>WasmModuleMagic[i] then
  1995. exit;
  1996. if not AReader.read(ModuleVersion,4) then
  1997. exit;
  1998. for i:=0 to 3 do
  1999. if ModuleVersion[i]<>WasmVersion[i] then
  2000. exit;
  2001. result:=true;
  2002. end;
  2003. function TWasmObjInput.ReadObjData(AReader: TObjectreader; out ObjData: TObjData): boolean;
  2004. type
  2005. TLimits = record
  2006. Min, Max: uint32;
  2007. HasMax: Boolean;
  2008. end;
  2009. var
  2010. SectionIndex: Integer = -1;
  2011. SectionId: Byte;
  2012. SectionSize: uint32;
  2013. SectionStart: LongInt;
  2014. CheckSectionBounds: Boolean;
  2015. TypeSectionRead: Boolean = false;
  2016. ImportSectionRead: Boolean = false;
  2017. FunctionSectionRead: Boolean = false;
  2018. GlobalSectionRead: Boolean = false;
  2019. ExportSectionRead: Boolean = false;
  2020. CodeSectionRead: Boolean = false;
  2021. DataSectionRead: Boolean = false;
  2022. DataCountSectionRead: Boolean = false;
  2023. SegmentInfoSectionRead: Boolean = false;
  2024. SymbolTableSectionRead: Boolean = false;
  2025. CodeSectionIndex: Integer = -1;
  2026. DataSectionIndex: Integer = -1;
  2027. FuncTypes: array of record
  2028. IsImport: Boolean;
  2029. ImportName: ansistring;
  2030. ImportModName: ansistring;
  2031. typidx: uint32;
  2032. IsExported: Boolean;
  2033. ExportName: ansistring;
  2034. end;
  2035. FuncTypeImportsCount: uint32;
  2036. TableTypes: array of record
  2037. IsImport: Boolean;
  2038. ImportName: ansistring;
  2039. ImportModName: ansistring;
  2040. reftype: TWasmBAsicType;
  2041. limits: TLimits;
  2042. IsExported: Boolean;
  2043. ExportName: ansistring;
  2044. end;
  2045. TableTypeImportsCount: uint32;
  2046. MemTypes: array of record
  2047. IsImport: Boolean;
  2048. ImportName: ansistring;
  2049. ImportModName: ansistring;
  2050. limits: TLimits;
  2051. IsExported: Boolean;
  2052. ExportName: ansistring;
  2053. end;
  2054. MemTypeImportsCount: uint32;
  2055. GlobalTypes: array of record
  2056. IsImport: Boolean;
  2057. ImportName: ansistring;
  2058. ImportModName: ansistring;
  2059. valtype: TWasmBasicType;
  2060. IsMutable: Boolean;
  2061. IsExported: Boolean;
  2062. ExportName: ansistring;
  2063. end;
  2064. GlobalTypeImportsCount: uint32;
  2065. CodeSegments: array of record
  2066. CodeSectionOffset: uint32;
  2067. CodeSize: uint32;
  2068. DataPos: LongInt;
  2069. SegName: ansistring;
  2070. SegIsExported: Boolean;
  2071. end;
  2072. DataSegments: array of record
  2073. DataSectionOffset: uint32;
  2074. Active: Boolean;
  2075. MemIdx: uint32;
  2076. Len: uint32;
  2077. Offset: int32;
  2078. DataPos: LongInt;
  2079. SegName: ansistring;
  2080. SegAlignment: uint32;
  2081. SegFlags: uint32;
  2082. end;
  2083. SymbolTable: array of record
  2084. SymFlags: uint32;
  2085. TargetSection: uint32;
  2086. SymIndex: uint32;
  2087. SymOffset: uint32;
  2088. SymSize: uint32;
  2089. SymKind: Byte;
  2090. SymName: ansistring;
  2091. ObjSym: TObjSymbol;
  2092. end;
  2093. { meaning of first index: }
  2094. { table 0 is code relocs }
  2095. { table 1 is data relocs }
  2096. { tables 2.. are custom section relocs }
  2097. RelocationTable: array of array of record
  2098. RelocType: Byte;
  2099. RelocOffset: uint32;
  2100. RelocIndex: uint32;
  2101. RelocAddend: int32;
  2102. end;
  2103. function ReadSection: Boolean;
  2104. function read(out b;len:longint):boolean;
  2105. begin
  2106. result:=false;
  2107. if not CheckSectionBounds or ((AReader.Pos+len)<=(SectionStart+SectionSize)) then
  2108. result:=AReader.read(b,len)
  2109. else
  2110. begin
  2111. { trying to read beyond the end of the section }
  2112. AReader.read(b,SectionStart+SectionSize-AReader.Pos);
  2113. result:=false;
  2114. end;
  2115. end;
  2116. function ReadUleb(out v: uint64): boolean;
  2117. var
  2118. b: byte;
  2119. shift:integer;
  2120. begin
  2121. result:=false;
  2122. b:=0;
  2123. v:=0;
  2124. shift:=0;
  2125. repeat
  2126. if not read(b,1) then
  2127. exit;
  2128. v:=v or (uint64(b and 127) shl shift);
  2129. inc(shift,7);
  2130. until (b and 128)=0;
  2131. result:=true;
  2132. end;
  2133. function ReadUleb32(out v: uint32): boolean;
  2134. var
  2135. vv: uint64;
  2136. begin
  2137. result:=false;
  2138. v:=default(uint32);
  2139. if not ReadUleb(vv) then
  2140. exit;
  2141. if vv>high(uint32) then
  2142. exit;
  2143. v:=vv;
  2144. result:=true;
  2145. end;
  2146. function ReadSleb(out v: int64): boolean;
  2147. var
  2148. b: byte;
  2149. shift:integer;
  2150. begin
  2151. result:=false;
  2152. b:=0;
  2153. v:=0;
  2154. shift:=0;
  2155. repeat
  2156. if not read(b,1) then
  2157. exit;
  2158. v:=v or (uint64(b and 127) shl shift);
  2159. inc(shift,7);
  2160. until (b and 128)=0;
  2161. {$ifopt Q+}
  2162. {$define overflowon}
  2163. {$Q-}
  2164. {$endif}
  2165. {$ifopt R+}
  2166. {$define rangeon}
  2167. {$R-}
  2168. {$endif}
  2169. if (b and 64)<>0 then
  2170. v:=v or (high(uint64) shl shift);
  2171. result:=true;
  2172. end;
  2173. {$ifdef overflowon}
  2174. {$Q+}
  2175. {$undef overflowon}
  2176. {$endif}
  2177. {$ifdef rangeon}
  2178. {$R+}
  2179. {$undef rangeon}
  2180. {$endif}
  2181. function ReadSleb32(out v: int32): boolean;
  2182. var
  2183. vv: int64;
  2184. begin
  2185. result:=false;
  2186. v:=default(int32);
  2187. if not ReadSleb(vv) then
  2188. exit;
  2189. if (vv>high(int32)) or (vv<low(int32)) then
  2190. exit;
  2191. v:=vv;
  2192. result:=true;
  2193. end;
  2194. function ReadName(out v: ansistring): boolean;
  2195. var
  2196. len: uint32;
  2197. begin
  2198. result:=false;
  2199. if not ReadUleb32(len) then
  2200. exit;
  2201. SetLength(v,len);
  2202. if len>0 then
  2203. result:=read(v[1],len)
  2204. else
  2205. result:=true;
  2206. end;
  2207. function ReadCustomSection: Boolean;
  2208. function ReadRelocationSection: Boolean;
  2209. var
  2210. TargetSection, RelocCount: uint32;
  2211. i: Integer;
  2212. RelocTableIndex: Integer;
  2213. begin
  2214. Result:=False;
  2215. if not ReadUleb32(TargetSection) then
  2216. begin
  2217. InputError('Error reading the index of the target section of a relocation section');
  2218. exit;
  2219. end;
  2220. if TargetSection=CodeSectionIndex then
  2221. RelocTableIndex:=0
  2222. else if TargetSection=DataSectionIndex then
  2223. RelocTableIndex:=1
  2224. else
  2225. begin
  2226. InputError('Relocation for custom sections not supported, yet');
  2227. exit;
  2228. end;
  2229. if not ReadUleb32(RelocCount) then
  2230. begin
  2231. InputError('Error reading the relocation entries count from a relocation section');
  2232. exit;
  2233. end;
  2234. SetLength(RelocationTable[RelocTableIndex],RelocCount);
  2235. for i:=0 to RelocCount-1 do
  2236. with RelocationTable[RelocTableIndex,i] do
  2237. begin
  2238. if not Read(RelocType,1) then
  2239. begin
  2240. InputError('Error reading the relocation type of a relocation entry');
  2241. exit;
  2242. end;
  2243. if not (TWasmRelocationType(RelocType) in [R_WASM_FUNCTION_INDEX_LEB,
  2244. R_WASM_MEMORY_ADDR_LEB,
  2245. R_WASM_TABLE_INDEX_SLEB,
  2246. R_WASM_MEMORY_ADDR_SLEB,
  2247. R_WASM_SECTION_OFFSET_I32,
  2248. R_WASM_TABLE_INDEX_I32,
  2249. R_WASM_FUNCTION_OFFSET_I32,
  2250. R_WASM_MEMORY_ADDR_I32,
  2251. R_WASM_TYPE_INDEX_LEB,
  2252. R_WASM_GLOBAL_INDEX_LEB,
  2253. R_WASM_TAG_INDEX_LEB]) then
  2254. begin
  2255. InputError('Unsupported relocation type: ' + tostr(RelocType));
  2256. exit;
  2257. end;
  2258. if not ReadUleb32(RelocOffset) then
  2259. begin
  2260. InputError('Error reading the relocation offset of a relocation entry');
  2261. exit;
  2262. end;
  2263. if not ReadUleb32(RelocIndex) then
  2264. begin
  2265. InputError('Error reading the relocation index of a relocation entry');
  2266. exit;
  2267. end;
  2268. if TWasmRelocationType(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
  2269. begin
  2270. if not ReadSleb32(RelocAddend) then
  2271. begin
  2272. InputError('Error reading the relocation addend of a relocation entry');
  2273. exit;
  2274. end;
  2275. end;
  2276. end;
  2277. if AReader.Pos<>(SectionStart+SectionSize) then
  2278. begin
  2279. InputError('Unexpected relocation section size');
  2280. exit;
  2281. end;
  2282. Result:=True;
  2283. end;
  2284. function ReadLinkingSection: Boolean;
  2285. function ReadSegmentInfo: Boolean;
  2286. var
  2287. SegmentCount: uint32;
  2288. i: Integer;
  2289. begin
  2290. Result:=False;
  2291. if SegmentInfoSectionRead then
  2292. begin
  2293. InputError('The WASM_SEGMENT_INFO subsection is duplicated');
  2294. exit;
  2295. end;
  2296. SegmentInfoSectionRead:=True;
  2297. if not ReadUleb32(SegmentCount) then
  2298. begin
  2299. InputError('Error reading the segment count from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
  2300. exit;
  2301. end;
  2302. if SegmentCount<>Length(DataSegments) then
  2303. begin
  2304. InputError('Segment count in the WASM_SEGMENT_INFO subsection does not match the data count in the data section');
  2305. exit;
  2306. end;
  2307. for i:=0 to SegmentCount-1 do
  2308. with DataSegments[i] do
  2309. begin
  2310. if not ReadName(SegName) then
  2311. begin
  2312. InputError('Error reading segment name from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
  2313. exit;
  2314. end;
  2315. if not ReadUleb32(SegAlignment) then
  2316. begin
  2317. InputError('Error reading segment alignment from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
  2318. exit;
  2319. end;
  2320. if not ReadUleb32(SegFlags) then
  2321. begin
  2322. InputError('Error reading segment flags from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
  2323. exit;
  2324. end;
  2325. end;
  2326. if AReader.Pos<>(SectionStart+SectionSize) then
  2327. begin
  2328. InputError('Unexpected WASM_SEGMENT_INFO section size');
  2329. exit;
  2330. end;
  2331. Result:=True;
  2332. end;
  2333. function ReadSymbolTable: Boolean;
  2334. var
  2335. SymCount: uint32;
  2336. i: Integer;
  2337. SymKindName: string;
  2338. begin
  2339. Result:=False;
  2340. if SymbolTableSectionRead then
  2341. begin
  2342. InputError('The WASM_SYMBOL_TABLE subsection is duplicated');
  2343. exit;
  2344. end;
  2345. SymbolTableSectionRead:=True;
  2346. if not ReadUleb32(SymCount) then
  2347. begin
  2348. InputError('Error reading the symbol count from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
  2349. exit;
  2350. end;
  2351. SetLength(SymbolTable,SymCount);
  2352. for i:=0 to SymCount-1 do
  2353. with SymbolTable[i] do
  2354. begin
  2355. if not Read(SymKind,1) then
  2356. begin
  2357. InputError('Error reading symbol type from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
  2358. exit;
  2359. end;
  2360. if not ReadUleb32(SymFlags) then
  2361. begin
  2362. InputError('Error reading symbol flags from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
  2363. exit;
  2364. end;
  2365. case SymKind of
  2366. byte(SYMTAB_FUNCTION),
  2367. byte(SYMTAB_GLOBAL),
  2368. byte(SYMTAB_EVENT),
  2369. byte(SYMTAB_TABLE):
  2370. begin
  2371. WriteStr(SymKindName, TWasmSymbolType(SymKind));
  2372. if not ReadUleb32(SymIndex) then
  2373. begin
  2374. InputError('Error reading the index of a ' + SymKindName + ' symbol');
  2375. exit;
  2376. end;
  2377. if ((SymKind=byte(SYMTAB_FUNCTION)) and (SymIndex>high(FuncTypes))) then
  2378. begin
  2379. InputError('Symbol index too high');
  2380. exit;
  2381. end;
  2382. if ((SymFlags and WASM_SYM_EXPLICIT_NAME)<>0) or
  2383. ((SymFlags and WASM_SYM_UNDEFINED)=0) then
  2384. begin
  2385. if not ReadName(SymName) then
  2386. begin
  2387. InputError('Error reading symbol name of a ' + SymKindName + ' symbol');
  2388. exit;
  2389. end;
  2390. end;
  2391. end;
  2392. byte(SYMTAB_DATA):
  2393. begin
  2394. if not ReadName(SymName) then
  2395. begin
  2396. InputError('Error reading symbol name of a SYMTAB_DATA symbol');
  2397. exit;
  2398. end;
  2399. if (SymFlags and WASM_SYM_UNDEFINED)=0 then
  2400. begin
  2401. if not ReadUleb32(SymIndex) then
  2402. begin
  2403. InputError('Error reading the data segment index of a SYMTAB_DATA symbol');
  2404. exit;
  2405. end;
  2406. if SymIndex>high(DataSegments) then
  2407. begin
  2408. InputError('Data segment index of SYMTAB_DATA symbol out of bounds');
  2409. exit;
  2410. end;
  2411. if not ReadUleb32(SymOffset) then
  2412. begin
  2413. InputError('Error reading the offset of a SYMTAB_DATA symbol');
  2414. exit;
  2415. end;
  2416. if not ReadUleb32(SymSize) then
  2417. begin
  2418. InputError('Error reading the size of a SYMTAB_DATA symbol');
  2419. exit;
  2420. end;
  2421. end;
  2422. end;
  2423. byte(SYMTAB_SECTION):
  2424. begin
  2425. if not ReadUleb32(TargetSection) then
  2426. begin
  2427. InputError('Error reading the target section of a SYMTAB_SECTION symbol');
  2428. exit;
  2429. end;
  2430. end;
  2431. else
  2432. begin
  2433. InputError('Unsupported symbol kind: ' + tostr(SymKind));
  2434. exit;
  2435. end;
  2436. end;
  2437. end;
  2438. if AReader.Pos<>(SectionStart+SectionSize) then
  2439. begin
  2440. InputError('Unexpected WASM_SYMBOL_TABLE section size');
  2441. exit;
  2442. end;
  2443. Result:=True;
  2444. end;
  2445. const
  2446. ExpectedVersion = 2;
  2447. var
  2448. Version, SubsectionSize, SaveSectionSize: uint32;
  2449. SubsectionType: Byte;
  2450. SaveSectionStart: LongInt;
  2451. begin
  2452. Result:=False;
  2453. if not ReadUleb32(Version) then
  2454. begin
  2455. InputError('Error reading the version of the ''linking'' section');
  2456. exit;
  2457. end;
  2458. if Version<>ExpectedVersion then
  2459. begin
  2460. InputError('The ''linking'' section has an unsupported version (expected version ' + tostr(ExpectedVersion) + ', got version ' + tostr(Version) + ')');
  2461. exit;
  2462. end;
  2463. while AReader.Pos<(SectionStart+SectionSize) do
  2464. begin
  2465. if not read(SubsectionType, 1) then
  2466. begin
  2467. InputError('Error reading subsection type in the ''linking'' section');
  2468. exit;
  2469. end;
  2470. if not ReadUleb32(SubsectionSize) then
  2471. begin
  2472. InputError('Error reading subsection size in the ''linking'' section');
  2473. exit;
  2474. end;
  2475. if (AReader.Pos+SubsectionSize)>(SectionStart+SectionSize) then
  2476. begin
  2477. InputError('Subsection size exceeds bounds of its parent ''linking'' section');
  2478. exit;
  2479. end;
  2480. SaveSectionStart:=SectionStart;
  2481. SaveSectionSize:=SectionSize;
  2482. SectionStart:=AReader.Pos;
  2483. SectionSize:=SubsectionSize;
  2484. case SubsectionType of
  2485. Byte(WASM_SEGMENT_INFO):
  2486. if not ReadSegmentInfo then
  2487. begin
  2488. InputError('Error reading the WASM_SEGMENT_INFO subsection of the ''linking'' section');
  2489. exit;
  2490. end;
  2491. Byte(WASM_SYMBOL_TABLE):
  2492. if not ReadSymbolTable then
  2493. begin
  2494. InputError('Error reading the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
  2495. exit;
  2496. end;
  2497. else
  2498. begin
  2499. InputError('Unsupported ''linking'' section subsection type ' + tostr(SubsectionType));
  2500. exit;
  2501. end;
  2502. end;
  2503. AReader.Seek(SectionStart+SectionSize);
  2504. SectionStart:=SaveSectionStart;
  2505. SectionSize:=SaveSectionSize;
  2506. end;
  2507. result:=True;
  2508. end;
  2509. function ReadProducersSection: Boolean;
  2510. begin
  2511. Result:=False;
  2512. end;
  2513. function ReadTargetFeaturesSection: Boolean;
  2514. begin
  2515. Result:=False;
  2516. end;
  2517. const
  2518. RelocationSectionPrefix = 'reloc.';
  2519. var
  2520. SectionName: ansistring;
  2521. begin
  2522. Result:=False;
  2523. ReadName(SectionName);
  2524. if Copy(SectionName,1,Length(RelocationSectionPrefix)) = RelocationSectionPrefix then
  2525. begin
  2526. if not ReadRelocationSection then
  2527. begin
  2528. InputError('Error reading the relocation section ''' + SectionName + '''');
  2529. exit;
  2530. end;
  2531. end
  2532. else
  2533. case SectionName of
  2534. 'linking':
  2535. if not ReadLinkingSection then
  2536. begin
  2537. InputError('Error reading the ''linking'' section');
  2538. exit;
  2539. end;
  2540. 'producers':
  2541. Result:=ReadProducersSection;
  2542. 'target_features':
  2543. Result:=ReadTargetFeaturesSection;
  2544. else
  2545. InputError('Unsupported custom section: ''' + SectionName + '''');
  2546. end;
  2547. Result:=True;
  2548. end;
  2549. function ReadTypeSection: Boolean;
  2550. var
  2551. FuncTypesCount, ParamsCount, ResultsCount: uint32;
  2552. FuncTypeId, WasmTypeId: Byte;
  2553. i, j: Integer;
  2554. wbt: TWasmBasicType;
  2555. begin
  2556. Result:=False;
  2557. if TypeSectionRead then
  2558. begin
  2559. InputError('Type section is duplicated');
  2560. exit;
  2561. end;
  2562. TypeSectionRead:=True;
  2563. if not ReadUleb32(FuncTypesCount) then
  2564. begin
  2565. InputError('Error reading the func types count');
  2566. exit;
  2567. end;
  2568. SetLength(FFuncTypes,FuncTypesCount);
  2569. for i:=0 to FuncTypesCount - 1 do
  2570. begin
  2571. FFuncTypes[i]:=TWasmFuncType.Create([],[]);
  2572. if not AReader.read(FuncTypeId,1) then
  2573. begin
  2574. InputError('Error reading the function type identifier');
  2575. exit;
  2576. end;
  2577. if FuncTypeId<>$60 then
  2578. begin
  2579. InputError('Incorrect function type identifier (expected $60, got $' + HexStr(FuncTypeId,2) + ')');
  2580. exit;
  2581. end;
  2582. if not ReadUleb32(ParamsCount) then
  2583. begin
  2584. InputError('Error reading the function parameters count');
  2585. exit;
  2586. end;
  2587. for j:=0 to ParamsCount-1 do
  2588. begin
  2589. if not AReader.read(WasmTypeId,1) then
  2590. begin
  2591. InputError('Error reading a function parameter basic type');
  2592. exit;
  2593. end;
  2594. if not decode_wasm_basic_type(WasmTypeId,wbt) then
  2595. begin
  2596. InputError('Unknown function parameter basic type: $' + HexStr(WasmTypeId,2));
  2597. exit;
  2598. end;
  2599. FFuncTypes[i].add_param(wbt);
  2600. end;
  2601. if not ReadUleb32(ResultsCount) then
  2602. begin
  2603. InputError('Error reading the function results count');
  2604. exit;
  2605. end;
  2606. for j:=0 to ResultsCount-1 do
  2607. begin
  2608. if not AReader.read(WasmTypeId,1) then
  2609. begin
  2610. InputError('Error reading a function result basic type');
  2611. exit;
  2612. end;
  2613. if not decode_wasm_basic_type(WasmTypeId,wbt) then
  2614. begin
  2615. InputError('Unknown function result basic type: $' + HexStr(WasmTypeId,2));
  2616. exit;
  2617. end;
  2618. FFuncTypes[i].add_result(wbt);
  2619. end;
  2620. end;
  2621. if AReader.Pos<>(SectionStart+SectionSize) then
  2622. begin
  2623. InputError('Unexpected type section size');
  2624. exit;
  2625. end;
  2626. Result:=true;
  2627. end;
  2628. function ReadImportSection: Boolean;
  2629. var
  2630. ImportsCount: uint32;
  2631. i: Integer;
  2632. ModName, Name: ansistring;
  2633. ImportType, TableElemTyp, TableLimitsKind, MemoryLimitsKind,
  2634. GlobalType, GlobalMutabilityType: Byte;
  2635. begin
  2636. Result:=False;
  2637. if ImportSectionRead then
  2638. begin
  2639. InputError('Import section is duplicated');
  2640. exit;
  2641. end;
  2642. ImportSectionRead:=True;
  2643. if not ReadUleb32(ImportsCount) then
  2644. begin
  2645. InputError('Error reading the imports count');
  2646. exit;
  2647. end;
  2648. for i:=0 to ImportsCount-1 do
  2649. begin
  2650. if not ReadName(ModName) then
  2651. begin
  2652. InputError('Error reading import module name');
  2653. exit;
  2654. end;
  2655. if not ReadName(Name) then
  2656. begin
  2657. InputError('Error import name');
  2658. exit;
  2659. end;
  2660. if not AReader.Read(ImportType,1) then
  2661. begin
  2662. InputError('Error reading import type');
  2663. exit;
  2664. end;
  2665. case ImportType of
  2666. $00: { func }
  2667. begin
  2668. Inc(FuncTypeImportsCount);
  2669. SetLength(FuncTypes,FuncTypeImportsCount);
  2670. with FuncTypes[FuncTypeImportsCount-1] do
  2671. begin
  2672. IsImport:=True;
  2673. ImportName:=Name;
  2674. ImportModName:=ModName;
  2675. if not ReadUleb32(typidx) then
  2676. begin
  2677. InputError('Error reading type index for func import');
  2678. exit;
  2679. end;
  2680. if typidx>high(FFuncTypes) then
  2681. begin
  2682. InputError('Type index in func import exceeds bounds of the types table');
  2683. exit;
  2684. end;
  2685. end;
  2686. end;
  2687. $01: { table }
  2688. begin
  2689. Inc(TableTypeImportsCount);
  2690. SetLength(TableTypes,TableTypeImportsCount);
  2691. with TableTypes[TableTypeImportsCount-1] do
  2692. begin
  2693. IsImport:=True;
  2694. ImportName:=Name;
  2695. ImportModName:=ModName;
  2696. if not AReader.read(TableElemTyp,1) then
  2697. begin
  2698. InputError('Error reading table element type for table import');
  2699. exit;
  2700. end;
  2701. if not decode_wasm_basic_type(TableElemTyp,reftype) then
  2702. begin
  2703. InputError('Invalid table element type for table import: $' + HexStr(TableElemTyp,2));
  2704. exit;
  2705. end;
  2706. if not (reftype in WasmReferenceTypes) then
  2707. begin
  2708. InputError('Table element type for table import must be a reference type');
  2709. exit;
  2710. end;
  2711. if not AReader.read(TableLimitsKind,1) then
  2712. begin
  2713. InputError('Error reading table limits kind for table import');
  2714. exit;
  2715. end;
  2716. case TableLimitsKind of
  2717. $00:
  2718. begin
  2719. limits.HasMax:=False;
  2720. limits.Max:=high(limits.Max);
  2721. if not ReadUleb32(limits.min) then
  2722. begin
  2723. InputError('Error reading table limits min for table import');
  2724. exit;
  2725. end;
  2726. end;
  2727. $01:
  2728. begin
  2729. limits.HasMax:=True;
  2730. if not ReadUleb32(limits.min) then
  2731. begin
  2732. InputError('Error reading table limits min for table import');
  2733. exit;
  2734. end;
  2735. if not ReadUleb32(limits.max) then
  2736. begin
  2737. InputError('Error reading table limits max for table import');
  2738. exit;
  2739. end;
  2740. if limits.min>limits.max then
  2741. begin
  2742. InputError('Table limits min exceed table limits max in table import');
  2743. exit;
  2744. end;
  2745. end;
  2746. else
  2747. begin
  2748. InputError('Unsupported table limits kind for table import: $' + HexStr(TableLimitsKind,2));
  2749. exit;
  2750. end;
  2751. end;
  2752. end;
  2753. end;
  2754. $02: { mem }
  2755. begin
  2756. Inc(MemTypeImportsCount);
  2757. SetLength(MemTypes,MemTypeImportsCount);
  2758. with MemTypes[MemTypeImportsCount-1] do
  2759. begin
  2760. IsImport:=True;
  2761. ImportName:=Name;
  2762. ImportModName:=ModName;
  2763. if not AReader.read(MemoryLimitsKind,1) then
  2764. begin
  2765. InputError('Error reading memory limits kind for memory import');
  2766. exit;
  2767. end;
  2768. case MemoryLimitsKind of
  2769. $00:
  2770. begin
  2771. limits.HasMax:=False;
  2772. limits.Max:=high(limits.Max);
  2773. if not ReadUleb32(limits.min) then
  2774. begin
  2775. InputError('Error reading memory limits min for memory import');
  2776. exit;
  2777. end;
  2778. end;
  2779. $01:
  2780. begin
  2781. limits.HasMax:=True;
  2782. if not ReadUleb32(limits.min) then
  2783. begin
  2784. InputError('Error reading memory limits min for memory import');
  2785. exit;
  2786. end;
  2787. if not ReadUleb32(limits.max) then
  2788. begin
  2789. InputError('Error reading memory limits max for memory import');
  2790. exit;
  2791. end;
  2792. if limits.Min>limits.Max then
  2793. begin
  2794. InputError('Memory limits min exceed memory limits max in memory import');
  2795. exit;
  2796. end;
  2797. end;
  2798. else
  2799. begin
  2800. InputError('Unsupported memory limits kind for memory import: $' + HexStr(MemoryLimitsKind,2));
  2801. exit;
  2802. end;
  2803. end;
  2804. end;
  2805. end;
  2806. $03: { global }
  2807. begin
  2808. Inc(GlobalTypeImportsCount);
  2809. SetLength(GlobalTypes,GlobalTypeImportsCount);
  2810. with GlobalTypes[GlobalTypeImportsCount-1] do
  2811. begin
  2812. IsImport:=True;
  2813. ImportName:=Name;
  2814. ImportModName:=ModName;
  2815. if not AReader.read(GlobalType,1) then
  2816. begin
  2817. InputError('Error reading global type for global import');
  2818. exit;
  2819. end;
  2820. if not decode_wasm_basic_type(GlobalType,valtype) then
  2821. begin
  2822. InputError('Unsupported global type for global import: ' + HexStr(GlobalType,2));
  2823. exit;
  2824. end;
  2825. if not AReader.read(GlobalMutabilityType,1) then
  2826. begin
  2827. InputError('Error reading global mutability flag for global import');
  2828. exit;
  2829. end;
  2830. case GlobalMutabilityType of
  2831. $00:
  2832. IsMutable:=False;
  2833. $01:
  2834. IsMutable:=True;
  2835. else
  2836. begin
  2837. InputError('Unknown global mutability flag for global import: $' + HexStr(GlobalMutabilityType,2));
  2838. exit;
  2839. end;
  2840. end;
  2841. end;
  2842. end;
  2843. else
  2844. begin
  2845. InputError('Unknown import type: $' + HexStr(ImportType,2));
  2846. exit;
  2847. end;
  2848. end;
  2849. end;
  2850. if AReader.Pos<>(SectionStart+SectionSize) then
  2851. begin
  2852. InputError('Unexpected import section size');
  2853. exit;
  2854. end;
  2855. Result:=true;
  2856. end;
  2857. function ReadFunctionSection: Boolean;
  2858. var
  2859. FunctionsCount: uint32;
  2860. i: Integer;
  2861. begin
  2862. Result:=False;
  2863. if FunctionSectionRead then
  2864. begin
  2865. InputError('Function section is duplicated');
  2866. exit;
  2867. end;
  2868. FunctionSectionRead:=True;
  2869. if not ReadUleb32(FunctionsCount) then
  2870. begin
  2871. InputError('Error reading the functions count');
  2872. exit;
  2873. end;
  2874. SetLength(FuncTypes, FuncTypeImportsCount + FunctionsCount);
  2875. for i:=0 to FunctionsCount-1 do
  2876. with FuncTypes[i + FuncTypeImportsCount] do
  2877. begin
  2878. IsImport:=False;
  2879. if not ReadUleb32(typidx) then
  2880. begin
  2881. InputError('Error reading type index for function');
  2882. exit;
  2883. end;
  2884. if typidx>high(FFuncTypes) then
  2885. begin
  2886. InputError('Type index in the function section exceeds bounds of the types table');
  2887. exit;
  2888. end;
  2889. end;
  2890. if AReader.Pos<>(SectionStart+SectionSize) then
  2891. begin
  2892. InputError('Unexpected function section size');
  2893. exit;
  2894. end;
  2895. Result:=true;
  2896. end;
  2897. function ReadGlobalSection: Boolean;
  2898. function ParseExpr: Boolean;
  2899. var
  2900. B: Byte;
  2901. tmpbuf: array [1..8] of Byte;
  2902. tmpInt32: int32;
  2903. tmpInt64: int64;
  2904. begin
  2905. Result:=False;
  2906. repeat
  2907. if not Read(B, 1) then
  2908. exit;
  2909. case B of
  2910. $0B: { end }
  2911. ;
  2912. $41: { i32.const }
  2913. if not ReadSleb32(tmpInt32) then
  2914. exit;
  2915. $42: { i64.const }
  2916. if not ReadSleb(tmpInt64) then
  2917. exit;
  2918. $43: { f32.const }
  2919. if not Read(tmpbuf, 4) then
  2920. exit;
  2921. $44: { f64.const }
  2922. if not Read(tmpbuf, 8) then
  2923. exit;
  2924. $D0: { ref.null }
  2925. if not Read(tmpbuf, 1) then
  2926. exit;
  2927. else
  2928. begin
  2929. InputError('Unsupported opcode in global initializer');
  2930. exit;
  2931. end;
  2932. end;
  2933. until b = $0B;
  2934. Result:=True;
  2935. end;
  2936. var
  2937. GlobalsCount: uint32;
  2938. i: Integer;
  2939. vt: Byte;
  2940. mut: Byte;
  2941. begin
  2942. Result:=False;
  2943. if GlobalSectionRead then
  2944. begin
  2945. InputError('Global section is duplicated');
  2946. exit;
  2947. end;
  2948. GlobalSectionRead:=True;
  2949. if not ReadUleb32(GlobalsCount) then
  2950. begin
  2951. InputError('Error reading the globals count from the global section');
  2952. exit;
  2953. end;
  2954. SetLength(GlobalTypes,Length(GlobalTypes)+GlobalsCount);
  2955. for i:=0 to GlobalsCount-1 do
  2956. with GlobalTypes[i + GlobalTypeImportsCount] do
  2957. begin
  2958. if not read(vt,1) then
  2959. begin
  2960. InputError('Error reading the type of a global from the global section');
  2961. exit;
  2962. end;
  2963. if not decode_wasm_basic_type(vt,valtype) then
  2964. begin
  2965. InputError('Unsupported type of global in the global section');
  2966. exit;
  2967. end;
  2968. if not read(mut,1) then
  2969. begin
  2970. InputError('Error reading the mutability flag of a global in the global section');
  2971. exit;
  2972. end;
  2973. case mut of
  2974. $00:
  2975. IsMutable:=False;
  2976. $01:
  2977. IsMutable:=True;
  2978. else
  2979. begin
  2980. InputError('Unsupported value (' + tostr(mut) + ') for the mutability flag of a global in the global section');
  2981. exit;
  2982. end;
  2983. end;
  2984. if not ParseExpr then
  2985. begin
  2986. InputError('Error parsing the global initializer expression in the global section');
  2987. exit;
  2988. end;
  2989. end;
  2990. if AReader.Pos<>(SectionStart+SectionSize) then
  2991. begin
  2992. InputError('Unexpected global section size');
  2993. exit;
  2994. end;
  2995. Result:=True;
  2996. end;
  2997. function ReadExportSection: Boolean;
  2998. var
  2999. ExportsCount, FuncIdx, TableIdx, MemIdx, GlobalIdx: uint32;
  3000. i: Integer;
  3001. Name: ansistring;
  3002. ExportType: Byte;
  3003. begin
  3004. Result:=False;
  3005. if ExportSectionRead then
  3006. begin
  3007. InputError('Export section is duplicated');
  3008. exit;
  3009. end;
  3010. ExportSectionRead:=True;
  3011. if not ReadUleb32(ExportsCount) then
  3012. begin
  3013. InputError('Error reading the exports count from the export section');
  3014. exit;
  3015. end;
  3016. for i:=0 to ExportsCount-1 do
  3017. begin
  3018. if not ReadName(Name) then
  3019. begin
  3020. InputError('Error reading an export name from the export section');
  3021. exit;
  3022. end;
  3023. if not Read(ExportType,1) then
  3024. begin
  3025. InputError('Error reading an export type from the export section');
  3026. exit;
  3027. end;
  3028. case ExportType of
  3029. $00: { func }
  3030. begin
  3031. if not ReadUleb32(FuncIdx) then
  3032. begin
  3033. InputError('Error reading a func index from the export section');
  3034. exit;
  3035. end;
  3036. if FuncIdx>high(FuncTypes) then
  3037. begin
  3038. InputError('Func index too high in the export section');
  3039. exit;
  3040. end;
  3041. with FuncTypes[FuncIdx] do
  3042. begin
  3043. IsExported:=True;
  3044. ExportName:=Name;
  3045. end;
  3046. end;
  3047. $01: { table }
  3048. begin
  3049. if not ReadUleb32(TableIdx) then
  3050. begin
  3051. InputError('Error reading a table index from the export section');
  3052. exit;
  3053. end;
  3054. if TableIdx>high(TableTypes) then
  3055. begin
  3056. InputError('Table index too high in the export section');
  3057. exit;
  3058. end;
  3059. with TableTypes[TableIdx] do
  3060. begin
  3061. IsExported:=True;
  3062. ExportName:=Name;
  3063. end;
  3064. end;
  3065. $02: { mem }
  3066. begin
  3067. if not ReadUleb32(MemIdx) then
  3068. begin
  3069. InputError('Error reading a mem index from the export section');
  3070. exit;
  3071. end;
  3072. if MemIdx>high(MemTypes) then
  3073. begin
  3074. InputError('Mem index too high in the export section');
  3075. exit;
  3076. end;
  3077. with MemTypes[MemIdx] do
  3078. begin
  3079. IsExported:=True;
  3080. ExportName:=Name;
  3081. end;
  3082. end;
  3083. $03: { global }
  3084. begin
  3085. if not ReadUleb32(GlobalIdx) then
  3086. begin
  3087. InputError('Error reading a global index from the export section');
  3088. exit;
  3089. end;
  3090. if GlobalIdx>high(GlobalTypes) then
  3091. begin
  3092. InputError('Global index too high in the export section');
  3093. exit;
  3094. end;
  3095. with GlobalTypes[GlobalIdx] do
  3096. begin
  3097. IsExported:=True;
  3098. ExportName:=Name;
  3099. end;
  3100. end;
  3101. else
  3102. begin
  3103. InputError('Unsupported export type in the export section: ' + tostr(ExportType));
  3104. exit;
  3105. end;
  3106. end;
  3107. end;
  3108. if AReader.Pos<>(SectionStart+SectionSize) then
  3109. begin
  3110. InputError('Unexpected export section size');
  3111. exit;
  3112. end;
  3113. Result:=True;
  3114. end;
  3115. function ReadCodeSection: Boolean;
  3116. var
  3117. CodeEntriesCount: uint32;
  3118. i: Integer;
  3119. begin
  3120. Result:=False;
  3121. if CodeSectionRead then
  3122. begin
  3123. InputError('Code section is duplicated');
  3124. exit;
  3125. end;
  3126. CodeSectionRead:=True;
  3127. CodeSectionIndex:=SectionIndex;
  3128. if not ReadUleb32(CodeEntriesCount) then
  3129. begin
  3130. InputError('Error reading the code entries cound from the code section');
  3131. exit;
  3132. end;
  3133. if CodeEntriesCount <> (Length(FuncTypes) - FuncTypeImportsCount) then
  3134. begin
  3135. InputError('Code segment count in the code section does not match the function definition count in the function section');
  3136. exit;
  3137. end;
  3138. SetLength(CodeSegments,CodeEntriesCount);
  3139. for i:=0 to CodeEntriesCount-1 do
  3140. with CodeSegments[i] do
  3141. begin
  3142. if not ReadUleb32(CodeSize) then
  3143. begin
  3144. InputError('Error reading the code size of an entry in the code section');
  3145. exit;
  3146. end;
  3147. if (AReader.Pos+CodeSize)>(SectionStart+SectionSize) then
  3148. begin
  3149. InputError('Code segment exceeds the bounds of the code section');
  3150. exit;
  3151. end;
  3152. DataPos:=AReader.Pos;
  3153. CodeSectionOffset:=AReader.Pos-SectionStart;
  3154. AReader.Seek(AReader.Pos+CodeSize);
  3155. end;
  3156. if AReader.Pos<>(SectionStart+SectionSize) then
  3157. begin
  3158. InputError('Unexpected code section size');
  3159. exit;
  3160. end;
  3161. Result:=true;
  3162. end;
  3163. function ReadDataSection: Boolean;
  3164. function ReadExpr(out ExprV: int32): Boolean;
  3165. var
  3166. b: Byte;
  3167. begin
  3168. Result:=False;
  3169. if not Read(b,1) then
  3170. exit;
  3171. if b<>$41 then
  3172. begin
  3173. InputError('Only i32.const expressions supported');
  3174. exit;
  3175. end;
  3176. if not ReadSleb32(ExprV) then
  3177. exit;
  3178. if not Read(b,1) then
  3179. exit;
  3180. if b<>$0B then
  3181. begin
  3182. InputError('Only single const expressions supported');
  3183. exit;
  3184. end;
  3185. Result:=True;
  3186. end;
  3187. var
  3188. DataCount: uint32;
  3189. DataType: Byte;
  3190. i: Integer;
  3191. begin
  3192. Result:=False;
  3193. if DataSectionRead then
  3194. begin
  3195. InputError('Data section is duplicated');
  3196. exit;
  3197. end;
  3198. DataSectionRead:=True;
  3199. DataSectionIndex:=SectionIndex;
  3200. if not ReadUleb32(DataCount) then
  3201. begin
  3202. InputError('Error reading the data entries count from the data section');
  3203. exit;
  3204. end;
  3205. if DataCountSectionRead then
  3206. begin
  3207. if Length(DataSegments)<>DataCount then
  3208. begin
  3209. InputError('Data entries count in the data section do not match the number, specified in the data count section');
  3210. exit;
  3211. end;
  3212. end
  3213. else
  3214. SetLength(DataSegments,DataCount);
  3215. for i:=0 to DataCount-1 do
  3216. with DataSegments[i] do
  3217. begin
  3218. if not read(DataType, 1) then
  3219. begin
  3220. InputError('Error reading data type of segment from the data section');
  3221. exit;
  3222. end;
  3223. case DataType of
  3224. 0:
  3225. begin
  3226. Active:=True;
  3227. MemIdx:=0;
  3228. if not ReadExpr(Offset) then
  3229. begin
  3230. InputError('Error reading memory offset of segment from the data section');
  3231. exit;
  3232. end;
  3233. end;
  3234. 1:
  3235. Active:=False;
  3236. 2:
  3237. begin
  3238. Active:=True;
  3239. if not ReadUleb32(MemIdx) then
  3240. begin
  3241. InputError('Error reading MemIdx of segment from the data section');
  3242. exit;
  3243. end;
  3244. if not ReadExpr(Offset) then
  3245. begin
  3246. InputError('Error reading memory offset of segment from the data section');
  3247. exit;
  3248. end;
  3249. end;
  3250. else
  3251. begin
  3252. InputError('Unsupported data type of segment in the data section: ' + tostr(DataType));
  3253. exit;
  3254. end;
  3255. end;
  3256. if MemIdx<>0 then
  3257. begin
  3258. InputError('Memory index other than 0 not supported (got ' + tostr(MemIdx) + ')');
  3259. exit;
  3260. end;
  3261. if not Active then
  3262. begin
  3263. InputError('Passive memory segments not supported');
  3264. exit;
  3265. end;
  3266. if not ReadUleb32(Len) then
  3267. begin
  3268. InputError('Error reading data segment length');
  3269. exit;
  3270. end;
  3271. if (AReader.Pos+Len)>(SectionStart+SectionSize) then
  3272. begin
  3273. InputError('Data segment exceeds the bounds of the data section');
  3274. exit;
  3275. end;
  3276. DataPos:=AReader.Pos;
  3277. DataSectionOffset:=AReader.Pos-SectionStart;
  3278. AReader.Seek(AReader.Pos+Len);
  3279. end;
  3280. if AReader.Pos<>(SectionStart+SectionSize) then
  3281. begin
  3282. InputError('Unexpected data section size');
  3283. exit;
  3284. end;
  3285. Result:=true;
  3286. end;
  3287. function ReadDataCountSection: Boolean;
  3288. var
  3289. DataCount: uint32;
  3290. begin
  3291. Result:=False;
  3292. if DataCountSectionRead then
  3293. begin
  3294. InputError('Data count section is duplicated');
  3295. exit;
  3296. end;
  3297. DataCountSectionRead:=True;
  3298. if DataSectionRead then
  3299. begin
  3300. InputError('The data count section must occur before the data section');
  3301. exit;
  3302. end;
  3303. if not ReadUleb32(DataCount) then
  3304. begin
  3305. InputError('Error reading the data count from the data count section');
  3306. exit;
  3307. end;
  3308. if AReader.Pos<>(SectionStart+SectionSize) then
  3309. begin
  3310. InputError('Unexpected data count section size');
  3311. exit;
  3312. end;
  3313. SetLength(DataSegments, DataCount);
  3314. Result:=true;
  3315. end;
  3316. begin
  3317. Result:=False;
  3318. Inc(SectionIndex);
  3319. if not AReader.read(SectionId,1) then
  3320. begin
  3321. InputError('Error reading section ID');
  3322. exit;
  3323. end;
  3324. CheckSectionBounds:=false;
  3325. if not ReadUleb32(SectionSize) then
  3326. begin
  3327. InputError('Error reading section size');
  3328. exit;
  3329. end;
  3330. if (AReader.Pos+SectionSize)>AReader.size then
  3331. begin
  3332. InputError('Section exceeds beyond the end of file');
  3333. exit;
  3334. end;
  3335. SectionStart:=AReader.Pos;
  3336. CheckSectionBounds:=true;
  3337. case SectionId of
  3338. Byte(wsiCustom):
  3339. if not ReadCustomSection then
  3340. begin
  3341. InputError('Error encountered, while reading a custom section');
  3342. exit;
  3343. end;
  3344. Byte(wsiType):
  3345. if not ReadTypeSection then
  3346. begin
  3347. InputError('Error reading the type section');
  3348. exit;
  3349. end;
  3350. Byte(wsiImport):
  3351. if not ReadImportSection then
  3352. begin
  3353. InputError('Error reading the import section');
  3354. exit;
  3355. end;
  3356. Byte(wsiFunction):
  3357. if not ReadFunctionSection then
  3358. begin
  3359. InputError('Error reading the function section');
  3360. exit;
  3361. end;
  3362. Byte(wsiGlobal):
  3363. if not ReadGlobalSection then
  3364. begin
  3365. InputError('Error reading the global section');
  3366. exit;
  3367. end;
  3368. Byte(wsiExport):
  3369. if not ReadExportSection then
  3370. begin
  3371. InputError('Error reading the export section');
  3372. exit;
  3373. end;
  3374. Byte(wsiCode):
  3375. if not ReadCodeSection then
  3376. begin
  3377. InputError('Error reading the code section');
  3378. exit;
  3379. end;
  3380. Byte(wsiData):
  3381. if not ReadDataSection then
  3382. begin
  3383. InputError('Error reading the data section');
  3384. exit;
  3385. end;
  3386. Byte(wsiDataCount):
  3387. begin
  3388. if not ReadDataCountSection then
  3389. begin
  3390. InputError('Error reading the data count section');
  3391. exit;
  3392. end;
  3393. end
  3394. else
  3395. begin
  3396. InputError('Unknown section: ' + ToStr(SectionId));
  3397. exit;
  3398. end;
  3399. end;
  3400. if SectionSize>0 then
  3401. AReader.seek(SectionStart+SectionSize);
  3402. Result:=True;
  3403. end;
  3404. function FindCodeSegment(Ofs: uint32): Integer;
  3405. var
  3406. L, R, M: Integer;
  3407. begin
  3408. L:=Low(CodeSegments);
  3409. R:=High(CodeSegments);
  3410. while L<=R do
  3411. begin
  3412. M:=(L+R) div 2;
  3413. if (CodeSegments[M].CodeSectionOffset+CodeSegments[M].CodeSize-1) < Ofs then
  3414. L:=M+1
  3415. else if CodeSegments[M].CodeSectionOffset > Ofs then
  3416. R:=M-1
  3417. else
  3418. begin
  3419. Result:=M;
  3420. exit;
  3421. end;
  3422. end;
  3423. Result:=-1;
  3424. end;
  3425. function FindDataSegment(Ofs: uint32): Integer;
  3426. var
  3427. L, R, M: Integer;
  3428. begin
  3429. L:=Low(DataSegments);
  3430. R:=High(DataSegments);
  3431. while L<=R do
  3432. begin
  3433. M:=(L+R) div 2;
  3434. if (DataSegments[M].DataSectionOffset+DataSegments[M].Len-1) < Ofs then
  3435. L:=M+1
  3436. else if DataSegments[M].DataSectionOffset > Ofs then
  3437. R:=M-1
  3438. else
  3439. begin
  3440. Result:=M;
  3441. exit;
  3442. end;
  3443. end;
  3444. Result:=-1;
  3445. end;
  3446. var
  3447. ModuleMagic: array [0..3] of Byte;
  3448. ModuleVersion: array [0..3] of Byte;
  3449. i, j, FirstDataSegmentIdx, SegI: Integer;
  3450. CurrSec, ObjSec: TObjSection;
  3451. BaseSectionOffset: UInt32;
  3452. ObjReloc: TWasmObjRelocation;
  3453. begin
  3454. FReader:=AReader;
  3455. InputFileName:=AReader.FileName;
  3456. objdata:=CObjData.Create(InputFileName);
  3457. result:=false;
  3458. CodeSegments:=nil;
  3459. DataSegments:=nil;
  3460. SymbolTable:=nil;
  3461. RelocationTable:=nil;
  3462. SetLength(RelocationTable,2);
  3463. FuncTypes:=nil;
  3464. FuncTypeImportsCount:=0;
  3465. TableTypes:=nil;
  3466. TableTypeImportsCount:=0;
  3467. MemTypes:=nil;
  3468. MemTypeImportsCount:=0;
  3469. GlobalTypes:=nil;
  3470. GlobalTypeImportsCount:=0;
  3471. if not AReader.read(ModuleMagic,4) then
  3472. exit;
  3473. for i:=0 to 3 do
  3474. if ModuleMagic[i]<>WasmModuleMagic[i] then
  3475. exit;
  3476. if not AReader.read(ModuleVersion,4) then
  3477. exit;
  3478. for i:=0 to 3 do
  3479. if ModuleVersion[i]<>WasmVersion[i] then
  3480. exit;
  3481. while AReader.Pos<AReader.size do
  3482. if not ReadSection then
  3483. exit;
  3484. { fill the code segment names }
  3485. for i:=low(SymbolTable) to high(SymbolTable) do
  3486. with SymbolTable[i] do
  3487. if (SymKind=byte(SYMTAB_FUNCTION)) and ((SymFlags and WASM_SYM_UNDEFINED)=0) then
  3488. begin
  3489. if FuncTypes[SymIndex].IsImport then
  3490. begin
  3491. InputError('WASM_SYM_UNDEFINED not set on a SYMTAB_FUNCTION symbol, that is an import');
  3492. exit;
  3493. end;
  3494. with CodeSegments[SymIndex-FuncTypeImportsCount] do
  3495. begin
  3496. SegName:='.text.n_'+SymName;
  3497. SegIsExported:=FuncTypes[SymIndex].IsExported;
  3498. end;
  3499. end;
  3500. { create segments }
  3501. for i:=low(CodeSegments) to high(CodeSegments) do
  3502. with CodeSegments[i] do
  3503. begin
  3504. if SegIsExported then
  3505. CurrSec:=ObjData.createsection(SegName,1,[oso_executable,oso_Data,oso_load,oso_keep],false)
  3506. else
  3507. CurrSec:=ObjData.createsection(SegName,1,[oso_executable,oso_Data,oso_load],false);
  3508. CurrSec.DataPos:=DataPos;
  3509. CurrSec.Size:=CodeSize;
  3510. end;
  3511. FirstDataSegmentIdx:=ObjData.ObjSectionList.Count;
  3512. for i:=low(DataSegments) to high(DataSegments) do
  3513. with DataSegments[i] do
  3514. if Active then
  3515. begin
  3516. CurrSec:=ObjData.createsection(SegName,1 shl SegAlignment,[oso_Data,oso_load,oso_write],false);
  3517. CurrSec.DataPos:=DataPos;
  3518. CurrSec.MemPos:=Offset;
  3519. CurrSec.Size:=Len;
  3520. end;
  3521. ReadSectionContent(ObjData);
  3522. for i:=low(SymbolTable) to high(SymbolTable) do
  3523. with SymbolTable[i] do
  3524. case SymKind of
  3525. byte(SYMTAB_DATA):
  3526. if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
  3527. begin
  3528. objsym:=ObjData.CreateSymbol(SymName);
  3529. objsym.bind:=AB_EXTERNAL;
  3530. objsym.typ:=AT_DATA;
  3531. objsym.objsection:=nil;
  3532. objsym.offset:=0;
  3533. objsym.size:=0;
  3534. end
  3535. else
  3536. begin
  3537. objsym:=ObjData.CreateSymbol(SymName);
  3538. if (SymFlags and WASM_SYM_BINDING_LOCAL)<> 0 then
  3539. objsym.bind:=AB_LOCAL
  3540. else
  3541. objsym.bind:=AB_GLOBAL;
  3542. objsym.typ:=AT_DATA;
  3543. objsym.objsection:=TObjSection(ObjData.ObjSectionList[FirstDataSegmentIdx+SymIndex]);
  3544. objsym.offset:=SymOffset;
  3545. objsym.size:=SymSize;
  3546. end;
  3547. byte(SYMTAB_FUNCTION):
  3548. if (SymFlags and WASM_SYM_UNDEFINED)<>0 then
  3549. begin
  3550. if not FuncTypes[SymIndex].IsImport then
  3551. begin
  3552. InputError('WASM_SYM_UNDEFINED set on a SYMTAB_FUNCTION symbol, that is not an import');
  3553. exit;
  3554. end;
  3555. if (SymFlags and WASM_SYM_EXPLICIT_NAME)<>0 then
  3556. begin
  3557. Writeln('Alias: ', SymName, ' -> ', FuncTypes[SymIndex].ImportModName, '.', FuncTypes[SymIndex].ImportName);
  3558. {todo...}
  3559. end
  3560. else
  3561. begin
  3562. if FuncTypes[SymIndex].ImportModName = 'env' then
  3563. objsym:=ObjData.CreateSymbol(FuncTypes[SymIndex].ImportName)
  3564. else
  3565. objsym:=ObjData.CreateSymbol(FuncTypes[SymIndex].ImportModName + '.' + FuncTypes[SymIndex].ImportName);
  3566. objsym.bind:=AB_EXTERNAL;
  3567. objsym.typ:=AT_FUNCTION;
  3568. objsym.objsection:=nil;
  3569. objsym.offset:=0;
  3570. objsym.size:=0;
  3571. end;
  3572. end
  3573. else
  3574. begin
  3575. objsym:=ObjData.CreateSymbol(SymName);
  3576. objsym.bind:=AB_GLOBAL;
  3577. objsym.typ:=AT_FUNCTION;
  3578. objsym.objsection:=TObjSection(ObjData.ObjSectionList[SymIndex-FuncTypeImportsCount]);
  3579. objsym.offset:=0;
  3580. objsym.size:=objsym.objsection.Size;
  3581. end;
  3582. byte(SYMTAB_GLOBAL),
  3583. byte(SYMTAB_SECTION),
  3584. byte(SYMTAB_EVENT),
  3585. byte(SYMTAB_TABLE):
  3586. {TODO};
  3587. else
  3588. internalerror(2023122701);
  3589. end;
  3590. for j:=0 to high(RelocationTable) do
  3591. for i:=0 to high(RelocationTable[j]) do
  3592. with RelocationTable[j,i] do
  3593. begin
  3594. case j of
  3595. 0:
  3596. begin
  3597. SegI:=FindCodeSegment(RelocOffset);
  3598. if SegI=-1 then
  3599. begin
  3600. InputError('Relocation offset not found in code segment');
  3601. Exit;
  3602. end;
  3603. BaseSectionOffset:=CodeSegments[SegI].CodeSectionOffset;
  3604. ObjSec:=TObjSection(ObjData.ObjSectionList[SegI]);
  3605. end;
  3606. 1:
  3607. begin
  3608. SegI:=FindDataSegment(RelocOffset);
  3609. if SegI=-1 then
  3610. begin
  3611. InputError('Relocation offset not found in data segment');
  3612. Exit;
  3613. end;
  3614. BaseSectionOffset:=DataSegments[SegI].DataSectionOffset;
  3615. ObjSec:=TObjSection(ObjData.ObjSectionList[FirstDataSegmentIdx+SegI]);
  3616. end
  3617. else
  3618. internalerror(2023122801);
  3619. end;
  3620. case TWasmRelocationType(RelocType) of
  3621. R_WASM_FUNCTION_INDEX_LEB:
  3622. begin
  3623. if RelocIndex>high(SymbolTable) then
  3624. begin
  3625. InputError('Symbol index in relocation too high');
  3626. exit;
  3627. end;
  3628. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3629. ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_FUNCTION_INDEX_LEB))
  3630. else
  3631. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3632. end;
  3633. R_WASM_TABLE_INDEX_SLEB:
  3634. begin
  3635. if RelocIndex>high(SymbolTable) then
  3636. begin
  3637. InputError('Symbol index in relocation too high');
  3638. exit;
  3639. end;
  3640. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3641. ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB))
  3642. else
  3643. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3644. end;
  3645. R_WASM_TABLE_INDEX_I32:
  3646. begin
  3647. if RelocIndex>high(SymbolTable) then
  3648. begin
  3649. InputError('Symbol index in relocation too high');
  3650. exit;
  3651. end;
  3652. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3653. ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE))
  3654. else
  3655. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3656. end;
  3657. R_WASM_MEMORY_ADDR_LEB:
  3658. begin
  3659. if RelocIndex>high(SymbolTable) then
  3660. begin
  3661. InputError('Symbol index in relocation too high');
  3662. exit;
  3663. end;
  3664. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3665. begin
  3666. ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_MEMORY_ADDR_LEB);
  3667. ObjReloc.Addend:=RelocAddend;
  3668. ObjSec.ObjRelocations.Add(ObjReloc);
  3669. end
  3670. else
  3671. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3672. end;
  3673. R_WASM_MEMORY_ADDR_SLEB:
  3674. begin
  3675. if RelocIndex>high(SymbolTable) then
  3676. begin
  3677. InputError('Symbol index in relocation too high');
  3678. exit;
  3679. end;
  3680. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3681. begin
  3682. ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB);
  3683. ObjReloc.Addend:=RelocAddend;
  3684. ObjSec.ObjRelocations.Add(ObjReloc);
  3685. end
  3686. else
  3687. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3688. end;
  3689. R_WASM_MEMORY_ADDR_I32:
  3690. begin
  3691. if RelocIndex>high(SymbolTable) then
  3692. begin
  3693. InputError('Symbol index in relocation too high');
  3694. exit;
  3695. end;
  3696. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3697. begin
  3698. ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE);
  3699. ObjReloc.Addend:=RelocAddend;
  3700. ObjSec.ObjRelocations.Add(ObjReloc);
  3701. end
  3702. else
  3703. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3704. end;
  3705. R_WASM_TYPE_INDEX_LEB:
  3706. begin
  3707. if RelocIndex>high(FFuncTypes) then
  3708. begin
  3709. InputError('Type index in relocation too high');
  3710. exit;
  3711. end;
  3712. ObjSec.ObjRelocations.Add(TWasmObjRelocation.CreateTypeIndex(RelocOffset-BaseSectionOffset,RelocIndex));
  3713. end;
  3714. R_WASM_FUNCTION_OFFSET_I32:
  3715. begin
  3716. if RelocIndex>high(SymbolTable) then
  3717. begin
  3718. InputError('Symbol index in relocation too high');
  3719. exit;
  3720. end;
  3721. if Assigned(SymbolTable[RelocIndex].ObjSym) then
  3722. begin
  3723. ObjReloc:=TWasmObjRelocation.CreateSymbol(RelocOffset-BaseSectionOffset,SymbolTable[RelocIndex].ObjSym,RELOC_ABSOLUTE);
  3724. ObjReloc.Addend:=RelocAddend;
  3725. ObjSec.ObjRelocations.Add(ObjReloc);
  3726. end
  3727. else
  3728. Writeln('Warning! No object symbol created for ', SymbolTable[RelocIndex].SymName);
  3729. end;
  3730. R_WASM_SECTION_OFFSET_I32,
  3731. R_WASM_GLOBAL_INDEX_LEB,
  3732. R_WASM_TAG_INDEX_LEB:
  3733. {TODO};
  3734. else
  3735. internalerror(2023122802);
  3736. end;
  3737. end;
  3738. Result:=True;
  3739. end;
  3740. {****************************************************************************
  3741. TWasmExeOutput
  3742. ****************************************************************************}
  3743. function TWasmExeOutput.writeData: boolean;
  3744. begin
  3745. result:=false;
  3746. {TODO: implement}
  3747. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  3748. Writer.write(WasmVersion,SizeOf(WasmVersion));
  3749. result := true;
  3750. end;
  3751. procedure TWasmExeOutput.DoRelocationFixup(objsec: TObjSection);
  3752. begin
  3753. {TODO: implement}
  3754. end;
  3755. constructor TWasmExeOutput.create;
  3756. begin
  3757. inherited create;
  3758. CObjData:=TWasmObjData;
  3759. MaxMemPos:=$FFFFFFFF;
  3760. end;
  3761. {****************************************************************************
  3762. TWasmAssembler
  3763. ****************************************************************************}
  3764. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  3765. begin
  3766. inherited;
  3767. CObjOutput:=TWasmObjOutput;
  3768. end;
  3769. {*****************************************************************************
  3770. Initialize
  3771. *****************************************************************************}
  3772. {$ifdef wasm32}
  3773. const
  3774. as_wasm32_wasm_info : tasminfo =
  3775. (
  3776. id : as_wasm32_wasm;
  3777. idtxt : 'WASM';
  3778. asmbin : '';
  3779. asmcmd : '';
  3780. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  3781. flags : [af_outputbinary,af_smartlink_sections];
  3782. labelprefix : '..@';
  3783. labelmaxlen : -1;
  3784. comment : '; ';
  3785. dollarsign: '$';
  3786. );
  3787. {$endif wasm32}
  3788. initialization
  3789. {$ifdef wasm32}
  3790. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  3791. {$endif wasm32}
  3792. end.