ogwasm.pas 84 KB

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