ogwasm.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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. objreloc:=nil;
  545. case Reloctype of
  546. RELOC_FUNCTION_INDEX_LEB:
  547. begin
  548. if Data<>0 then
  549. internalerror(2021092502);
  550. if len<>5 then
  551. internalerror(2021092503);
  552. if not assigned(p) then
  553. internalerror(2021092504);
  554. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  555. CurrObjSec.ObjRelocations.Add(objreloc);
  556. writebytes(leb_zero,5);
  557. end;
  558. RELOC_MEMORY_ADDR_LEB,
  559. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  560. begin
  561. if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then
  562. internalerror(2021092602);
  563. if len<>5 then
  564. internalerror(2021092503);
  565. if not assigned(p) then
  566. internalerror(2021092504);
  567. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  568. objreloc.Addend:=Data;
  569. CurrObjSec.ObjRelocations.Add(objreloc);
  570. if RelocType=RELOC_MEMORY_ADDR_LEB then
  571. WriteUleb5(CurrObjSec,Data)
  572. else
  573. WriteSleb5(CurrObjSec,Data);
  574. end;
  575. RELOC_ABSOLUTE:
  576. begin
  577. if len<>4 then
  578. internalerror(2021092607);
  579. if not assigned(p) then
  580. internalerror(2021092608);
  581. if (p.objsection<>nil) and TWasmObjSection(p.objsection).IsDebug and
  582. (p.bind<>AB_COMMON) then
  583. begin
  584. CurrObjSec.addsectionreloc(CurrObjSec.mempos+CurrObjSec.Size,CurrObjSec,RELOC_ABSOLUTE);
  585. inc(data,p.address);
  586. Data:=NtoLE(Data);
  587. writebytes(Data,4);
  588. end
  589. else
  590. begin
  591. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  592. objreloc.Addend:=Data;
  593. CurrObjSec.ObjRelocations.Add(objreloc);
  594. Data:=NtoLE(Data);
  595. writebytes(Data,4);
  596. end;
  597. end;
  598. RELOC_TYPE_INDEX_LEB:
  599. begin
  600. if len<>5 then
  601. internalerror(2021092612);
  602. if assigned(p) then
  603. internalerror(2021092613);
  604. objreloc:=TWasmObjRelocation.CreateTypeIndex(CurrObjSec.Size,Data);
  605. CurrObjSec.ObjRelocations.Add(objreloc);
  606. WriteUleb5(CurrObjSec,Data);
  607. end;
  608. RELOC_GLOBAL_INDEX_LEB:
  609. begin
  610. if len<>5 then
  611. internalerror(2021092701);
  612. if Data<>0 then
  613. internalerror(2021092702);
  614. if not assigned(p) then
  615. internalerror(2021092703);
  616. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  617. CurrObjSec.ObjRelocations.Add(objreloc);
  618. WriteUleb5(CurrObjSec,0);
  619. end;
  620. RELOC_TAG_INDEX_LEB:
  621. begin
  622. if len<>5 then
  623. internalerror(2021092712);
  624. if Data<>0 then
  625. internalerror(2021092713);
  626. if not assigned(p) then
  627. internalerror(2021092714);
  628. objreloc:=TWasmObjRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  629. CurrObjSec.ObjRelocations.Add(objreloc);
  630. WriteSleb5(CurrObjSec,0);
  631. end;
  632. else
  633. internalerror(2021092501);
  634. end;
  635. end;
  636. function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
  637. begin
  638. result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
  639. if not assigned(result) then
  640. result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
  641. end;
  642. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  643. var
  644. i: Integer;
  645. begin
  646. for i:=low(FFuncTypes) to high(FFuncTypes) do
  647. if wft.Equals(FFuncTypes[i]) then
  648. exit(i);
  649. result:=Length(FFuncTypes);
  650. SetLength(FFuncTypes,result+1);
  651. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  652. end;
  653. function TWasmObjData.globalref(asmsym: TAsmSymbol): TObjSymbol;
  654. begin
  655. if assigned(asmsym) then
  656. begin
  657. if (asmsym.typ<>AT_WASM_GLOBAL) and (asmsym.typ<>AT_TLS) then
  658. internalerror(2021092706);
  659. result:=symbolref(asmsym);
  660. result.typ:=asmsym.typ;
  661. end
  662. else
  663. result:=nil;
  664. end;
  665. function TWasmObjData.ExceptionTagRef(asmsym: TAsmSymbol): TObjSymbol;
  666. begin
  667. if assigned(asmsym) then
  668. begin
  669. if asmsym.typ<>AT_WASM_EXCEPTION_TAG then
  670. internalerror(2021092707);
  671. result:=symbolref(asmsym);
  672. result.typ:=AT_WASM_EXCEPTION_TAG;
  673. end
  674. else
  675. result:=nil;
  676. end;
  677. procedure TWasmObjData.DeclareGlobalType(gt: tai_globaltype);
  678. var
  679. ObjSymExtraData: TWasmObjSymbolExtraData;
  680. begin
  681. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(gt.globalname);
  682. ObjSymExtraData.GlobalType:=gt.gtype;
  683. ObjSymExtraData.GlobalIsImmutable:=gt.immutable;
  684. end;
  685. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  686. var
  687. i: Integer;
  688. ObjSymExtraData: TWasmObjSymbolExtraData;
  689. begin
  690. FLastFuncName:=ft.funcname;
  691. i:=AddFuncType(ft.functype);
  692. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
  693. ObjSymExtraData.TypeIdx:=i;
  694. end;
  695. procedure TWasmObjData.DeclareTagType(tt: tai_tagtype);
  696. var
  697. ObjSymExtraData: TWasmObjSymbolExtraData;
  698. ft: TWasmFuncType;
  699. i: Integer;
  700. begin
  701. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(tt.tagname);
  702. ft:=TWasmFuncType.Create([],tt.params);
  703. i:=AddFuncType(ft);
  704. ft.free;
  705. ObjSymExtraData.ExceptionTagTypeIdx:=i;
  706. end;
  707. procedure TWasmObjData.DeclareExportName(en: tai_export_name);
  708. var
  709. ObjSymExtraData: TWasmObjSymbolExtraData;
  710. begin
  711. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(en.intname);
  712. ObjSymExtraData.ExportName:=en.extname;
  713. end;
  714. procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
  715. var
  716. ObjSymExtraData: TWasmObjSymbolExtraData;
  717. begin
  718. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
  719. ObjSymExtraData.ImportModule:=aim.importmodule;
  720. end;
  721. procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
  722. var
  723. ObjSymExtraData: TWasmObjSymbolExtraData;
  724. begin
  725. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
  726. ObjSymExtraData.ImportName:=ain.importname;
  727. end;
  728. procedure TWasmObjData.DeclareLocal(al: tai_local);
  729. var
  730. ObjSymExtraData: TWasmObjSymbolExtraData;
  731. begin
  732. ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
  733. ObjSymExtraData.AddLocal(al.bastyp);
  734. end;
  735. procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
  736. var
  737. valsym: TObjSymbol;
  738. aliassym: TWasmObjSymbol;
  739. begin
  740. valsym:=CreateSymbol(avalue);
  741. aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
  742. aliassym.AliasOf:=valsym.Name;
  743. end;
  744. {****************************************************************************
  745. TWasmObjOutput
  746. ****************************************************************************}
  747. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  748. var
  749. b: byte;
  750. begin
  751. repeat
  752. b:=byte(v) and 127;
  753. v:=v shr 7;
  754. if v<>0 then
  755. b:=b or 128;
  756. d.write(b,1);
  757. until v=0;
  758. end;
  759. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  760. var
  761. b: byte;
  762. begin
  763. repeat
  764. b:=byte(v) and 127;
  765. v:=v shr 7;
  766. if v<>0 then
  767. b:=b or 128;
  768. w.write(b,1);
  769. until v=0;
  770. end;
  771. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  772. var
  773. b: byte;
  774. Done: Boolean=false;
  775. begin
  776. repeat
  777. b:=byte(v) and 127;
  778. v:=SarInt64(v,7);
  779. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  780. Done:=true
  781. else
  782. b:=b or 128;
  783. d.write(b,1);
  784. until Done;
  785. end;
  786. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  787. begin
  788. d.write(b,1);
  789. end;
  790. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  791. begin
  792. WriteUleb(d,Length(s));
  793. d.writestr(s);
  794. end;
  795. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  796. var
  797. b: byte;
  798. begin
  799. b:=ord(wsid);
  800. Writer.write(b,1);
  801. WriteUleb(Writer,FWasmSections[wsid].size);
  802. Writer.writearray(FWasmSections[wsid]);
  803. end;
  804. procedure TWasmObjOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
  805. var
  806. b: byte;
  807. begin
  808. b:=0;
  809. Writer.write(b,1);
  810. WriteUleb(Writer,FWasmCustomSections[wcst].size);
  811. Writer.writearray(FWasmCustomSections[wcst]);
  812. end;
  813. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  814. var
  815. buf: array [0..4095] of byte;
  816. bs: Integer;
  817. begin
  818. while size>0 do
  819. begin
  820. if size<SizeOf(buf) then
  821. bs:=Integer(size)
  822. else
  823. bs:=SizeOf(buf);
  824. src.read(buf,bs);
  825. dest.write(buf,bs);
  826. dec(size,bs);
  827. end;
  828. end;
  829. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  830. var
  831. buf : array[0..1023] of byte;
  832. bs: Integer;
  833. begin
  834. fillchar(buf,sizeof(buf),0);
  835. while size>0 do
  836. begin
  837. if size<SizeOf(buf) then
  838. bs:=Integer(size)
  839. else
  840. bs:=SizeOf(buf);
  841. dest.write(buf,bs);
  842. dec(size,bs);
  843. end;
  844. end;
  845. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  846. var
  847. i: Integer;
  848. begin
  849. WriteUleb(dest,Length(wrt));
  850. for i:=low(wrt) to high(wrt) do
  851. WriteWasmBasicType(dest,wrt[i]);
  852. end;
  853. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  854. begin
  855. WriteByte(dest,encode_wasm_basic_type(wbt));
  856. end;
  857. function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
  858. var
  859. ExtraData: TWasmObjSymbolExtraData;
  860. begin
  861. if sym.bind=AB_EXTERNAL then
  862. begin
  863. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  864. result:=(ExtraData<>nil) and (ExtraData.TypeIdx<>-1);
  865. end
  866. else
  867. result:=false;
  868. end;
  869. function TWasmObjOutput.IsExportedFunction(sym: TWasmObjSymbol): Boolean;
  870. var
  871. ExtraData: TWasmObjSymbolExtraData;
  872. begin
  873. if (sym.typ=AT_FUNCTION) and not sym.IsAlias then
  874. begin
  875. ExtraData:=TWasmObjSymbolExtraData(TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name));
  876. result:=(ExtraData<>nil) and (ExtraData.ExportName<>'');
  877. end
  878. else
  879. result:=false;
  880. end;
  881. procedure TWasmObjOutput.WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  882. var
  883. i,
  884. rle_entries,
  885. cnt: Integer;
  886. lasttype: TWasmBasicType;
  887. begin
  888. if Length(ed.Locals)=0 then
  889. begin
  890. WriteUleb(dest,0);
  891. exit;
  892. end;
  893. rle_entries:=1;
  894. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  895. if ed.Locals[i]<>ed.Locals[i-1] then
  896. inc(rle_entries);
  897. WriteUleb(dest,rle_entries);
  898. lasttype:=ed.Locals[Low(ed.Locals)];
  899. cnt:=1;
  900. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  901. if ed.Locals[i]=ed.Locals[i-1] then
  902. inc(cnt)
  903. else
  904. begin
  905. WriteUleb(dest,cnt);
  906. WriteWasmBasicType(dest,lasttype);
  907. lasttype:=ed.Locals[i];
  908. cnt:=1;
  909. end;
  910. WriteUleb(dest,cnt);
  911. WriteWasmBasicType(dest,lasttype);
  912. end;
  913. procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  914. var
  915. encoded_locals: tdynamicarray;
  916. ObjSymExtraData: TWasmObjSymbolExtraData;
  917. codelen: LongWord;
  918. ObjSection: TWasmObjSection;
  919. codeexprlen: QWord;
  920. begin
  921. ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  922. ObjSection:=TWasmObjSection(objsym.objsection);
  923. ObjSection.Data.seek(objsym.address);
  924. codeexprlen:=objsym.size;
  925. encoded_locals:=tdynamicarray.Create(64);
  926. WriteFunctionLocals(encoded_locals,ObjSymExtraData);
  927. codelen:=encoded_locals.size+codeexprlen+1;
  928. WriteUleb(dest,codelen);
  929. encoded_locals.seek(0);
  930. CopyDynamicArray(encoded_locals,dest,encoded_locals.size);
  931. ObjSection.FileSectionOfs:=dest.size-objsym.offset;
  932. CopyDynamicArray(ObjSection.Data,dest,codeexprlen);
  933. WriteByte(dest,$0B);
  934. encoded_locals.Free;
  935. end;
  936. procedure TWasmObjOutput.WriteSymbolTable;
  937. begin
  938. WriteUleb(FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTableEntriesCount);
  939. FWasmSymbolTable.seek(0);
  940. CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
  941. end;
  942. procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
  943. begin
  944. WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
  945. WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
  946. FWasmRelocationCodeTable.seek(0);
  947. CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
  948. end;
  949. procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
  950. begin
  951. WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
  952. WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
  953. FWasmRelocationDataTable.seek(0);
  954. CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
  955. end;
  956. procedure TWasmObjOutput.MaybeWriteRelocationDebugTable(cst: TWasmCustomSectionType; SectionIndex: Integer; EntriesCount: Integer; Table: tdynamicarray);
  957. begin
  958. if EntriesCount>0 then
  959. begin
  960. WriteUleb(FWasmCustomSections[cst],SectionIndex);
  961. WriteUleb(FWasmCustomSections[cst],EntriesCount);
  962. Table.seek(0);
  963. CopyDynamicArray(Table,FWasmCustomSections[cst],Table.size);
  964. WriteWasmCustomSection(cst);
  965. end;
  966. end;
  967. procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
  968. begin
  969. if FWasmLinkingSubsections[wlst].size>0 then
  970. begin
  971. WriteByte(FWasmCustomSections[wcstLinking],Ord(wlst));
  972. WriteUleb(FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  973. FWasmLinkingSubsections[wlst].seek(0);
  974. CopyDynamicArray(FWasmLinkingSubsections[wlst],FWasmCustomSections[wcstLinking],FWasmLinkingSubsections[wlst].size);
  975. end;
  976. end;
  977. procedure TWasmObjOutput.DoRelocations;
  978. var
  979. si, ri: Integer;
  980. objsec: TWasmObjSection;
  981. objrel: TWasmObjRelocation;
  982. begin
  983. for si:=0 to FData.ObjSectionList.Count-1 do
  984. begin
  985. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  986. for ri:=0 to objsec.ObjRelocations.Count-1 do
  987. begin
  988. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  989. case objrel.typ of
  990. RELOC_FUNCTION_INDEX_LEB:
  991. begin
  992. if not assigned(objrel.symbol) then
  993. internalerror(2021092509);
  994. objsec.Data.seek(objrel.DataOffset);
  995. if TWasmObjSymbol(objrel.symbol).FuncIndex<0 then
  996. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  997. else
  998. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).FuncIndex);
  999. end;
  1000. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1001. begin
  1002. if not assigned(objrel.symbol) then
  1003. internalerror(2021092605);
  1004. if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  1005. begin
  1006. objsec.Data.seek(objrel.DataOffset);
  1007. AddSleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1008. end;
  1009. end;
  1010. RELOC_MEMORY_ADDR_LEB:
  1011. begin
  1012. if not assigned(objrel.symbol) then
  1013. internalerror(2021092606);
  1014. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1015. internalerror(2021092628);
  1016. if objrel.symbol.bind<>AB_EXTERNAL then
  1017. begin
  1018. objsec.Data.seek(objrel.DataOffset);
  1019. AddUleb5(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1020. end;
  1021. end;
  1022. RELOC_ABSOLUTE:
  1023. begin
  1024. if assigned(objrel.ObjSection) then
  1025. begin
  1026. { todo: should we do something here? }
  1027. //Writeln('todo: section relocation');
  1028. end
  1029. else if not (IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) or (objrel.symbol.bind=AB_EXTERNAL)) then
  1030. begin
  1031. objsec.Data.seek(objrel.DataOffset);
  1032. AddInt32(objsec.Data,objrel.symbol.offset+TWasmObjSection(objrel.symbol.objsection).SegOfs);
  1033. end;
  1034. end;
  1035. RELOC_TYPE_INDEX_LEB:
  1036. ;
  1037. RELOC_GLOBAL_INDEX_LEB:
  1038. begin
  1039. if not assigned(objrel.symbol) then
  1040. internalerror(2021092509);
  1041. objsec.Data.seek(objrel.DataOffset);
  1042. if TWasmObjSymbol(objrel.symbol).GlobalIndex<0 then
  1043. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1044. else
  1045. WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).GlobalIndex);
  1046. end;
  1047. RELOC_TAG_INDEX_LEB:
  1048. begin
  1049. if not assigned(objrel.symbol) then
  1050. internalerror(2021092716);
  1051. objsec.Data.seek(objrel.DataOffset);
  1052. if TWasmObjSymbol(objrel.symbol).TagIndex<0 then
  1053. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1054. else
  1055. WriteSleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).TagIndex);
  1056. end;
  1057. else
  1058. internalerror(2021092510);
  1059. end;
  1060. end;
  1061. end;
  1062. end;
  1063. procedure TWasmObjOutput.WriteRelocations;
  1064. var
  1065. si, ri: Integer;
  1066. objsec: TWasmObjSection;
  1067. objrel: TWasmObjRelocation;
  1068. relout: tdynamicarray;
  1069. relcount: PInteger;
  1070. FuncSym: TWasmObjSymbol;
  1071. begin
  1072. for si:=0 to FData.ObjSectionList.Count-1 do
  1073. begin
  1074. objsec:=TWasmObjSection(FData.ObjSectionList[si]);
  1075. if objsec.IsCode then
  1076. begin
  1077. relout:=FWasmRelocationCodeTable;
  1078. relcount:=@FWasmRelocationCodeTableEntriesCount;
  1079. end
  1080. else if objsec.IsData then
  1081. begin
  1082. relout:=FWasmRelocationDataTable;
  1083. relcount:=@FWasmRelocationDataTableEntriesCount;
  1084. end
  1085. else if objsec.IsDebug then
  1086. begin
  1087. case objsec.Name of
  1088. '.debug_frame':
  1089. begin
  1090. relout:=FWasmRelocationDebugFrameTable;
  1091. relcount:=@FWasmRelocationDebugFrameTableEntriesCount;
  1092. end;
  1093. '.debug_info':
  1094. begin
  1095. relout:=FWasmRelocationDebugInfoTable;
  1096. relcount:=@FWasmRelocationDebugInfoTableEntriesCount;
  1097. end;
  1098. '.debug_line':
  1099. begin
  1100. relout:=FWasmRelocationDebugLineTable;
  1101. relcount:=@FWasmRelocationDebugLineTableEntriesCount;
  1102. end;
  1103. '.debug_abbrev':
  1104. begin
  1105. relout:=FWasmRelocationDebugAbbrevTable;
  1106. relcount:=@FWasmRelocationDebugAbbrevTableEntriesCount;
  1107. end;
  1108. '.debug_aranges':
  1109. begin
  1110. relout:=FWasmRelocationDebugArangesTable;
  1111. relcount:=@FWasmRelocationDebugArangesTableEntriesCount;
  1112. end;
  1113. '.debug_ranges':
  1114. begin
  1115. relout:=FWasmRelocationDebugRangesTable;
  1116. relcount:=@FWasmRelocationDebugRangesTableEntriesCount;
  1117. end;
  1118. '.debug_str':
  1119. begin
  1120. relout:=FWasmRelocationDebugStrTable;
  1121. relcount:=@FWasmRelocationDebugStrTableEntriesCount;
  1122. end;
  1123. else
  1124. internalerror(2022071601);
  1125. end;
  1126. end
  1127. else
  1128. continue;
  1129. for ri:=0 to objsec.ObjRelocations.Count-1 do
  1130. begin
  1131. objrel:=TWasmObjRelocation(objsec.ObjRelocations[ri]);
  1132. case objrel.typ of
  1133. RELOC_FUNCTION_INDEX_LEB:
  1134. begin
  1135. if not assigned(objrel.symbol) then
  1136. internalerror(2021092508);
  1137. Inc(relcount^);
  1138. WriteByte(relout,Ord(R_WASM_FUNCTION_INDEX_LEB));
  1139. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1140. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1141. end;
  1142. RELOC_MEMORY_ADDR_LEB:
  1143. begin
  1144. if not assigned(objrel.symbol) then
  1145. internalerror(2021092603);
  1146. Inc(relcount^);
  1147. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1148. internalerror(2021092628);
  1149. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_LEB));
  1150. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1151. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1152. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1153. end;
  1154. RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB:
  1155. begin
  1156. if not assigned(objrel.symbol) then
  1157. internalerror(2021092604);
  1158. Inc(relcount^);
  1159. if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1160. begin
  1161. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_SLEB));
  1162. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1163. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1164. end
  1165. else
  1166. begin
  1167. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB));
  1168. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1169. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1170. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1171. end;
  1172. end;
  1173. RELOC_ABSOLUTE:
  1174. begin
  1175. // todo: figure this out, why do these exist?
  1176. //if assigned(objrel.symbol) and not assigned(objrel.symbol.objsection) then
  1177. // Writeln('!!! ', objrel.symbol.name);
  1178. if assigned(objrel.objsection) then
  1179. begin
  1180. Inc(relcount^);
  1181. WriteByte(relout,Ord(R_WASM_SECTION_OFFSET_I32));
  1182. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1183. if (TWasmObjSection(objrel.objsection).SegSymIdx<0) then
  1184. message1(asmw_e_illegal_unset_index,objrel.objsection.name)
  1185. else
  1186. WriteUleb(relout,TWasmObjSection(objrel.objsection).SegSymIdx);
  1187. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1188. end
  1189. else if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then
  1190. begin
  1191. Inc(relcount^);
  1192. WriteByte(relout,Ord(R_WASM_TABLE_INDEX_I32));
  1193. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1194. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1195. end
  1196. else if assigned(objrel.symbol) and assigned(objrel.symbol.objsection) and TWasmObjSection(objrel.symbol.objsection).IsCode then
  1197. begin
  1198. Inc(relcount^);
  1199. WriteByte(relout,Ord(R_WASM_FUNCTION_OFFSET_I32));
  1200. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1201. FuncSym:=FindFunctionSymbol(TWasmObjSymbol(objrel.Symbol));
  1202. if FuncSym.SymbolIndex<0 then
  1203. message1(asmw_e_illegal_unset_index,FuncSym.Name)
  1204. else
  1205. WriteUleb(relout,FuncSym.SymbolIndex);
  1206. WriteSleb(relout,objrel.Addend+objrel.symbol.address); { addend to add to the address }
  1207. end
  1208. else
  1209. begin
  1210. Inc(relcount^);
  1211. WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_I32));
  1212. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1213. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1214. begin
  1215. Writeln(objrel.symbol.objsection.Name, ' ', objrel.symbol.name, ' ', objsec.Name);
  1216. message1(asmw_e_illegal_unset_index,objrel.symbol.name);
  1217. end
  1218. else
  1219. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1220. WriteSleb(relout,objrel.Addend); { addend to add to the address }
  1221. end;
  1222. end;
  1223. RELOC_TYPE_INDEX_LEB:
  1224. begin
  1225. Inc(relcount^);
  1226. WriteByte(relout,Ord(R_WASM_TYPE_INDEX_LEB));
  1227. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1228. WriteUleb(relout,objrel.TypeIndex);
  1229. end;
  1230. RELOC_GLOBAL_INDEX_LEB:
  1231. begin
  1232. if not assigned(objrel.symbol) then
  1233. internalerror(2021092704);
  1234. Inc(relcount^);
  1235. WriteByte(relout,Ord(R_WASM_GLOBAL_INDEX_LEB));
  1236. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1237. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1238. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1239. else
  1240. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1241. end;
  1242. RELOC_TAG_INDEX_LEB:
  1243. begin
  1244. if not assigned(objrel.symbol) then
  1245. internalerror(2021092717);
  1246. Inc(relcount^);
  1247. WriteByte(relout,Ord(R_WASM_TAG_INDEX_LEB));
  1248. WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
  1249. if (TWasmObjSymbol(objrel.symbol).SymbolIndex<0) then
  1250. message1(asmw_e_illegal_unset_index,objrel.symbol.name)
  1251. else
  1252. WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex);
  1253. end;
  1254. else
  1255. internalerror(2021092507);
  1256. end;
  1257. end;
  1258. end;
  1259. end;
  1260. function TWasmObjOutput.FindFunctionSymbol(Symbol: TWasmObjSymbol): TWasmObjSymbol;
  1261. begin
  1262. Result:=TWasmObjSection(Symbol.objsection).MainFuncSymbol;
  1263. end;
  1264. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  1265. var
  1266. section_nr: Integer;
  1267. procedure MaybeWriteDebugSection(const sn: string; st: TWasmCustomSectionType; var debug_section_nr: Integer);
  1268. var
  1269. i: Integer;
  1270. objsec: TWasmObjSection;
  1271. begin
  1272. for i:=0 to Data.ObjSectionList.Count-1 do
  1273. begin
  1274. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1275. if objsec.Name=sn then
  1276. begin
  1277. debug_section_nr:=section_nr;
  1278. Inc(section_nr);
  1279. if oso_Data in objsec.SecOptions then
  1280. begin
  1281. objsec.Data.seek(0);
  1282. CopyDynamicArray(objsec.Data,FWasmCustomSections[st],objsec.Size);
  1283. end
  1284. else
  1285. WriteZeros(FWasmCustomSections[st],objsec.Size);
  1286. WriteWasmCustomSection(st);
  1287. end;
  1288. end;
  1289. end;
  1290. var
  1291. i: Integer;
  1292. objsec: TWasmObjSection;
  1293. segment_count: Integer = 0;
  1294. cur_seg_ofs: qword = 0;
  1295. types_count,
  1296. imports_count, NextImportFunctionIndex, NextFunctionIndex,
  1297. code_section_nr, data_section_nr,
  1298. debug_abbrev_section_nr,debug_info_section_nr,debug_str_section_nr,
  1299. debug_line_section_nr,debug_frame_section_nr,debug_aranges_section_nr,
  1300. debug_ranges_section_nr,
  1301. NextGlobalIndex, NextTagIndex: Integer;
  1302. import_globals_count: Integer = 0;
  1303. globals_count: Integer = 0;
  1304. import_functions_count: Integer = 0;
  1305. export_functions_count: Integer = 0;
  1306. functions_count: Integer = 0;
  1307. import_exception_tags_count: Integer = 0;
  1308. exception_tags_count: Integer = 0;
  1309. objsym, ObjSymAlias: TWasmObjSymbol;
  1310. cust_sec: TWasmCustomSectionType;
  1311. SegmentFlags, SymbolFlags: UInt64;
  1312. begin
  1313. FData:=TWasmObjData(Data);
  1314. { each custom sections starts with its name }
  1315. for cust_sec in TWasmCustomSectionType do
  1316. WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
  1317. WriteUleb(FWasmCustomSections[wcstLinking],2); { linking metadata version }
  1318. for i:=0 to Data.ObjSymbolList.Count-1 do
  1319. begin
  1320. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1321. if objsym.typ=AT_WASM_EXCEPTION_TAG then
  1322. if objsym.bind=AB_EXTERNAL then
  1323. Inc(import_exception_tags_count)
  1324. else
  1325. Inc(exception_tags_count);
  1326. if objsym.typ=AT_WASM_GLOBAL then
  1327. if objsym.bind=AB_EXTERNAL then
  1328. Inc(import_globals_count)
  1329. else
  1330. Inc(globals_count);
  1331. if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1332. Inc(import_globals_count);
  1333. if IsExternalFunction(objsym) then
  1334. Inc(import_functions_count);
  1335. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1336. begin
  1337. TWasmObjSection(objsym.objsection).MainFuncSymbol:=objsym;
  1338. Inc(functions_count);
  1339. end;
  1340. if IsExportedFunction(objsym) then
  1341. Inc(export_functions_count);
  1342. end;
  1343. types_count:=Length(FData.FFuncTypes);
  1344. WriteUleb(FWasmSections[wsiType],types_count);
  1345. for i:=0 to types_count-1 do
  1346. with FData.FFuncTypes[i] do
  1347. begin
  1348. WriteByte(FWasmSections[wsiType],$60);
  1349. WriteWasmResultType(FWasmSections[wsiType],params);
  1350. WriteWasmResultType(FWasmSections[wsiType],results);
  1351. end;
  1352. for i:=0 to Data.ObjSectionList.Count-1 do
  1353. begin
  1354. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1355. if objsec.IsCode then
  1356. objsec.SegIdx:=-1
  1357. else if objsec.IsData then
  1358. begin
  1359. objsec.SegIdx:=segment_count;
  1360. objsec.SegOfs:=cur_seg_ofs;
  1361. Inc(segment_count);
  1362. Inc(cur_seg_ofs,objsec.Size);
  1363. end;
  1364. end;
  1365. if segment_count>0 then
  1366. begin
  1367. WriteUleb(FWasmSections[wsiData],segment_count);
  1368. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  1369. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],segment_count);
  1370. for i:=0 to Data.ObjSectionList.Count-1 do
  1371. begin
  1372. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  1373. if objsec.IsData then
  1374. begin
  1375. WriteName(FWasmLinkingSubsections[WASM_SEGMENT_INFO],objsec.Name);
  1376. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],BsrQWord(objsec.SecAlign));
  1377. SegmentFlags:=0;
  1378. if (ts_wasm_threads in current_settings.targetswitches) and
  1379. (oso_threadvar in objsec.SecOptions) then
  1380. SegmentFlags:=SegmentFlags or WASM_SEG_FLAG_TLS;
  1381. WriteUleb(FWasmLinkingSubsections[WASM_SEGMENT_INFO],SegmentFlags); { flags }
  1382. WriteByte(FWasmSections[wsiData],0);
  1383. WriteByte(FWasmSections[wsiData],$41);
  1384. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  1385. WriteByte(FWasmSections[wsiData],$0b);
  1386. WriteUleb(FWasmSections[wsiData],objsec.Size);
  1387. objsec.FileSectionOfs:=FWasmSections[wsiData].size;
  1388. if oso_Data in objsec.SecOptions then
  1389. begin
  1390. objsec.Data.seek(0);
  1391. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  1392. end
  1393. else
  1394. begin
  1395. WriteZeros(FWasmSections[wsiData],objsec.Size);
  1396. end;
  1397. end;
  1398. end;
  1399. end;
  1400. imports_count:=2+import_globals_count+import_functions_count+import_exception_tags_count;
  1401. WriteUleb(FWasmSections[wsiImport],imports_count);
  1402. { import memories }
  1403. WriteName(FWasmSections[wsiImport],'env');
  1404. WriteName(FWasmSections[wsiImport],'__linear_memory');
  1405. WriteByte(FWasmSections[wsiImport],$02); { mem }
  1406. WriteByte(FWasmSections[wsiImport],$00); { min }
  1407. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  1408. { import globals }
  1409. NextGlobalIndex:=0;
  1410. for i:=0 to Data.ObjSymbolList.Count-1 do
  1411. begin
  1412. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1413. if (objsym.bind=AB_EXTERNAL) and (objsym.typ=AT_WASM_GLOBAL) then
  1414. begin
  1415. objsym.GlobalIndex:=NextGlobalIndex;
  1416. Inc(NextGlobalIndex);
  1417. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1418. if objsym.ExtraData.ImportModule<>'' then
  1419. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1420. else
  1421. WriteName(FWasmSections[wsiImport],'env');
  1422. WriteName(FWasmSections[wsiImport],objsym.Name);
  1423. WriteByte(FWasmSections[wsiImport],$03); { global }
  1424. WriteWasmBasicType(FWasmSections[wsiImport],objsym.ExtraData.GlobalType);
  1425. if objsym.ExtraData.GlobalIsImmutable then
  1426. WriteByte(FWasmSections[wsiImport],$00) { const }
  1427. else
  1428. WriteByte(FWasmSections[wsiImport],$01); { var }
  1429. end
  1430. else if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1431. begin
  1432. objsym.GlobalIndex:=NextGlobalIndex;
  1433. Inc(NextGlobalIndex);
  1434. objsym.ExtraData:=nil;
  1435. WriteName(FWasmSections[wsiImport],'GOT.mem');
  1436. WriteName(FWasmSections[wsiImport],objsym.Name);
  1437. WriteByte(FWasmSections[wsiImport],$03); { global }
  1438. WriteWasmBasicType(FWasmSections[wsiImport],wbt_i32); { i32 }
  1439. WriteByte(FWasmSections[wsiImport],$01); { var }
  1440. end;
  1441. end;
  1442. { import functions }
  1443. NextImportFunctionIndex:=0;
  1444. for i:=0 to Data.ObjSymbolList.Count-1 do
  1445. begin
  1446. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1447. if IsExternalFunction(objsym) then
  1448. begin
  1449. objsym.FuncIndex:=NextImportFunctionIndex;
  1450. Inc(NextImportFunctionIndex);
  1451. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1452. if objsym.ExtraData.ImportModule<>'' then
  1453. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1454. else
  1455. WriteName(FWasmSections[wsiImport],'env');
  1456. WriteName(FWasmSections[wsiImport],objsym.Name);
  1457. WriteByte(FWasmSections[wsiImport],$00); { func }
  1458. WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1459. end;
  1460. end;
  1461. { import tables }
  1462. WriteName(FWasmSections[wsiImport],'env');
  1463. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  1464. WriteByte(FWasmSections[wsiImport],$01); { table }
  1465. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  1466. WriteByte(FWasmSections[wsiImport],$00); { min }
  1467. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  1468. { import tags }
  1469. NextTagIndex:=0;
  1470. for i:=0 to Data.ObjSymbolList.Count-1 do
  1471. begin
  1472. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1473. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind=AB_EXTERNAL) then
  1474. begin
  1475. objsym.TagIndex:=NextTagIndex;
  1476. Inc(NextTagIndex);
  1477. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1478. if objsym.ExtraData.ImportModule<>'' then
  1479. WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
  1480. else
  1481. WriteName(FWasmSections[wsiImport],'env');
  1482. WriteName(FWasmSections[wsiImport],objsym.Name);
  1483. WriteByte(FWasmSections[wsiImport],$04); { tag }
  1484. WriteByte(FWasmSections[wsiImport],$00); { exception }
  1485. WriteUleb(FWasmSections[wsiImport],objsym.ExtraData.ExceptionTagTypeIdx);
  1486. end;
  1487. end;
  1488. WriteUleb(FWasmSections[wsiFunction],functions_count);
  1489. NextFunctionIndex:=NextImportFunctionIndex;
  1490. for i:=0 to Data.ObjSymbolList.Count-1 do
  1491. begin
  1492. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1493. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1494. begin
  1495. objsym.FuncIndex:=NextFunctionIndex;
  1496. Inc(NextFunctionIndex);
  1497. WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  1498. end;
  1499. end;
  1500. if exception_tags_count>0 then
  1501. begin
  1502. WriteUleb(FWasmSections[wsiTag],exception_tags_count);
  1503. for i:=0 to Data.ObjSymbolList.Count-1 do
  1504. begin
  1505. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1506. if (objsym.typ=AT_WASM_EXCEPTION_TAG) and (objsym.bind<>AB_EXTERNAL) then
  1507. begin
  1508. objsym.TagIndex:=NextTagIndex;
  1509. Inc(NextTagIndex);
  1510. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1511. WriteByte(FWasmSections[wsiTag],$00); { exception }
  1512. WriteUleb(FWasmSections[wsiTag],objsym.ExtraData.ExceptionTagTypeIdx);
  1513. end;
  1514. end;
  1515. end;
  1516. if globals_count>0 then
  1517. begin
  1518. WriteUleb(FWasmSections[wsiGlobal],globals_count);
  1519. for i:=0 to Data.ObjSymbolList.Count-1 do
  1520. begin
  1521. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1522. if (objsym.typ=AT_WASM_GLOBAL) and (objsym.bind<>AB_EXTERNAL) then
  1523. begin
  1524. objsym.GlobalIndex:=NextGlobalIndex;
  1525. Inc(NextGlobalIndex);
  1526. objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  1527. WriteWasmBasicType(FWasmSections[wsiGlobal],objsym.ExtraData.GlobalType);
  1528. if objsym.ExtraData.GlobalIsImmutable then
  1529. WriteByte(FWasmSections[wsiGlobal],$00) { const }
  1530. else
  1531. WriteByte(FWasmSections[wsiGlobal],$01); { var }
  1532. { init expr }
  1533. case objsym.ExtraData.GlobalType of
  1534. wbt_i32:
  1535. begin
  1536. WriteByte(FWasmSections[wsiGlobal],$41); { i32.const }
  1537. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1538. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1539. end;
  1540. wbt_i64:
  1541. begin
  1542. WriteByte(FWasmSections[wsiGlobal],$42); { i64.const }
  1543. WriteByte(FWasmSections[wsiGlobal],0); { 0 (in signed LEB128 format) }
  1544. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1545. end;
  1546. wbt_f32:
  1547. begin
  1548. WriteByte(FWasmSections[wsiGlobal],$43); { f32.const }
  1549. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE single precision floating point format) }
  1550. WriteByte(FWasmSections[wsiGlobal],$00);
  1551. WriteByte(FWasmSections[wsiGlobal],$00);
  1552. WriteByte(FWasmSections[wsiGlobal],$00);
  1553. WriteByte(FWasmSections[wsiGlobal],$0B); { end }
  1554. end;
  1555. wbt_f64:
  1556. begin
  1557. WriteByte(FWasmSections[wsiGlobal],$44); { f64.const }
  1558. WriteByte(FWasmSections[wsiGlobal],$00); { 0 (in little endian IEEE double precision floating point format) }
  1559. WriteByte(FWasmSections[wsiGlobal],$00);
  1560. WriteByte(FWasmSections[wsiGlobal],$00);
  1561. WriteByte(FWasmSections[wsiGlobal],$00);
  1562. WriteByte(FWasmSections[wsiGlobal],$00);
  1563. WriteByte(FWasmSections[wsiGlobal],$00);
  1564. WriteByte(FWasmSections[wsiGlobal],$00);
  1565. WriteByte(FWasmSections[wsiGlobal],$00);
  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. {not implemented yet}
  1631. internalerror(2021092705);
  1632. end
  1633. else if IsExternalFunction(objsym) then
  1634. begin
  1635. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1636. Inc(FWasmSymbolTableEntriesCount);
  1637. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1638. if objsym.ExtraData.ImportModule<>'' then
  1639. begin
  1640. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
  1641. if (objsym.FuncIndex<0) then
  1642. message1(asmw_e_illegal_unset_index,objsym.name)
  1643. else
  1644. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1645. WriteName(FWasmSymbolTable,objsym.Name);
  1646. end
  1647. else
  1648. begin
  1649. WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
  1650. if (objsym.FuncIndex<0) then
  1651. message1(asmw_e_illegal_unset_index,objsym.name)
  1652. else
  1653. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1654. end;
  1655. end
  1656. else if objsym.typ=AT_FUNCTION then
  1657. begin
  1658. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1659. Inc(FWasmSymbolTableEntriesCount);
  1660. WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
  1661. if objsym.IsAlias then
  1662. begin
  1663. ObjSymAlias:=TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf));
  1664. ObjSym.FuncIndex:=ObjSymAlias.FuncIndex;
  1665. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
  1666. WriteUleb(FWasmSymbolTable,ObjSymAlias.FuncIndex);
  1667. end
  1668. else
  1669. begin
  1670. if IsExportedFunction(objsym) then
  1671. WriteUleb(FWasmSymbolTable,WASM_SYM_EXPORTED)
  1672. else
  1673. WriteUleb(FWasmSymbolTable,0);
  1674. if (objsym.FuncIndex<0) then
  1675. message1(asmw_e_illegal_unset_index,objsym.name)
  1676. else
  1677. WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
  1678. end;
  1679. WriteName(FWasmSymbolTable,objsym.Name);
  1680. end
  1681. else if (objsym.typ in [AT_DATA,AT_TLS]) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
  1682. begin
  1683. if (objsym.bind<>AB_EXTERNAL) and TWasmObjSection(objsym.objsection).IsDebug then
  1684. begin
  1685. {todo: debug symbols}
  1686. end
  1687. else
  1688. begin
  1689. objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
  1690. Inc(FWasmSymbolTableEntriesCount);
  1691. WriteByte(FWasmSymbolTable,Ord(SYMTAB_DATA));
  1692. if objsym.bind=AB_GLOBAL then
  1693. SymbolFlags:=0
  1694. else if objsym.bind=AB_LOCAL then
  1695. SymbolFlags:=WASM_SYM_BINDING_LOCAL
  1696. else if objsym.bind=AB_EXTERNAL then
  1697. SymbolFlags:=WASM_SYM_UNDEFINED
  1698. else
  1699. internalerror(2021092506);
  1700. if (objsym.typ=AT_TLS) and (ts_wasm_threads in current_settings.targetswitches) then
  1701. SymbolFlags:=(SymbolFlags and not WASM_SYM_BINDING_LOCAL) or WASM_SYM_TLS;
  1702. WriteUleb(FWasmSymbolTable,SymbolFlags);
  1703. WriteName(FWasmSymbolTable,objsym.Name);
  1704. if objsym.bind<>AB_EXTERNAL then
  1705. begin
  1706. WriteUleb(FWasmSymbolTable,TWasmObjSection(objsym.objsection).SegIdx);
  1707. WriteUleb(FWasmSymbolTable,objsym.offset);
  1708. WriteUleb(FWasmSymbolTable,objsym.size);
  1709. end;
  1710. end;
  1711. end;
  1712. end;
  1713. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  1714. Writer.write(WasmVersion,SizeOf(WasmVersion));
  1715. if ts_wasm_threads in current_settings.targetswitches then
  1716. begin
  1717. WriteUleb(FWasmCustomSections[wcstTargetFeatures],4);
  1718. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1719. WriteName(FWasmCustomSections[wcstTargetFeatures],'atomics');
  1720. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1721. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1722. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1723. WriteName(FWasmCustomSections[wcstTargetFeatures],'mutable-globals');
  1724. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1725. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1726. end
  1727. else
  1728. begin
  1729. WriteUleb(FWasmCustomSections[wcstTargetFeatures],2);
  1730. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1731. WriteName(FWasmCustomSections[wcstTargetFeatures],'bulk-memory');
  1732. WriteUleb(FWasmCustomSections[wcstTargetFeatures],$2B);
  1733. WriteName(FWasmCustomSections[wcstTargetFeatures],'sign-ext');
  1734. end;
  1735. { Write the producers section:
  1736. https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md }
  1737. WriteUleb(FWasmCustomSections[wcstProducers],2);
  1738. WriteName(FWasmCustomSections[wcstProducers],'language');
  1739. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1740. WriteName(FWasmCustomSections[wcstProducers],'Pascal');
  1741. WriteName(FWasmCustomSections[wcstProducers],'');
  1742. WriteName(FWasmCustomSections[wcstProducers],'processed-by');
  1743. WriteUleb(FWasmCustomSections[wcstProducers],1);
  1744. WriteName(FWasmCustomSections[wcstProducers],'Free Pascal Compiler (FPC)');
  1745. WriteName(FWasmCustomSections[wcstProducers],full_version_string+' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname);
  1746. code_section_nr:=-1;
  1747. data_section_nr:=-1;
  1748. debug_abbrev_section_nr:=-1;
  1749. debug_info_section_nr:=-1;
  1750. debug_str_section_nr:=-1;
  1751. debug_line_section_nr:=-1;
  1752. debug_frame_section_nr:=-1;
  1753. debug_aranges_section_nr:=-1;
  1754. debug_ranges_section_nr:=-1;
  1755. section_nr:=0;
  1756. WriteWasmSection(wsiType);
  1757. Inc(section_nr);
  1758. WriteWasmSection(wsiImport);
  1759. Inc(section_nr);
  1760. WriteWasmSection(wsiFunction);
  1761. Inc(section_nr);
  1762. if exception_tags_count>0 then
  1763. begin
  1764. WriteWasmSection(wsiTag);
  1765. Inc(section_nr);
  1766. end;
  1767. if globals_count>0 then
  1768. begin
  1769. WriteWasmSection(wsiGlobal);
  1770. Inc(section_nr);
  1771. end;
  1772. if export_functions_count>0 then
  1773. begin
  1774. WriteWasmSection(wsiExport);
  1775. Inc(section_nr);
  1776. end;
  1777. if segment_count>0 then
  1778. begin
  1779. WriteWasmSection(wsiDataCount);
  1780. Inc(section_nr);
  1781. end;
  1782. WriteUleb(FWasmSections[wsiCode],functions_count);
  1783. for i:=0 to Data.ObjSymbolList.Count-1 do
  1784. begin
  1785. objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
  1786. if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
  1787. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  1788. end;
  1789. WriteWasmSection(wsiCode);
  1790. code_section_nr:=section_nr;
  1791. Inc(section_nr);
  1792. if segment_count>0 then
  1793. begin
  1794. WriteWasmSection(wsiData);
  1795. data_section_nr:=section_nr;
  1796. Inc(section_nr);
  1797. end;
  1798. MaybeWriteDebugSection('.debug_abbrev',wcstDebugAbbrev,debug_abbrev_section_nr);
  1799. MaybeWriteDebugSection('.debug_info',wcstDebugInfo,debug_info_section_nr);
  1800. MaybeWriteDebugSection('.debug_str',wcstDebugStr,debug_str_section_nr);
  1801. MaybeWriteDebugSection('.debug_line',wcstDebugLine,debug_line_section_nr);
  1802. MaybeWriteDebugSection('.debug_frame',wcstDebugFrame,debug_frame_section_nr);
  1803. MaybeWriteDebugSection('.debug_aranges',wcstDebugAranges,debug_aranges_section_nr);
  1804. MaybeWriteDebugSection('.debug_ranges',wcstDebugRanges,debug_ranges_section_nr);
  1805. if debug_abbrev_section_nr<>-1 then
  1806. begin
  1807. TWasmObjSection(Data.ObjSectionList.Find('.debug_abbrev')).SegSymIdx:=FWasmSymbolTableEntriesCount;
  1808. Inc(FWasmSymbolTableEntriesCount);
  1809. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1810. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1811. WriteUleb(FWasmSymbolTable,debug_abbrev_section_nr);
  1812. end;
  1813. if debug_info_section_nr<>-1 then
  1814. begin
  1815. TWasmObjSection(Data.ObjSectionList.Find('.debug_info')).SegSymIdx:=FWasmSymbolTableEntriesCount;
  1816. Inc(FWasmSymbolTableEntriesCount);
  1817. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1818. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1819. WriteUleb(FWasmSymbolTable,debug_info_section_nr);
  1820. end;
  1821. if debug_str_section_nr<>-1 then
  1822. begin
  1823. TWasmObjSection(Data.ObjSectionList.Find('.debug_str')).SegSymIdx:=FWasmSymbolTableEntriesCount;
  1824. Inc(FWasmSymbolTableEntriesCount);
  1825. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1826. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1827. WriteUleb(FWasmSymbolTable,debug_str_section_nr);
  1828. end;
  1829. if debug_line_section_nr<>-1 then
  1830. begin
  1831. TWasmObjSection(Data.ObjSectionList.Find('.debug_line')).SegSymIdx:=FWasmSymbolTableEntriesCount;
  1832. Inc(FWasmSymbolTableEntriesCount);
  1833. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1834. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1835. WriteUleb(FWasmSymbolTable,debug_line_section_nr);
  1836. end;
  1837. if debug_frame_section_nr<>-1 then
  1838. begin
  1839. TWasmObjSection(Data.ObjSectionList.Find('.debug_frame')).SegSymIdx:=FWasmSymbolTableEntriesCount;
  1840. Inc(FWasmSymbolTableEntriesCount);
  1841. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1842. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1843. WriteUleb(FWasmSymbolTable,debug_frame_section_nr);
  1844. end;
  1845. if debug_aranges_section_nr<>-1 then
  1846. begin
  1847. TWasmObjSection(Data.ObjSectionList.Find('.debug_aranges')).SegSymIdx:=FWasmSymbolTableEntriesCount;
  1848. Inc(FWasmSymbolTableEntriesCount);
  1849. WriteByte(FWasmSymbolTable,Ord(SYMTAB_SECTION));
  1850. WriteUleb(FWasmSymbolTable,WASM_SYM_BINDING_LOCAL);
  1851. WriteUleb(FWasmSymbolTable,debug_aranges_section_nr);
  1852. end;
  1853. DoRelocations;
  1854. WriteRelocations;
  1855. WriteSymbolTable;
  1856. WriteLinkingSubsection(WASM_SYMBOL_TABLE);
  1857. if segment_count>0 then
  1858. WriteLinkingSubsection(WASM_SEGMENT_INFO);
  1859. WriteRelocationCodeTable(code_section_nr);
  1860. if segment_count>0 then
  1861. WriteRelocationDataTable(data_section_nr);
  1862. WriteWasmCustomSection(wcstLinking);
  1863. Inc(section_nr);
  1864. WriteWasmCustomSection(wcstRelocCode);
  1865. Inc(section_nr);
  1866. if segment_count>0 then
  1867. begin
  1868. WriteWasmCustomSection(wcstRelocData);
  1869. Inc(section_nr);
  1870. end;
  1871. MaybeWriteRelocationDebugTable(wcstRelocDebugAbbrev,debug_abbrev_section_nr,FWasmRelocationDebugAbbrevTableEntriesCount,FWasmRelocationDebugAbbrevTable);
  1872. MaybeWriteRelocationDebugTable(wcstRelocDebugInfo,debug_info_section_nr,FWasmRelocationDebugInfoTableEntriesCount,FWasmRelocationDebugInfoTable);
  1873. MaybeWriteRelocationDebugTable(wcstRelocDebugStr,debug_str_section_nr,FWasmRelocationDebugStrTableEntriesCount,FWasmRelocationDebugStrTable);
  1874. MaybeWriteRelocationDebugTable(wcstRelocDebugLine,debug_line_section_nr,FWasmRelocationDebugLineTableEntriesCount,FWasmRelocationDebugLineTable);
  1875. MaybeWriteRelocationDebugTable(wcstRelocDebugFrame,debug_frame_section_nr,FWasmRelocationDebugFrameTableEntriesCount,FWasmRelocationDebugFrameTable);
  1876. MaybeWriteRelocationDebugTable(wcstRelocDebugAranges,debug_aranges_section_nr,FWasmRelocationDebugArangesTableEntriesCount,FWasmRelocationDebugArangesTable);
  1877. MaybeWriteRelocationDebugTable(wcstRelocDebugRanges,debug_ranges_section_nr,FWasmRelocationDebugRangesTableEntriesCount,FWasmRelocationDebugRangesTable);
  1878. WriteWasmCustomSection(wcstProducers);
  1879. Inc(section_nr);
  1880. WriteWasmCustomSection(wcstTargetFeatures);
  1881. Inc(section_nr);
  1882. result:=true;
  1883. end;
  1884. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  1885. var
  1886. i: TWasmSectionID;
  1887. j: TWasmCustomSectionType;
  1888. k: TWasmLinkingSubsectionType;
  1889. begin
  1890. inherited;
  1891. cobjdata:=TWasmObjData;
  1892. for i in TWasmSectionID do
  1893. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  1894. for j in TWasmCustomSectionType do
  1895. FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
  1896. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1897. FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
  1898. FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
  1899. FWasmSymbolTableEntriesCount:=0;
  1900. FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
  1901. FWasmRelocationCodeTableEntriesCount:=0;
  1902. FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
  1903. FWasmRelocationDataTableEntriesCount:=0;
  1904. FWasmRelocationDebugFrameTable:=tdynamicarray.create(SectionDataMaxGrow);
  1905. FWasmRelocationDebugFrameTableEntriesCount:=0;
  1906. FWasmRelocationDebugInfoTable:=tdynamicarray.create(SectionDataMaxGrow);
  1907. FWasmRelocationDebugInfoTableEntriesCount:=0;
  1908. FWasmRelocationDebugLineTable:=tdynamicarray.create(SectionDataMaxGrow);
  1909. FWasmRelocationDebugLineTableEntriesCount:=0;
  1910. FWasmRelocationDebugAbbrevTable:=tdynamicarray.create(SectionDataMaxGrow);
  1911. FWasmRelocationDebugAbbrevTableEntriesCount:=0;
  1912. FWasmRelocationDebugArangesTable:=tdynamicarray.create(SectionDataMaxGrow);
  1913. FWasmRelocationDebugArangesTableEntriesCount:=0;
  1914. FWasmRelocationDebugRangesTable:=tdynamicarray.create(SectionDataMaxGrow);
  1915. FWasmRelocationDebugRangesTableEntriesCount:=0;
  1916. FWasmRelocationDebugStrTable:=tdynamicarray.create(SectionDataMaxGrow);
  1917. FWasmRelocationDebugStrTableEntriesCount:=0;
  1918. end;
  1919. destructor TWasmObjOutput.destroy;
  1920. var
  1921. i: TWasmSectionID;
  1922. j: TWasmCustomSectionType;
  1923. k: TWasmLinkingSubsectionType;
  1924. begin
  1925. for i in TWasmSectionID do
  1926. FWasmSections[i].Free;
  1927. for j in TWasmCustomSectionType do
  1928. FWasmCustomSections[j].Free;
  1929. for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
  1930. FWasmLinkingSubsections[k].Free;
  1931. FWasmSymbolTable.Free;
  1932. FWasmRelocationCodeTable.Free;
  1933. FWasmRelocationDataTable.Free;
  1934. FWasmRelocationDebugFrameTable.Free;
  1935. FWasmRelocationDebugInfoTable.Free;
  1936. FWasmRelocationDebugLineTable.Free;
  1937. FWasmRelocationDebugAbbrevTable.Free;
  1938. FWasmRelocationDebugArangesTable.Free;
  1939. FWasmRelocationDebugRangesTable.Free;
  1940. FWasmRelocationDebugStrTable.Free;
  1941. inherited destroy;
  1942. end;
  1943. {****************************************************************************
  1944. TWasmAssembler
  1945. ****************************************************************************}
  1946. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  1947. begin
  1948. inherited;
  1949. CObjOutput:=TWasmObjOutput;
  1950. end;
  1951. {*****************************************************************************
  1952. Initialize
  1953. *****************************************************************************}
  1954. {$ifdef wasm32}
  1955. const
  1956. as_wasm32_wasm_info : tasminfo =
  1957. (
  1958. id : as_wasm32_wasm;
  1959. idtxt : 'WASM';
  1960. asmbin : '';
  1961. asmcmd : '';
  1962. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  1963. flags : [af_outputbinary,af_smartlink_sections];
  1964. labelprefix : '..@';
  1965. labelmaxlen : -1;
  1966. comment : '; ';
  1967. dollarsign: '$';
  1968. );
  1969. {$endif wasm32}
  1970. initialization
  1971. {$ifdef wasm32}
  1972. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  1973. {$endif wasm32}
  1974. end.