ogwasm.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  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. function ReadUleb(r: TObjectReader; out v: uint64): boolean;
  164. public
  165. constructor create;override;
  166. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  167. function ReadObjData(AReader:TObjectreader;out ObjData:TObjData):boolean;override;
  168. end;
  169. { TWasmAssembler }
  170. TWasmAssembler = class(tinternalassembler)
  171. constructor create(info: pasminfo; smart:boolean);override;
  172. end;
  173. implementation
  174. uses
  175. verbose,version,globals;
  176. procedure WriteUleb5(d: tdynamicarray; v: uint64);
  177. var
  178. b: byte;
  179. i: Integer;
  180. begin
  181. for i:=1 to 5 do
  182. begin
  183. b:=byte(v) and 127;
  184. v:=v shr 7;
  185. if i<>5 then
  186. b:=b or 128;
  187. d.write(b,1);
  188. end;
  189. end;
  190. procedure WriteUleb5(d: tobjsection; v: uint64);
  191. var
  192. b: byte;
  193. i: Integer;
  194. begin
  195. for i:=1 to 5 do
  196. begin
  197. b:=byte(v) and 127;
  198. v:=v shr 7;
  199. if i<>5 then
  200. b:=b or 128;
  201. d.write(b,1);
  202. end;
  203. end;
  204. procedure WriteSleb5(d: tdynamicarray; v: int64);
  205. var
  206. b: byte;
  207. i: Integer;
  208. begin
  209. for i:=1 to 5 do
  210. begin
  211. b:=byte(v) and 127;
  212. v:=SarInt64(v,7);
  213. if i<>5 then
  214. b:=b or 128;
  215. d.write(b,1);
  216. end;
  217. end;
  218. procedure WriteSleb5(d: tobjsection; v: int64);
  219. var
  220. b: byte;
  221. i: Integer;
  222. begin
  223. for i:=1 to 5 do
  224. begin
  225. b:=byte(v) and 127;
  226. v:=SarInt64(v,7);
  227. if i<>5 then
  228. b:=b or 128;
  229. d.write(b,1);
  230. end;
  231. end;
  232. function ReadUleb(d: tdynamicarray): uint64;
  233. var
  234. b: byte;
  235. shift:integer;
  236. begin
  237. b:=0;
  238. result:=0;
  239. shift:=0;
  240. repeat
  241. d.read(b,1);
  242. result:=result or (uint64(b and 127) shl shift);
  243. inc(shift,7);
  244. until (b and 128)=0;
  245. end;
  246. function ReadSleb(d: tdynamicarray): int64;
  247. var
  248. b: byte;
  249. shift:integer;
  250. begin
  251. b:=0;
  252. result:=0;
  253. shift:=0;
  254. repeat
  255. d.read(b,1);
  256. result:=result or (uint64(b and 127) shl shift);
  257. inc(shift,7);
  258. until (b and 128)=0;
  259. {$ifopt Q+}
  260. {$define overflowon}
  261. {$Q-}
  262. {$endif}
  263. {$ifopt R+}
  264. {$define rangeon}
  265. {$R-}
  266. {$endif}
  267. if (b and 64)<>0 then
  268. result:=result or (high(uint64) shl shift);
  269. end;
  270. {$ifdef overflowon}
  271. {$Q+}
  272. {$undef overflowon}
  273. {$endif}
  274. {$ifdef rangeon}
  275. {$R+}
  276. {$undef rangeon}
  277. {$endif}
  278. procedure AddSleb5(d: tdynamicarray; v: int64);
  279. var
  280. q: Int64;
  281. p: LongWord;
  282. begin
  283. p:=d.Pos;
  284. q:=ReadSleb(d);
  285. q:=q+v;
  286. d.seek(p);
  287. WriteSleb5(d,q);
  288. end;
  289. procedure AddUleb5(d: tdynamicarray; v: int64);
  290. var
  291. q: UInt64;
  292. p: LongWord;
  293. begin
  294. p:=d.Pos;
  295. q:=ReadUleb(d);
  296. q:=q+v;
  297. d.seek(p);
  298. WriteUleb5(d,q);
  299. end;
  300. procedure AddInt32(d: tdynamicarray; v: int32);
  301. var
  302. q: int32;
  303. p: LongWord;
  304. begin
  305. p:=d.Pos;
  306. d.read(q,4);
  307. {$ifdef FPC_BIG_ENDIAN}
  308. q:=SwapEndian(q);
  309. {$endif FPC_BIG_ENDIAN}
  310. q:=q+v;
  311. {$ifdef FPC_BIG_ENDIAN}
  312. q:=SwapEndian(q);
  313. {$endif FPC_BIG_ENDIAN}
  314. d.seek(p);
  315. d.write(q,4);
  316. end;
  317. {****************************************************************************
  318. TWasmObjRelocation
  319. ****************************************************************************}
  320. constructor TWasmObjRelocation.CreateTypeIndex(ADataOffset: TObjSectionOfs; ATypeIndex: Integer);
  321. begin
  322. DataOffset:=ADataOffset;
  323. Symbol:=nil;
  324. OrgSize:=0;
  325. Group:=nil;
  326. ObjSection:=nil;
  327. ftype:=ord(RELOC_TYPE_INDEX_LEB);
  328. TypeIndex:=ATypeIndex;
  329. end;
  330. {****************************************************************************
  331. TWasmObjSymbol
  332. ****************************************************************************}
  333. constructor TWasmObjSymbol.create(AList: TFPHashObjectList; const AName: string);
  334. begin
  335. inherited create(AList,AName);
  336. FuncIndex:=-1;
  337. SymbolIndex:=-1;
  338. GlobalIndex:=-1;
  339. TagIndex:=-1;
  340. AliasOf:='';
  341. ExtraData:=nil;
  342. end;
  343. function TWasmObjSymbol.IsAlias: Boolean;
  344. begin
  345. result:=AliasOf<>'';
  346. end;
  347. {****************************************************************************
  348. TWasmObjSymbolExtraData
  349. ****************************************************************************}
  350. constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  351. begin
  352. inherited Create(HashObjectList,s);
  353. TypeIdx:=-1;
  354. ExceptionTagTypeIdx:=-1;
  355. end;
  356. procedure TWasmObjSymbolExtraData.AddLocal(bastyp: TWasmBasicType);
  357. begin
  358. SetLength(Locals,Length(Locals)+1);
  359. Locals[High(Locals)]:=bastyp;
  360. end;
  361. {****************************************************************************
  362. TWasmObjSection
  363. ****************************************************************************}
  364. constructor TWasmObjSection.create(AList: TFPHashObjectList; const Aname: string; Aalign: longint; Aoptions: TObjSectionOptions);
  365. begin
  366. inherited create(AList, Aname, Aalign, Aoptions);
  367. SegIdx:=-1;
  368. SegSymIdx:=-1;
  369. MainFuncSymbol:=nil;
  370. end;
  371. function TWasmObjSection.IsCode: Boolean;
  372. const
  373. CodePrefix = '.text';
  374. begin
  375. result:=(Length(Name)>=Length(CodePrefix)) and
  376. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  377. end;
  378. function TWasmObjSection.IsData: Boolean;
  379. begin
  380. result:=not (IsCode or IsDebug);
  381. end;
  382. function TWasmObjSection.IsDebug: Boolean;
  383. const
  384. DebugPrefix = '.debug';
  385. begin
  386. result:=(Length(Name)>=Length(DebugPrefix)) and
  387. (Copy(Name,1,Length(DebugPrefix))=DebugPrefix);
  388. end;
  389. {****************************************************************************
  390. TWasmObjData
  391. ****************************************************************************}
  392. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  393. begin
  394. { For bss we need to set some flags that are target dependent,
  395. it is easier to disable it for smartlinking. It doesn't take up
  396. filespace }
  397. result:=not(target_info.system in systems_darwin) and
  398. create_smartlink_sections and
  399. (atype<>sec_toc) and
  400. (atype<>sec_user) and
  401. { on embedded systems every byte counts, so smartlink bss too }
  402. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  403. end;
  404. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  405. const aname: string; aorder: TAsmSectionOrder): string;
  406. const
  407. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  408. '.text',
  409. '.data',
  410. { why doesn't .rodata work? (FK) }
  411. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  412. { vtables (and anything else containing relocations), otherwise those are }
  413. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  414. { vtable for a class called Window: }
  415. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  416. { TODO: .data.ro not yet working}
  417. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  418. '.rodata',
  419. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  420. '.data',
  421. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  422. '.rodata',
  423. '.bss',
  424. '.tbss',
  425. '.pdata',
  426. '', { stubs }
  427. '__DATA,__nl_symbol_ptr',
  428. '__DATA,__la_symbol_ptr',
  429. '__DATA,__mod_init_func',
  430. '__DATA,__mod_term_func',
  431. '.stab',
  432. '.stabstr',
  433. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  434. '.eh_frame',
  435. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  436. '.fpc',
  437. '.toc',
  438. '.init',
  439. '.fini',
  440. '.objc_class',
  441. '.objc_meta_class',
  442. '.objc_cat_cls_meth',
  443. '.objc_cat_inst_meth',
  444. '.objc_protocol',
  445. '.objc_string_object',
  446. '.objc_cls_meth',
  447. '.objc_inst_meth',
  448. '.objc_cls_refs',
  449. '.objc_message_refs',
  450. '.objc_symbols',
  451. '.objc_category',
  452. '.objc_class_vars',
  453. '.objc_instance_vars',
  454. '.objc_module_info',
  455. '.objc_class_names',
  456. '.objc_meth_var_types',
  457. '.objc_meth_var_names',
  458. '.objc_selector_strs',
  459. '.objc_protocol_ext',
  460. '.objc_class_ext',
  461. '.objc_property',
  462. '.objc_image_info',
  463. '.objc_cstring_object',
  464. '.objc_sel_fixup',
  465. '__DATA,__objc_data',
  466. '__DATA,__objc_const',
  467. '.objc_superrefs',
  468. '__DATA, __datacoal_nt,coalesced',
  469. '.objc_classlist',
  470. '.objc_nlclasslist',
  471. '.objc_catlist',
  472. '.obcj_nlcatlist',
  473. '.objc_protolist',
  474. '.stack',
  475. '.heap',
  476. '.gcc_except_table',
  477. '.ARM.attributes'
  478. );
  479. var
  480. sep : string[3];
  481. secname : string;
  482. begin
  483. secname:=secnames[atype];
  484. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  485. begin
  486. result:=secname+'.'+aname;
  487. exit;
  488. end;
  489. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  490. Thus, data which normally goes into .rodata and .rodata_norel sections must
  491. end up in .data section }
  492. if (atype in [sec_rodata,sec_rodata_norel]) and
  493. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  494. secname:='.data';
  495. { Windows correctly handles reallocations in readonly sections }
  496. if (atype=sec_rodata) and
  497. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  498. secname:='.rodata';
  499. { section type user gives the user full controll on the section name }
  500. if atype=sec_user then
  501. secname:=aname;
  502. if is_smart_section(atype) and (aname<>'') then
  503. begin
  504. case aorder of
  505. secorder_begin :
  506. sep:='.b_';
  507. secorder_end :
  508. sep:='.z_';
  509. else
  510. sep:='.n_';
  511. end;
  512. result:=secname+sep+aname
  513. end
  514. else
  515. result:=secname;
  516. end;
  517. constructor TWasmObjData.create(const n: string);
  518. begin
  519. inherited;
  520. CObjSection:=TWasmObjSection;
  521. CObjSymbol:=TWasmObjSymbol;
  522. FObjSymbolsExtraDataList:=TFPHashObjectList.Create;
  523. end;
  524. destructor TWasmObjData.destroy;
  525. var
  526. i: Integer;
  527. begin
  528. FObjSymbolsExtraDataList.Free;
  529. for i:=low(FFuncTypes) to high(FFuncTypes) do
  530. begin
  531. FFuncTypes[i].free;
  532. FFuncTypes[i]:=nil;
  533. end;
  534. inherited destroy;
  535. end;
  536. function TWasmObjData.sectionname(atype: TAsmSectiontype;
  537. const aname: string; aorder: TAsmSectionOrder): string;
  538. begin
  539. if (atype=sec_fpc) or
  540. ((atype=sec_threadvar) and not (ts_wasm_threads in current_settings.targetswitches)) then
  541. atype:=sec_data;
  542. Result:=sectionname_gas(atype, aname, aorder);
  543. end;
  544. procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
  545. p: TObjSymbol; Reloctype: TObjRelocationType);
  546. const
  547. leb_zero: array[0..4] of byte=($80,$80,$80,$80,$00);
  548. var
  549. objreloc: TWasmObjRelocation;
  550. begin
  551. if CurrObjSec=nil then
  552. internalerror(200403072);
  553. { workaround crash, when generating debug info for threadvars, when multithreading is turned off.
  554. todo: ensure the debug info for threadvars is actually correct, once we've got WebAssembly debug info working in general }
  555. if (Reloctype=RELOC_DTPOFF) and not (ts_wasm_threads in current_settings.targetswitches) then
  556. Reloctype:=RELOC_ABSOLUTE;
  557. objreloc:=nil;
  558. case Reloctype of
  559. RELOC_FUNCTION_INDEX_LEB:
  560. begin
  561. if Data<>0 then
  562. internalerror(2021092502);
  563. if len<>5 then
  564. internalerror(2021092503);
  565. if not assigned(p) then
  566. internalerror(2021092504);
  567. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  568. CurrObjSec.ObjRelocations.Add(objreloc);
  569. writebytes(leb_zero,5);
  570. end;
  571. RELOC_MEMORY_ADDR_LEB,
  572. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  573. begin
  574. if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then
  575. internalerror(2021092602);
  576. if len<>5 then
  577. internalerror(2021092503);
  578. if not assigned(p) then
  579. internalerror(2021092504);
  580. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  581. objreloc.Addend:=Data;
  582. CurrObjSec.ObjRelocations.Add(objreloc);
  583. if RelocType=RELOC_MEMORY_ADDR_LEB then
  584. WriteUleb5(CurrObjSec,Data)
  585. else
  586. WriteSleb5(CurrObjSec,Data);
  587. end;
  588. RELOC_ABSOLUTE:
  589. begin
  590. if len<>4 then
  591. internalerror(2021092607);
  592. if not assigned(p) then
  593. internalerror(2021092608);
  594. if (p.objsection<>nil) and TWasmObjSection(p.objsection).IsDebug and
  595. (p.bind<>AB_COMMON) and (p.bind<>AB_EXTERNAL) then
  596. begin
  597. objreloc:=TWasmObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,RELOC_ABSOLUTE);
  598. objreloc.Addend:=Data+p.Address;
  599. CurrObjSec.ObjRelocations.Add(objreloc);
  600. {inc(data,p.address);}
  601. data:=0;
  602. Data:=NtoLE(Data);
  603. writebytes(Data,4);
  604. end
  605. else
  606. begin
  607. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  608. objreloc.Addend:=Data;
  609. CurrObjSec.ObjRelocations.Add(objreloc);
  610. Data:=NtoLE(Data);
  611. writebytes(Data,4);
  612. end;
  613. end;
  614. RELOC_TYPE_INDEX_LEB:
  615. begin
  616. if len<>5 then
  617. internalerror(2021092612);
  618. if assigned(p) then
  619. internalerror(2021092613);
  620. objreloc:=TWasmObjRelocation.CreateTypeIndex(CurrObjSec.Size,Data);
  621. CurrObjSec.ObjRelocations.Add(objreloc);
  622. WriteUleb5(CurrObjSec,Data);
  623. end;
  624. RELOC_GLOBAL_INDEX_LEB:
  625. begin
  626. if len<>5 then
  627. internalerror(2021092701);
  628. if Data<>0 then
  629. internalerror(2021092702);
  630. if not assigned(p) then
  631. internalerror(2021092703);
  632. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  633. CurrObjSec.ObjRelocations.Add(objreloc);
  634. WriteUleb5(CurrObjSec,0);
  635. end;
  636. RELOC_TAG_INDEX_LEB:
  637. begin
  638. if len<>5 then
  639. internalerror(2021092712);
  640. if Data<>0 then
  641. internalerror(2021092713);
  642. if not assigned(p) then
  643. internalerror(2021092714);
  644. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  645. CurrObjSec.ObjRelocations.Add(objreloc);
  646. WriteSleb5(CurrObjSec,0);
  647. end;
  648. else
  649. internalerror(2021092501);
  650. end;
  651. end;
  652. function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
  653. begin
  654. result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
  655. if not assigned(result) then
  656. result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
  657. end;
  658. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  659. var
  660. i: Integer;
  661. begin
  662. for i:=low(FFuncTypes) to high(FFuncTypes) do
  663. if wft.Equals(FFuncTypes[i]) then
  664. exit(i);
  665. result:=Length(FFuncTypes);
  666. SetLength(FFuncTypes,result+1);
  667. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  668. end;
  669. function TWasmObjData.globalref(asmsym: TAsmSymbol): TObjSymbol;
  670. begin
  671. if assigned(asmsym) then
  672. begin
  673. if (asmsym.typ<>AT_WASM_GLOBAL) and (asmsym.typ<>AT_TLS) then
  674. internalerror(2021092706);
  675. result:=symbolref(asmsym);
  676. result.typ:=asmsym.typ;
  677. end
  678. else
  679. result:=nil;
  680. end;
  681. function TWasmObjData.ExceptionTagRef(asmsym: TAsmSymbol): TObjSymbol;
  682. begin
  683. if assigned(asmsym) then
  684. begin
  685. if asmsym.typ<>AT_WASM_EXCEPTION_TAG then
  686. internalerror(2021092707);
  687. result:=symbolref(asmsym);
  688. result.typ:=AT_WASM_EXCEPTION_TAG;
  689. end
  690. else
  691. result:=nil;
  692. end;
  693. procedure TWasmObjData.DeclareGlobalType(gt: tai_globaltype);
  694. var
  695. ObjSymExtraData: TWasmObjSymbolExtraData;
  696. ObjSym: TObjSymbol;
  697. begin
  698. if not gt.is_external then
  699. begin
  700. ObjSym:=symboldefine(gt.sym);
  701. ObjSym.typ:=AT_WASM_GLOBAL;
  702. end;
  703. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(gt.globalname);
  704. ObjSymExtraData.GlobalType:=gt.gtype;
  705. ObjSymExtraData.GlobalIsImmutable:=gt.immutable;
  706. end;
  707. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  708. var
  709. i: Integer;
  710. ObjSymExtraData: TWasmObjSymbolExtraData;
  711. begin
  712. FLastFuncName:=ft.funcname;
  713. i:=AddFuncType(ft.functype);
  714. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
  715. ObjSymExtraData.TypeIdx:=i;
  716. end;
  717. procedure TWasmObjData.DeclareTagType(tt: tai_tagtype);
  718. var
  719. ObjSymExtraData: TWasmObjSymbolExtraData;
  720. ft: TWasmFuncType;
  721. i: Integer;
  722. begin
  723. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(tt.tagname);
  724. ft:=TWasmFuncType.Create([],tt.params);
  725. i:=AddFuncType(ft);
  726. ft.free;
  727. ObjSymExtraData.ExceptionTagTypeIdx:=i;
  728. end;
  729. procedure TWasmObjData.DeclareExportName(en: tai_export_name);
  730. var
  731. ObjSymExtraData: TWasmObjSymbolExtraData;
  732. begin
  733. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(en.intname);
  734. ObjSymExtraData.ExportName:=en.extname;
  735. end;
  736. procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
  737. var
  738. ObjSymExtraData: TWasmObjSymbolExtraData;
  739. begin
  740. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
  741. ObjSymExtraData.ImportModule:=aim.importmodule;
  742. end;
  743. procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
  744. var
  745. ObjSymExtraData: TWasmObjSymbolExtraData;
  746. begin
  747. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
  748. ObjSymExtraData.ImportName:=ain.importname;
  749. end;
  750. procedure TWasmObjData.DeclareLocal(al: tai_local);
  751. var
  752. ObjSymExtraData: TWasmObjSymbolExtraData;
  753. begin
  754. ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
  755. ObjSymExtraData.AddLocal(al.bastyp);
  756. end;
  757. procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
  758. var
  759. valsym: TObjSymbol;
  760. aliassym: TWasmObjSymbol;
  761. begin
  762. valsym:=CreateSymbol(avalue);
  763. aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
  764. aliassym.AliasOf:=valsym.Name;
  765. end;
  766. {****************************************************************************
  767. TWasmObjOutput
  768. ****************************************************************************}
  769. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  770. var
  771. b: byte;
  772. begin
  773. repeat
  774. b:=byte(v) and 127;
  775. v:=v shr 7;
  776. if v<>0 then
  777. b:=b or 128;
  778. d.write(b,1);
  779. until v=0;
  780. end;
  781. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  782. var
  783. b: byte;
  784. begin
  785. repeat
  786. b:=byte(v) and 127;
  787. v:=v shr 7;
  788. if v<>0 then
  789. b:=b or 128;
  790. w.write(b,1);
  791. until v=0;
  792. end;
  793. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  794. var
  795. b: byte;
  796. Done: Boolean=false;
  797. begin
  798. repeat
  799. b:=byte(v) and 127;
  800. v:=SarInt64(v,7);
  801. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  802. Done:=true
  803. else
  804. b:=b or 128;
  805. d.write(b,1);
  806. until Done;
  807. end;
  808. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  809. begin
  810. d.write(b,1);
  811. end;
  812. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  813. begin
  814. WriteUleb(d,Length(s));
  815. d.writestr(s);
  816. end;
  817. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  818. var
  819. b: byte;
  820. begin
  821. b:=ord(wsid);
  822. Writer.write(b,1);
  823. WriteUleb(Writer,FWasmSections[wsid].size);
  824. Writer.writearray(FWasmSections[wsid]);
  825. end;
  826. procedure TWasmObjOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  827. var
  828. b: byte;
  829. begin
  830. b:=0;
  831. Writer.write(b,1);
  832. WriteUleb(Writer,FWasmCustomSections[wcst].size);
  833. Writer.writearray(FWasmCustomSections[wcst]);
  834. end;
  835. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  836. var
  837. buf: array [0..4095] of byte;
  838. bs: Integer;
  839. begin
  840. while size>0 do
  841. begin
  842. if size<SizeOf(buf) then
  843. bs:=Integer(size)
  844. else
  845. bs:=SizeOf(buf);
  846. src.read(buf,bs);
  847. dest.write(buf,bs);
  848. dec(size,bs);
  849. end;
  850. end;
  851. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  852. var
  853. buf : array[0..1023] of byte;
  854. bs: Integer;
  855. begin
  856. fillchar(buf,sizeof(buf),0);
  857. while size>0 do
  858. begin
  859. if size<SizeOf(buf) then
  860. bs:=Integer(size)
  861. else
  862. bs:=SizeOf(buf);
  863. dest.write(buf,bs);
  864. dec(size,bs);
  865. end;
  866. end;
  867. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  868. var
  869. i: Integer;
  870. begin
  871. WriteUleb(dest,Length(wrt));
  872. for i:=low(wrt) to high(wrt) do
  873. WriteWasmBasicType(dest,wrt[i]);
  874. end;
  875. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  876. begin
  877. WriteByte(dest,encode_wasm_basic_type(wbt));
  878. end;
  879. function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
  880. var
  881. ExtraData: TWasmObjSymbolExtraData;
  882. begin
  883. if sym.bind=AB_EXTERNAL then
  884. begin
  885. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  886. result:=(ExtraData<>nil) and (ExtraData.TypeIdx<>-1);
  887. end
  888. else
  889. result:=false;
  890. end;
  891. function TWasmObjOutput.IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  892. var
  893. ExtraData: TWasmObjSymbolExtraData;
  894. begin
  895. if (sym.typ=AT_FUNCTION) and not sym.IsAlias then
  896. begin
  897. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  898. result:=(ExtraData<>nil) and (ExtraData.ExportName<>'');
  899. end
  900. else
  901. result:=false;
  902. end;
  903. procedure TWasmObjOutput.WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  904. var
  905. i,
  906. rle_entries,
  907. cnt: Integer;
  908. lasttype: TWasmBasicType;
  909. begin
  910. if Length(ed.Locals)=0 then
  911. begin
  912. WriteUleb(dest,0);
  913. exit;
  914. end;
  915. rle_entries:=1;
  916. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  917. if ed.Locals[i]<>ed.Locals[i-1] then
  918. inc(rle_entries);
  919. WriteUleb(dest,rle_entries);
  920. lasttype:=ed.Locals[Low(ed.Locals)];
  921. cnt:=1;
  922. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  923. if ed.Locals[i]=ed.Locals[i-1] then
  924. inc(cnt)
  925. else
  926. begin
  927. WriteUleb(dest,cnt);
  928. WriteWasmBasicType(dest,lasttype);
  929. lasttype:=ed.Locals[i];
  930. cnt:=1;
  931. end;
  932. WriteUleb(dest,cnt);
  933. WriteWasmBasicType(dest,lasttype);
  934. end;
  935. procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  936. var
  937. encoded_locals: tdynamicarray;
  938. ObjSymExtraData: TWasmObjSymbolExtraData;
  939. codelen: LongWord;
  940. ObjSection: TWasmObjSection;
  941. codeexprlen: QWord;
  942. begin
  943. ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  944. ObjSection:=TWasmObjSection(objsym.objsection);
  945. ObjSection.Data.seek(objsym.address);
  946. codeexprlen:=objsym.size;
  947. encoded_locals:=tdynamicarray.Create(64);
  948. WriteFunctionLocals(encoded_locals,ObjSymExtraData);
  949. codelen:=encoded_locals.size+codeexprlen+1;
  950. WriteUleb(dest,codelen);
  951. encoded_locals.seek(0);
  952. CopyDynamicArray(encoded_locals,dest,encoded_locals.size);
  953. ObjSection.FileSectionOfs:=dest.size-objsym.offset;
  954. CopyDynamicArray(ObjSection.Data,dest,codeexprlen);
  955. WriteByte(dest,$0B);
  956. encoded_locals.Free;
  957. end;
  958. procedure TWasmObjOutput.WriteSymbolTable;
  959. begin
  960. WriteUleb(FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTableEntriesCount);
  961. FWasmSymbolTable.seek(0);
  962. CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
  963. end;
  964. procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
  965. begin
  966. WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
  967. WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
  968. FWasmRelocationCodeTable.seek(0);
  969. CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
  970. end;
  971. procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
  972. begin
  973. WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
  974. WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
  975. FWasmRelocationDataTable.seek(0);
  976. CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
  977. end;
  978. procedure TWasmObjOutput.MaybeWriteRelocationDebugTable(cst: TWasmCustomSectionType; SectionIndex: Integer; EntriesCount: Integer; Table: tdynamicarray);
  979. begin
  980. if EntriesCount>0 then
  981. begin
  982. WriteUleb(FWasmCustomSections[cst],SectionIndex);
  983. WriteUleb(FWasmCustomSections[cst],EntriesCount);
  984. Table.seek(0);
  985. CopyDynamicArray(Table,FWasmCustomSections[cst],Table.size);
  986. WriteWasmCustomSection(cst);
  987. end;
  988. end;
  989. procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  990. begin
  991. if FWasmLinkingSubsections[wlst].size>0 then
  992. begin
  993. WriteByte(FWasmCustomSections[wcstLinking],Ord(wlst));
  994. WriteUleb(FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  995. FWasmLinkingSubsections[wlst].seek(0);
  996. CopyDynamicArray(FWasmLinkingSubsections[wlst],FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  997. end;
  998. end;
  999. procedure TWasmObjOutput.DoRelocations;
  1000. var
  1001. si, ri: Integer;
  1002. objsec: TWasmObjSection;
  1003. objrel: TWasmObjRelocation;
  1004. begin
  1005. for si:=0 to FData.ObjSectionList.Count-1 do
  1006. begin
  1007. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  1008. for ri:=0 to objsec.ObjRelocations.Count-1 do
  1009. begin
  1010. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  1011. case objrel.typ of
  1012. RELOC_FUNCTION_INDEX_LEB:
  1013. begin
  1014. if not assigned(objrel.symbol) then
  1015. internalerror(2021092509);
  1016. objsec.Data.seek(objrel.DataOffset);
  1017. if TWasmObjSymbol(objrel.symbol).FuncIndex<0 then
  1018. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1019. else
  1020. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).FuncIndex);
  1021. end;
  1022. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1023. begin
  1024. if not assigned(objrel.symbol) then
  1025. internalerror(2021092605);
  1026. if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  1027. begin
  1028. objsec.Data.seek(objrel.DataOffset);
  1029. AddSleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1030. end;
  1031. end;
  1032. RELOC_MEMORY_ADDR_LEB:
  1033. begin
  1034. if not assigned(objrel.symbol) then
  1035. internalerror(2021092606);
  1036. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1037. internalerror(2021092628);
  1038. if objrel.symbol.bind<>AB_EXTERNAL then
  1039. begin
  1040. objsec.Data.seek(objrel.DataOffset);
  1041. AddUleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1042. end;
  1043. end;
  1044. RELOC_ABSOLUTE:
  1045. begin
  1046. if assigned(objrel.ObjSection) then
  1047. begin
  1048. { todo: should we do something here? }
  1049. //Writeln('todo: section relocation');
  1050. end
  1051. else if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  1052. begin
  1053. objsec.Data.seek(objrel.DataOffset);
  1054. AddInt32(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1055. end;
  1056. end;
  1057. RELOC_TYPE_INDEX_LEB:
  1058. ;
  1059. RELOC_GLOBAL_INDEX_LEB:
  1060. begin
  1061. if not assigned(objrel.symbol) then
  1062. internalerror(2021092509);
  1063. objsec.Data.seek(objrel.DataOffset);
  1064. if TWasmObjSymbol(objrel.symbol).GlobalIndex<0 then
  1065. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1066. else
  1067. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).GlobalIndex);
  1068. end;
  1069. RELOC_TAG_INDEX_LEB:
  1070. begin
  1071. if not assigned(objrel.symbol) then
  1072. internalerror(2021092716);
  1073. objsec.Data.seek(objrel.DataOffset);
  1074. if TWasmObjSymbol(objrel.symbol).TagIndex<0 then
  1075. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1076. else
  1077. WriteSleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).TagIndex);
  1078. end;
  1079. else
  1080. internalerror(2021092510);
  1081. end;
  1082. end;
  1083. end;
  1084. end;
  1085. procedure TWasmObjOutput.WriteRelocations;
  1086. var
  1087. si, ri: Integer;
  1088. objsec: TWasmObjSection;
  1089. objrel: TWasmObjRelocation;
  1090. relout: tdynamicarray;
  1091. relcount: PInteger;
  1092. FuncSym: TWasmObjSymbol;
  1093. begin
  1094. for si:=0 to FData.ObjSectionList.Count-1 do
  1095. begin
  1096. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  1097. if objsec.IsCode then
  1098. begin
  1099. relout:=FWasmRelocationCodeTable;
  1100. relcount:=@FWasmRelocationCodeTableEntriesCount;
  1101. end
  1102. else if objsec.IsData then
  1103. begin
  1104. relout:=FWasmRelocationDataTable;
  1105. relcount:=@FWasmRelocationDataTableEntriesCount;
  1106. end
  1107. else if objsec.IsDebug then
  1108. begin
  1109. case objsec.Name of
  1110. '.debug_frame':
  1111. begin
  1112. relout:=FWasmRelocationDebugFrameTable;
  1113. relcount:=@FWasmRelocationDebugFrameTableEntriesCount;
  1114. end;
  1115. '.debug_info':
  1116. begin
  1117. relout:=FWasmRelocationDebugInfoTable;
  1118. relcount:=@FWasmRelocationDebugInfoTableEntriesCount;
  1119. end;
  1120. '.debug_line':
  1121. begin
  1122. relout:=FWasmRelocationDebugLineTable;
  1123. relcount:=@FWasmRelocationDebugLineTableEntriesCount;
  1124. end;
  1125. '.debug_abbrev':
  1126. begin
  1127. relout:=FWasmRelocationDebugAbbrevTable;
  1128. relcount:=@FWasmRelocationDebugAbbrevTableEntriesCount;
  1129. end;
  1130. '.debug_aranges':
  1131. begin
  1132. relout:=FWasmRelocationDebugArangesTable;
  1133. relcount:=@FWasmRelocationDebugArangesTableEntriesCount;
  1134. end;
  1135. '.debug_ranges':
  1136. begin
  1137. relout:=FWasmRelocationDebugRangesTable;
  1138. relcount:=@FWasmRelocationDebugRangesTableEntriesCount;
  1139. end;
  1140. '.debug_str':
  1141. begin
  1142. relout:=FWasmRelocationDebugStrTable;
  1143. relcount:=@FWasmRelocationDebugStrTableEntriesCount;
  1144. end;
  1145. else
  1146. internalerror(2022071601);
  1147. end;
  1148. end
  1149. else
  1150. continue;
  1151. for ri:=0 to objsec.ObjRelocations.Count-1 do
  1152. begin
  1153. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  1154. case objrel.typ of
  1155. RELOC_FUNCTION_INDEX_LEB:
  1156. begin
  1157. if not assigned(objrel.symbol) then
  1158. internalerror(2021092508);
  1159. Inc(relcount^);
  1160. WriteByte(relout,Ord(R_WASM_FUNCTION_INDEX_LEB));
  1161. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1162. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1163. end;
  1164. RELOC_MEMORY_ADDR_LEB:
  1165. begin
  1166. if not assigned(objrel.symbol) then
  1167. internalerror(2021092603);
  1168. Inc(relcount^);
  1169. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1170. internalerror(2021092628);
  1171. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_LEB));
  1172. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1173. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1174. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1175. end;
  1176. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1177. begin
  1178. if not assigned(objrel.symbol) then
  1179. internalerror(2021092604);
  1180. Inc(relcount^);
  1181. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1182. begin
  1183. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_SLEB));
  1184. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1185. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1186. end
  1187. else
  1188. begin
  1189. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB));
  1190. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1191. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1192. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1193. end;
  1194. end;
  1195. RELOC_ABSOLUTE:
  1196. begin
  1197. // todo: figure this out, why do these exist?
  1198. //if assigned(objrel.symbol) and not assigned(objrel.symbol.objsection) then
  1199. // Writeln('!!! ', objrel.symbol.name);
  1200. if assigned(objrel.objsection) then
  1201. begin
  1202. Inc(relcount^);
  1203. WriteByte(relout,Ord(R_WASM_SECTION_OFFSET_I32));
  1204. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1205. if (TWasmObjSection(objrel.objsection).SegSymIdx<0) then
  1206. message1(asmw_e_illegal_unset_index,objrel.objsection.name)
  1207. else
  1208. WriteUleb(relout,TWasmObjSection(objrel.objsection).SegSymIdx);
  1209. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1210. end
  1211. else if (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION)) and not objsec.IsDebug then
  1212. begin
  1213. Inc(relcount^);
  1214. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_I32));
  1215. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1216. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1217. end
  1218. else if assigned(objrel.symbol) and assigned(objrel.symbol.objsection) and TWasmObjSection(objrel.symbol.objsection).IsCode then
  1219. begin
  1220. Inc(relcount^);
  1221. WriteByte(relout,Ord(R_WASM_FUNCTION_OFFSET_I32));
  1222. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1223. FuncSym:=FindFunctionSymbol(TWasmObjSymbol(objrel.Symbol));
  1224. if FuncSym.SymbolIndex<0 then
  1225. message1(asmw_e_illegal_unset_index,FuncSym.Name)
  1226. else
  1227. WriteUleb(relout,FuncSym.SymbolIndex);
  1228. WriteSleb(relout,objrel.Addend+objrel.symbol.address); { addend to add to the address }
  1229. end
  1230. else
  1231. begin
  1232. Inc(relcount^);
  1233. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_I32));
  1234. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1235. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1236. begin
  1237. Writeln(objrel.symbol.objsection.Name, ' ', objrel.symbol.name, ' ', objsec.Name);
  1238. message1(asmw_e_illegal_unset_index,objrel.symbol.name);
  1239. end
  1240. else
  1241. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1242. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1243. end;
  1244. end;
  1245. RELOC_TYPE_INDEX_LEB:
  1246. begin
  1247. Inc(relcount^);
  1248. WriteByte(relout,Ord(R_WASM_TYPE_INDEX_LEB));
  1249. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1250. WriteUleb(relout,objrel.TypeIndex);
  1251. end;
  1252. RELOC_GLOBAL_INDEX_LEB:
  1253. begin
  1254. if not assigned(objrel.symbol) then
  1255. internalerror(2021092704);
  1256. Inc(relcount^);
  1257. WriteByte(relout,Ord(R_WASM_GLOBAL_INDEX_LEB));
  1258. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1259. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1260. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1261. else
  1262. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1263. end;
  1264. RELOC_TAG_INDEX_LEB:
  1265. begin
  1266. if not assigned(objrel.symbol) then
  1267. internalerror(2021092717);
  1268. Inc(relcount^);
  1269. WriteByte(relout,Ord(R_WASM_TAG_INDEX_LEB));
  1270. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1271. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1272. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1273. else
  1274. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1275. end;
  1276. else
  1277. internalerror(2021092507);
  1278. end;
  1279. end;
  1280. end;
  1281. end;
  1282. function TWasmObjOutput.FindFunctionSymbol(Symbol: TWasmObjSymbol): TWasmObjSymbol;
  1283. begin
  1284. Result:=TWasmObjSection(Symbol.objsection).MainFuncSymbol;
  1285. end;
  1286. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  1287. var
  1288. section_nr: Integer;
  1289. procedure MaybeAddDebugSectionToSymbolTable(st: TWasmCustomDebugSectionType; var debug_section_nr: Integer);
  1290. var
  1291. objsec: TWasmObjSection;
  1292. begin
  1293. objsec:=TWasmObjSection(Data.ObjSectionList.Find(WasmCustomSectionName[st]));
  1294. if Assigned(objsec) then
  1295. begin
  1296. debug_section_nr:=section_nr;
  1297. Inc(section_nr);
  1298. objsec.SegSymIdx:=FWasmSymbolTableEntriesCount;
  1299. Inc(FWasmSymbolTableEntriesCount);
  1300. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1301. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1302. WriteUleb(FWasmSymbolTable,debug_section_nr);
  1303. end;
  1304. end;
  1305. procedure MaybeWriteDebugSection(st: TWasmCustomDebugSectionType);
  1306. var
  1307. objsec: TWasmObjSection;
  1308. begin
  1309. objsec:=TWasmObjSection(Data.ObjSectionList.Find(WasmCustomSectionName[st]));
  1310. if Assigned(objsec) then
  1311. begin
  1312. if oso_Data in objsec.SecOptions then
  1313. begin
  1314. objsec.Data.seek(0);
  1315. CopyDynamicArray(objsec.Data,FWasmCustomSections[st],objsec.Size);
  1316. end
  1317. else
  1318. WriteZeros(FWasmCustomSections[st],objsec.Size);
  1319. WriteWasmCustomSection(st);
  1320. end;
  1321. end;
  1322. var
  1323. i: Integer;
  1324. objsec: TWasmObjSection;
  1325. segment_count: Integer = 0;
  1326. cur_seg_ofs: qword = 0;
  1327. types_count,
  1328. imports_count, NextImportFunctionIndex, NextFunctionIndex,
  1329. code_section_nr, data_section_nr,
  1330. debug_abbrev_section_nr,debug_info_section_nr,debug_str_section_nr,
  1331. debug_line_section_nr,debug_frame_section_nr,debug_aranges_section_nr,
  1332. debug_ranges_section_nr,
  1333. NextGlobalIndex, NextTagIndex: Integer;
  1334. import_globals_count: Integer = 0;
  1335. globals_count: Integer = 0;
  1336. import_functions_count: Integer = 0;
  1337. export_functions_count: Integer = 0;
  1338. functions_count: Integer = 0;
  1339. import_exception_tags_count: Integer = 0;
  1340. exception_tags_count: Integer = 0;
  1341. objsym, ObjSymAlias: TWasmObjSymbol;
  1342. cust_sec: TWasmCustomSectionType;
  1343. SegmentFlags, SymbolFlags: UInt64;
  1344. begin
  1345. FData:=TWasmObjData(Data);
  1346. { each custom sections starts with its name }
  1347. for cust_sec in TWasmCustomSectionType do
  1348. WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
  1349. WriteUleb(FWasmCustomSections[wcstLinking],2); { linking metadata version }
  1350. for i:=0 to Data.ObjSymbolList.Count-1 do
  1351. begin
  1352. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1353. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1354. if objsym.bind=AB_EXTERNAL then
  1355. Inc(import_exception_tags_count)
  1356. else
  1357. Inc(exception_tags_count);
  1358. if objsym.typ=AT_WASM_GLOBAL then
  1359. if objsym.bind=AB_EXTERNAL then
  1360. Inc(import_globals_count)
  1361. else
  1362. Inc(globals_count);
  1363. if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1364. Inc(import_globals_count);
  1365. if IsExternalFunction(objsym) then
  1366. Inc(import_functions_count);
  1367. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1368. begin
  1369. TWasmObjSection(objsym.objsection).MainFuncSymbol:=objsym;
  1370. Inc(functions_count);
  1371. end;
  1372. if IsExportedFunction(objsym) then
  1373. Inc(export_functions_count);
  1374. end;
  1375. types_count:=Length(FData.FFuncTypes);
  1376. WriteUleb(FWasmSections[wsiType],types_count);
  1377. for i:=0 to types_count-1 do
  1378. with FData.FFuncTypes[i] do
  1379. begin
  1380. WriteByte(FWasmSections[wsiType],$60);
  1381. WriteWasmResultType(FWasmSections[wsiType],params);
  1382. WriteWasmResultType(FWasmSections[wsiType],results);
  1383. end;
  1384. for i:=0 to Data.ObjSectionList.Count-1 do
  1385. begin
  1386. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1387. if objsec.IsCode then
  1388. objsec.SegIdx:=-1
  1389. else if objsec.IsData then
  1390. begin
  1391. objsec.SegIdx:=segment_count;
  1392. objsec.SegOfs:=cur_seg_ofs;
  1393. Inc(segment_count);
  1394. Inc(cur_seg_ofs,objsec.Size);
  1395. end;
  1396. end;
  1397. imports_count:=2+import_globals_count+import_functions_count+import_exception_tags_count;
  1398. WriteUleb(FWasmSections[wsiImport],imports_count);
  1399. { import memories }
  1400. WriteName(FWasmSections[wsiImport],'env');
  1401. WriteName(FWasmSections[wsiImport],'__linear_memory');
  1402. WriteByte(FWasmSections[wsiImport],$02); { mem }
  1403. WriteByte(FWasmSections[wsiImport],$00); { min }
  1404. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  1405. { import globals }
  1406. NextGlobalIndex:=0;
  1407. for i:=0 to Data.ObjSymbolList.Count-1 do
  1408. begin
  1409. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1410. if (objsym.bind=AB_EXTERNAL) and (objsym.typ=AT_WASM_GLOBAL) then
  1411. begin
  1412. objsym.GlobalIndex:=NextGlobalIndex;
  1413. Inc(NextGlobalIndex);
  1414. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1415. if objsym.ExtraData.ImportModule<>'' then
  1416. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1417. else
  1418. WriteName(FWasmSections[wsiImport],'env');
  1419. WriteName(FWasmSections[wsiImport],objsym.Name);
  1420. WriteByte(FWasmSections[wsiImport],$03); { global }
  1421. WriteWasmBasicType(FWasmSections[wsiImport],objsym.ExtraData.GlobalType);
  1422. if objsym.ExtraData.GlobalIsImmutable then
  1423. WriteByte(FWasmSections[wsiImport],$00) { const }
  1424. else
  1425. WriteByte(FWasmSections[wsiImport],$01); { var }
  1426. end
  1427. else if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1428. begin
  1429. objsym.GlobalIndex:=NextGlobalIndex;
  1430. Inc(NextGlobalIndex);
  1431. objsym.ExtraData:=nil;
  1432. WriteName(FWasmSections[wsiImport],'GOT.mem');
  1433. WriteName(FWasmSections[wsiImport],objsym.Name);
  1434. WriteByte(FWasmSections[wsiImport],$03); { global }
  1435. WriteWasmBasicType(FWasmSections[wsiImport],wbt_i32); { i32 }
  1436. WriteByte(FWasmSections[wsiImport],$01); { var }
  1437. end;
  1438. end;
  1439. { import functions }
  1440. NextImportFunctionIndex:=0;
  1441. for i:=0 to Data.ObjSymbolList.Count-1 do
  1442. begin
  1443. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1444. if IsExternalFunction(objsym) then
  1445. begin
  1446. objsym.FuncIndex:=NextImportFunctionIndex;
  1447. Inc(NextImportFunctionIndex);
  1448. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1449. if objsym.ExtraData.ImportModule<>'' then
  1450. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1451. else
  1452. WriteName(FWasmSections[wsiImport],'env');
  1453. WriteName(FWasmSections[wsiImport],objsym.Name);
  1454. WriteByte(FWasmSections[wsiImport],$00); { func }
  1455. WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1456. end;
  1457. end;
  1458. { import tables }
  1459. WriteName(FWasmSections[wsiImport],'env');
  1460. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  1461. WriteByte(FWasmSections[wsiImport],$01); { table }
  1462. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  1463. WriteByte(FWasmSections[wsiImport],$00); { min }
  1464. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  1465. { import tags }
  1466. NextTagIndex:=0;
  1467. for i:=0 to Data.ObjSymbolList.Count-1 do
  1468. begin
  1469. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1470. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind=AB_EXTERNAL) then
  1471. begin
  1472. objsym.TagIndex:=NextTagIndex;
  1473. Inc(NextTagIndex);
  1474. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1475. if objsym.ExtraData.ImportModule<>'' then
  1476. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1477. else
  1478. WriteName(FWasmSections[wsiImport],'env');
  1479. WriteName(FWasmSections[wsiImport],objsym.Name);
  1480. WriteByte(FWasmSections[wsiImport],$04); { tag }
  1481. WriteByte(FWasmSections[wsiImport],$00); { exception }
  1482. WriteUleb(FWasmSections[wsiImport],objsym.ExtraData.ExceptionTagTypeIdx);
  1483. end;
  1484. end;
  1485. WriteUleb(FWasmSections[wsiFunction],functions_count);
  1486. NextFunctionIndex:=NextImportFunctionIndex;
  1487. for i:=0 to Data.ObjSymbolList.Count-1 do
  1488. begin
  1489. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1490. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1491. begin
  1492. objsym.FuncIndex:=NextFunctionIndex;
  1493. Inc(NextFunctionIndex);
  1494. WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1495. end;
  1496. end;
  1497. if exception_tags_count>0 then
  1498. begin
  1499. WriteUleb(FWasmSections[wsiTag],exception_tags_count);
  1500. for i:=0 to Data.ObjSymbolList.Count-1 do
  1501. begin
  1502. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1503. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind<>AB_EXTERNAL) then
  1504. begin
  1505. objsym.TagIndex:=NextTagIndex;
  1506. Inc(NextTagIndex);
  1507. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1508. WriteByte(FWasmSections[wsiTag],$00); { exception }
  1509. WriteUleb(FWasmSections[wsiTag],objsym.ExtraData.ExceptionTagTypeIdx);
  1510. end;
  1511. end;
  1512. end;
  1513. if globals_count>0 then
  1514. begin
  1515. WriteUleb(FWasmSections[wsiGlobal],globals_count);
  1516. for i:=0 to Data.ObjSymbolList.Count-1 do
  1517. begin
  1518. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1519. if (objsym.typ=AT_WASM_GLOBAL) and (objsym.bind<>AB_EXTERNAL) then
  1520. begin
  1521. objsym.GlobalIndex:=NextGlobalIndex;
  1522. Inc(NextGlobalIndex);
  1523. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1524. WriteWasmBasicType(FWasmSections[wsiGlobal],objsym.ExtraData.GlobalType);
  1525. if objsym.ExtraData.GlobalIsImmutable then
  1526. WriteByte(FWasmSections[wsiGlobal],$00) { const }
  1527. else
  1528. WriteByte(FWasmSections[wsiGlobal],$01); { var }
  1529. { init expr }
  1530. case objsym.ExtraData.GlobalType of
  1531. wbt_i32:
  1532. begin
  1533. WriteByte(FWasmSections[wsiGlobal],$41); { i32.const }
  1534. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1535. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1536. end;
  1537. wbt_i64:
  1538. begin
  1539. WriteByte(FWasmSections[wsiGlobal],$42); { i64.const }
  1540. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1541. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1542. end;
  1543. wbt_f32:
  1544. begin
  1545. WriteByte(FWasmSections[wsiGlobal],$43); { f32.const }
  1546. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE single precision floating point format) }
  1547. WriteByte(FWasmSections[wsiGlobal],$00);
  1548. WriteByte(FWasmSections[wsiGlobal],$00);
  1549. WriteByte(FWasmSections[wsiGlobal],$00);
  1550. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1551. end;
  1552. wbt_f64:
  1553. begin
  1554. WriteByte(FWasmSections[wsiGlobal],$44); { f64.const }
  1555. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE double precision floating point format) }
  1556. WriteByte(FWasmSections[wsiGlobal],$00);
  1557. WriteByte(FWasmSections[wsiGlobal],$00);
  1558. WriteByte(FWasmSections[wsiGlobal],$00);
  1559. WriteByte(FWasmSections[wsiGlobal],$00);
  1560. WriteByte(FWasmSections[wsiGlobal],$00);
  1561. WriteByte(FWasmSections[wsiGlobal],$00);
  1562. WriteByte(FWasmSections[wsiGlobal],$00);
  1563. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1564. end;
  1565. wbt_externref:
  1566. begin
  1567. WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null extern }
  1568. WriteByte(FWasmSections[wsiGlobal],$6F);
  1569. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1570. end;
  1571. wbt_funcref:
  1572. begin
  1573. WriteByte(FWasmSections[wsiGlobal],$D0); { ref.null func }
  1574. WriteByte(FWasmSections[wsiGlobal],$70);
  1575. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1576. end;
  1577. else
  1578. internalerror(2022052801);
  1579. end;
  1580. end;
  1581. end;
  1582. end;
  1583. if export_functions_count>0 then
  1584. begin
  1585. WriteUleb(FWasmSections[wsiExport],export_functions_count);
  1586. for i:=0 to Data.ObjSymbolList.Count-1 do
  1587. begin
  1588. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1589. if IsExportedFunction(objsym) then
  1590. begin
  1591. WriteName(FWasmSections[wsiExport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).ExportName);
  1592. WriteByte(FWasmSections[wsiExport],0); { func }
  1593. if (objsym.FuncIndex<0) then
  1594. message1(asmw_e_illegal_unset_index,objsym.name)
  1595. else
  1596. WriteUleb(FWasmSections[wsiExport],objsym.FuncIndex);
  1597. end;
  1598. end;
  1599. end;
  1600. for i:=0 to Data.ObjSymbolList.Count-1 do
  1601. begin
  1602. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1603. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1604. begin
  1605. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1606. Inc(FWasmSymbolTableEntriesCount);
  1607. WriteByte(FWasmSymbolTable,Ord(SYMTAB_EVENT));
  1608. if objsym.bind=AB_GLOBAL then
  1609. WriteUleb(FWasmSymbolTable,0)
  1610. else if objsym.bind=AB_LOCAL then
  1611. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1612. else if objsym.bind=AB_EXTERNAL then
  1613. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1614. else if objsym.bind=AB_WEAK then
  1615. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_WEAK)
  1616. else
  1617. internalerror(2021092715);
  1618. if (objsym.TagIndex<0) then
  1619. message1(asmw_e_illegal_unset_index,objsym.name)
  1620. else
  1621. WriteUleb(FWasmSymbolTable,objsym.TagIndex);
  1622. if objsym.bind<>AB_EXTERNAL then
  1623. WriteName(FWasmSymbolTable,objsym.Name);
  1624. end
  1625. else if objsym.typ=AT_WASM_GLOBAL then
  1626. begin
  1627. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1628. Inc(FWasmSymbolTableEntriesCount);
  1629. WriteByte(FWasmSymbolTable,Ord(SYMTAB_GLOBAL));
  1630. if objsym.bind=AB_EXTERNAL then
  1631. begin
  1632. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1633. if (objsym.GlobalIndex<0) then
  1634. message1(asmw_e_illegal_unset_index,objsym.name)
  1635. else
  1636. WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
  1637. end
  1638. else
  1639. begin
  1640. WriteUleb(FWasmSymbolTable,0);
  1641. if (objsym.GlobalIndex<0) then
  1642. message1(asmw_e_illegal_unset_index,objsym.name)
  1643. else
  1644. WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
  1645. WriteName(FWasmSymbolTable,objsym.Name);
  1646. end;
  1647. end
  1648. else if IsExternalFunction(objsym) then
  1649. begin
  1650. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1651. Inc(FWasmSymbolTableEntriesCount);
  1652. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1653. if objsym.ExtraData.ImportModule<>'' then
  1654. begin
  1655. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
  1656. if (objsym.FuncIndex<0) then
  1657. message1(asmw_e_illegal_unset_index,objsym.name)
  1658. else
  1659. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1660. WriteName(FWasmSymbolTable,objsym.Name);
  1661. end
  1662. else
  1663. begin
  1664. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1665. if (objsym.FuncIndex<0) then
  1666. message1(asmw_e_illegal_unset_index,objsym.name)
  1667. else
  1668. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1669. end;
  1670. end
  1671. else if objsym.typ=AT_FUNCTION then
  1672. begin
  1673. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1674. Inc(FWasmSymbolTableEntriesCount);
  1675. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1676. if objsym.IsAlias then
  1677. begin
  1678. ObjSymAlias:=TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf));
  1679. ObjSym.FuncIndex:=ObjSymAlias.FuncIndex;
  1680. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
  1681. WriteUleb(FWasmSymbolTable,ObjSymAlias.FuncIndex);
  1682. end
  1683. else
  1684. begin
  1685. if IsExportedFunction(objsym) then
  1686. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPORTED)
  1687. else
  1688. WriteUleb(FWasmSymbolTable,0);
  1689. if (objsym.FuncIndex<0) then
  1690. message1(asmw_e_illegal_unset_index,objsym.name)
  1691. else
  1692. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1693. end;
  1694. WriteName(FWasmSymbolTable,objsym.Name);
  1695. end
  1696. else if (objsym.typ in [AT_DATA,AT_TLS]) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
  1697. begin
  1698. if (objsym.bind<>AB_EXTERNAL) and TWasmObjSection(objsym.objsection).IsDebug then
  1699. begin
  1700. {todo: debug symbols}
  1701. end
  1702. else
  1703. begin
  1704. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1705. Inc(FWasmSymbolTableEntriesCount);
  1706. WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
  1707. if objsym.bind=AB_GLOBAL then
  1708. SymbolFlags:=0
  1709. else if objsym.bind=AB_LOCAL then
  1710. SymbolFlags:=WASM_SYM_BINDING_LOCAL
  1711. else if objsym.bind=AB_EXTERNAL then
  1712. SymbolFlags:=WASM_SYM_UNDEFINED
  1713. else
  1714. internalerror(2021092506);
  1715. if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1716. SymbolFlags:=(SymbolFlags and not WASM_SYM_BINDING_LOCAL) or WASM_SYM_TLS;
  1717. WriteUleb(FWasmSymbolTable,SymbolFlags);
  1718. WriteName(FWasmSymbolTable,objsym.Name);
  1719. if objsym.bind<>AB_EXTERNAL then
  1720. begin
  1721. WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
  1722. WriteUleb(FWasmSymbolTable,objsym.offset);
  1723. WriteUleb(FWasmSymbolTable,objsym.size);
  1724. end;
  1725. end;
  1726. end;
  1727. end;
  1728. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  1729. Writer.write(WasmVersion,SizeOf(WasmVersion));
  1730. if ts_wasm_threads in current_settings.targetswitches then
  1731. begin
  1732. WriteUleb(FWasmCustomSections[wcstTargetFeatures],4);
  1733. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1734. WriteName(FWasmCustomSections[wcstTargetFeatures],'atomics');
  1735. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1736. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1737. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1738. WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
  1739. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1740. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1741. end
  1742. else
  1743. begin
  1744. WriteUleb(FWasmCustomSections[wcstTargetFeatures],3);
  1745. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1746. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1747. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1748. WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
  1749. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1750. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1751. end;
  1752. { Write the producers section:
  1753. https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md }
  1754. WriteUleb(FWasmCustomSections[wcstProducers],2);
  1755. WriteName(FWasmCustomSections[wcstProducers],'language');
  1756. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1757. WriteName(FWasmCustomSections[wcstProducers],'Pascal');
  1758. WriteName(FWasmCustomSections[wcstProducers],'');
  1759. WriteName(FWasmCustomSections[wcstProducers],'processed-by');
  1760. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1761. WriteName(FWasmCustomSections[wcstProducers],'Free Pascal Compiler (FPC)');
  1762. WriteName(FWasmCustomSections[wcstProducers],full_version_string+' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname);
  1763. code_section_nr:=-1;
  1764. data_section_nr:=-1;
  1765. debug_abbrev_section_nr:=-1;
  1766. debug_info_section_nr:=-1;
  1767. debug_str_section_nr:=-1;
  1768. debug_line_section_nr:=-1;
  1769. debug_frame_section_nr:=-1;
  1770. debug_aranges_section_nr:=-1;
  1771. debug_ranges_section_nr:=-1;
  1772. section_nr:=0;
  1773. WriteWasmSection(wsiType);
  1774. Inc(section_nr);
  1775. WriteWasmSection(wsiImport);
  1776. Inc(section_nr);
  1777. WriteWasmSection(wsiFunction);
  1778. Inc(section_nr);
  1779. if exception_tags_count>0 then
  1780. begin
  1781. WriteWasmSection(wsiTag);
  1782. Inc(section_nr);
  1783. end;
  1784. if globals_count>0 then
  1785. begin
  1786. WriteWasmSection(wsiGlobal);
  1787. Inc(section_nr);
  1788. end;
  1789. if export_functions_count>0 then
  1790. begin
  1791. WriteWasmSection(wsiExport);
  1792. Inc(section_nr);
  1793. end;
  1794. { determine the section numbers for the datacount, code, data and debug sections ahead of time }
  1795. if segment_count>0 then
  1796. Inc(section_nr); { the DataCount section }
  1797. code_section_nr:=section_nr; { the Code section }
  1798. Inc(section_nr);
  1799. if segment_count>0 then
  1800. begin
  1801. data_section_nr:=section_nr; { the Data section }
  1802. Inc(section_nr);
  1803. end;
  1804. { the debug sections }
  1805. MaybeAddDebugSectionToSymbolTable(wcstDebugAbbrev,debug_abbrev_section_nr);
  1806. MaybeAddDebugSectionToSymbolTable(wcstDebugInfo,debug_info_section_nr);
  1807. MaybeAddDebugSectionToSymbolTable(wcstDebugStr,debug_str_section_nr);
  1808. MaybeAddDebugSectionToSymbolTable(wcstDebugLine,debug_line_section_nr);
  1809. MaybeAddDebugSectionToSymbolTable(wcstDebugFrame,debug_frame_section_nr);
  1810. MaybeAddDebugSectionToSymbolTable(wcstDebugAranges,debug_aranges_section_nr);
  1811. MaybeAddDebugSectionToSymbolTable(wcstDebugRanges,debug_ranges_section_nr);
  1812. DoRelocations;
  1813. if segment_count>0 then
  1814. begin
  1815. WriteUleb(FWasmSections[wsiData],segment_count);
  1816. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  1817. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],segment_count);
  1818. for i:=0 to Data.ObjSectionList.Count-1 do
  1819. begin
  1820. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1821. if objsec.IsData then
  1822. begin
  1823. WriteName(FWasmLinkingSubsections[WASM_SEGMENT_INFO],objsec.Name);
  1824. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],BsrQWord(objsec.SecAlign));
  1825. SegmentFlags:=0;
  1826. if (ts_wasm_threads in current_settings.targetswitches) and
  1827. (oso_threadvar in objsec.SecOptions) then
  1828. SegmentFlags:=SegmentFlags or WASM_SEG_FLAG_TLS;
  1829. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],SegmentFlags); { flags }
  1830. WriteByte(FWasmSections[wsiData],0);
  1831. WriteByte(FWasmSections[wsiData],$41);
  1832. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  1833. WriteByte(FWasmSections[wsiData],$0b);
  1834. WriteUleb(FWasmSections[wsiData],objsec.Size);
  1835. objsec.FileSectionOfs:=FWasmSections[wsiData].size;
  1836. if oso_Data in objsec.SecOptions then
  1837. begin
  1838. objsec.Data.seek(0);
  1839. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  1840. end
  1841. else
  1842. begin
  1843. WriteZeros(FWasmSections[wsiData],objsec.Size);
  1844. end;
  1845. end;
  1846. end;
  1847. end;
  1848. WriteUleb(FWasmSections[wsiCode],functions_count);
  1849. for i:=0 to Data.ObjSymbolList.Count-1 do
  1850. begin
  1851. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1852. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1853. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  1854. end;
  1855. if segment_count>0 then
  1856. WriteWasmSection(wsiDataCount);
  1857. WriteWasmSection(wsiCode);
  1858. if segment_count>0 then
  1859. WriteWasmSection(wsiData);
  1860. MaybeWriteDebugSection(wcstDebugAbbrev);
  1861. MaybeWriteDebugSection(wcstDebugInfo);
  1862. MaybeWriteDebugSection(wcstDebugStr);
  1863. MaybeWriteDebugSection(wcstDebugLine);
  1864. MaybeWriteDebugSection(wcstDebugFrame);
  1865. MaybeWriteDebugSection(wcstDebugAranges);
  1866. MaybeWriteDebugSection(wcstDebugRanges);
  1867. WriteRelocations;
  1868. WriteSymbolTable;
  1869. WriteLinkingSubsection(WASM_SYMBOL_TABLE);
  1870. if segment_count>0 then
  1871. WriteLinkingSubsection(WASM_SEGMENT_INFO);
  1872. WriteRelocationCodeTable(code_section_nr);
  1873. if segment_count>0 then
  1874. WriteRelocationDataTable(data_section_nr);
  1875. WriteWasmCustomSection(wcstLinking);
  1876. Inc(section_nr);
  1877. WriteWasmCustomSection(wcstRelocCode);
  1878. Inc(section_nr);
  1879. if segment_count>0 then
  1880. begin
  1881. WriteWasmCustomSection(wcstRelocData);
  1882. Inc(section_nr);
  1883. end;
  1884. MaybeWriteRelocationDebugTable(wcstRelocDebugAbbrev,debug_abbrev_section_nr,FWasmRelocationDebugAbbrevTableEntriesCount,FWasmRelocationDebugAbbrevTable);
  1885. MaybeWriteRelocationDebugTable(wcstRelocDebugInfo,debug_info_section_nr,FWasmRelocationDebugInfoTableEntriesCount,FWasmRelocationDebugInfoTable);
  1886. MaybeWriteRelocationDebugTable(wcstRelocDebugStr,debug_str_section_nr,FWasmRelocationDebugStrTableEntriesCount,FWasmRelocationDebugStrTable);
  1887. MaybeWriteRelocationDebugTable(wcstRelocDebugLine,debug_line_section_nr,FWasmRelocationDebugLineTableEntriesCount,FWasmRelocationDebugLineTable);
  1888. MaybeWriteRelocationDebugTable(wcstRelocDebugFrame,debug_frame_section_nr,FWasmRelocationDebugFrameTableEntriesCount,FWasmRelocationDebugFrameTable);
  1889. MaybeWriteRelocationDebugTable(wcstRelocDebugAranges,debug_aranges_section_nr,FWasmRelocationDebugArangesTableEntriesCount,FWasmRelocationDebugArangesTable);
  1890. MaybeWriteRelocationDebugTable(wcstRelocDebugRanges,debug_ranges_section_nr,FWasmRelocationDebugRangesTableEntriesCount,FWasmRelocationDebugRangesTable);
  1891. WriteWasmCustomSection(wcstProducers);
  1892. Inc(section_nr);
  1893. WriteWasmCustomSection(wcstTargetFeatures);
  1894. Inc(section_nr);
  1895. result:=true;
  1896. end;
  1897. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  1898. var
  1899. i: TWasmSectionID;
  1900. j: TWasmCustomSectionType;
  1901. k: TWasmLinkingSubsectionType;
  1902. begin
  1903. inherited;
  1904. cobjdata:=TWasmObjData;
  1905. for i in TWasmSectionID do
  1906. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  1907. for j in TWasmCustomSectionType do
  1908. FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
  1909. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1910. FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
  1911. FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
  1912. FWasmSymbolTableEntriesCount:=0;
  1913. FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
  1914. FWasmRelocationCodeTableEntriesCount:=0;
  1915. FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
  1916. FWasmRelocationDataTableEntriesCount:=0;
  1917. FWasmRelocationDebugFrameTable:=tdynamicarray.create(SectionDataMaxGrow);
  1918. FWasmRelocationDebugFrameTableEntriesCount:=0;
  1919. FWasmRelocationDebugInfoTable:=tdynamicarray.create(SectionDataMaxGrow);
  1920. FWasmRelocationDebugInfoTableEntriesCount:=0;
  1921. FWasmRelocationDebugLineTable:=tdynamicarray.create(SectionDataMaxGrow);
  1922. FWasmRelocationDebugLineTableEntriesCount:=0;
  1923. FWasmRelocationDebugAbbrevTable:=tdynamicarray.create(SectionDataMaxGrow);
  1924. FWasmRelocationDebugAbbrevTableEntriesCount:=0;
  1925. FWasmRelocationDebugArangesTable:=tdynamicarray.create(SectionDataMaxGrow);
  1926. FWasmRelocationDebugArangesTableEntriesCount:=0;
  1927. FWasmRelocationDebugRangesTable:=tdynamicarray.create(SectionDataMaxGrow);
  1928. FWasmRelocationDebugRangesTableEntriesCount:=0;
  1929. FWasmRelocationDebugStrTable:=tdynamicarray.create(SectionDataMaxGrow);
  1930. FWasmRelocationDebugStrTableEntriesCount:=0;
  1931. end;
  1932. destructor TWasmObjOutput.destroy;
  1933. var
  1934. i: TWasmSectionID;
  1935. j: TWasmCustomSectionType;
  1936. k: TWasmLinkingSubsectionType;
  1937. begin
  1938. for i in TWasmSectionID do
  1939. FWasmSections[i].Free;
  1940. for j in TWasmCustomSectionType do
  1941. FWasmCustomSections[j].Free;
  1942. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1943. FWasmLinkingSubsections[k].Free;
  1944. FWasmSymbolTable.Free;
  1945. FWasmRelocationCodeTable.Free;
  1946. FWasmRelocationDataTable.Free;
  1947. FWasmRelocationDebugFrameTable.Free;
  1948. FWasmRelocationDebugInfoTable.Free;
  1949. FWasmRelocationDebugLineTable.Free;
  1950. FWasmRelocationDebugAbbrevTable.Free;
  1951. FWasmRelocationDebugArangesTable.Free;
  1952. FWasmRelocationDebugRangesTable.Free;
  1953. FWasmRelocationDebugStrTable.Free;
  1954. inherited destroy;
  1955. end;
  1956. {****************************************************************************
  1957. TWasmObjInput
  1958. ****************************************************************************}
  1959. function TWasmObjInput.ReadUleb(r: TObjectReader; out v: uint64): boolean;
  1960. var
  1961. b: byte;
  1962. shift:integer;
  1963. begin
  1964. result:=false;
  1965. b:=0;
  1966. v:=0;
  1967. shift:=0;
  1968. repeat
  1969. if not r.read(b,1) then
  1970. exit;
  1971. v:=v or (uint64(b and 127) shl shift);
  1972. inc(shift,7);
  1973. until (b and 128)=0;
  1974. result:=true;
  1975. end;
  1976. constructor TWasmObjInput.create;
  1977. begin
  1978. inherited create;
  1979. cobjdata:=TWasmObjData;
  1980. end;
  1981. class function TWasmObjInput.CanReadObjData(AReader: TObjectreader): boolean;
  1982. var
  1983. ModuleMagic: array [0..3] of Byte;
  1984. ModuleVersion: array [0..3] of Byte;
  1985. i: Integer;
  1986. begin
  1987. result:=false;
  1988. if not AReader.read(ModuleMagic,4) then
  1989. exit;
  1990. for i:=0 to 3 do
  1991. if ModuleMagic[i]<>WasmModuleMagic[i] then
  1992. exit;
  1993. if not AReader.read(ModuleVersion,4) then
  1994. exit;
  1995. for i:=0 to 3 do
  1996. if ModuleVersion[i]<>WasmVersion[i] then
  1997. exit;
  1998. result:=true;
  1999. end;
  2000. function TWasmObjInput.ReadObjData(AReader: TObjectreader; out ObjData: TObjData): boolean;
  2001. function ReadSection: Boolean;
  2002. var
  2003. SectionId: Byte;
  2004. SectionSize: uint64;
  2005. begin
  2006. Result:=False;
  2007. if not AReader.read(SectionId,1) then
  2008. begin
  2009. InputError('Error reading section ID');
  2010. exit;
  2011. end;
  2012. if not ReadUleb(AReader,SectionSize) then
  2013. begin
  2014. InputError('Error reading section size');
  2015. exit;
  2016. end;
  2017. if SectionSize>high(uint32) then
  2018. begin
  2019. InputError('Invalid section size');
  2020. exit;
  2021. end;
  2022. if (AReader.Pos+SectionSize)>AReader.size then
  2023. begin
  2024. InputError('Section exceeds beyond the end of file');
  2025. exit;
  2026. end;
  2027. { skip the section for now... TODO: parse the section }
  2028. if SectionSize>0 then
  2029. AReader.seek(AReader.Pos+SectionSize);
  2030. Result:=True;
  2031. end;
  2032. var
  2033. ModuleMagic: array [0..3] of Byte;
  2034. ModuleVersion: array [0..3] of Byte;
  2035. i: Integer;
  2036. begin
  2037. objdata:=CObjData.Create(InputFileName);
  2038. result:=false;
  2039. if not AReader.read(ModuleMagic,4) then
  2040. exit;
  2041. for i:=0 to 3 do
  2042. if ModuleMagic[i]<>WasmModuleMagic[i] then
  2043. exit;
  2044. if not AReader.read(ModuleVersion,4) then
  2045. exit;
  2046. for i:=0 to 3 do
  2047. if ModuleVersion[i]<>WasmVersion[i] then
  2048. exit;
  2049. while AReader.Pos<AReader.size do
  2050. if not ReadSection then
  2051. exit;
  2052. Result:=True;
  2053. end;
  2054. {****************************************************************************
  2055. TWasmAssembler
  2056. ****************************************************************************}
  2057. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  2058. begin
  2059. inherited;
  2060. CObjOutput:=TWasmObjOutput;
  2061. end;
  2062. {*****************************************************************************
  2063. Initialize
  2064. *****************************************************************************}
  2065. {$ifdef wasm32}
  2066. const
  2067. as_wasm32_wasm_info : tasminfo =
  2068. (
  2069. id : as_wasm32_wasm;
  2070. idtxt : 'WASM';
  2071. asmbin : '';
  2072. asmcmd : '';
  2073. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  2074. flags : [af_outputbinary,af_smartlink_sections];
  2075. labelprefix : '..@';
  2076. labelmaxlen : -1;
  2077. comment : '; ';
  2078. dollarsign: '$';
  2079. );
  2080. {$endif wasm32}
  2081. initialization
  2082. {$ifdef wasm32}
  2083. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  2084. {$endif wasm32}
  2085. end.