ogwasm.pas 83 KB

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