ogwasm.pas 69 KB

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