ogwasm.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. { TWasmObjSection }
  34. TWasmObjSection = class(TObjSection)
  35. public
  36. SegIdx: Integer;
  37. SegOfs: qword;
  38. function IsCode: Boolean;
  39. function IsData: Boolean;
  40. end;
  41. { TWasmObjData }
  42. TWasmObjData = class(TObjData)
  43. private
  44. FFuncTypes: array of TWasmFuncType;
  45. FFuncTypeNames: TFPHashList;
  46. function is_smart_section(atype:TAsmSectiontype):boolean;
  47. function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  48. public
  49. constructor create(const n:string);override;
  50. destructor destroy; override;
  51. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  52. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  53. function AddFuncType(wft: TWasmFuncType): integer;
  54. procedure DeclareFuncType(ft: tai_functype);
  55. end;
  56. { TWasmObjOutput }
  57. TWasmObjOutput = class(tObjOutput)
  58. private
  59. FWasmSections: array [TWasmSectionID] of tdynamicarray;
  60. procedure WriteUleb(d: tdynamicarray; v: uint64);
  61. procedure WriteUleb(w: TObjectWriter; v: uint64);
  62. procedure WriteSleb(d: tdynamicarray; v: int64);
  63. procedure WriteByte(d: tdynamicarray; b: byte);
  64. procedure WriteName(d: tdynamicarray; const s: string);
  65. procedure WriteWasmSection(wsid: TWasmSectionID);
  66. procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  67. procedure WriteZeros(dest: tdynamicarray; size: QWord);
  68. procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  69. procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  70. protected
  71. function writeData(Data:TObjData):boolean;override;
  72. public
  73. constructor create(AWriter:TObjectWriter);override;
  74. destructor destroy;override;
  75. end;
  76. { TWasmAssembler }
  77. TWasmAssembler = class(tinternalassembler)
  78. constructor create(info: pasminfo; smart:boolean);override;
  79. end;
  80. implementation
  81. uses
  82. verbose;
  83. {****************************************************************************
  84. TWasmObjSection
  85. ****************************************************************************}
  86. function TWasmObjSection.IsCode: Boolean;
  87. const
  88. CodePrefix = '.text';
  89. begin
  90. result:=(Length(Name)>=Length(CodePrefix)) and
  91. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  92. end;
  93. function TWasmObjSection.IsData: Boolean;
  94. begin
  95. result:=not IsCode;
  96. end;
  97. {****************************************************************************
  98. TWasmObjData
  99. ****************************************************************************}
  100. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  101. begin
  102. { For bss we need to set some flags that are target dependent,
  103. it is easier to disable it for smartlinking. It doesn't take up
  104. filespace }
  105. result:=not(target_info.system in systems_darwin) and
  106. create_smartlink_sections and
  107. (atype<>sec_toc) and
  108. (atype<>sec_user) and
  109. { on embedded systems every byte counts, so smartlink bss too }
  110. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  111. end;
  112. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  113. const aname: string; aorder: TAsmSectionOrder): string;
  114. const
  115. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  116. '.text',
  117. '.data',
  118. { why doesn't .rodata work? (FK) }
  119. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  120. { vtables (and anything else containing relocations), otherwise those are }
  121. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  122. { vtable for a class called Window: }
  123. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  124. { TODO: .data.ro not yet working}
  125. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  126. '.rodata',
  127. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  128. '.data',
  129. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  130. '.rodata',
  131. '.bss',
  132. '.threadvar',
  133. '.pdata',
  134. '', { stubs }
  135. '__DATA,__nl_symbol_ptr',
  136. '__DATA,__la_symbol_ptr',
  137. '__DATA,__mod_init_func',
  138. '__DATA,__mod_term_func',
  139. '.stab',
  140. '.stabstr',
  141. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  142. '.eh_frame',
  143. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  144. '.fpc',
  145. '.toc',
  146. '.init',
  147. '.fini',
  148. '.objc_class',
  149. '.objc_meta_class',
  150. '.objc_cat_cls_meth',
  151. '.objc_cat_inst_meth',
  152. '.objc_protocol',
  153. '.objc_string_object',
  154. '.objc_cls_meth',
  155. '.objc_inst_meth',
  156. '.objc_cls_refs',
  157. '.objc_message_refs',
  158. '.objc_symbols',
  159. '.objc_category',
  160. '.objc_class_vars',
  161. '.objc_instance_vars',
  162. '.objc_module_info',
  163. '.objc_class_names',
  164. '.objc_meth_var_types',
  165. '.objc_meth_var_names',
  166. '.objc_selector_strs',
  167. '.objc_protocol_ext',
  168. '.objc_class_ext',
  169. '.objc_property',
  170. '.objc_image_info',
  171. '.objc_cstring_object',
  172. '.objc_sel_fixup',
  173. '__DATA,__objc_data',
  174. '__DATA,__objc_const',
  175. '.objc_superrefs',
  176. '__DATA, __datacoal_nt,coalesced',
  177. '.objc_classlist',
  178. '.objc_nlclasslist',
  179. '.objc_catlist',
  180. '.obcj_nlcatlist',
  181. '.objc_protolist',
  182. '.stack',
  183. '.heap',
  184. '.gcc_except_table',
  185. '.ARM.attributes'
  186. );
  187. var
  188. sep : string[3];
  189. secname : string;
  190. begin
  191. secname:=secnames[atype];
  192. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  193. begin
  194. result:=secname+'.'+aname;
  195. exit;
  196. end;
  197. if atype=sec_threadvar then
  198. begin
  199. if (target_info.system in (systems_windows+systems_wince)) then
  200. secname:='.tls'
  201. else if (target_info.system in systems_linux) then
  202. secname:='.tbss';
  203. end;
  204. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  205. Thus, data which normally goes into .rodata and .rodata_norel sections must
  206. end up in .data section }
  207. if (atype in [sec_rodata,sec_rodata_norel]) and
  208. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  209. secname:='.data';
  210. { Windows correctly handles reallocations in readonly sections }
  211. if (atype=sec_rodata) and
  212. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  213. secname:='.rodata';
  214. { section type user gives the user full controll on the section name }
  215. if atype=sec_user then
  216. secname:=aname;
  217. if is_smart_section(atype) and (aname<>'') then
  218. begin
  219. case aorder of
  220. secorder_begin :
  221. sep:='.b_';
  222. secorder_end :
  223. sep:='.z_';
  224. else
  225. sep:='.n_';
  226. end;
  227. result:=secname+sep+aname
  228. end
  229. else
  230. result:=secname;
  231. end;
  232. constructor TWasmObjData.create(const n: string);
  233. begin
  234. inherited;
  235. CObjSection:=TWasmObjSection;
  236. FFuncTypeNames:=TFPHashList.Create;
  237. end;
  238. destructor TWasmObjData.destroy;
  239. var
  240. i: Integer;
  241. begin
  242. FFuncTypeNames.free;
  243. for i:=low(FFuncTypes) to high(FFuncTypes) do
  244. begin
  245. FFuncTypes[i].free;
  246. FFuncTypes[i]:=nil;
  247. end;
  248. inherited destroy;
  249. end;
  250. function TWasmObjData.sectionname(atype: TAsmSectiontype;
  251. const aname: string; aorder: TAsmSectionOrder): string;
  252. begin
  253. if (atype=sec_fpc) or (atype=sec_threadvar) then
  254. atype:=sec_data;
  255. Result:=sectionname_gas(atype, aname, aorder);
  256. end;
  257. procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
  258. p: TObjSymbol; Reloctype: TObjRelocationType);
  259. begin
  260. end;
  261. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  262. var
  263. i: Integer;
  264. begin
  265. for i:=low(FFuncTypes) to high(FFuncTypes) do
  266. if wft.Equals(FFuncTypes[i]) then
  267. exit(i);
  268. result:=Length(FFuncTypes);
  269. SetLength(FFuncTypes,result+1);
  270. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  271. end;
  272. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  273. var
  274. i: Integer;
  275. begin
  276. i:=AddFuncType(ft.functype);
  277. FFuncTypeNames.Add(ft.funcname, @(FFuncTypes[i]));
  278. end;
  279. {****************************************************************************
  280. TWasmObjOutput
  281. ****************************************************************************}
  282. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  283. var
  284. b: byte;
  285. begin
  286. repeat
  287. b:=byte(v) and 127;
  288. v:=v shr 7;
  289. if v<>0 then
  290. b:=b or 128;
  291. d.write(b,1);
  292. until v=0;
  293. end;
  294. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  295. var
  296. b: byte;
  297. begin
  298. repeat
  299. b:=byte(v) and 127;
  300. v:=v shr 7;
  301. if v<>0 then
  302. b:=b or 128;
  303. w.write(b,1);
  304. until v=0;
  305. end;
  306. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  307. var
  308. b: byte;
  309. Done: Boolean=false;
  310. begin
  311. repeat
  312. b:=byte(v) and 127;
  313. v:=SarInt64(v,7);
  314. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  315. Done:=true
  316. else
  317. b:=b or 128;
  318. d.write(b,1);
  319. until Done;
  320. end;
  321. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  322. begin
  323. d.write(b,1);
  324. end;
  325. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  326. begin
  327. WriteUleb(d,Length(s));
  328. d.writestr(s);
  329. end;
  330. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  331. var
  332. b: byte;
  333. begin
  334. b:=ord(wsid);
  335. Writer.write(b,1);
  336. WriteUleb(Writer,FWasmSections[wsid].size);
  337. Writer.writearray(FWasmSections[wsid]);
  338. end;
  339. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  340. var
  341. buf: array [0..4095] of byte;
  342. bs: Integer;
  343. begin
  344. while size>0 do
  345. begin
  346. if size<SizeOf(buf) then
  347. bs:=Integer(size)
  348. else
  349. bs:=SizeOf(buf);
  350. src.read(buf,bs);
  351. dest.write(buf,bs);
  352. dec(size,bs);
  353. end;
  354. end;
  355. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  356. var
  357. buf : array[0..1023] of byte;
  358. bs: Integer;
  359. begin
  360. fillchar(buf,sizeof(buf),0);
  361. while size>0 do
  362. begin
  363. if size<SizeOf(buf) then
  364. bs:=Integer(size)
  365. else
  366. bs:=SizeOf(buf);
  367. dest.write(buf,bs);
  368. dec(size,bs);
  369. end;
  370. end;
  371. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  372. var
  373. i: Integer;
  374. begin
  375. WriteUleb(dest,Length(wrt));
  376. for i:=low(wrt) to high(wrt) do
  377. WriteWasmBasicType(dest,wrt[i]);
  378. end;
  379. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  380. begin
  381. case wbt of
  382. wbt_i32:
  383. WriteByte(dest,$7F);
  384. wbt_i64:
  385. WriteByte(dest,$7E);
  386. wbt_f32:
  387. WriteByte(dest,$7D);
  388. wbt_f64:
  389. WriteByte(dest,$7C);
  390. end;
  391. end;
  392. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  393. var
  394. i: Integer;
  395. objsec: TWasmObjSection;
  396. segment_count: Integer = 0;
  397. cur_seg_ofs: qword = 0;
  398. types_count,
  399. imports_count: Integer;
  400. objsym: TObjSymbol;
  401. begin
  402. types_count:=Length(TWasmObjData(Data).FFuncTypes);
  403. WriteUleb(FWasmSections[wsiType],types_count);
  404. for i:=0 to types_count-1 do
  405. with TWasmObjData(Data).FFuncTypes[i] do
  406. begin
  407. WriteByte(FWasmSections[wsiType],$60);
  408. WriteWasmResultType(FWasmSections[wsiType],params);
  409. WriteWasmResultType(FWasmSections[wsiType],results);
  410. end;
  411. for i:=0 to Data.ObjSectionList.Count-1 do
  412. begin
  413. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  414. if objsec.IsCode then
  415. objsec.SegIdx:=-1
  416. else
  417. begin
  418. objsec.SegIdx:=segment_count;
  419. objsec.SegOfs:=cur_seg_ofs;
  420. Inc(segment_count);
  421. Inc(cur_seg_ofs,objsec.Size);
  422. end;
  423. end;
  424. WriteUleb(FWasmSections[wsiData],segment_count);
  425. for i:=0 to Data.ObjSectionList.Count-1 do
  426. begin
  427. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  428. if objsec.IsData then
  429. begin
  430. WriteByte(FWasmSections[wsiData],0);
  431. WriteByte(FWasmSections[wsiData],$41);
  432. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  433. WriteByte(FWasmSections[wsiData],$0b);
  434. WriteUleb(FWasmSections[wsiData],objsec.Size);
  435. if oso_Data in objsec.SecOptions then
  436. begin
  437. objsec.Data.seek(0);
  438. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  439. end
  440. else
  441. begin
  442. WriteZeros(FWasmSections[wsiData],objsec.Size);
  443. end;
  444. end;
  445. end;
  446. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  447. imports_count:=3;
  448. WriteUleb(FWasmSections[wsiImport],imports_count);
  449. { import[0] }
  450. WriteName(FWasmSections[wsiImport],'env');
  451. WriteName(FWasmSections[wsiImport],'__linear_memory');
  452. WriteByte(FWasmSections[wsiImport],$02); { mem }
  453. WriteByte(FWasmSections[wsiImport],$00); { min }
  454. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  455. { import[1] }
  456. WriteName(FWasmSections[wsiImport],'env');
  457. WriteName(FWasmSections[wsiImport],'__stack_pointer');
  458. WriteByte(FWasmSections[wsiImport],$03); { global }
  459. WriteByte(FWasmSections[wsiImport],$7F); { i32 }
  460. WriteByte(FWasmSections[wsiImport],$01); { var }
  461. { import[imports_count-1] }
  462. WriteName(FWasmSections[wsiImport],'env');
  463. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  464. WriteByte(FWasmSections[wsiImport],$01); { table }
  465. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  466. WriteByte(FWasmSections[wsiImport],$00); { min }
  467. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  468. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  469. Writer.write(WasmVersion,SizeOf(WasmVersion));
  470. WriteWasmSection(wsiType);
  471. WriteWasmSection(wsiImport);
  472. WriteWasmSection(wsiDataCount);
  473. WriteWasmSection(wsiData);
  474. Writeln('ObjSymbolList:');
  475. for i:=0 to Data.ObjSymbolList.Count-1 do
  476. begin
  477. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  478. Write(objsym.Name, ' bind=', objsym.Bind, ' typ=', objsym.typ, ' address=', objsym.address, ' objsection=');
  479. if assigned(objsym.objsection) then
  480. Write(objsym.objsection.Name)
  481. else
  482. Write('nil');
  483. Writeln;
  484. end;
  485. Writeln('ObjSectionList:');
  486. for i:=0 to Data.ObjSectionList.Count-1 do
  487. begin
  488. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  489. Writeln(objsec.Name, ' IsCode=', objsec.IsCode, ' IsData=', objsec.IsData, ' Size=', objsec.Size, ' MemPos=', objsec.MemPos, ' DataPos=', objsec.DataPos, ' SegIdx=', objsec.SegIdx);
  490. end;
  491. result:=true;
  492. end;
  493. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  494. var
  495. i: TWasmSectionID;
  496. begin
  497. inherited;
  498. cobjdata:=TWasmObjData;
  499. for i in TWasmSectionID do
  500. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  501. end;
  502. destructor TWasmObjOutput.destroy;
  503. var
  504. i: TWasmSectionID;
  505. begin
  506. for i in TWasmSectionID do
  507. FWasmSections[i].Free;
  508. inherited destroy;
  509. end;
  510. {****************************************************************************
  511. TWasmAssembler
  512. ****************************************************************************}
  513. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  514. begin
  515. inherited;
  516. CObjOutput:=TWasmObjOutput;
  517. end;
  518. {*****************************************************************************
  519. Initialize
  520. *****************************************************************************}
  521. {$ifdef wasm32}
  522. const
  523. as_wasm32_wasm_info : tasminfo =
  524. (
  525. id : as_wasm32_wasm;
  526. idtxt : 'OMF';
  527. asmbin : '';
  528. asmcmd : '';
  529. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  530. flags : [af_outputbinary,af_smartlink_sections];
  531. labelprefix : '..@';
  532. labelmaxlen : -1;
  533. comment : '; ';
  534. dollarsign: '$';
  535. );
  536. {$endif wasm32}
  537. initialization
  538. {$ifdef wasm32}
  539. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  540. {$endif wasm32}
  541. end.