ogwasm.pas 87 KB

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