ogwasm.pas 83 KB

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