ogwasm.pas 15 KB

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