ogwasm.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. {
  2. Copyright (c) 2021 by Nikolay Nikolov
  3. Contains the WebAssembly binary module format reader and writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogwasm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,cpubase,
  25. { assembler }
  26. aasmbase,assemble,aasmcpu,
  27. { WebAssembly module format definitions }
  28. wasmbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. TWasmObjSymbolExtraData = class;
  34. { TWasmObjSymbol }
  35. TWasmObjSymbol = class(TObjSymbol)
  36. FuncIndex: Integer;
  37. SymbolIndex: Integer;
  38. GlobalIndex: Integer;
  39. TagIndex: Integer;
  40. AliasOf: string;
  41. ExtraData: TWasmObjSymbolExtraData;
  42. constructor create(AList:TFPHashObjectList;const AName:string);override;
  43. function IsAlias: Boolean;
  44. end;
  45. { TWasmObjRelocation }
  46. TWasmObjRelocation = class(TObjRelocation)
  47. public
  48. TypeIndex: Integer;
  49. Addend: LongInt;
  50. constructor CreateTypeIndex(ADataOffset:TObjSectionOfs; ATypeIndex: Integer);
  51. end;
  52. { TWasmObjSymbolExtraData }
  53. TWasmObjSymbolExtraData = class(TFPHashObject)
  54. TypeIdx: Integer;
  55. ExceptionTagTypeIdx: Integer;
  56. ImportModule: string;
  57. ImportName: string;
  58. ExportName: string;
  59. GlobalType: TWasmBasicType;
  60. GlobalIsImmutable: Boolean;
  61. Locals: array of TWasmBasicType;
  62. constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  63. procedure AddLocal(bastyp: TWasmBasicType);
  64. end;
  65. { TWasmObjSection }
  66. TWasmObjSection = class(TObjSection)
  67. public
  68. SegIdx: Integer;
  69. SegOfs: qword;
  70. FileSectionOfs: qword;
  71. function IsCode: Boolean;
  72. function IsData: Boolean;
  73. end;
  74. { TWasmObjData }
  75. TWasmObjData = class(TObjData)
  76. private
  77. FFuncTypes: array of TWasmFuncType;
  78. FObjSymbolsExtraDataList: TFPHashObjectList;
  79. FLastFuncName: string;
  80. function is_smart_section(atype:TAsmSectiontype):boolean;
  81. function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  82. public
  83. constructor create(const n:string);override;
  84. destructor destroy; override;
  85. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  86. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  87. function AddOrCreateObjSymbolExtraData(const symname:TSymStr): TWasmObjSymbolExtraData;
  88. function AddFuncType(wft: TWasmFuncType): integer;
  89. function globalref(asmsym:TAsmSymbol):TObjSymbol;
  90. function ExceptionTagRef(asmsym:TAsmSymbol):TObjSymbol;
  91. procedure DeclareGlobalType(gt: tai_globaltype);
  92. procedure DeclareFuncType(ft: tai_functype);
  93. procedure DeclareTagType(tt: tai_tagtype);
  94. procedure DeclareExportName(en: tai_export_name);
  95. procedure DeclareImportModule(aim: tai_import_module);
  96. procedure DeclareImportName(ain: tai_import_name);
  97. procedure DeclareLocal(al: tai_local);
  98. procedure symbolpairdefine(akind: TSymbolPairKind;const asym, avalue: string);override;
  99. end;
  100. { TWasmObjOutput }
  101. TWasmObjOutput = class(tObjOutput)
  102. private
  103. FData: TWasmObjData;
  104. FWasmRelocationCodeTable: tdynamicarray;
  105. FWasmRelocationCodeTableEntriesCount: Integer;
  106. FWasmRelocationDataTable: tdynamicarray;
  107. FWasmRelocationDataTableEntriesCount: Integer;
  108. FWasmSymbolTable: tdynamicarray;
  109. FWasmSymbolTableEntriesCount: Integer;
  110. FWasmSections: array [TWasmSectionID] of tdynamicarray;
  111. FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
  112. FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of tdynamicarray;
  113. procedure WriteUleb(d: tdynamicarray; v: uint64);
  114. procedure WriteUleb(w: TObjectWriter; v: uint64);
  115. procedure WriteSleb(d: tdynamicarray; v: int64);
  116. procedure WriteByte(d: tdynamicarray; b: byte);
  117. procedure WriteName(d: tdynamicarray; const s: string);
  118. procedure WriteWasmSection(wsid: TWasmSectionID);
  119. procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  120. procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  121. procedure WriteZeros(dest: tdynamicarray; size: QWord);
  122. procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  123. procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  124. function IsExternalFunction(sym: TObjSymbol): Boolean;
  125. function IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  126. procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  127. procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  128. procedure WriteSymbolTable;
  129. procedure WriteRelocationCodeTable(CodeSectionIndex: Integer);
  130. procedure WriteRelocationDataTable(DataSectionIndex: Integer);
  131. procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  132. procedure DoRelocations;
  133. procedure WriteRelocations;
  134. protected
  135. function writeData(Data:TObjData):boolean;override;
  136. public
  137. constructor create(AWriter:TObjectWriter);override;
  138. destructor destroy;override;
  139. end;
  140. { TWasmAssembler }
  141. TWasmAssembler = class(tinternalassembler)
  142. constructor create(info: pasminfo; smart:boolean);override;
  143. end;
  144. implementation
  145. uses
  146. verbose,version,globals;
  147. procedure WriteUleb5(d: tdynamicarray; v: uint64);
  148. var
  149. b: byte;
  150. i: Integer;
  151. begin
  152. for i:=1 to 5 do
  153. begin
  154. b:=byte(v) and 127;
  155. v:=v shr 7;
  156. if i<>5 then
  157. b:=b or 128;
  158. d.write(b,1);
  159. end;
  160. end;
  161. procedure WriteUleb5(d: tobjsection; v: uint64);
  162. var
  163. b: byte;
  164. i: Integer;
  165. begin
  166. for i:=1 to 5 do
  167. begin
  168. b:=byte(v) and 127;
  169. v:=v shr 7;
  170. if i<>5 then
  171. b:=b or 128;
  172. d.write(b,1);
  173. end;
  174. end;
  175. procedure WriteSleb5(d: tdynamicarray; v: int64);
  176. var
  177. b: byte;
  178. i: Integer;
  179. begin
  180. for i:=1 to 5 do
  181. begin
  182. b:=byte(v) and 127;
  183. v:=SarInt64(v,7);
  184. if i<>5 then
  185. b:=b or 128;
  186. d.write(b,1);
  187. end;
  188. end;
  189. procedure WriteSleb5(d: tobjsection; v: int64);
  190. var
  191. b: byte;
  192. i: Integer;
  193. begin
  194. for i:=1 to 5 do
  195. begin
  196. b:=byte(v) and 127;
  197. v:=SarInt64(v,7);
  198. if i<>5 then
  199. b:=b or 128;
  200. d.write(b,1);
  201. end;
  202. end;
  203. function ReadUleb(d: tdynamicarray): uint64;
  204. var
  205. b: byte;
  206. shift:integer;
  207. begin
  208. b:=0;
  209. result:=0;
  210. shift:=0;
  211. repeat
  212. d.read(b,1);
  213. result:=result or (uint64(b and 127) shl shift);
  214. inc(shift,7);
  215. until (b and 128)=0;
  216. end;
  217. function ReadSleb(d: tdynamicarray): int64;
  218. var
  219. b: byte;
  220. shift:integer;
  221. begin
  222. b:=0;
  223. result:=0;
  224. shift:=0;
  225. repeat
  226. d.read(b,1);
  227. result:=result or (uint64(b and 127) shl shift);
  228. inc(shift,7);
  229. until (b and 128)=0;
  230. {$ifopt Q+}
  231. {$define overflowon}
  232. {$Q-}
  233. {$endif}
  234. {$ifopt R+}
  235. {$define rangeon}
  236. {$R-}
  237. {$endif}
  238. if (b and 64)<>0 then
  239. result:=result or (high(uint64) shl shift);
  240. end;
  241. {$ifdef overflowon}
  242. {$Q+}
  243. {$undef overflowon}
  244. {$endif}
  245. {$ifdef rangeon}
  246. {$R+}
  247. {$undef rangeon}
  248. {$endif}
  249. procedure AddSleb5(d: tdynamicarray; v: int64);
  250. var
  251. q: Int64;
  252. p: LongWord;
  253. begin
  254. p:=d.Pos;
  255. q:=ReadSleb(d);
  256. q:=q+v;
  257. d.seek(p);
  258. WriteSleb5(d,q);
  259. end;
  260. procedure AddUleb5(d: tdynamicarray; v: int64);
  261. var
  262. q: UInt64;
  263. p: LongWord;
  264. begin
  265. p:=d.Pos;
  266. q:=ReadUleb(d);
  267. q:=q+v;
  268. d.seek(p);
  269. WriteUleb5(d,q);
  270. end;
  271. procedure AddInt32(d: tdynamicarray; v: int32);
  272. var
  273. q: int32;
  274. p: LongWord;
  275. begin
  276. p:=d.Pos;
  277. d.read(q,4);
  278. {$ifdef FPC_BIG_ENDIAN}
  279. q:=SwapEndian(q);
  280. {$endif FPC_BIG_ENDIAN}
  281. q:=q+v;
  282. {$ifdef FPC_BIG_ENDIAN}
  283. q:=SwapEndian(q);
  284. {$endif FPC_BIG_ENDIAN}
  285. d.seek(p);
  286. d.write(q,4);
  287. end;
  288. {****************************************************************************
  289. TWasmObjRelocation
  290. ****************************************************************************}
  291. constructor TWasmObjRelocation.CreateTypeIndex(ADataOffset: TObjSectionOfs; ATypeIndex: Integer);
  292. begin
  293. DataOffset:=ADataOffset;
  294. Symbol:=nil;
  295. OrgSize:=0;
  296. Group:=nil;
  297. ObjSection:=nil;
  298. ftype:=ord(RELOC_TYPE_INDEX_LEB);
  299. TypeIndex:=ATypeIndex;
  300. end;
  301. {****************************************************************************
  302. TWasmObjSymbol
  303. ****************************************************************************}
  304. constructor TWasmObjSymbol.create(AList: TFPHashObjectList; const AName: string);
  305. begin
  306. inherited create(AList,AName);
  307. FuncIndex:=-1;
  308. SymbolIndex:=-1;
  309. GlobalIndex:=-1;
  310. TagIndex:=-1;
  311. AliasOf:='';
  312. ExtraData:=nil;
  313. end;
  314. function TWasmObjSymbol.IsAlias: Boolean;
  315. begin
  316. result:=AliasOf<>'';
  317. end;
  318. {****************************************************************************
  319. TWasmObjSymbolExtraData
  320. ****************************************************************************}
  321. constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  322. begin
  323. inherited Create(HashObjectList,s);
  324. TypeIdx:=-1;
  325. ExceptionTagTypeIdx:=-1;
  326. end;
  327. procedure TWasmObjSymbolExtraData.AddLocal(bastyp: TWasmBasicType);
  328. begin
  329. SetLength(Locals,Length(Locals)+1);
  330. Locals[High(Locals)]:=bastyp;
  331. end;
  332. {****************************************************************************
  333. TWasmObjSection
  334. ****************************************************************************}
  335. function TWasmObjSection.IsCode: Boolean;
  336. const
  337. CodePrefix = '.text';
  338. begin
  339. result:=(Length(Name)>=Length(CodePrefix)) and
  340. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  341. end;
  342. function TWasmObjSection.IsData: Boolean;
  343. begin
  344. result:=not IsCode;
  345. end;
  346. {****************************************************************************
  347. TWasmObjData
  348. ****************************************************************************}
  349. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  350. begin
  351. { For bss we need to set some flags that are target dependent,
  352. it is easier to disable it for smartlinking. It doesn't take up
  353. filespace }
  354. result:=not(target_info.system in systems_darwin) and
  355. create_smartlink_sections and
  356. (atype<>sec_toc) and
  357. (atype<>sec_user) and
  358. { on embedded systems every byte counts, so smartlink bss too }
  359. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  360. end;
  361. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  362. const aname: string; aorder: TAsmSectionOrder): string;
  363. const
  364. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  365. '.text',
  366. '.data',
  367. { why doesn't .rodata work? (FK) }
  368. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  369. { vtables (and anything else containing relocations), otherwise those are }
  370. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  371. { vtable for a class called Window: }
  372. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  373. { TODO: .data.ro not yet working}
  374. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  375. '.rodata',
  376. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  377. '.data',
  378. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  379. '.rodata',
  380. '.bss',
  381. '.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. else
  1389. internalerror(2022052801);
  1390. end;
  1391. end;
  1392. end;
  1393. end;
  1394. if export_functions_count>0 then
  1395. begin
  1396. WriteUleb(FWasmSections[wsiExport],export_functions_count);
  1397. for i:=0 to Data.ObjSymbolList.Count-1 do
  1398. begin
  1399. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1400. if IsExportedFunction(objsym) then
  1401. begin
  1402. WriteName(FWasmSections[wsiExport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).ExportName);
  1403. WriteByte(FWasmSections[wsiExport],0); { func }
  1404. if (objsym.FuncIndex<0) then
  1405. message1(asmw_e_illegal_unset_index,objsym.name)
  1406. else
  1407. WriteUleb(FWasmSections[wsiExport],objsym.FuncIndex);
  1408. end;
  1409. end;
  1410. end;
  1411. for i:=0 to Data.ObjSymbolList.Count-1 do
  1412. begin
  1413. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1414. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1415. begin
  1416. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1417. Inc(FWasmSymbolTableEntriesCount);
  1418. WriteByte(FWasmSymbolTable,Ord(SYMTAB_EVENT));
  1419. if objsym.bind=AB_GLOBAL then
  1420. WriteUleb(FWasmSymbolTable,0)
  1421. else if objsym.bind=AB_LOCAL then
  1422. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1423. else if objsym.bind=AB_EXTERNAL then
  1424. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1425. else if objsym.bind=AB_WEAK then
  1426. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_WEAK)
  1427. else
  1428. internalerror(2021092715);
  1429. if (objsym.TagIndex<0) then
  1430. message1(asmw_e_illegal_unset_index,objsym.name)
  1431. else
  1432. WriteUleb(FWasmSymbolTable,objsym.TagIndex);
  1433. if objsym.bind<>AB_EXTERNAL then
  1434. WriteName(FWasmSymbolTable,objsym.Name);
  1435. end
  1436. else if objsym.typ=AT_WASM_GLOBAL then
  1437. begin
  1438. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1439. Inc(FWasmSymbolTableEntriesCount);
  1440. WriteByte(FWasmSymbolTable,Ord(SYMTAB_GLOBAL));
  1441. if objsym.bind=AB_EXTERNAL then
  1442. begin
  1443. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1444. if (objsym.GlobalIndex<0) then
  1445. message1(asmw_e_illegal_unset_index,objsym.name)
  1446. else
  1447. WriteUleb(FWasmSymbolTable,objsym.GlobalIndex);
  1448. end
  1449. else
  1450. {not implemented yet}
  1451. internalerror(2021092705);
  1452. end
  1453. else if IsExternalFunction(objsym) then
  1454. begin
  1455. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1456. Inc(FWasmSymbolTableEntriesCount);
  1457. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1458. if objsym.ExtraData.ImportModule<>'' then
  1459. begin
  1460. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
  1461. if (objsym.FuncIndex<0) then
  1462. message1(asmw_e_illegal_unset_index,objsym.name)
  1463. else
  1464. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1465. WriteName(FWasmSymbolTable,objsym.Name);
  1466. end
  1467. else
  1468. begin
  1469. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1470. if (objsym.FuncIndex<0) then
  1471. message1(asmw_e_illegal_unset_index,objsym.name)
  1472. else
  1473. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1474. end;
  1475. end
  1476. else if objsym.typ=AT_FUNCTION then
  1477. begin
  1478. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1479. Inc(FWasmSymbolTableEntriesCount);
  1480. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1481. if objsym.IsAlias then
  1482. begin
  1483. ObjSymAlias:=TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf));
  1484. ObjSym.FuncIndex:=ObjSymAlias.FuncIndex;
  1485. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
  1486. WriteUleb(FWasmSymbolTable,ObjSymAlias.FuncIndex);
  1487. end
  1488. else
  1489. begin
  1490. if IsExportedFunction(objsym) then
  1491. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPORTED)
  1492. else
  1493. WriteUleb(FWasmSymbolTable,0);
  1494. if (objsym.FuncIndex<0) then
  1495. message1(asmw_e_illegal_unset_index,objsym.name)
  1496. else
  1497. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1498. end;
  1499. WriteName(FWasmSymbolTable,objsym.Name);
  1500. end
  1501. else if (objsym.typ in [AT_DATA,AT_TLS]) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
  1502. begin
  1503. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1504. Inc(FWasmSymbolTableEntriesCount);
  1505. WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
  1506. if objsym.bind=AB_GLOBAL then
  1507. WriteUleb(FWasmSymbolTable,0)
  1508. else if objsym.bind=AB_LOCAL then
  1509. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL)
  1510. else if objsym.bind=AB_EXTERNAL then
  1511. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED)
  1512. else
  1513. internalerror(2021092506);
  1514. WriteName(FWasmSymbolTable,objsym.Name);
  1515. if objsym.bind<>AB_EXTERNAL then
  1516. begin
  1517. WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
  1518. WriteUleb(FWasmSymbolTable,objsym.offset);
  1519. WriteUleb(FWasmSymbolTable,objsym.size);
  1520. end;
  1521. end;
  1522. end;
  1523. DoRelocations;
  1524. WriteUleb(FWasmSections[wsiCode],functions_count);
  1525. for i:=0 to Data.ObjSymbolList.Count-1 do
  1526. begin
  1527. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1528. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1529. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  1530. end;
  1531. WriteRelocations;
  1532. WriteSymbolTable;
  1533. WriteLinkingSubsection(WASM_SYMBOL_TABLE);
  1534. if segment_count>0 then
  1535. WriteLinkingSubsection(WASM_SEGMENT_INFO);
  1536. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  1537. Writer.write(WasmVersion,SizeOf(WasmVersion));
  1538. if ts_wasm_threads in current_settings.targetswitches then
  1539. begin
  1540. WriteUleb(FWasmCustomSections[wcstTargetFeatures],4);
  1541. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1542. WriteName(FWasmCustomSections[wcstTargetFeatures],'atomics');
  1543. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1544. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1545. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1546. WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
  1547. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1548. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1549. end;
  1550. { Write the producers section:
  1551. https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md }
  1552. WriteUleb(FWasmCustomSections[wcstProducers],2);
  1553. WriteName(FWasmCustomSections[wcstProducers],'language');
  1554. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1555. WriteName(FWasmCustomSections[wcstProducers],'Pascal');
  1556. WriteName(FWasmCustomSections[wcstProducers],'');
  1557. WriteName(FWasmCustomSections[wcstProducers],'processed-by');
  1558. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1559. WriteName(FWasmCustomSections[wcstProducers],'Free Pascal Compiler (FPC)');
  1560. WriteName(FWasmCustomSections[wcstProducers],full_version_string+' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname);
  1561. code_section_nr:=-1;
  1562. data_section_nr:=-1;
  1563. section_nr:=0;
  1564. WriteWasmSection(wsiType);
  1565. Inc(section_nr);
  1566. WriteWasmSection(wsiImport);
  1567. Inc(section_nr);
  1568. WriteWasmSection(wsiFunction);
  1569. Inc(section_nr);
  1570. if exception_tags_count>0 then
  1571. begin
  1572. WriteWasmSection(wsiTag);
  1573. Inc(section_nr);
  1574. end;
  1575. if globals_count>0 then
  1576. begin
  1577. WriteWasmSection(wsiGlobal);
  1578. Inc(section_nr);
  1579. end;
  1580. if export_functions_count>0 then
  1581. begin
  1582. WriteWasmSection(wsiExport);
  1583. Inc(section_nr);
  1584. end;
  1585. if segment_count>0 then
  1586. begin
  1587. WriteWasmSection(wsiDataCount);
  1588. Inc(section_nr);
  1589. end;
  1590. WriteWasmSection(wsiCode);
  1591. code_section_nr:=section_nr;
  1592. Inc(section_nr);
  1593. if segment_count>0 then
  1594. begin
  1595. WriteWasmSection(wsiData);
  1596. data_section_nr:=section_nr;
  1597. Inc(section_nr);
  1598. end;
  1599. WriteRelocationCodeTable(code_section_nr);
  1600. if segment_count>0 then
  1601. WriteRelocationDataTable(data_section_nr);
  1602. WriteWasmCustomSection(wcstLinking);
  1603. Inc(section_nr);
  1604. WriteWasmCustomSection(wcstRelocCode);
  1605. Inc(section_nr);
  1606. if segment_count>0 then
  1607. begin
  1608. WriteWasmCustomSection(wcstRelocData);
  1609. Inc(section_nr);
  1610. end;
  1611. WriteWasmCustomSection(wcstProducers);
  1612. Inc(section_nr);
  1613. if ts_wasm_threads in current_settings.targetswitches then
  1614. begin
  1615. WriteWasmCustomSection(wcstTargetFeatures);
  1616. Inc(section_nr);
  1617. end;
  1618. result:=true;
  1619. end;
  1620. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  1621. var
  1622. i: TWasmSectionID;
  1623. j: TWasmCustomSectionType;
  1624. k: TWasmLinkingSubsectionType;
  1625. begin
  1626. inherited;
  1627. cobjdata:=TWasmObjData;
  1628. for i in TWasmSectionID do
  1629. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  1630. for j in TWasmCustomSectionType do
  1631. FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
  1632. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1633. FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
  1634. FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
  1635. FWasmSymbolTableEntriesCount:=0;
  1636. FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
  1637. FWasmRelocationCodeTableEntriesCount:=0;
  1638. FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
  1639. FWasmRelocationDataTableEntriesCount:=0;
  1640. end;
  1641. destructor TWasmObjOutput.destroy;
  1642. var
  1643. i: TWasmSectionID;
  1644. j: TWasmCustomSectionType;
  1645. k: TWasmLinkingSubsectionType;
  1646. begin
  1647. for i in TWasmSectionID do
  1648. FWasmSections[i].Free;
  1649. for j in TWasmCustomSectionType do
  1650. FWasmCustomSections[j].Free;
  1651. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1652. FWasmLinkingSubsections[k].Free;
  1653. FWasmSymbolTable.Free;
  1654. FWasmRelocationCodeTable.Free;
  1655. FWasmRelocationDataTable.Free;
  1656. inherited destroy;
  1657. end;
  1658. {****************************************************************************
  1659. TWasmAssembler
  1660. ****************************************************************************}
  1661. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  1662. begin
  1663. inherited;
  1664. CObjOutput:=TWasmObjOutput;
  1665. end;
  1666. {*****************************************************************************
  1667. Initialize
  1668. *****************************************************************************}
  1669. {$ifdef wasm32}
  1670. const
  1671. as_wasm32_wasm_info : tasminfo =
  1672. (
  1673. id : as_wasm32_wasm;
  1674. idtxt : 'WASM';
  1675. asmbin : '';
  1676. asmcmd : '';
  1677. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  1678. flags : [af_outputbinary,af_smartlink_sections];
  1679. labelprefix : '..@';
  1680. labelmaxlen : -1;
  1681. comment : '; ';
  1682. dollarsign: '$';
  1683. );
  1684. {$endif wasm32}
  1685. initialization
  1686. {$ifdef wasm32}
  1687. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  1688. {$endif wasm32}
  1689. end.