ogwasm.pas 84 KB

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