ogwasm.pas 87 KB

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