ogwasm.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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. { TWasmObjSymbol }
  34. TWasmObjSymbol = class(TObjSymbol)
  35. ImportIndex: Integer;
  36. FuncIndex: Integer;
  37. SymbolIndex: Integer;
  38. AliasOf: string;
  39. constructor create(AList:TFPHashObjectList;const AName:string);
  40. function ImportOrFuncIndex: Integer;
  41. function IsAlias: Boolean;
  42. end;
  43. { TWasmObjRelocation }
  44. TWasmObjRelocation = class(TObjRelocation)
  45. end;
  46. { TWasmObjSymbolExtraData }
  47. TWasmObjSymbolExtraData = class(TFPHashObject)
  48. TypeIdx: Integer;
  49. ImportModule: string;
  50. ImportName: string;
  51. Locals: array of TWasmBasicType;
  52. constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  53. procedure AddLocal(bastyp: TWasmBasicType);
  54. end;
  55. { TWasmObjSection }
  56. TWasmObjSection = class(TObjSection)
  57. public
  58. SegIdx: Integer;
  59. SegOfs: qword;
  60. FileSectionOfs: qword;
  61. function IsCode: Boolean;
  62. function IsData: Boolean;
  63. end;
  64. { TWasmObjData }
  65. TWasmObjData = class(TObjData)
  66. private
  67. FFuncTypes: array of TWasmFuncType;
  68. FObjSymbolsExtraDataList: TFPHashObjectList;
  69. FLastFuncName: string;
  70. function is_smart_section(atype:TAsmSectiontype):boolean;
  71. function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  72. public
  73. constructor create(const n:string);override;
  74. destructor destroy; override;
  75. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  76. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  77. function AddOrCreateObjSymbolExtraData(const symname:TSymStr): TWasmObjSymbolExtraData;
  78. function AddFuncType(wft: TWasmFuncType): integer;
  79. procedure DeclareFuncType(ft: tai_functype);
  80. procedure DeclareImportModule(aim: tai_import_module);
  81. procedure DeclareImportName(ain: tai_import_name);
  82. procedure DeclareLocal(al: tai_local);
  83. procedure symbolpairdefine(akind: TSymbolPairKind;const asym, avalue: string);override;
  84. end;
  85. { TWasmObjOutput }
  86. TWasmObjOutput = class(tObjOutput)
  87. private
  88. FData: TWasmObjData;
  89. FWasmRelocationCodeTable: tdynamicarray;
  90. FWasmRelocationCodeTableEntriesCount: Integer;
  91. FWasmRelocationDataTable: tdynamicarray;
  92. FWasmRelocationDataTableEntriesCount: Integer;
  93. FWasmSymbolTable: tdynamicarray;
  94. FWasmSymbolTableEntriesCount: Integer;
  95. FWasmSections: array [TWasmSectionID] of tdynamicarray;
  96. FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
  97. FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of tdynamicarray;
  98. procedure WriteUleb(d: tdynamicarray; v: uint64);
  99. procedure WriteUleb(w: TObjectWriter; v: uint64);
  100. procedure WriteSleb(d: tdynamicarray; v: int64);
  101. procedure WriteByte(d: tdynamicarray; b: byte);
  102. procedure WriteName(d: tdynamicarray; const s: string);
  103. procedure WriteWasmSection(wsid: TWasmSectionID);
  104. procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  105. procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  106. procedure WriteZeros(dest: tdynamicarray; size: QWord);
  107. procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  108. procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  109. function IsExternalFunction(sym: TObjSymbol): Boolean;
  110. procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  111. procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  112. procedure WriteSymbolTable;
  113. procedure WriteRelocationCodeTable(CodeSectionIndex: Integer);
  114. procedure WriteRelocationDataTable(DataSectionIndex: Integer);
  115. procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  116. procedure DoRelocations;
  117. procedure WriteRelocations;
  118. protected
  119. function writeData(Data:TObjData):boolean;override;
  120. public
  121. constructor create(AWriter:TObjectWriter);override;
  122. destructor destroy;override;
  123. end;
  124. { TWasmAssembler }
  125. TWasmAssembler = class(tinternalassembler)
  126. constructor create(info: pasminfo; smart:boolean);override;
  127. end;
  128. implementation
  129. uses
  130. verbose;
  131. procedure WriteUleb5(d: tdynamicarray; v: uint64);
  132. var
  133. b: byte;
  134. i: Integer;
  135. begin
  136. for i:=1 to 5 do
  137. begin
  138. b:=byte(v) and 127;
  139. v:=v shr 7;
  140. if i<>5 then
  141. b:=b or 128;
  142. d.write(b,1);
  143. end;
  144. end;
  145. procedure WriteUleb5(d: tobjsection; v: uint64);
  146. var
  147. b: byte;
  148. i: Integer;
  149. begin
  150. for i:=1 to 5 do
  151. begin
  152. b:=byte(v) and 127;
  153. v:=v shr 7;
  154. if i<>5 then
  155. b:=b or 128;
  156. d.write(b,1);
  157. end;
  158. end;
  159. procedure WriteSleb5(d: tdynamicarray; v: int64);
  160. var
  161. b: byte;
  162. i: Integer;
  163. begin
  164. for i:=1 to 5 do
  165. begin
  166. b:=byte(v) and 127;
  167. v:=SarInt64(v,7);
  168. if i<>5 then
  169. b:=b or 128;
  170. d.write(b,1);
  171. end;
  172. end;
  173. procedure WriteSleb5(d: tobjsection; v: int64);
  174. var
  175. b: byte;
  176. i: Integer;
  177. begin
  178. for i:=1 to 5 do
  179. begin
  180. b:=byte(v) and 127;
  181. v:=SarInt64(v,7);
  182. if i<>5 then
  183. b:=b or 128;
  184. d.write(b,1);
  185. end;
  186. end;
  187. function ReadUleb(d: tdynamicarray): uint64;
  188. var
  189. b: byte;
  190. shift:integer;
  191. begin
  192. b:=0;
  193. result:=0;
  194. shift:=0;
  195. repeat
  196. d.read(b,1);
  197. result:=result or (uint64(b and 127) shl shift);
  198. inc(shift,7);
  199. until (b and 128)=0;
  200. end;
  201. function ReadSleb(d: tdynamicarray): int64;
  202. var
  203. b: byte;
  204. shift:integer;
  205. begin
  206. b:=0;
  207. result:=0;
  208. shift:=0;
  209. repeat
  210. d.read(b,1);
  211. result:=result or (uint64(b and 127) shl shift);
  212. inc(shift,7);
  213. until (b and 128)=0;
  214. if (b and 64)<>0 then
  215. result:=result or (high(uint64) shl shift);
  216. end;
  217. procedure AddSleb5(d: tdynamicarray; v: int64);
  218. var
  219. q: Int64;
  220. p: LongWord;
  221. begin
  222. p:=d.Pos;
  223. q:=ReadSleb(d);
  224. q:=q+v;
  225. d.seek(p);
  226. WriteSleb5(d,q);
  227. end;
  228. procedure AddUleb5(d: tdynamicarray; v: int64);
  229. var
  230. q: UInt64;
  231. p: LongWord;
  232. begin
  233. p:=d.Pos;
  234. q:=ReadUleb(d);
  235. q:=q+v;
  236. d.seek(p);
  237. WriteUleb5(d,q);
  238. end;
  239. {****************************************************************************
  240. TWasmObjSymbol
  241. ****************************************************************************}
  242. constructor TWasmObjSymbol.create(AList: TFPHashObjectList; const AName: string);
  243. begin
  244. inherited create(AList,AName);
  245. ImportIndex:=-1;
  246. FuncIndex:=-1;
  247. SymbolIndex:=-1;
  248. AliasOf:='';
  249. end;
  250. function TWasmObjSymbol.ImportOrFuncIndex: Integer;
  251. begin
  252. if ImportIndex<>-1 then
  253. result:=ImportIndex
  254. else if FuncIndex<>-1 then
  255. result:=FuncIndex
  256. else
  257. internalerror(2021092601);
  258. end;
  259. function TWasmObjSymbol.IsAlias: Boolean;
  260. begin
  261. result:=AliasOf<>'';
  262. end;
  263. {****************************************************************************
  264. TWasmObjSymbolExtraData
  265. ****************************************************************************}
  266. constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  267. begin
  268. inherited Create(HashObjectList,s);
  269. TypeIdx:=-1;
  270. end;
  271. procedure TWasmObjSymbolExtraData.AddLocal(bastyp: TWasmBasicType);
  272. begin
  273. SetLength(Locals,Length(Locals)+1);
  274. Locals[High(Locals)]:=bastyp;
  275. end;
  276. {****************************************************************************
  277. TWasmObjSection
  278. ****************************************************************************}
  279. function TWasmObjSection.IsCode: Boolean;
  280. const
  281. CodePrefix = '.text';
  282. begin
  283. result:=(Length(Name)>=Length(CodePrefix)) and
  284. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  285. end;
  286. function TWasmObjSection.IsData: Boolean;
  287. begin
  288. result:=not IsCode;
  289. end;
  290. {****************************************************************************
  291. TWasmObjData
  292. ****************************************************************************}
  293. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  294. begin
  295. { For bss we need to set some flags that are target dependent,
  296. it is easier to disable it for smartlinking. It doesn't take up
  297. filespace }
  298. result:=not(target_info.system in systems_darwin) and
  299. create_smartlink_sections and
  300. (atype<>sec_toc) and
  301. (atype<>sec_user) and
  302. { on embedded systems every byte counts, so smartlink bss too }
  303. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  304. end;
  305. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  306. const aname: string; aorder: TAsmSectionOrder): string;
  307. const
  308. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  309. '.text',
  310. '.data',
  311. { why doesn't .rodata work? (FK) }
  312. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  313. { vtables (and anything else containing relocations), otherwise those are }
  314. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  315. { vtable for a class called Window: }
  316. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  317. { TODO: .data.ro not yet working}
  318. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  319. '.rodata',
  320. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  321. '.data',
  322. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  323. '.rodata',
  324. '.bss',
  325. '.threadvar',
  326. '.pdata',
  327. '', { stubs }
  328. '__DATA,__nl_symbol_ptr',
  329. '__DATA,__la_symbol_ptr',
  330. '__DATA,__mod_init_func',
  331. '__DATA,__mod_term_func',
  332. '.stab',
  333. '.stabstr',
  334. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  335. '.eh_frame',
  336. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  337. '.fpc',
  338. '.toc',
  339. '.init',
  340. '.fini',
  341. '.objc_class',
  342. '.objc_meta_class',
  343. '.objc_cat_cls_meth',
  344. '.objc_cat_inst_meth',
  345. '.objc_protocol',
  346. '.objc_string_object',
  347. '.objc_cls_meth',
  348. '.objc_inst_meth',
  349. '.objc_cls_refs',
  350. '.objc_message_refs',
  351. '.objc_symbols',
  352. '.objc_category',
  353. '.objc_class_vars',
  354. '.objc_instance_vars',
  355. '.objc_module_info',
  356. '.objc_class_names',
  357. '.objc_meth_var_types',
  358. '.objc_meth_var_names',
  359. '.objc_selector_strs',
  360. '.objc_protocol_ext',
  361. '.objc_class_ext',
  362. '.objc_property',
  363. '.objc_image_info',
  364. '.objc_cstring_object',
  365. '.objc_sel_fixup',
  366. '__DATA,__objc_data',
  367. '__DATA,__objc_const',
  368. '.objc_superrefs',
  369. '__DATA, __datacoal_nt,coalesced',
  370. '.objc_classlist',
  371. '.objc_nlclasslist',
  372. '.objc_catlist',
  373. '.obcj_nlcatlist',
  374. '.objc_protolist',
  375. '.stack',
  376. '.heap',
  377. '.gcc_except_table',
  378. '.ARM.attributes'
  379. );
  380. var
  381. sep : string[3];
  382. secname : string;
  383. begin
  384. secname:=secnames[atype];
  385. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  386. begin
  387. result:=secname+'.'+aname;
  388. exit;
  389. end;
  390. if atype=sec_threadvar then
  391. begin
  392. if (target_info.system in (systems_windows+systems_wince)) then
  393. secname:='.tls'
  394. else if (target_info.system in systems_linux) then
  395. secname:='.tbss';
  396. end;
  397. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  398. Thus, data which normally goes into .rodata and .rodata_norel sections must
  399. end up in .data section }
  400. if (atype in [sec_rodata,sec_rodata_norel]) and
  401. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  402. secname:='.data';
  403. { Windows correctly handles reallocations in readonly sections }
  404. if (atype=sec_rodata) and
  405. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  406. secname:='.rodata';
  407. { section type user gives the user full controll on the section name }
  408. if atype=sec_user then
  409. secname:=aname;
  410. if is_smart_section(atype) and (aname<>'') then
  411. begin
  412. case aorder of
  413. secorder_begin :
  414. sep:='.b_';
  415. secorder_end :
  416. sep:='.z_';
  417. else
  418. sep:='.n_';
  419. end;
  420. result:=secname+sep+aname
  421. end
  422. else
  423. result:=secname;
  424. end;
  425. constructor TWasmObjData.create(const n: string);
  426. begin
  427. inherited;
  428. CObjSection:=TWasmObjSection;
  429. CObjSymbol:=TWasmObjSymbol;
  430. FObjSymbolsExtraDataList:=TFPHashObjectList.Create;
  431. end;
  432. destructor TWasmObjData.destroy;
  433. var
  434. i: Integer;
  435. begin
  436. FObjSymbolsExtraDataList.Free;
  437. for i:=low(FFuncTypes) to high(FFuncTypes) do
  438. begin
  439. FFuncTypes[i].free;
  440. FFuncTypes[i]:=nil;
  441. end;
  442. inherited destroy;
  443. end;
  444. function TWasmObjData.sectionname(atype: TAsmSectiontype;
  445. const aname: string; aorder: TAsmSectionOrder): string;
  446. begin
  447. if (atype=sec_fpc) or (atype=sec_threadvar) then
  448. atype:=sec_data;
  449. Result:=sectionname_gas(atype, aname, aorder);
  450. end;
  451. procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
  452. p: TObjSymbol; Reloctype: TObjRelocationType);
  453. const
  454. leb_zero: array[0..4] of byte=($80,$80,$80,$80,$00);
  455. var
  456. objreloc: TWasmObjRelocation;
  457. begin
  458. if CurrObjSec=nil then
  459. internalerror(200403072);
  460. objreloc:=nil;
  461. case Reloctype of
  462. RELOC_FUNCTION_INDEX_LEB:
  463. begin
  464. if Data<>0 then
  465. internalerror(2021092502);
  466. if len<>5 then
  467. internalerror(2021092503);
  468. if not assigned(p) then
  469. internalerror(2021092504);
  470. if p.bind<>AB_EXTERNAL then
  471. internalerror(2021092505);
  472. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  473. CurrObjSec.ObjRelocations.Add(objreloc);
  474. writebytes(leb_zero,5);
  475. end;
  476. RELOC_MEMORY_ADDR_LEB,
  477. RELOC_MEMORY_ADDR_SLEB:
  478. begin
  479. if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then
  480. internalerror(2021092602);
  481. if len<>5 then
  482. internalerror(2021092503);
  483. if not assigned(p) then
  484. internalerror(2021092504);
  485. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  486. CurrObjSec.ObjRelocations.Add(objreloc);
  487. if RelocType=RELOC_MEMORY_ADDR_LEB then
  488. WriteUleb5(CurrObjSec,Data)
  489. else
  490. WriteSleb5(CurrObjSec,Data);
  491. end;
  492. RELOC_ABSOLUTE:
  493. begin
  494. if len<>4 then
  495. internalerror(2021092607);
  496. if not assigned(p) then
  497. internalerror(2021092608);
  498. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  499. CurrObjSec.ObjRelocations.Add(objreloc);
  500. Data:=NtoLE(Data);
  501. writebytes(Data,4);
  502. end;
  503. else
  504. internalerror(2021092501);
  505. end;
  506. end;
  507. function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
  508. begin
  509. result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
  510. if not assigned(result) then
  511. result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
  512. end;
  513. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  514. var
  515. i: Integer;
  516. begin
  517. for i:=low(FFuncTypes) to high(FFuncTypes) do
  518. if wft.Equals(FFuncTypes[i]) then
  519. exit(i);
  520. result:=Length(FFuncTypes);
  521. SetLength(FFuncTypes,result+1);
  522. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  523. end;
  524. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  525. var
  526. i: Integer;
  527. ObjSymExtraData: TWasmObjSymbolExtraData;
  528. begin
  529. FLastFuncName:=ft.funcname;
  530. i:=AddFuncType(ft.functype);
  531. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
  532. ObjSymExtraData.TypeIdx:=i;
  533. end;
  534. procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
  535. var
  536. ObjSymExtraData: TWasmObjSymbolExtraData;
  537. begin
  538. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
  539. ObjSymExtraData.ImportModule:=aim.importmodule;
  540. end;
  541. procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
  542. var
  543. ObjSymExtraData: TWasmObjSymbolExtraData;
  544. begin
  545. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
  546. ObjSymExtraData.ImportName:=ain.importname;
  547. end;
  548. procedure TWasmObjData.DeclareLocal(al: tai_local);
  549. var
  550. ObjSymExtraData: TWasmObjSymbolExtraData;
  551. begin
  552. ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
  553. ObjSymExtraData.AddLocal(al.bastyp);
  554. end;
  555. procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
  556. var
  557. valsym: TObjSymbol;
  558. aliassym: TWasmObjSymbol;
  559. begin
  560. valsym:=CreateSymbol(avalue);
  561. aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
  562. aliassym.AliasOf:=valsym.Name;
  563. end;
  564. {****************************************************************************
  565. TWasmObjOutput
  566. ****************************************************************************}
  567. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  568. var
  569. b: byte;
  570. begin
  571. repeat
  572. b:=byte(v) and 127;
  573. v:=v shr 7;
  574. if v<>0 then
  575. b:=b or 128;
  576. d.write(b,1);
  577. until v=0;
  578. end;
  579. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  580. var
  581. b: byte;
  582. begin
  583. repeat
  584. b:=byte(v) and 127;
  585. v:=v shr 7;
  586. if v<>0 then
  587. b:=b or 128;
  588. w.write(b,1);
  589. until v=0;
  590. end;
  591. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  592. var
  593. b: byte;
  594. Done: Boolean=false;
  595. begin
  596. repeat
  597. b:=byte(v) and 127;
  598. v:=SarInt64(v,7);
  599. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  600. Done:=true
  601. else
  602. b:=b or 128;
  603. d.write(b,1);
  604. until Done;
  605. end;
  606. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  607. begin
  608. d.write(b,1);
  609. end;
  610. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  611. begin
  612. WriteUleb(d,Length(s));
  613. d.writestr(s);
  614. end;
  615. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  616. var
  617. b: byte;
  618. begin
  619. b:=ord(wsid);
  620. Writer.write(b,1);
  621. WriteUleb(Writer,FWasmSections[wsid].size);
  622. Writer.writearray(FWasmSections[wsid]);
  623. end;
  624. procedure TWasmObjOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  625. var
  626. b: byte;
  627. begin
  628. b:=0;
  629. Writer.write(b,1);
  630. WriteUleb(Writer,FWasmCustomSections[wcst].size);
  631. Writer.writearray(FWasmCustomSections[wcst]);
  632. end;
  633. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  634. var
  635. buf: array [0..4095] of byte;
  636. bs: Integer;
  637. begin
  638. while size>0 do
  639. begin
  640. if size<SizeOf(buf) then
  641. bs:=Integer(size)
  642. else
  643. bs:=SizeOf(buf);
  644. src.read(buf,bs);
  645. dest.write(buf,bs);
  646. dec(size,bs);
  647. end;
  648. end;
  649. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  650. var
  651. buf : array[0..1023] of byte;
  652. bs: Integer;
  653. begin
  654. fillchar(buf,sizeof(buf),0);
  655. while size>0 do
  656. begin
  657. if size<SizeOf(buf) then
  658. bs:=Integer(size)
  659. else
  660. bs:=SizeOf(buf);
  661. dest.write(buf,bs);
  662. dec(size,bs);
  663. end;
  664. end;
  665. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  666. var
  667. i: Integer;
  668. begin
  669. WriteUleb(dest,Length(wrt));
  670. for i:=low(wrt) to high(wrt) do
  671. WriteWasmBasicType(dest,wrt[i]);
  672. end;
  673. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  674. begin
  675. case wbt of
  676. wbt_i32:
  677. WriteByte(dest,$7F);
  678. wbt_i64:
  679. WriteByte(dest,$7E);
  680. wbt_f32:
  681. WriteByte(dest,$7D);
  682. wbt_f64:
  683. WriteByte(dest,$7C);
  684. end;
  685. end;
  686. function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
  687. begin
  688. result:=(sym.bind=AB_EXTERNAL) and (TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name)<>nil);
  689. end;
  690. procedure TWasmObjOutput.WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  691. var
  692. i,
  693. rle_entries,
  694. cnt: Integer;
  695. lasttype: TWasmBasicType;
  696. begin
  697. if Length(ed.Locals)=0 then
  698. begin
  699. WriteUleb(dest,0);
  700. exit;
  701. end;
  702. rle_entries:=1;
  703. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  704. if ed.Locals[i]<>ed.Locals[i-1] then
  705. inc(rle_entries);
  706. WriteUleb(dest,rle_entries);
  707. lasttype:=ed.Locals[Low(ed.Locals)];
  708. cnt:=1;
  709. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  710. if ed.Locals[i]=ed.Locals[i-1] then
  711. inc(cnt)
  712. else
  713. begin
  714. WriteUleb(dest,cnt);
  715. WriteWasmBasicType(dest,lasttype);
  716. lasttype:=ed.Locals[i];
  717. cnt:=1;
  718. end;
  719. WriteUleb(dest,cnt);
  720. WriteWasmBasicType(dest,lasttype);
  721. end;
  722. procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  723. var
  724. encoded_locals: tdynamicarray;
  725. ObjSymExtraData: TWasmObjSymbolExtraData;
  726. codelen: LongWord;
  727. ObjSection: TWasmObjSection;
  728. codeexprlen: QWord;
  729. begin
  730. ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  731. ObjSection:=TWasmObjSection(objsym.objsection);
  732. ObjSection.Data.seek(objsym.address);
  733. codeexprlen:=ObjSection.Size-objsym.address;
  734. encoded_locals:=tdynamicarray.Create(64);
  735. WriteFunctionLocals(encoded_locals,ObjSymExtraData);
  736. codelen:=encoded_locals.size+codeexprlen+1;
  737. WriteUleb(dest,codelen);
  738. encoded_locals.seek(0);
  739. CopyDynamicArray(encoded_locals,dest,encoded_locals.size);
  740. ObjSection.FileSectionOfs:=dest.size-objsym.offset;
  741. CopyDynamicArray(ObjSection.Data,dest,codeexprlen);
  742. WriteByte(dest,$0B);
  743. encoded_locals.Free;
  744. end;
  745. procedure TWasmObjOutput.WriteSymbolTable;
  746. begin
  747. WriteUleb(FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTableEntriesCount);
  748. FWasmSymbolTable.seek(0);
  749. CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
  750. end;
  751. procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
  752. begin
  753. WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
  754. WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
  755. FWasmRelocationCodeTable.seek(0);
  756. CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
  757. end;
  758. procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
  759. begin
  760. WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
  761. WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
  762. FWasmRelocationDataTable.seek(0);
  763. CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
  764. end;
  765. procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  766. begin
  767. if FWasmLinkingSubsections[wlst].size>0 then
  768. begin
  769. WriteByte(FWasmCustomSections[wcstLinking],Ord(wlst));
  770. WriteUleb(FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  771. FWasmLinkingSubsections[wlst].seek(0);
  772. CopyDynamicArray(FWasmLinkingSubsections[wlst],FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  773. end;
  774. end;
  775. procedure TWasmObjOutput.DoRelocations;
  776. var
  777. si, ri: Integer;
  778. objsec: TWasmObjSection;
  779. objrel: TWasmObjRelocation;
  780. begin
  781. for si:=0 to FData.ObjSectionList.Count-1 do
  782. begin
  783. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  784. for ri:=0 to objsec.ObjRelocations.Count-1 do
  785. begin
  786. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  787. case objrel.typ of
  788. RELOC_FUNCTION_INDEX_LEB:
  789. begin
  790. if not assigned(objrel.symbol) then
  791. internalerror(2021092509);
  792. objsec.Data.seek(objrel.DataOffset);
  793. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).ImportOrFuncIndex);
  794. end;
  795. RELOC_MEMORY_ADDR_SLEB:
  796. begin
  797. if not assigned(objrel.symbol) then
  798. internalerror(2021092605);
  799. if objrel.symbol.bind<>AB_EXTERNAL then
  800. begin
  801. objsec.Data.seek(objrel.DataOffset);
  802. AddSleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  803. end;
  804. end;
  805. RELOC_MEMORY_ADDR_LEB:
  806. begin
  807. if not assigned(objrel.symbol) then
  808. internalerror(2021092606);
  809. if objrel.symbol.bind<>AB_EXTERNAL then
  810. begin
  811. objsec.Data.seek(objrel.DataOffset);
  812. AddUleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  813. end;
  814. end;
  815. RELOC_ABSOLUTE:
  816. ;
  817. else
  818. internalerror(2021092510);
  819. end;
  820. end;
  821. end;
  822. end;
  823. procedure TWasmObjOutput.WriteRelocations;
  824. var
  825. si, ri: Integer;
  826. objsec: TWasmObjSection;
  827. objrel: TWasmObjRelocation;
  828. relout: tdynamicarray;
  829. relcount: PInteger;
  830. begin
  831. for si:=0 to FData.ObjSectionList.Count-1 do
  832. begin
  833. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  834. if objsec.IsCode then
  835. begin
  836. relout:=FWasmRelocationCodeTable;
  837. relcount:=@FWasmRelocationCodeTableEntriesCount;
  838. end
  839. else
  840. begin
  841. relout:=FWasmRelocationDataTable;
  842. relcount:=@FWasmRelocationDataTableEntriesCount;
  843. end;
  844. for ri:=0 to objsec.ObjRelocations.Count-1 do
  845. begin
  846. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  847. case objrel.typ of
  848. RELOC_FUNCTION_INDEX_LEB:
  849. begin
  850. if not assigned(objrel.symbol) then
  851. internalerror(2021092508);
  852. Inc(relcount^);
  853. WriteByte(relout,Ord(R_WASM_FUNCTION_INDEX_LEB));
  854. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  855. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  856. end;
  857. RELOC_MEMORY_ADDR_LEB:
  858. begin
  859. if not assigned(objrel.symbol) then
  860. internalerror(2021092603);
  861. Inc(relcount^);
  862. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_LEB));
  863. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  864. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  865. WriteUleb(relout,0); { addend to add to the address }
  866. end;
  867. RELOC_MEMORY_ADDR_SLEB:
  868. begin
  869. if not assigned(objrel.symbol) then
  870. internalerror(2021092604);
  871. Inc(relcount^);
  872. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB));
  873. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  874. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  875. WriteUleb(relout,0); { addend to add to the address }
  876. end;
  877. RELOC_ABSOLUTE:
  878. begin
  879. if not assigned(objrel.symbol) then
  880. internalerror(2021092604);
  881. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  882. begin
  883. Inc(relcount^);
  884. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_I32));
  885. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  886. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  887. end
  888. else
  889. internalerror(2021092609);
  890. end;
  891. else
  892. internalerror(2021092507);
  893. end;
  894. end;
  895. end;
  896. end;
  897. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  898. var
  899. i: Integer;
  900. objsec: TWasmObjSection;
  901. segment_count: Integer = 0;
  902. cur_seg_ofs: qword = 0;
  903. types_count,
  904. imports_count, NextImportFunctionIndex, NextFunctionIndex: Integer;
  905. import_functions_count: Integer = 0;
  906. functions_count: Integer = 0;
  907. objsym: TWasmObjSymbol;
  908. cust_sec: TWasmCustomSectionType;
  909. begin
  910. FData:=TWasmObjData(Data);
  911. { each custom sections starts with its name }
  912. for cust_sec in TWasmCustomSectionType do
  913. WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
  914. WriteUleb(FWasmCustomSections[wcstLinking],2); { linking metadata version }
  915. for i:=0 to Data.ObjSymbolList.Count-1 do
  916. begin
  917. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  918. if IsExternalFunction(objsym) then
  919. Inc(import_functions_count);
  920. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  921. Inc(functions_count);
  922. end;
  923. types_count:=Length(FData.FFuncTypes);
  924. WriteUleb(FWasmSections[wsiType],types_count);
  925. for i:=0 to types_count-1 do
  926. with FData.FFuncTypes[i] do
  927. begin
  928. WriteByte(FWasmSections[wsiType],$60);
  929. WriteWasmResultType(FWasmSections[wsiType],params);
  930. WriteWasmResultType(FWasmSections[wsiType],results);
  931. end;
  932. for i:=0 to Data.ObjSectionList.Count-1 do
  933. begin
  934. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  935. if objsec.IsCode then
  936. objsec.SegIdx:=-1
  937. else
  938. begin
  939. objsec.SegIdx:=segment_count;
  940. objsec.SegOfs:=cur_seg_ofs;
  941. Inc(segment_count);
  942. Inc(cur_seg_ofs,objsec.Size);
  943. end;
  944. end;
  945. WriteUleb(FWasmSections[wsiData],segment_count);
  946. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  947. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],segment_count);
  948. for i:=0 to Data.ObjSectionList.Count-1 do
  949. begin
  950. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  951. if objsec.IsData then
  952. begin
  953. WriteName(FWasmLinkingSubsections[WASM_SEGMENT_INFO],objsec.Name);
  954. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],BsrQWord(objsec.SecAlign));
  955. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],0); { flags }
  956. WriteByte(FWasmSections[wsiData],0);
  957. WriteByte(FWasmSections[wsiData],$41);
  958. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  959. WriteByte(FWasmSections[wsiData],$0b);
  960. WriteUleb(FWasmSections[wsiData],objsec.Size);
  961. objsec.FileSectionOfs:=FWasmSections[wsiData].size;
  962. if oso_Data in objsec.SecOptions then
  963. begin
  964. objsec.Data.seek(0);
  965. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  966. end
  967. else
  968. begin
  969. WriteZeros(FWasmSections[wsiData],objsec.Size);
  970. end;
  971. end;
  972. end;
  973. imports_count:=3+import_functions_count;
  974. WriteUleb(FWasmSections[wsiImport],imports_count);
  975. { import[0] }
  976. WriteName(FWasmSections[wsiImport],'env');
  977. WriteName(FWasmSections[wsiImport],'__linear_memory');
  978. WriteByte(FWasmSections[wsiImport],$02); { mem }
  979. WriteByte(FWasmSections[wsiImport],$00); { min }
  980. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  981. { import[1] }
  982. WriteName(FWasmSections[wsiImport],'env');
  983. WriteName(FWasmSections[wsiImport],'__stack_pointer');
  984. WriteByte(FWasmSections[wsiImport],$03); { global }
  985. WriteByte(FWasmSections[wsiImport],$7F); { i32 }
  986. WriteByte(FWasmSections[wsiImport],$01); { var }
  987. { import[2]..import[imports_count-2] }
  988. NextImportFunctionIndex:=0;
  989. for i:=0 to Data.ObjSymbolList.Count-1 do
  990. begin
  991. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  992. if IsExternalFunction(objsym) then
  993. begin
  994. objsym.ImportIndex:=NextImportFunctionIndex;
  995. Inc(NextImportFunctionIndex);
  996. WriteName(FWasmSections[wsiImport],'env');
  997. WriteName(FWasmSections[wsiImport],objsym.Name);
  998. WriteByte(FWasmSections[wsiImport],$00); { func }
  999. WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1000. end;
  1001. end;
  1002. { import[imports_count-1] }
  1003. WriteName(FWasmSections[wsiImport],'env');
  1004. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  1005. WriteByte(FWasmSections[wsiImport],$01); { table }
  1006. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  1007. WriteByte(FWasmSections[wsiImport],$00); { min }
  1008. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  1009. WriteUleb(FWasmSections[wsiFunction],functions_count);
  1010. NextFunctionIndex:=NextImportFunctionIndex;
  1011. for i:=0 to Data.ObjSymbolList.Count-1 do
  1012. begin
  1013. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1014. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1015. begin
  1016. objsym.FuncIndex:=NextFunctionIndex;
  1017. Inc(NextFunctionIndex);
  1018. WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1019. end;
  1020. end;
  1021. for i:=0 to Data.ObjSymbolList.Count-1 do
  1022. begin
  1023. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1024. if IsExternalFunction(objsym) then
  1025. begin
  1026. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1027. Inc(FWasmSymbolTableEntriesCount);
  1028. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1029. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1030. WriteUleb(FWasmSymbolTable,objsym.ImportIndex);
  1031. end
  1032. else if objsym.typ=AT_FUNCTION then
  1033. begin
  1034. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1035. Inc(FWasmSymbolTableEntriesCount);
  1036. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1037. if objsym.IsAlias then
  1038. begin
  1039. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
  1040. WriteUleb(FWasmSymbolTable,TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf)).FuncIndex);
  1041. end
  1042. else
  1043. begin
  1044. WriteUleb(FWasmSymbolTable,0);
  1045. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1046. end;
  1047. WriteName(FWasmSymbolTable,objsym.Name);
  1048. end
  1049. else if (objsym.typ=AT_DATA) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
  1050. begin
  1051. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1052. Inc(FWasmSymbolTableEntriesCount);
  1053. WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
  1054. if objsym.bind=AB_GLOBAL then
  1055. WriteUleb(FWasmSymbolTable,0)
  1056. else if objsym.bind=AB_LOCAL then
  1057. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1058. else if objsym.bind=AB_EXTERNAL then
  1059. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1060. else
  1061. internalerror(2021092506);
  1062. WriteName(FWasmSymbolTable,objsym.Name);
  1063. if objsym.bind<>AB_EXTERNAL then
  1064. begin
  1065. WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
  1066. WriteUleb(FWasmSymbolTable,objsym.offset);
  1067. WriteUleb(FWasmSymbolTable,objsym.size);
  1068. end;
  1069. end;
  1070. end;
  1071. DoRelocations;
  1072. WriteUleb(FWasmSections[wsiCode],functions_count);
  1073. for i:=0 to Data.ObjSymbolList.Count-1 do
  1074. begin
  1075. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1076. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1077. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  1078. end;
  1079. WriteRelocations;
  1080. WriteSymbolTable;
  1081. WriteLinkingSubsection(WASM_SYMBOL_TABLE);
  1082. WriteLinkingSubsection(WASM_SEGMENT_INFO);
  1083. WriteRelocationCodeTable(4); { code section is section #4 }
  1084. WriteRelocationDataTable(5); { code section is section #5 }
  1085. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  1086. Writer.write(WasmVersion,SizeOf(WasmVersion));
  1087. WriteWasmSection(wsiType); { section #0 }
  1088. WriteWasmSection(wsiImport); { section #1 }
  1089. WriteWasmSection(wsiFunction); { section #2 }
  1090. WriteWasmSection(wsiDataCount); { section #3 }
  1091. WriteWasmSection(wsiCode); { section #4 }
  1092. WriteWasmSection(wsiData); { section #5 }
  1093. WriteWasmCustomSection(wcstLinking); { section #6 }
  1094. WriteWasmCustomSection(wcstRelocCode); { section #7 }
  1095. WriteWasmCustomSection(wcstRelocData); { section #8 }
  1096. Writeln('ObjSymbolList:');
  1097. for i:=0 to Data.ObjSymbolList.Count-1 do
  1098. begin
  1099. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1100. Write(objsym.Name, ' bind=', objsym.Bind, ' typ=', objsym.typ, ' address=', objsym.address, ' objsection=');
  1101. if assigned(objsym.objsection) then
  1102. Write(objsym.objsection.Name)
  1103. else
  1104. Write('nil');
  1105. Writeln;
  1106. end;
  1107. Writeln('ObjSectionList:');
  1108. for i:=0 to Data.ObjSectionList.Count-1 do
  1109. begin
  1110. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1111. Writeln(objsec.Name, ' IsCode=', objsec.IsCode, ' IsData=', objsec.IsData, ' Size=', objsec.Size, ' MemPos=', objsec.MemPos, ' DataPos=', objsec.DataPos, ' SegIdx=', objsec.SegIdx);
  1112. end;
  1113. result:=true;
  1114. end;
  1115. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  1116. var
  1117. i: TWasmSectionID;
  1118. j: TWasmCustomSectionType;
  1119. k: TWasmLinkingSubsectionType;
  1120. begin
  1121. inherited;
  1122. cobjdata:=TWasmObjData;
  1123. for i in TWasmSectionID do
  1124. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  1125. for j in TWasmCustomSectionType do
  1126. FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
  1127. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1128. FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
  1129. FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
  1130. FWasmSymbolTableEntriesCount:=0;
  1131. FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
  1132. FWasmRelocationCodeTableEntriesCount:=0;
  1133. FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
  1134. FWasmRelocationDataTableEntriesCount:=0;
  1135. end;
  1136. destructor TWasmObjOutput.destroy;
  1137. var
  1138. i: TWasmSectionID;
  1139. j: TWasmCustomSectionType;
  1140. k: TWasmLinkingSubsectionType;
  1141. begin
  1142. for i in TWasmSectionID do
  1143. FWasmSections[i].Free;
  1144. for j in TWasmCustomSectionType do
  1145. FWasmCustomSections[j].Free;
  1146. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1147. FWasmLinkingSubsections[k].Free;
  1148. FWasmSymbolTable.Free;
  1149. FWasmRelocationCodeTable.Free;
  1150. FWasmRelocationDataTable.Free;
  1151. inherited destroy;
  1152. end;
  1153. {****************************************************************************
  1154. TWasmAssembler
  1155. ****************************************************************************}
  1156. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  1157. begin
  1158. inherited;
  1159. CObjOutput:=TWasmObjOutput;
  1160. end;
  1161. {*****************************************************************************
  1162. Initialize
  1163. *****************************************************************************}
  1164. {$ifdef wasm32}
  1165. const
  1166. as_wasm32_wasm_info : tasminfo =
  1167. (
  1168. id : as_wasm32_wasm;
  1169. idtxt : 'OMF';
  1170. asmbin : '';
  1171. asmcmd : '';
  1172. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  1173. flags : [af_outputbinary,af_smartlink_sections];
  1174. labelprefix : '..@';
  1175. labelmaxlen : -1;
  1176. comment : '; ';
  1177. dollarsign: '$';
  1178. );
  1179. {$endif wasm32}
  1180. initialization
  1181. {$ifdef wasm32}
  1182. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  1183. {$endif wasm32}
  1184. end.