ogwasm.pas 83 KB

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