ogwasm.pas 64 KB

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