ogwasm.pas 38 KB

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