ogwasm.pas 66 KB

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