ogwasm.pas 69 KB

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