ogwasm.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. {
  2. Copyright (c) 2021 by Nikolay Nikolov
  3. Contains the WebAssembly binary module format reader and writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogwasm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,cpubase,
  25. { assembler }
  26. aasmbase,assemble,aasmcpu,
  27. { WebAssembly module format definitions }
  28. wasmbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. TWasmObjSymbolExtraData = class;
  34. { TWasmObjSymbol }
  35. TWasmObjSymbol = class(TObjSymbol)
  36. FuncIndex: Integer;
  37. SymbolIndex: Integer;
  38. GlobalIndex: Integer;
  39. TagIndex: Integer;
  40. AliasOf: string;
  41. ExtraData: TWasmObjSymbolExtraData;
  42. constructor create(AList:TFPHashObjectList;const AName:string);override;
  43. function IsAlias: Boolean;
  44. end;
  45. { TWasmObjRelocation }
  46. TWasmObjRelocation = class(TObjRelocation)
  47. public
  48. TypeIndex: Integer;
  49. Addend: LongInt;
  50. constructor CreateTypeIndex(ADataOffset:TObjSectionOfs; ATypeIndex: Integer);
  51. end;
  52. { TWasmObjSymbolExtraData }
  53. TWasmObjSymbolExtraData = class(TFPHashObject)
  54. TypeIdx: Integer;
  55. ExceptionTagTypeIdx: Integer;
  56. ImportModule: string;
  57. ImportName: string;
  58. ExportName: string;
  59. GlobalType: TWasmBasicType;
  60. GlobalIsImmutable: Boolean;
  61. Locals: array of TWasmBasicType;
  62. constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  63. procedure AddLocal(bastyp: TWasmBasicType);
  64. end;
  65. { TWasmObjSection }
  66. TWasmObjSection = class(TObjSection)
  67. public
  68. SegIdx: Integer;
  69. SegOfs: qword;
  70. FileSectionOfs: qword;
  71. function IsCode: Boolean;
  72. function IsData: Boolean;
  73. end;
  74. { TWasmObjData }
  75. TWasmObjData = class(TObjData)
  76. private
  77. FFuncTypes: array of TWasmFuncType;
  78. FObjSymbolsExtraDataList: TFPHashObjectList;
  79. FLastFuncName: string;
  80. function is_smart_section(atype:TAsmSectiontype):boolean;
  81. function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  82. public
  83. constructor create(const n:string);override;
  84. destructor destroy; override;
  85. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  86. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  87. function AddOrCreateObjSymbolExtraData(const symname:TSymStr): TWasmObjSymbolExtraData;
  88. function AddFuncType(wft: TWasmFuncType): integer;
  89. function globalref(asmsym:TAsmSymbol):TObjSymbol;
  90. function ExceptionTagRef(asmsym:TAsmSymbol):TObjSymbol;
  91. procedure DeclareGlobalType(gt: tai_globaltype);
  92. procedure DeclareFuncType(ft: tai_functype);
  93. procedure DeclareTagType(tt: tai_tagtype);
  94. procedure DeclareExportName(en: tai_export_name);
  95. procedure DeclareImportModule(aim: tai_import_module);
  96. procedure DeclareImportName(ain: tai_import_name);
  97. procedure DeclareLocal(al: tai_local);
  98. procedure symbolpairdefine(akind: TSymbolPairKind;const asym, avalue: string);override;
  99. end;
  100. { TWasmObjOutput }
  101. TWasmObjOutput = class(tObjOutput)
  102. private
  103. FData: TWasmObjData;
  104. FWasmRelocationCodeTable: tdynamicarray;
  105. FWasmRelocationCodeTableEntriesCount: Integer;
  106. FWasmRelocationDataTable: tdynamicarray;
  107. FWasmRelocationDataTableEntriesCount: Integer;
  108. FWasmSymbolTable: tdynamicarray;
  109. FWasmSymbolTableEntriesCount: Integer;
  110. FWasmSections: array [TWasmSectionID] of tdynamicarray;
  111. FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
  112. FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of tdynamicarray;
  113. procedure WriteUleb(d: tdynamicarray; v: uint64);
  114. procedure WriteUleb(w: TObjectWriter; v: uint64);
  115. procedure WriteSleb(d: tdynamicarray; v: int64);
  116. procedure WriteByte(d: tdynamicarray; b: byte);
  117. procedure WriteName(d: tdynamicarray; const s: string);
  118. procedure WriteWasmSection(wsid: TWasmSectionID);
  119. procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  120. procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  121. procedure WriteZeros(dest: tdynamicarray; size: QWord);
  122. procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  123. procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  124. function IsExternalFunction(sym: TObjSymbol): Boolean;
  125. function IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  126. procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  127. procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  128. procedure WriteSymbolTable;
  129. procedure WriteRelocationCodeTable(CodeSectionIndex: Integer);
  130. procedure WriteRelocationDataTable(DataSectionIndex: Integer);
  131. procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  132. procedure DoRelocations;
  133. procedure WriteRelocations;
  134. protected
  135. function writeData(Data:TObjData):boolean;override;
  136. public
  137. constructor create(AWriter:TObjectWriter);override;
  138. destructor destroy;override;
  139. end;
  140. { TWasmAssembler }
  141. TWasmAssembler = class(tinternalassembler)
  142. constructor create(info: pasminfo; smart:boolean);override;
  143. end;
  144. implementation
  145. uses
  146. verbose;
  147. procedure WriteUleb5(d: tdynamicarray; v: uint64);
  148. var
  149. b: byte;
  150. i: Integer;
  151. begin
  152. for i:=1 to 5 do
  153. begin
  154. b:=byte(v) and 127;
  155. v:=v shr 7;
  156. if i<>5 then
  157. b:=b or 128;
  158. d.write(b,1);
  159. end;
  160. end;
  161. procedure WriteUleb5(d: tobjsection; v: uint64);
  162. var
  163. b: byte;
  164. i: Integer;
  165. begin
  166. for i:=1 to 5 do
  167. begin
  168. b:=byte(v) and 127;
  169. v:=v shr 7;
  170. if i<>5 then
  171. b:=b or 128;
  172. d.write(b,1);
  173. end;
  174. end;
  175. procedure WriteSleb5(d: tdynamicarray; v: int64);
  176. var
  177. b: byte;
  178. i: Integer;
  179. begin
  180. for i:=1 to 5 do
  181. begin
  182. b:=byte(v) and 127;
  183. v:=SarInt64(v,7);
  184. if i<>5 then
  185. b:=b or 128;
  186. d.write(b,1);
  187. end;
  188. end;
  189. procedure WriteSleb5(d: tobjsection; v: int64);
  190. var
  191. b: byte;
  192. i: Integer;
  193. begin
  194. for i:=1 to 5 do
  195. begin
  196. b:=byte(v) and 127;
  197. v:=SarInt64(v,7);
  198. if i<>5 then
  199. b:=b or 128;
  200. d.write(b,1);
  201. end;
  202. end;
  203. function ReadUleb(d: tdynamicarray): uint64;
  204. var
  205. b: byte;
  206. shift:integer;
  207. begin
  208. b:=0;
  209. result:=0;
  210. shift:=0;
  211. repeat
  212. d.read(b,1);
  213. result:=result or (uint64(b and 127) shl shift);
  214. inc(shift,7);
  215. until (b and 128)=0;
  216. end;
  217. function ReadSleb(d: tdynamicarray): int64;
  218. var
  219. b: byte;
  220. shift:integer;
  221. begin
  222. b:=0;
  223. result:=0;
  224. shift:=0;
  225. repeat
  226. d.read(b,1);
  227. result:=result or (uint64(b and 127) shl shift);
  228. inc(shift,7);
  229. until (b and 128)=0;
  230. if (b and 64)<>0 then
  231. result:=result or (high(uint64) shl shift);
  232. end;
  233. procedure AddSleb5(d: tdynamicarray; v: int64);
  234. var
  235. q: Int64;
  236. p: LongWord;
  237. begin
  238. p:=d.Pos;
  239. q:=ReadSleb(d);
  240. q:=q+v;
  241. d.seek(p);
  242. WriteSleb5(d,q);
  243. end;
  244. procedure AddUleb5(d: tdynamicarray; v: int64);
  245. var
  246. q: UInt64;
  247. p: LongWord;
  248. begin
  249. p:=d.Pos;
  250. q:=ReadUleb(d);
  251. q:=q+v;
  252. d.seek(p);
  253. WriteUleb5(d,q);
  254. end;
  255. procedure AddInt32(d: tdynamicarray; v: int32);
  256. var
  257. q: int32;
  258. p: LongWord;
  259. begin
  260. p:=d.Pos;
  261. d.read(q,4);
  262. {$ifdef FPC_BIG_ENDIAN}
  263. q:=SwapEndian(q);
  264. {$endif FPC_BIG_ENDIAN}
  265. q:=q+v;
  266. {$ifdef FPC_BIG_ENDIAN}
  267. q:=SwapEndian(q);
  268. {$endif FPC_BIG_ENDIAN}
  269. d.seek(p);
  270. d.write(q,4);
  271. end;
  272. {****************************************************************************
  273. TWasmObjRelocation
  274. ****************************************************************************}
  275. constructor TWasmObjRelocation.CreateTypeIndex(ADataOffset: TObjSectionOfs; ATypeIndex: Integer);
  276. begin
  277. DataOffset:=ADataOffset;
  278. Symbol:=nil;
  279. OrgSize:=0;
  280. Group:=nil;
  281. ObjSection:=nil;
  282. ftype:=ord(RELOC_TYPE_INDEX_LEB);
  283. TypeIndex:=ATypeIndex;
  284. end;
  285. {****************************************************************************
  286. TWasmObjSymbol
  287. ****************************************************************************}
  288. constructor TWasmObjSymbol.create(AList: TFPHashObjectList; const AName: string);
  289. begin
  290. inherited create(AList,AName);
  291. FuncIndex:=-1;
  292. SymbolIndex:=-1;
  293. GlobalIndex:=-1;
  294. TagIndex:=-1;
  295. AliasOf:='';
  296. ExtraData:=nil;
  297. end;
  298. function TWasmObjSymbol.IsAlias: Boolean;
  299. begin
  300. result:=AliasOf<>'';
  301. end;
  302. {****************************************************************************
  303. TWasmObjSymbolExtraData
  304. ****************************************************************************}
  305. constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  306. begin
  307. inherited Create(HashObjectList,s);
  308. TypeIdx:=-1;
  309. ExceptionTagTypeIdx:=-1;
  310. end;
  311. procedure TWasmObjSymbolExtraData.AddLocal(bastyp: TWasmBasicType);
  312. begin
  313. SetLength(Locals,Length(Locals)+1);
  314. Locals[High(Locals)]:=bastyp;
  315. end;
  316. {****************************************************************************
  317. TWasmObjSection
  318. ****************************************************************************}
  319. function TWasmObjSection.IsCode: Boolean;
  320. const
  321. CodePrefix = '.text';
  322. begin
  323. result:=(Length(Name)>=Length(CodePrefix)) and
  324. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  325. end;
  326. function TWasmObjSection.IsData: Boolean;
  327. begin
  328. result:=not IsCode;
  329. end;
  330. {****************************************************************************
  331. TWasmObjData
  332. ****************************************************************************}
  333. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  334. begin
  335. { For bss we need to set some flags that are target dependent,
  336. it is easier to disable it for smartlinking. It doesn't take up
  337. filespace }
  338. result:=not(target_info.system in systems_darwin) and
  339. create_smartlink_sections and
  340. (atype<>sec_toc) and
  341. (atype<>sec_user) and
  342. { on embedded systems every byte counts, so smartlink bss too }
  343. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  344. end;
  345. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  346. const aname: string; aorder: TAsmSectionOrder): string;
  347. const
  348. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  349. '.text',
  350. '.data',
  351. { why doesn't .rodata work? (FK) }
  352. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  353. { vtables (and anything else containing relocations), otherwise those are }
  354. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  355. { vtable for a class called Window: }
  356. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  357. { TODO: .data.ro not yet working}
  358. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  359. '.rodata',
  360. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  361. '.data',
  362. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  363. '.rodata',
  364. '.bss',
  365. '.threadvar',
  366. '.pdata',
  367. '', { stubs }
  368. '__DATA,__nl_symbol_ptr',
  369. '__DATA,__la_symbol_ptr',
  370. '__DATA,__mod_init_func',
  371. '__DATA,__mod_term_func',
  372. '.stab',
  373. '.stabstr',
  374. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  375. '.eh_frame',
  376. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  377. '.fpc',
  378. '.toc',
  379. '.init',
  380. '.fini',
  381. '.objc_class',
  382. '.objc_meta_class',
  383. '.objc_cat_cls_meth',
  384. '.objc_cat_inst_meth',
  385. '.objc_protocol',
  386. '.objc_string_object',
  387. '.objc_cls_meth',
  388. '.objc_inst_meth',
  389. '.objc_cls_refs',
  390. '.objc_message_refs',
  391. '.objc_symbols',
  392. '.objc_category',
  393. '.objc_class_vars',
  394. '.objc_instance_vars',
  395. '.objc_module_info',
  396. '.objc_class_names',
  397. '.objc_meth_var_types',
  398. '.objc_meth_var_names',
  399. '.objc_selector_strs',
  400. '.objc_protocol_ext',
  401. '.objc_class_ext',
  402. '.objc_property',
  403. '.objc_image_info',
  404. '.objc_cstring_object',
  405. '.objc_sel_fixup',
  406. '__DATA,__objc_data',
  407. '__DATA,__objc_const',
  408. '.objc_superrefs',
  409. '__DATA, __datacoal_nt,coalesced',
  410. '.objc_classlist',
  411. '.objc_nlclasslist',
  412. '.objc_catlist',
  413. '.obcj_nlcatlist',
  414. '.objc_protolist',
  415. '.stack',
  416. '.heap',
  417. '.gcc_except_table',
  418. '.ARM.attributes'
  419. );
  420. var
  421. sep : string[3];
  422. secname : string;
  423. begin
  424. secname:=secnames[atype];
  425. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  426. begin
  427. result:=secname+'.'+aname;
  428. exit;
  429. end;
  430. if atype=sec_threadvar then
  431. begin
  432. if (target_info.system in (systems_windows+systems_wince)) then
  433. secname:='.tls'
  434. else if (target_info.system in systems_linux) then
  435. secname:='.tbss';
  436. end;
  437. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  438. Thus, data which normally goes into .rodata and .rodata_norel sections must
  439. end up in .data section }
  440. if (atype in [sec_rodata,sec_rodata_norel]) and
  441. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  442. secname:='.data';
  443. { Windows correctly handles reallocations in readonly sections }
  444. if (atype=sec_rodata) and
  445. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  446. secname:='.rodata';
  447. { section type user gives the user full controll on the section name }
  448. if atype=sec_user then
  449. secname:=aname;
  450. if is_smart_section(atype) and (aname<>'') then
  451. begin
  452. case aorder of
  453. secorder_begin :
  454. sep:='.b_';
  455. secorder_end :
  456. sep:='.z_';
  457. else
  458. sep:='.n_';
  459. end;
  460. result:=secname+sep+aname
  461. end
  462. else
  463. result:=secname;
  464. end;
  465. constructor TWasmObjData.create(const n: string);
  466. begin
  467. inherited;
  468. CObjSection:=TWasmObjSection;
  469. CObjSymbol:=TWasmObjSymbol;
  470. FObjSymbolsExtraDataList:=TFPHashObjectList.Create;
  471. end;
  472. destructor TWasmObjData.destroy;
  473. var
  474. i: Integer;
  475. begin
  476. FObjSymbolsExtraDataList.Free;
  477. for i:=low(FFuncTypes) to high(FFuncTypes) do
  478. begin
  479. FFuncTypes[i].free;
  480. FFuncTypes[i]:=nil;
  481. end;
  482. inherited destroy;
  483. end;
  484. function TWasmObjData.sectionname(atype: TAsmSectiontype;
  485. const aname: string; aorder: TAsmSectionOrder): string;
  486. begin
  487. if (atype=sec_fpc) or (atype=sec_threadvar) then
  488. atype:=sec_data;
  489. Result:=sectionname_gas(atype, aname, aorder);
  490. end;
  491. procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
  492. p: TObjSymbol; Reloctype: TObjRelocationType);
  493. const
  494. leb_zero: array[0..4] of byte=($80,$80,$80,$80,$00);
  495. var
  496. objreloc: TWasmObjRelocation;
  497. begin
  498. if CurrObjSec=nil then
  499. internalerror(200403072);
  500. objreloc:=nil;
  501. case Reloctype of
  502. RELOC_FUNCTION_INDEX_LEB:
  503. begin
  504. if Data<>0 then
  505. internalerror(2021092502);
  506. if len<>5 then
  507. internalerror(2021092503);
  508. if not assigned(p) then
  509. internalerror(2021092504);
  510. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  511. CurrObjSec.ObjRelocations.Add(objreloc);
  512. writebytes(leb_zero,5);
  513. end;
  514. RELOC_MEMORY_ADDR_LEB,
  515. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  516. begin
  517. if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then
  518. internalerror(2021092602);
  519. if len<>5 then
  520. internalerror(2021092503);
  521. if not assigned(p) then
  522. internalerror(2021092504);
  523. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  524. objreloc.Addend:=Data;
  525. CurrObjSec.ObjRelocations.Add(objreloc);
  526. if RelocType=RELOC_MEMORY_ADDR_LEB then
  527. WriteUleb5(CurrObjSec,Data)
  528. else
  529. WriteSleb5(CurrObjSec,Data);
  530. end;
  531. RELOC_ABSOLUTE:
  532. begin
  533. if len<>4 then
  534. internalerror(2021092607);
  535. if not assigned(p) then
  536. internalerror(2021092608);
  537. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  538. objreloc.Addend:=Data;
  539. CurrObjSec.ObjRelocations.Add(objreloc);
  540. Data:=NtoLE(Data);
  541. writebytes(Data,4);
  542. end;
  543. RELOC_TYPE_INDEX_LEB:
  544. begin
  545. if len<>5 then
  546. internalerror(2021092612);
  547. if assigned(p) then
  548. internalerror(2021092613);
  549. objreloc:=TWasmObjRelocation.CreateTypeIndex(CurrObjSec.Size,Data);
  550. CurrObjSec.ObjRelocations.Add(objreloc);
  551. WriteUleb5(CurrObjSec,Data);
  552. end;
  553. RELOC_GLOBAL_INDEX_LEB:
  554. begin
  555. if len<>5 then
  556. internalerror(2021092701);
  557. if Data<>0 then
  558. internalerror(2021092702);
  559. if not assigned(p) then
  560. internalerror(2021092703);
  561. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  562. CurrObjSec.ObjRelocations.Add(objreloc);
  563. WriteUleb5(CurrObjSec,0);
  564. end;
  565. RELOC_TAG_INDEX_LEB:
  566. begin
  567. if len<>5 then
  568. internalerror(2021092712);
  569. if Data<>0 then
  570. internalerror(2021092713);
  571. if not assigned(p) then
  572. internalerror(2021092714);
  573. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  574. CurrObjSec.ObjRelocations.Add(objreloc);
  575. WriteSleb5(CurrObjSec,0);
  576. end;
  577. else
  578. internalerror(2021092501);
  579. end;
  580. end;
  581. function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
  582. begin
  583. result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
  584. if not assigned(result) then
  585. result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
  586. end;
  587. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  588. var
  589. i: Integer;
  590. begin
  591. for i:=low(FFuncTypes) to high(FFuncTypes) do
  592. if wft.Equals(FFuncTypes[i]) then
  593. exit(i);
  594. result:=Length(FFuncTypes);
  595. SetLength(FFuncTypes,result+1);
  596. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  597. end;
  598. function TWasmObjData.globalref(asmsym: TAsmSymbol): TObjSymbol;
  599. begin
  600. if assigned(asmsym) then
  601. begin
  602. if asmsym.typ<>AT_WASM_GLOBAL then
  603. internalerror(2021092706);
  604. result:=symbolref(asmsym);
  605. result.typ:=AT_WASM_GLOBAL;
  606. end
  607. else
  608. result:=nil;
  609. end;
  610. function TWasmObjData.ExceptionTagRef(asmsym: TAsmSymbol): TObjSymbol;
  611. begin
  612. if assigned(asmsym) then
  613. begin
  614. if asmsym.typ<>AT_WASM_EXCEPTION_TAG then
  615. internalerror(2021092707);
  616. result:=symbolref(asmsym);
  617. result.typ:=AT_WASM_EXCEPTION_TAG;
  618. end
  619. else
  620. result:=nil;
  621. end;
  622. procedure TWasmObjData.DeclareGlobalType(gt: tai_globaltype);
  623. var
  624. ObjSymExtraData: TWasmObjSymbolExtraData;
  625. begin
  626. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(gt.globalname);
  627. ObjSymExtraData.GlobalType:=gt.gtype;
  628. ObjSymExtraData.GlobalIsImmutable:=gt.immutable;
  629. end;
  630. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  631. var
  632. i: Integer;
  633. ObjSymExtraData: TWasmObjSymbolExtraData;
  634. begin
  635. FLastFuncName:=ft.funcname;
  636. i:=AddFuncType(ft.functype);
  637. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
  638. ObjSymExtraData.TypeIdx:=i;
  639. end;
  640. procedure TWasmObjData.DeclareTagType(tt: tai_tagtype);
  641. var
  642. ObjSymExtraData: TWasmObjSymbolExtraData;
  643. ft: TWasmFuncType;
  644. i: Integer;
  645. begin
  646. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(tt.tagname);
  647. ft:=TWasmFuncType.Create([],tt.params);
  648. i:=AddFuncType(ft);
  649. ft.free;
  650. ObjSymExtraData.ExceptionTagTypeIdx:=i;
  651. end;
  652. procedure TWasmObjData.DeclareExportName(en: tai_export_name);
  653. var
  654. ObjSymExtraData: TWasmObjSymbolExtraData;
  655. begin
  656. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(en.intname);
  657. ObjSymExtraData.ExportName:=en.extname;
  658. end;
  659. procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
  660. var
  661. ObjSymExtraData: TWasmObjSymbolExtraData;
  662. begin
  663. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
  664. ObjSymExtraData.ImportModule:=aim.importmodule;
  665. end;
  666. procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
  667. var
  668. ObjSymExtraData: TWasmObjSymbolExtraData;
  669. begin
  670. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
  671. ObjSymExtraData.ImportName:=ain.importname;
  672. end;
  673. procedure TWasmObjData.DeclareLocal(al: tai_local);
  674. var
  675. ObjSymExtraData: TWasmObjSymbolExtraData;
  676. begin
  677. ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
  678. ObjSymExtraData.AddLocal(al.bastyp);
  679. end;
  680. procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
  681. var
  682. valsym: TObjSymbol;
  683. aliassym: TWasmObjSymbol;
  684. begin
  685. valsym:=CreateSymbol(avalue);
  686. aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
  687. aliassym.AliasOf:=valsym.Name;
  688. end;
  689. {****************************************************************************
  690. TWasmObjOutput
  691. ****************************************************************************}
  692. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  693. var
  694. b: byte;
  695. begin
  696. repeat
  697. b:=byte(v) and 127;
  698. v:=v shr 7;
  699. if v<>0 then
  700. b:=b or 128;
  701. d.write(b,1);
  702. until v=0;
  703. end;
  704. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  705. var
  706. b: byte;
  707. begin
  708. repeat
  709. b:=byte(v) and 127;
  710. v:=v shr 7;
  711. if v<>0 then
  712. b:=b or 128;
  713. w.write(b,1);
  714. until v=0;
  715. end;
  716. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  717. var
  718. b: byte;
  719. Done: Boolean=false;
  720. begin
  721. repeat
  722. b:=byte(v) and 127;
  723. v:=SarInt64(v,7);
  724. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  725. Done:=true
  726. else
  727. b:=b or 128;
  728. d.write(b,1);
  729. until Done;
  730. end;
  731. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  732. begin
  733. d.write(b,1);
  734. end;
  735. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  736. begin
  737. WriteUleb(d,Length(s));
  738. d.writestr(s);
  739. end;
  740. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  741. var
  742. b: byte;
  743. begin
  744. b:=ord(wsid);
  745. Writer.write(b,1);
  746. WriteUleb(Writer,FWasmSections[wsid].size);
  747. Writer.writearray(FWasmSections[wsid]);
  748. end;
  749. procedure TWasmObjOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  750. var
  751. b: byte;
  752. begin
  753. b:=0;
  754. Writer.write(b,1);
  755. WriteUleb(Writer,FWasmCustomSections[wcst].size);
  756. Writer.writearray(FWasmCustomSections[wcst]);
  757. end;
  758. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  759. var
  760. buf: array [0..4095] of byte;
  761. bs: Integer;
  762. begin
  763. while size>0 do
  764. begin
  765. if size<SizeOf(buf) then
  766. bs:=Integer(size)
  767. else
  768. bs:=SizeOf(buf);
  769. src.read(buf,bs);
  770. dest.write(buf,bs);
  771. dec(size,bs);
  772. end;
  773. end;
  774. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  775. var
  776. buf : array[0..1023] of byte;
  777. bs: Integer;
  778. begin
  779. fillchar(buf,sizeof(buf),0);
  780. while size>0 do
  781. begin
  782. if size<SizeOf(buf) then
  783. bs:=Integer(size)
  784. else
  785. bs:=SizeOf(buf);
  786. dest.write(buf,bs);
  787. dec(size,bs);
  788. end;
  789. end;
  790. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  791. var
  792. i: Integer;
  793. begin
  794. WriteUleb(dest,Length(wrt));
  795. for i:=low(wrt) to high(wrt) do
  796. WriteWasmBasicType(dest,wrt[i]);
  797. end;
  798. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  799. begin
  800. WriteByte(dest,encode_wasm_basic_type(wbt));
  801. end;
  802. function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
  803. var
  804. ExtraData: TWasmObjSymbolExtraData;
  805. begin
  806. if sym.bind=AB_EXTERNAL then
  807. begin
  808. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  809. result:=(ExtraData<>nil) and (ExtraData.TypeIdx<>-1);
  810. end
  811. else
  812. result:=false;
  813. end;
  814. function TWasmObjOutput.IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  815. var
  816. ExtraData: TWasmObjSymbolExtraData;
  817. begin
  818. if (sym.typ=AT_FUNCTION) and not sym.IsAlias then
  819. begin
  820. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  821. result:=(ExtraData<>nil) and (ExtraData.ExportName<>'');
  822. end
  823. else
  824. result:=false;
  825. end;
  826. procedure TWasmObjOutput.WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  827. var
  828. i,
  829. rle_entries,
  830. cnt: Integer;
  831. lasttype: TWasmBasicType;
  832. begin
  833. if Length(ed.Locals)=0 then
  834. begin
  835. WriteUleb(dest,0);
  836. exit;
  837. end;
  838. rle_entries:=1;
  839. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  840. if ed.Locals[i]<>ed.Locals[i-1] then
  841. inc(rle_entries);
  842. WriteUleb(dest,rle_entries);
  843. lasttype:=ed.Locals[Low(ed.Locals)];
  844. cnt:=1;
  845. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  846. if ed.Locals[i]=ed.Locals[i-1] then
  847. inc(cnt)
  848. else
  849. begin
  850. WriteUleb(dest,cnt);
  851. WriteWasmBasicType(dest,lasttype);
  852. lasttype:=ed.Locals[i];
  853. cnt:=1;
  854. end;
  855. WriteUleb(dest,cnt);
  856. WriteWasmBasicType(dest,lasttype);
  857. end;
  858. procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  859. var
  860. encoded_locals: tdynamicarray;
  861. ObjSymExtraData: TWasmObjSymbolExtraData;
  862. codelen: LongWord;
  863. ObjSection: TWasmObjSection;
  864. codeexprlen: QWord;
  865. begin
  866. ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  867. ObjSection:=TWasmObjSection(objsym.objsection);
  868. ObjSection.Data.seek(objsym.address);
  869. codeexprlen:=objsym.size;
  870. encoded_locals:=tdynamicarray.Create(64);
  871. WriteFunctionLocals(encoded_locals,ObjSymExtraData);
  872. codelen:=encoded_locals.size+codeexprlen+1;
  873. WriteUleb(dest,codelen);
  874. encoded_locals.seek(0);
  875. CopyDynamicArray(encoded_locals,dest,encoded_locals.size);
  876. ObjSection.FileSectionOfs:=dest.size-objsym.offset;
  877. CopyDynamicArray(ObjSection.Data,dest,codeexprlen);
  878. WriteByte(dest,$0B);
  879. encoded_locals.Free;
  880. end;
  881. procedure TWasmObjOutput.WriteSymbolTable;
  882. begin
  883. WriteUleb(FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTableEntriesCount);
  884. FWasmSymbolTable.seek(0);
  885. CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
  886. end;
  887. procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
  888. begin
  889. WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
  890. WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
  891. FWasmRelocationCodeTable.seek(0);
  892. CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
  893. end;
  894. procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
  895. begin
  896. WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
  897. WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
  898. FWasmRelocationDataTable.seek(0);
  899. CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
  900. end;
  901. procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  902. begin
  903. if FWasmLinkingSubsections[wlst].size>0 then
  904. begin
  905. WriteByte(FWasmCustomSections[wcstLinking],Ord(wlst));
  906. WriteUleb(FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  907. FWasmLinkingSubsections[wlst].seek(0);
  908. CopyDynamicArray(FWasmLinkingSubsections[wlst],FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  909. end;
  910. end;
  911. procedure TWasmObjOutput.DoRelocations;
  912. var
  913. si, ri: Integer;
  914. objsec: TWasmObjSection;
  915. objrel: TWasmObjRelocation;
  916. begin
  917. for si:=0 to FData.ObjSectionList.Count-1 do
  918. begin
  919. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  920. for ri:=0 to objsec.ObjRelocations.Count-1 do
  921. begin
  922. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  923. case objrel.typ of
  924. RELOC_FUNCTION_INDEX_LEB:
  925. begin
  926. if not assigned(objrel.symbol) then
  927. internalerror(2021092509);
  928. objsec.Data.seek(objrel.DataOffset);
  929. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).FuncIndex);
  930. end;
  931. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  932. begin
  933. if not assigned(objrel.symbol) then
  934. internalerror(2021092605);
  935. if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  936. begin
  937. objsec.Data.seek(objrel.DataOffset);
  938. AddSleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  939. end;
  940. end;
  941. RELOC_MEMORY_ADDR_LEB:
  942. begin
  943. if not assigned(objrel.symbol) then
  944. internalerror(2021092606);
  945. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  946. internalerror(2021092628);
  947. if objrel.symbol.bind<>AB_EXTERNAL then
  948. begin
  949. objsec.Data.seek(objrel.DataOffset);
  950. AddUleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  951. end;
  952. end;
  953. RELOC_ABSOLUTE:
  954. begin
  955. if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  956. begin
  957. objsec.Data.seek(objrel.DataOffset);
  958. AddInt32(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  959. end;
  960. end;
  961. RELOC_TYPE_INDEX_LEB:
  962. ;
  963. RELOC_GLOBAL_INDEX_LEB:
  964. begin
  965. if not assigned(objrel.symbol) then
  966. internalerror(2021092509);
  967. objsec.Data.seek(objrel.DataOffset);
  968. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).GlobalIndex);
  969. end;
  970. RELOC_TAG_INDEX_LEB:
  971. begin
  972. if not assigned(objrel.symbol) then
  973. internalerror(2021092716);
  974. objsec.Data.seek(objrel.DataOffset);
  975. WriteSleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).TagIndex);
  976. end;
  977. else
  978. internalerror(2021092510);
  979. end;
  980. end;
  981. end;
  982. end;
  983. procedure TWasmObjOutput.WriteRelocations;
  984. var
  985. si, ri: Integer;
  986. objsec: TWasmObjSection;
  987. objrel: TWasmObjRelocation;
  988. relout: tdynamicarray;
  989. relcount: PInteger;
  990. begin
  991. for si:=0 to FData.ObjSectionList.Count-1 do
  992. begin
  993. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  994. if objsec.IsCode then
  995. begin
  996. relout:=FWasmRelocationCodeTable;
  997. relcount:=@FWasmRelocationCodeTableEntriesCount;
  998. end
  999. else
  1000. begin
  1001. relout:=FWasmRelocationDataTable;
  1002. relcount:=@FWasmRelocationDataTableEntriesCount;
  1003. end;
  1004. for ri:=0 to objsec.ObjRelocations.Count-1 do
  1005. begin
  1006. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  1007. case objrel.typ of
  1008. RELOC_FUNCTION_INDEX_LEB:
  1009. begin
  1010. if not assigned(objrel.symbol) then
  1011. internalerror(2021092508);
  1012. Inc(relcount^);
  1013. WriteByte(relout,Ord(R_WASM_FUNCTION_INDEX_LEB));
  1014. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1015. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1016. end;
  1017. RELOC_MEMORY_ADDR_LEB:
  1018. begin
  1019. if not assigned(objrel.symbol) then
  1020. internalerror(2021092603);
  1021. Inc(relcount^);
  1022. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1023. internalerror(2021092628);
  1024. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_LEB));
  1025. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1026. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1027. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1028. end;
  1029. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1030. begin
  1031. if not assigned(objrel.symbol) then
  1032. internalerror(2021092604);
  1033. Inc(relcount^);
  1034. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1035. begin
  1036. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_SLEB));
  1037. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1038. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1039. end
  1040. else
  1041. begin
  1042. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB));
  1043. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1044. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1045. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1046. end;
  1047. end;
  1048. RELOC_ABSOLUTE:
  1049. begin
  1050. if not assigned(objrel.symbol) then
  1051. internalerror(2021092604);
  1052. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1053. begin
  1054. Inc(relcount^);
  1055. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_I32));
  1056. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1057. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1058. end
  1059. else
  1060. begin
  1061. Inc(relcount^);
  1062. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_I32));
  1063. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1064. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1065. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1066. end;
  1067. end;
  1068. RELOC_TYPE_INDEX_LEB:
  1069. begin
  1070. Inc(relcount^);
  1071. WriteByte(relout,Ord(R_WASM_TYPE_INDEX_LEB));
  1072. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1073. WriteUleb(relout,objrel.TypeIndex);
  1074. end;
  1075. RELOC_GLOBAL_INDEX_LEB:
  1076. begin
  1077. if not assigned(objrel.symbol) then
  1078. internalerror(2021092704);
  1079. Inc(relcount^);
  1080. WriteByte(relout,Ord(R_WASM_GLOBAL_INDEX_LEB));
  1081. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1082. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1083. end;
  1084. RELOC_TAG_INDEX_LEB:
  1085. begin
  1086. if not assigned(objrel.symbol) then
  1087. internalerror(2021092717);
  1088. Inc(relcount^);
  1089. WriteByte(relout,Ord(R_WASM_TAG_INDEX_LEB));
  1090. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1091. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1092. end;
  1093. else
  1094. internalerror(2021092507);
  1095. end;
  1096. end;
  1097. end;
  1098. end;
  1099. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  1100. var
  1101. i: Integer;
  1102. objsec: TWasmObjSection;
  1103. segment_count: Integer = 0;
  1104. cur_seg_ofs: qword = 0;
  1105. types_count,
  1106. imports_count, NextImportFunctionIndex, NextFunctionIndex,
  1107. section_nr, code_section_nr, data_section_nr,
  1108. NextGlobalIndex, NextTagIndex: Integer;
  1109. import_globals_count: Integer = 0;
  1110. globals_count: Integer = 0;
  1111. import_functions_count: Integer = 0;
  1112. export_functions_count: Integer = 0;
  1113. functions_count: Integer = 0;
  1114. import_exception_tags_count: Integer = 0;
  1115. exception_tags_count: Integer = 0;
  1116. objsym, ObjSymAlias: TWasmObjSymbol;
  1117. cust_sec: TWasmCustomSectionType;
  1118. begin
  1119. FData:=TWasmObjData(Data);
  1120. { each custom sections starts with its name }
  1121. for cust_sec in TWasmCustomSectionType do
  1122. WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
  1123. WriteUleb(FWasmCustomSections[wcstLinking],2); { linking metadata version }
  1124. for i:=0 to Data.ObjSymbolList.Count-1 do
  1125. begin
  1126. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1127. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1128. if objsym.bind=AB_EXTERNAL then
  1129. Inc(import_exception_tags_count)
  1130. else
  1131. Inc(exception_tags_count);
  1132. if objsym.typ=AT_WASM_GLOBAL then
  1133. if objsym.bind=AB_EXTERNAL then
  1134. Inc(import_globals_count)
  1135. else
  1136. Inc(globals_count);
  1137. if IsExternalFunction(objsym) then
  1138. Inc(import_functions_count);
  1139. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1140. Inc(functions_count);
  1141. if IsExportedFunction(objsym) then
  1142. Inc(export_functions_count);
  1143. end;
  1144. types_count:=Length(FData.FFuncTypes);
  1145. WriteUleb(FWasmSections[wsiType],types_count);
  1146. for i:=0 to types_count-1 do
  1147. with FData.FFuncTypes[i] do
  1148. begin
  1149. WriteByte(FWasmSections[wsiType],$60);
  1150. WriteWasmResultType(FWasmSections[wsiType],params);
  1151. WriteWasmResultType(FWasmSections[wsiType],results);
  1152. end;
  1153. for i:=0 to Data.ObjSectionList.Count-1 do
  1154. begin
  1155. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1156. if objsec.IsCode then
  1157. objsec.SegIdx:=-1
  1158. else
  1159. begin
  1160. objsec.SegIdx:=segment_count;
  1161. objsec.SegOfs:=cur_seg_ofs;
  1162. Inc(segment_count);
  1163. Inc(cur_seg_ofs,objsec.Size);
  1164. end;
  1165. end;
  1166. if segment_count>0 then
  1167. begin
  1168. WriteUleb(FWasmSections[wsiData],segment_count);
  1169. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  1170. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],segment_count);
  1171. for i:=0 to Data.ObjSectionList.Count-1 do
  1172. begin
  1173. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1174. if objsec.IsData then
  1175. begin
  1176. WriteName(FWasmLinkingSubsections[WASM_SEGMENT_INFO],objsec.Name);
  1177. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],BsrQWord(objsec.SecAlign));
  1178. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],0); { flags }
  1179. WriteByte(FWasmSections[wsiData],0);
  1180. WriteByte(FWasmSections[wsiData],$41);
  1181. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  1182. WriteByte(FWasmSections[wsiData],$0b);
  1183. WriteUleb(FWasmSections[wsiData],objsec.Size);
  1184. objsec.FileSectionOfs:=FWasmSections[wsiData].size;
  1185. if oso_Data in objsec.SecOptions then
  1186. begin
  1187. objsec.Data.seek(0);
  1188. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  1189. end
  1190. else
  1191. begin
  1192. WriteZeros(FWasmSections[wsiData],objsec.Size);
  1193. end;
  1194. end;
  1195. end;
  1196. end;
  1197. imports_count:=2+import_globals_count+import_functions_count+import_exception_tags_count;
  1198. WriteUleb(FWasmSections[wsiImport],imports_count);
  1199. { import memories }
  1200. WriteName(FWasmSections[wsiImport],'env');
  1201. WriteName(FWasmSections[wsiImport],'__linear_memory');
  1202. WriteByte(FWasmSections[wsiImport],$02); { mem }
  1203. WriteByte(FWasmSections[wsiImport],$00); { min }
  1204. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  1205. { import globals }
  1206. NextGlobalIndex:=0;
  1207. for i:=0 to Data.ObjSymbolList.Count-1 do
  1208. begin
  1209. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1210. if (objsym.bind=AB_EXTERNAL) and (objsym.typ=AT_WASM_GLOBAL) then
  1211. begin
  1212. objsym.GlobalIndex:=NextGlobalIndex;
  1213. Inc(NextGlobalIndex);
  1214. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1215. if objsym.ExtraData.ImportModule<>'' then
  1216. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1217. else
  1218. WriteName(FWasmSections[wsiImport],'env');
  1219. WriteName(FWasmSections[wsiImport],objsym.Name);
  1220. WriteByte(FWasmSections[wsiImport],$03); { global }
  1221. WriteWasmBasicType(FWasmSections[wsiImport],objsym.ExtraData.GlobalType);
  1222. if objsym.ExtraData.GlobalIsImmutable then
  1223. WriteByte(FWasmSections[wsiImport],$00) { const }
  1224. else
  1225. WriteByte(FWasmSections[wsiImport],$01); { var }
  1226. end;
  1227. end;
  1228. { import functions }
  1229. NextImportFunctionIndex:=0;
  1230. for i:=0 to Data.ObjSymbolList.Count-1 do
  1231. begin
  1232. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1233. if IsExternalFunction(objsym) then
  1234. begin
  1235. objsym.FuncIndex:=NextImportFunctionIndex;
  1236. Inc(NextImportFunctionIndex);
  1237. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1238. if objsym.ExtraData.ImportModule<>'' then
  1239. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1240. else
  1241. WriteName(FWasmSections[wsiImport],'env');
  1242. WriteName(FWasmSections[wsiImport],objsym.Name);
  1243. WriteByte(FWasmSections[wsiImport],$00); { func }
  1244. WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1245. end;
  1246. end;
  1247. { import tables }
  1248. WriteName(FWasmSections[wsiImport],'env');
  1249. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  1250. WriteByte(FWasmSections[wsiImport],$01); { table }
  1251. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  1252. WriteByte(FWasmSections[wsiImport],$00); { min }
  1253. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  1254. { import tags }
  1255. NextTagIndex:=0;
  1256. for i:=0 to Data.ObjSymbolList.Count-1 do
  1257. begin
  1258. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1259. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind=AB_EXTERNAL) then
  1260. begin
  1261. objsym.TagIndex:=NextTagIndex;
  1262. Inc(NextTagIndex);
  1263. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1264. if objsym.ExtraData.ImportModule<>'' then
  1265. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1266. else
  1267. WriteName(FWasmSections[wsiImport],'env');
  1268. WriteName(FWasmSections[wsiImport],objsym.Name);
  1269. WriteByte(FWasmSections[wsiImport],$04); { tag }
  1270. WriteByte(FWasmSections[wsiImport],$00); { exception }
  1271. WriteUleb(FWasmSections[wsiImport],objsym.ExtraData.ExceptionTagTypeIdx);
  1272. end;
  1273. end;
  1274. WriteUleb(FWasmSections[wsiFunction],functions_count);
  1275. NextFunctionIndex:=NextImportFunctionIndex;
  1276. for i:=0 to Data.ObjSymbolList.Count-1 do
  1277. begin
  1278. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1279. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1280. begin
  1281. objsym.FuncIndex:=NextFunctionIndex;
  1282. Inc(NextFunctionIndex);
  1283. WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1284. end;
  1285. end;
  1286. if exception_tags_count>0 then
  1287. begin
  1288. WriteUleb(FWasmSections[wsiTag],exception_tags_count);
  1289. for i:=0 to Data.ObjSymbolList.Count-1 do
  1290. begin
  1291. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1292. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind<>AB_EXTERNAL) then
  1293. begin
  1294. objsym.TagIndex:=NextTagIndex;
  1295. Inc(NextTagIndex);
  1296. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1297. WriteByte(FWasmSections[wsiTag],$00); { exception }
  1298. WriteUleb(FWasmSections[wsiTag],objsym.ExtraData.ExceptionTagTypeIdx);
  1299. end;
  1300. end;
  1301. end;
  1302. if globals_count>0 then
  1303. begin
  1304. WriteUleb(FWasmSections[wsiGlobal],globals_count);
  1305. for i:=0 to Data.ObjSymbolList.Count-1 do
  1306. begin
  1307. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1308. if (objsym.typ=AT_WASM_GLOBAL) and (objsym.bind<>AB_EXTERNAL) then
  1309. begin
  1310. objsym.GlobalIndex:=NextGlobalIndex;
  1311. Inc(NextGlobalIndex);
  1312. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1313. WriteWasmBasicType(FWasmSections[wsiGlobal],objsym.ExtraData.GlobalType);
  1314. if objsym.ExtraData.GlobalIsImmutable then
  1315. WriteByte(FWasmSections[wsiGlobal],$00) { const }
  1316. else
  1317. WriteByte(FWasmSections[wsiGlobal],$01); { var }
  1318. { init expr }
  1319. case objsym.ExtraData.GlobalType of
  1320. wbt_i32:
  1321. begin
  1322. WriteByte(FWasmSections[wsiGlobal],$41); { i32.const }
  1323. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1324. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1325. end;
  1326. wbt_i64:
  1327. begin
  1328. WriteByte(FWasmSections[wsiGlobal],$42); { i64.const }
  1329. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1330. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1331. end;
  1332. wbt_f32:
  1333. begin
  1334. WriteByte(FWasmSections[wsiGlobal],$43); { f32.const }
  1335. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE single precision floating point format) }
  1336. WriteByte(FWasmSections[wsiGlobal],$00);
  1337. WriteByte(FWasmSections[wsiGlobal],$00);
  1338. WriteByte(FWasmSections[wsiGlobal],$00);
  1339. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1340. end;
  1341. wbt_f64:
  1342. begin
  1343. WriteByte(FWasmSections[wsiGlobal],$44); { f64.const }
  1344. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE double precision floating point format) }
  1345. WriteByte(FWasmSections[wsiGlobal],$00);
  1346. WriteByte(FWasmSections[wsiGlobal],$00);
  1347. WriteByte(FWasmSections[wsiGlobal],$00);
  1348. WriteByte(FWasmSections[wsiGlobal],$00);
  1349. WriteByte(FWasmSections[wsiGlobal],$00);
  1350. WriteByte(FWasmSections[wsiGlobal],$00);
  1351. WriteByte(FWasmSections[wsiGlobal],$00);
  1352. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1353. end;
  1354. end;
  1355. end;
  1356. end;
  1357. end;
  1358. if export_functions_count>0 then
  1359. begin
  1360. WriteUleb(FWasmSections[wsiExport],export_functions_count);
  1361. for i:=0 to Data.ObjSymbolList.Count-1 do
  1362. begin
  1363. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1364. if IsExportedFunction(objsym) then
  1365. begin
  1366. WriteName(FWasmSections[wsiExport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).ExportName);
  1367. WriteByte(FWasmSections[wsiExport],0); { func }
  1368. WriteUleb(FWasmSections[wsiExport],objsym.FuncIndex);
  1369. end;
  1370. end;
  1371. end;
  1372. for i:=0 to Data.ObjSymbolList.Count-1 do
  1373. begin
  1374. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1375. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1376. begin
  1377. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1378. Inc(FWasmSymbolTableEntriesCount);
  1379. WriteByte(FWasmSymbolTable,Ord(SYMTAB_EVENT));
  1380. if objsym.bind=AB_GLOBAL then
  1381. WriteUleb(FWasmSymbolTable,0)
  1382. else if objsym.bind=AB_LOCAL then
  1383. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1384. else if objsym.bind=AB_EXTERNAL then
  1385. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1386. else
  1387. internalerror(2021092715);
  1388. WriteUleb(FWasmSymbolTable,objsym.TagIndex);
  1389. if objsym.bind<>AB_EXTERNAL then
  1390. WriteName(FWasmSymbolTable,objsym.Name);
  1391. end
  1392. else if objsym.typ=AT_WASM_GLOBAL then
  1393. begin
  1394. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1395. Inc(FWasmSymbolTableEntriesCount);
  1396. WriteByte(FWasmSymbolTable,Ord(SYMTAB_GLOBAL));
  1397. if objsym.bind=AB_EXTERNAL then
  1398. begin
  1399. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1400. WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
  1401. end
  1402. else
  1403. {not implemented yet}
  1404. internalerror(2021092705);
  1405. end
  1406. else if IsExternalFunction(objsym) then
  1407. begin
  1408. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1409. Inc(FWasmSymbolTableEntriesCount);
  1410. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1411. if objsym.ExtraData.ImportModule<>'' then
  1412. begin
  1413. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
  1414. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1415. WriteName(FWasmSymbolTable,objsym.Name);
  1416. end
  1417. else
  1418. begin
  1419. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1420. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1421. end;
  1422. end
  1423. else if objsym.typ=AT_FUNCTION then
  1424. begin
  1425. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1426. Inc(FWasmSymbolTableEntriesCount);
  1427. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1428. if objsym.IsAlias then
  1429. begin
  1430. ObjSymAlias:=TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf));
  1431. ObjSym.FuncIndex:=ObjSymAlias.FuncIndex;
  1432. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
  1433. WriteUleb(FWasmSymbolTable,ObjSymAlias.FuncIndex);
  1434. end
  1435. else
  1436. begin
  1437. if IsExportedFunction(objsym) then
  1438. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPORTED)
  1439. else
  1440. WriteUleb(FWasmSymbolTable,0);
  1441. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1442. end;
  1443. WriteName(FWasmSymbolTable,objsym.Name);
  1444. end
  1445. else if (objsym.typ in [AT_DATA,AT_TLS]) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
  1446. begin
  1447. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1448. Inc(FWasmSymbolTableEntriesCount);
  1449. WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
  1450. if objsym.bind=AB_GLOBAL then
  1451. WriteUleb(FWasmSymbolTable,0)
  1452. else if objsym.bind=AB_LOCAL then
  1453. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1454. else if objsym.bind=AB_EXTERNAL then
  1455. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1456. else
  1457. internalerror(2021092506);
  1458. WriteName(FWasmSymbolTable,objsym.Name);
  1459. if objsym.bind<>AB_EXTERNAL then
  1460. begin
  1461. WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
  1462. WriteUleb(FWasmSymbolTable,objsym.offset);
  1463. WriteUleb(FWasmSymbolTable,objsym.size);
  1464. end;
  1465. end;
  1466. end;
  1467. DoRelocations;
  1468. WriteUleb(FWasmSections[wsiCode],functions_count);
  1469. for i:=0 to Data.ObjSymbolList.Count-1 do
  1470. begin
  1471. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1472. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1473. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  1474. end;
  1475. WriteRelocations;
  1476. WriteSymbolTable;
  1477. WriteLinkingSubsection(WASM_SYMBOL_TABLE);
  1478. if segment_count>0 then
  1479. WriteLinkingSubsection(WASM_SEGMENT_INFO);
  1480. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  1481. Writer.write(WasmVersion,SizeOf(WasmVersion));
  1482. code_section_nr:=-1;
  1483. data_section_nr:=-1;
  1484. section_nr:=0;
  1485. WriteWasmSection(wsiType);
  1486. Inc(section_nr);
  1487. WriteWasmSection(wsiImport);
  1488. Inc(section_nr);
  1489. WriteWasmSection(wsiFunction);
  1490. Inc(section_nr);
  1491. if exception_tags_count>0 then
  1492. begin
  1493. WriteWasmSection(wsiTag);
  1494. Inc(section_nr);
  1495. end;
  1496. if globals_count>0 then
  1497. begin
  1498. WriteWasmSection(wsiGlobal);
  1499. Inc(section_nr);
  1500. end;
  1501. if export_functions_count>0 then
  1502. begin
  1503. WriteWasmSection(wsiExport);
  1504. Inc(section_nr);
  1505. end;
  1506. if segment_count>0 then
  1507. begin
  1508. WriteWasmSection(wsiDataCount);
  1509. Inc(section_nr);
  1510. end;
  1511. WriteWasmSection(wsiCode);
  1512. code_section_nr:=section_nr;
  1513. Inc(section_nr);
  1514. if segment_count>0 then
  1515. begin
  1516. WriteWasmSection(wsiData);
  1517. data_section_nr:=section_nr;
  1518. Inc(section_nr);
  1519. end;
  1520. WriteRelocationCodeTable(code_section_nr);
  1521. if segment_count>0 then
  1522. WriteRelocationDataTable(data_section_nr);
  1523. WriteWasmCustomSection(wcstLinking);
  1524. Inc(section_nr);
  1525. WriteWasmCustomSection(wcstRelocCode);
  1526. Inc(section_nr);
  1527. if segment_count>0 then
  1528. begin
  1529. WriteWasmCustomSection(wcstRelocData);
  1530. Inc(section_nr);
  1531. end;
  1532. result:=true;
  1533. end;
  1534. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  1535. var
  1536. i: TWasmSectionID;
  1537. j: TWasmCustomSectionType;
  1538. k: TWasmLinkingSubsectionType;
  1539. begin
  1540. inherited;
  1541. cobjdata:=TWasmObjData;
  1542. for i in TWasmSectionID do
  1543. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  1544. for j in TWasmCustomSectionType do
  1545. FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
  1546. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1547. FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
  1548. FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
  1549. FWasmSymbolTableEntriesCount:=0;
  1550. FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
  1551. FWasmRelocationCodeTableEntriesCount:=0;
  1552. FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
  1553. FWasmRelocationDataTableEntriesCount:=0;
  1554. end;
  1555. destructor TWasmObjOutput.destroy;
  1556. var
  1557. i: TWasmSectionID;
  1558. j: TWasmCustomSectionType;
  1559. k: TWasmLinkingSubsectionType;
  1560. begin
  1561. for i in TWasmSectionID do
  1562. FWasmSections[i].Free;
  1563. for j in TWasmCustomSectionType do
  1564. FWasmCustomSections[j].Free;
  1565. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1566. FWasmLinkingSubsections[k].Free;
  1567. FWasmSymbolTable.Free;
  1568. FWasmRelocationCodeTable.Free;
  1569. FWasmRelocationDataTable.Free;
  1570. inherited destroy;
  1571. end;
  1572. {****************************************************************************
  1573. TWasmAssembler
  1574. ****************************************************************************}
  1575. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  1576. begin
  1577. inherited;
  1578. CObjOutput:=TWasmObjOutput;
  1579. end;
  1580. {*****************************************************************************
  1581. Initialize
  1582. *****************************************************************************}
  1583. {$ifdef wasm32}
  1584. const
  1585. as_wasm32_wasm_info : tasminfo =
  1586. (
  1587. id : as_wasm32_wasm;
  1588. idtxt : 'OMF';
  1589. asmbin : '';
  1590. asmcmd : '';
  1591. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  1592. flags : [af_outputbinary,af_smartlink_sections];
  1593. labelprefix : '..@';
  1594. labelmaxlen : -1;
  1595. comment : '; ';
  1596. dollarsign: '$';
  1597. );
  1598. {$endif wasm32}
  1599. initialization
  1600. {$ifdef wasm32}
  1601. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  1602. {$endif wasm32}
  1603. end.