ogcoff.pas 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary coff/PE 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 ogcoff;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. aasmbase,assemble,
  27. { output }
  28. ogbase,
  29. owbase;
  30. const
  31. PE_DATADIR_ENTRIES = 16;
  32. type
  33. tcoffpedatadir = packed record
  34. vaddr : longword;
  35. size : longword;
  36. end;
  37. tcoffheader = packed record
  38. mach : word;
  39. nsects : word;
  40. time : longword;
  41. sympos : longword;
  42. syms : longword;
  43. opthdr : word;
  44. flag : word;
  45. end;
  46. tcoffbigobjheader = packed record
  47. Sig1 : word;
  48. Sig2 : word;
  49. Version : word;
  50. Machine : word;
  51. TimeDateStame : longword;
  52. UUID : array[0..15] of byte;
  53. unused : array[0..3] of longword;
  54. NumberOfSections : longword;
  55. PointerToSymbolTable : longword;
  56. NumberOfSymbols : longword;
  57. end;
  58. tcoffpeoptheader = packed record
  59. Magic : word;
  60. MajorLinkerVersion : byte;
  61. MinorLinkerVersion : byte;
  62. tsize : longword;
  63. dsize : longword;
  64. bsize : longword;
  65. entry : longword;
  66. text_start : longword;
  67. {$ifndef cpu64bitaddr}
  68. data_start : longword;
  69. {$endif cpu64bitaddr}
  70. ImageBase : aword;
  71. SectionAlignment : longword;
  72. FileAlignment : longword;
  73. MajorOperatingSystemVersion : word;
  74. MinorOperatingSystemVersion : word;
  75. MajorImageVersion : word;
  76. MinorImageVersion : word;
  77. MajorSubsystemVersion : word;
  78. MinorSubsystemVersion : word;
  79. Win32Version : longword;
  80. SizeOfImage : longword;
  81. SizeOfHeaders : longword;
  82. CheckSum : longword;
  83. Subsystem : word;
  84. DllCharacteristics : word;
  85. SizeOfStackReserve : aword;
  86. SizeOfStackCommit : aword;
  87. SizeOfHeapReserve : aword;
  88. SizeOfHeapCommit : aword;
  89. LoaderFlags : longword; { This field is obsolete }
  90. NumberOfRvaAndSizes : longword;
  91. DataDirectory : array[0..PE_DATADIR_ENTRIES-1] of tcoffpedatadir;
  92. end;
  93. tcoffsechdr = packed record
  94. name : array[0..7] of char;
  95. vsize : longword;
  96. rvaofs : longword;
  97. datasize : longword;
  98. datapos : longword;
  99. relocpos : longword;
  100. lineno1 : longword;
  101. nrelocs : word;
  102. lineno2 : word;
  103. flags : longword;
  104. end;
  105. TCoffObjSection = class(TObjSection)
  106. private
  107. orgmempos,
  108. coffrelocs,
  109. coffrelocpos : aword;
  110. public
  111. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);override;
  112. procedure writereloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);override;
  113. end;
  114. TCoffObjData = class(TObjData)
  115. private
  116. win32 : boolean;
  117. {$ifdef arm}
  118. eVCobj : boolean;
  119. {$endif arm}
  120. public
  121. constructor createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  122. procedure CreateDebugSections;override;
  123. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  124. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reloctype:TObjRelocationType);override;
  125. end;
  126. TDJCoffObjData = class(TCoffObjData)
  127. constructor create(const n:string);override;
  128. end;
  129. TPECoffObjData = class(TCoffObjData)
  130. constructor create(const n:string);override;
  131. end;
  132. TCoffObjOutput = class(tObjOutput)
  133. private
  134. win32 : boolean;
  135. bigobj : boolean;
  136. symidx : longint;
  137. FCoffSyms,
  138. FCoffStrs : tdynamicarray;
  139. procedure write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  140. procedure section_write_symbol(p:TObject;arg:pointer);
  141. procedure section_write_relocs(p:TObject;arg:pointer);
  142. procedure create_symbols(data:TObjData);
  143. procedure section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  144. procedure section_write_header(p:TObject;arg:pointer);
  145. protected
  146. function writedata(data:TObjData):boolean;override;
  147. public
  148. constructor createcoff(AWriter:TObjectWriter;awin32:boolean);
  149. destructor destroy;override;
  150. end;
  151. TDJCoffObjOutput = class(TCoffObjOutput)
  152. constructor create(AWriter:TObjectWriter);override;
  153. end;
  154. TPECoffObjOutput = class(TCoffObjOutput)
  155. constructor create(AWriter:TObjectWriter);override;
  156. end;
  157. TCoffObjInput = class(tObjInput)
  158. private
  159. FCoffsyms : tdynamicarray;
  160. FCoffStrs : PChar;
  161. FCoffStrSize: longword;
  162. { Convert symidx -> TObjSymbol }
  163. FSymTbl : ^TObjSymbolArray;
  164. { Convert secidx -> TObjSection }
  165. FSecCount : Longint;
  166. FSecTbl : ^TObjSectionArray;
  167. win32 : boolean;
  168. bigobj : boolean;
  169. function GetSection(secidx:longint):TObjSection;
  170. function Read_str(strpos:longword):string;
  171. procedure read_relocs(s:TCoffObjSection);
  172. procedure read_symbols(objdata:TObjData);
  173. procedure ObjSections_read_relocs(p:TObject;arg:pointer);
  174. public
  175. constructor createcoff(awin32:boolean);
  176. destructor destroy;override;
  177. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  178. end;
  179. TDJCoffObjInput = class(TCoffObjInput)
  180. constructor create;override;
  181. end;
  182. TPECoffObjInput = class(TCoffObjInput)
  183. constructor create;override;
  184. end;
  185. TCoffexeoutput = class(texeoutput)
  186. private
  187. FCoffStrs : tdynamicarray;
  188. win32 : boolean;
  189. nsects : word;
  190. nsyms,
  191. sympos : aword;
  192. datapos_offset: longword;
  193. function totalheadersize:longword;
  194. procedure ExeSectionList_pass2_header(p:TObject;arg:pointer);
  195. procedure write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  196. procedure globalsyms_write_symbol(p:TObject;arg:pointer);
  197. procedure ExeSectionList_write_header(p:TObject;arg:pointer);
  198. protected
  199. function writedata:boolean;override;
  200. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList;const aPattern:string);override;
  201. procedure DoRelocationFixup(objsec:TObjSection);override;
  202. public
  203. constructor createcoff(awin32:boolean);
  204. procedure MemPos_Header;override;
  205. procedure DataPos_Header;override;
  206. procedure DataPos_Symbols;override;
  207. end;
  208. TDJCoffexeoutput = class(TCoffexeoutput)
  209. constructor create;override;
  210. procedure MemPos_Header;override;
  211. end;
  212. TPECoffexeoutput = class(TCoffexeoutput)
  213. private
  214. FImports: TFPHashObjectList;
  215. textobjsection,
  216. idata2objsection,
  217. idata4objsection,
  218. idata5objsection,
  219. idata6objsection,
  220. idata7objsection : TObjSection;
  221. FRelocsGenerated : boolean;
  222. procedure GenerateRelocs;
  223. public
  224. constructor create;override;
  225. procedure MarkTargetSpecificSections(WorkList:TFPObjectList);override;
  226. procedure AfterUnusedSectionRemoval;override;
  227. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  228. procedure MemPos_Start;override;
  229. procedure MemPos_ExeSection(const aname:string);override;
  230. end;
  231. TObjSymbolArray = array[0..high(word)] of TObjSymbol;
  232. TObjSectionArray = array[0..high(smallint)] of TObjSection;
  233. TDJCoffAssembler = class(tinternalassembler)
  234. constructor create(info: pasminfo; smart:boolean);override;
  235. end;
  236. TPECoffassembler = class(tinternalassembler)
  237. constructor create(info: pasminfo; smart:boolean);override;
  238. end;
  239. type
  240. Treaddllproc = procedure(const dllname,funcname:string) of object;
  241. const
  242. {$ifdef i386}
  243. COFF_MAGIC = $14c;
  244. COFF_OPT_MAGIC = $10b;
  245. TLSDIR_SIZE = $18;
  246. {$endif i386}
  247. {$ifdef arm}
  248. COFF_OPT_MAGIC = $10b;
  249. TLSDIR_SIZE = $18;
  250. function COFF_MAGIC: word;
  251. const
  252. {$endif arm}
  253. {$ifdef x86_64}
  254. COFF_MAGIC = $8664;
  255. COFF_OPT_MAGIC = $20b;
  256. TLSDIR_SIZE = $28;
  257. {$endif x86_64}
  258. {$ifdef aarch64}
  259. COFF_MAGIC = $AA64;
  260. COFF_OPT_MAGIC = $20b;
  261. TLSDIR_SIZE = $28;
  262. {$endif aarch64}
  263. COFF_BIG_OBJ_MAGIC: array[0..15] of byte = ($C7, $A1, $BA, $D1, $EE, $BA, $A9, $4B, $AF, $20, $FA, $F6, $6A, $A4, $DC, $B8);
  264. COFF_BIG_OBJ_VERSION = 2;
  265. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  266. implementation
  267. uses
  268. {$ifdef win32}
  269. Windows,
  270. {$endif win32}
  271. SysUtils,
  272. cutils,verbose,globals,
  273. cpubase,cpuinfo,
  274. fmodule,
  275. ogmap,
  276. owar,
  277. version
  278. ;
  279. const
  280. COFF_FLAG_NORELOCS = $0001;
  281. COFF_FLAG_EXE = $0002;
  282. COFF_FLAG_NOLINES = $0004;
  283. COFF_FLAG_NOLSYMS = $0008;
  284. COFF_FLAG_AR16WR = $0080; { 16bit little endian }
  285. COFF_FLAG_AR32WR = $0100; { 32bit little endian }
  286. COFF_FLAG_AR32W = $0200; { 32bit big endian }
  287. COFF_FLAG_DLL = $2000;
  288. COFF_SYM_GLOBAL = 2;
  289. COFF_SYM_LOCAL = 3;
  290. COFF_SYM_LABEL = 6;
  291. COFF_SYM_FUNCTION = 101;
  292. COFF_SYM_FILE = 103;
  293. COFF_SYM_SECTION = 104;
  294. COFF_STYP_REG = $0000; { "regular": allocated, relocated, loaded }
  295. COFF_STYP_DSECT = $0001; { "dummy": relocated only }
  296. COFF_STYP_NOLOAD = $0002; { "noload": allocated, relocated, not loaded }
  297. COFF_STYP_GROUP = $0004; { "grouped": formed of input sections }
  298. COFF_STYP_PAD = $0008;
  299. COFF_STYP_COPY = $0010;
  300. COFF_STYP_TEXT = $0020;
  301. COFF_STYP_DATA = $0040;
  302. COFF_STYP_BSS = $0080;
  303. COFF_STYP_INFO = $0200;
  304. COFF_STYP_OVER = $0400;
  305. COFF_STYP_LIB = $0800;
  306. PE_SUBSYSTEM_NATIVE = 1;
  307. PE_SUBSYSTEM_WINDOWS_GUI = 2;
  308. PE_SUBSYSTEM_WINDOWS_CUI = 3;
  309. PE_SUBSYSTEM_WINDOWS_CE_GUI = 9;
  310. PE_FILE_RELOCS_STRIPPED = $0001;
  311. PE_FILE_EXECUTABLE_IMAGE = $0002;
  312. PE_FILE_LINE_NUMS_STRIPPED = $0004;
  313. PE_FILE_LOCAL_SYMS_STRIPPED = $0008;
  314. PE_FILE_AGGRESSIVE_WS_TRIM = $0010;
  315. PE_FILE_LARGE_ADDRESS_AWARE = $0020;
  316. PE_FILE_16BIT_MACHINE = $0040;
  317. PE_FILE_BYTES_REVERSED_LO = $0080;
  318. PE_FILE_32BIT_MACHINE = $0100;
  319. PE_FILE_DEBUG_STRIPPED = $0200;
  320. PE_FILE_REMOVABLE_RUN_FROM_SWAP = $0400;
  321. PE_FILE_NET_RUN_FROM_SWAP = $0800;
  322. PE_FILE_SYSTEM = $1000;
  323. PE_FILE_DLL = $2000;
  324. PE_FILE_UP_SYSTEM_ONLY = $4000;
  325. PE_FILE_BYTES_REVERSED_HI = $8000;
  326. PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA = $0020;
  327. PE_DLLCHARACTERISTICS_DYNAMIC_BASE = $0040;
  328. PE_DLLCHARACTERISTICS_FORCE_INTEGRITY = $0080;
  329. PE_DLLCHARACTERISTICS_NX_COMPAT = $0100;
  330. PE_DLLCHARACTERISTICS_NO_ISOLATION = $0200;
  331. PE_DLLCHARACTERISTICS_NO_SEH = $0400;
  332. PE_DLLCHARACTERISTICS_NO_BIND = $0800;
  333. PE_DLLCHARACTERISTICS_APPCONTAINER = $1000;
  334. PE_DLLCHARACTERISTICS_WDM_DRIVER = $2000;
  335. PE_DLLCHARACTERISTICS_GUARD_CF = $4000;
  336. PE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = $8000;
  337. PE_SCN_CNT_CODE = $00000020; { Section contains code. }
  338. PE_SCN_CNT_INITIALIZED_DATA = $00000040; { Section contains initialized data. }
  339. PE_SCN_CNT_UNINITIALIZED_DATA = $00000080; { Section contains uninitialized data. }
  340. PE_SCN_LNK_OTHER = $00000100; { Reserved. }
  341. PE_SCN_LNK_INFO = $00000200; { Section contains comments or some other type of information. }
  342. PE_SCN_LNK_REMOVE = $00000800; { Section contents will not become part of image. }
  343. PE_SCN_LNK_COMDAT = $00001000; { Section contents comdat. }
  344. PE_SCN_MEM_FARDATA = $00008000;
  345. PE_SCN_MEM_PURGEABLE = $00020000;
  346. PE_SCN_MEM_16BIT = $00020000;
  347. PE_SCN_MEM_LOCKED = $00040000;
  348. PE_SCN_MEM_PRELOAD = $00080000;
  349. PE_SCN_ALIGN_MASK = $00f00000;
  350. PE_SCN_ALIGN_1BYTES = $00100000;
  351. PE_SCN_ALIGN_2BYTES = $00200000;
  352. PE_SCN_ALIGN_4BYTES = $00300000;
  353. PE_SCN_ALIGN_8BYTES = $00400000;
  354. PE_SCN_ALIGN_16BYTES = $00500000; { Default alignment if no others are specified. }
  355. PE_SCN_ALIGN_32BYTES = $00600000;
  356. PE_SCN_ALIGN_64BYTES = $00700000;
  357. PE_SCN_ALIGN_128BYTES = $00800000;
  358. PE_SCN_ALIGN_256BYTES = $00900000;
  359. PE_SCN_ALIGN_512BYTES = $00A00000;
  360. PE_SCN_ALIGN_1024BYTES = $00B00000;
  361. PE_SCN_ALIGN_2048BYTES = $00C00000;
  362. PE_SCN_ALIGN_4096BYTES = $00D00000;
  363. PE_SCN_ALIGN_8192BYTES = $00E00000;
  364. PE_SCN_LNK_NRELOC_OVFL = $01000000; { Section contains extended relocations. }
  365. PE_SCN_MEM_NOT_CACHED = $04000000; { Section is not cachable. }
  366. PE_SCN_MEM_NOT_PAGED = $08000000; { Section is not pageable. }
  367. PE_SCN_MEM_SHARED = $10000000; { Section is shareable. }
  368. PE_SCN_MEM_DISCARDABLE = $02000000;
  369. PE_SCN_MEM_EXECUTE = $20000000;
  370. PE_SCN_MEM_READ = $40000000;
  371. PE_SCN_MEM_WRITE = $80000000;
  372. PE_DATADIR_EDATA = 0;
  373. PE_DATADIR_IDATA = 1;
  374. PE_DATADIR_RSRC = 2;
  375. PE_DATADIR_PDATA = 3;
  376. PE_DATADIR_SECURITY = 4;
  377. PE_DATADIR_RELOC = 5;
  378. PE_DATADIR_DEBUG = 6;
  379. PE_DATADIR_DESCRIPTION = 7;
  380. PE_DATADIR_SPECIAL = 8;
  381. PE_DATADIR_TLS = 9;
  382. PE_DATADIR_LOADCFG = 10;
  383. PE_DATADIR_BOUNDIMPORT = 11;
  384. PE_DATADIR_IMPORTADDRESSTABLE = 12;
  385. PE_DATADIR_DELAYIMPORT = 13;
  386. {$ifdef x86_64}
  387. IMAGE_REL_AMD64_ABSOLUTE = $0000; { Reference is absolute, no relocation is necessary }
  388. IMAGE_REL_AMD64_ADDR64 = $0001; { 64-bit address (VA). }
  389. IMAGE_REL_AMD64_ADDR32 = $0002; { 32-bit address (VA). }
  390. IMAGE_REL_AMD64_ADDR32NB = $0003; { 32-bit address w/o image base (RVA). }
  391. IMAGE_REL_AMD64_REL32 = $0004; { 32-bit relative address from byte following reloc }
  392. IMAGE_REL_AMD64_REL32_1 = $0005; { 32-bit relative address from byte distance 1 from reloc }
  393. IMAGE_REL_AMD64_REL32_2 = $0006; { 32-bit relative address from byte distance 2 from reloc }
  394. IMAGE_REL_AMD64_REL32_3 = $0007; { 32-bit relative address from byte distance 3 from reloc }
  395. IMAGE_REL_AMD64_REL32_4 = $0008; { 32-bit relative address from byte distance 4 from reloc }
  396. IMAGE_REL_AMD64_REL32_5 = $0009; { 32-bit relative address from byte distance 5 from reloc }
  397. IMAGE_REL_AMD64_SECTION = $000A; { Section index }
  398. IMAGE_REL_AMD64_SECREL = $000B; { 32 bit offset from base of section containing target }
  399. IMAGE_REL_AMD64_SECREL7 = $000C; { 7 bit unsigned offset from base of section containing target }
  400. IMAGE_REL_AMD64_TOKEN = $000D; { 32 bit metadata token }
  401. IMAGE_REL_AMD64_SREL32 = $000E; { 32 bit signed span-dependent value emitted into object }
  402. IMAGE_REL_AMD64_PAIR = $000F;
  403. IMAGE_REL_AMD64_SSPAN32 = $0010; { 32 bit signed span-dependent value applied at link time }
  404. { Direct 32 bit sign extended,
  405. win64 mingw GNU compiler
  406. also generates this type
  407. inside coff objects
  408. We assume they are equivalent to
  409. IMAGE_REL_AMD64_ADDR32 PM 2010-11-27 }
  410. R_X86_64_32S = $11;
  411. {$endif x86_64}
  412. {$ifdef arm}
  413. IMAGE_REL_ARM_ABSOLUTE = $0000; { No relocation required }
  414. IMAGE_REL_ARM_ADDR32 = $0001; { 32 bit address }
  415. IMAGE_REL_ARM_ADDR32NB = $0002; { 32 bit address w/o image base }
  416. IMAGE_REL_ARM_BRANCH24 = $0003; { 24 bit offset << 2 & sign ext. }
  417. IMAGE_REL_ARM_BRANCH11 = $0004; { Thumb: 2 11 bit offsets }
  418. IMAGE_REL_ARM_TOKEN = $0005; { clr token }
  419. IMAGE_REL_ARM_GPREL12 = $0006; { GP-relative addressing (ARM) }
  420. IMAGE_REL_ARM_GPREL7 = $0007; { GP-relative addressing (Thumb) }
  421. IMAGE_REL_ARM_BLX24 = $0008;
  422. IMAGE_REL_ARM_BLX11 = $0009;
  423. IMAGE_REL_ARM_SECTION = $000E; { Section table index }
  424. IMAGE_REL_ARM_SECREL = $000F; { Offset within section }
  425. IMAGE_REL_ARM_MOV32A = $0010; { 32-bit VA applied to MOVW+MOVT pair, added to existing imm (ARM) }
  426. IMAGE_REL_ARM_MOV32T = $0011; { 32-bit VA applied to MOVW+MOVT pair, added to existing imm (THUMB) }
  427. IMAGE_REL_ARM_BRANCH20T = $0012; { Thumb: 20 most significant bits of 32 bit B cond instruction }
  428. IMAGE_REL_ARM_BRANCH24T = $0014; { Thumb: 24 most significant bits of 32 bit B uncond instruction }
  429. IMAGE_REL_ARM_BLX23T = $0015; { 23 most significant bits of 32 bit BL/BLX instruction. Transformed to BLX if target is Thumb }
  430. {$endif arm}
  431. {$ifdef i386}
  432. IMAGE_REL_I386_DIR32 = 6;
  433. IMAGE_REL_I386_IMAGEBASE = 7;
  434. IMAGE_REL_I386_SECREL32 = 11;
  435. IMAGE_REL_I386_PCRLONG = 20;
  436. {$endif i386}
  437. {$ifdef aarch64}
  438. IMAGE_REL_ARM64_ABSOLUTE = $0000; // No relocation required
  439. IMAGE_REL_ARM64_ADDR32 = $0001; // 32 bit address. Review! do we need it?
  440. IMAGE_REL_ARM64_ADDR32NB = $0002; // 32 bit address w/o image base (RVA: for Data/PData/XData)
  441. IMAGE_REL_ARM64_BRANCH26 = $0003; // 26 bit offset << 2 & sign ext. for B & BL
  442. IMAGE_REL_ARM64_PAGEBASE_REL21 = $0004; // ADRP
  443. IMAGE_REL_ARM64_REL21 = $0005; // ADR
  444. IMAGE_REL_ARM64_PAGEOFFSET_12A = $0006; // ADD/ADDS (immediate) with zero shift, for page offset
  445. IMAGE_REL_ARM64_PAGEOFFSET_12L = $0007; // LDR (indexed, unsigned immediate), for page offset
  446. IMAGE_REL_ARM64_SECREL = $0008; // Offset within section
  447. IMAGE_REL_ARM64_SECREL_LOW12A = $0009; // ADD/ADDS (immediate) with zero shift, for bit 0:11 of section offset
  448. IMAGE_REL_ARM64_SECREL_HIGH12A = $000A; // ADD/ADDS (immediate) with zero shift, for bit 12:23 of section offset
  449. IMAGE_REL_ARM64_SECREL_LOW12L = $000B; // LDR (indexed, unsigned immediate), for bit 0:11 of section offset
  450. IMAGE_REL_ARM64_TOKEN = $000C;
  451. IMAGE_REL_ARM64_SECTION = $000D; // Section table index
  452. IMAGE_REL_ARM64_ADDR64 = $000E; // 64 bit address
  453. IMAGE_REL_ARM64_BRANCH19 = $000F; // 19 bit offset << 2 & sign ext. for conditional B
  454. {$endif aarch64}
  455. { .reloc section fixup types }
  456. IMAGE_REL_BASED_HIGHLOW = 3; { Applies the delta to the 32-bit field at Offset. }
  457. IMAGE_REL_BASED_DIR64 = 10; { Applies the delta to the 64-bit field at Offset. }
  458. { values for coffsectionrec.select }
  459. IMAGE_COMDAT_SELECT_NODUPLICATES = 1;
  460. IMAGE_COMDAT_SELECT_ANY = 2;
  461. IMAGE_COMDAT_SELECT_SAME_SIZE = 3;
  462. IMAGE_COMDAT_SELECT_EXACT_MATCH = 4;
  463. IMAGE_COMDAT_SELECT_ASSOCIATIVE = 5;
  464. IMAGE_COMDAT_SELECT_LARGEST = 6;
  465. type
  466. coffdjoptheader=packed record
  467. magic : word;
  468. vstamp : word;
  469. tsize : longint;
  470. dsize : longint;
  471. bsize : longint;
  472. entry : longint;
  473. text_start : longint;
  474. data_start : longint;
  475. end;
  476. coffsectionrec=packed record
  477. len : longword;
  478. nrelocs : word;
  479. nlines : word;
  480. checksum: longword;
  481. assoc : word;
  482. select : byte;
  483. empty : array[0..2] of char;
  484. end;
  485. pcoffsectionrec=^coffsectionrec;
  486. coffreloc=packed record
  487. address : longword;
  488. sym : longword;
  489. reloctype : word;
  490. end;
  491. strtableoffset=packed record
  492. Zeroes : longword;
  493. Offset : longword;
  494. end;
  495. coffsymbol=packed record
  496. name : array[0..3] of char; { real is [0..7], which overlaps the strpos ! }
  497. strpos : longword;
  498. value : longword;
  499. section : smallint;
  500. empty : word; { actually type, $20: function, 0: not a function }
  501. typ : byte;
  502. aux : byte;
  503. end;
  504. coffbigobjsymbol=packed record
  505. Name : record
  506. case boolean of
  507. True: (ShortName : array[0..7] of char);
  508. False: (Offset : strtableoffset)
  509. end;
  510. Value : longword;
  511. SectionNumber : longword;
  512. _Type : word;
  513. StorageClass : byte;
  514. NumberOfAuxSymbols : byte;
  515. end;
  516. { This is defined in rtl/win/sysos.inc source }
  517. tlsdirectory=packed record
  518. data_start, data_end : PUInt;
  519. index_pointer, callbacks_pointer : PUInt;
  520. zero_fill_size : dword;
  521. flags : dword;
  522. end;
  523. const
  524. SymbolMaxGrow = 200*sizeof(coffsymbol);
  525. StrsMaxGrow = 8192;
  526. coffsecnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  527. '.text','.data','.rdata','.rdata','.bss','.tls',
  528. '.pdata',{pdata}
  529. '.text', {stub}
  530. '.data',
  531. '.data',
  532. '.data',
  533. '.data',
  534. '.stab','.stabstr',
  535. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  536. '.eh_frame',
  537. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  538. '.fpc',
  539. '',
  540. '.init',
  541. '.fini',
  542. '.objc_class',
  543. '.objc_meta_class',
  544. '.objc_cat_cls_meth',
  545. '.objc_cat_inst_meth',
  546. '.objc_protocol',
  547. '.objc_string_object',
  548. '.objc_cls_meth',
  549. '.objc_inst_meth',
  550. '.objc_cls_refs',
  551. '.objc_message_refs',
  552. '.objc_symbols',
  553. '.objc_category',
  554. '.objc_class_vars',
  555. '.objc_instance_vars',
  556. '.objc_module_info',
  557. '.objc_class_names',
  558. '.objc_meth_var_types',
  559. '.objc_meth_var_names',
  560. '.objc_selector_strs',
  561. '.objc_protocol_ext',
  562. '.objc_class_ext',
  563. '.objc_property',
  564. '.objc_image_info',
  565. '.objc_cstring_object',
  566. '.objc_sel_fixup',
  567. '__DATA,__objc_data',
  568. '__DATA,__objc_const',
  569. '.objc_superrefs',
  570. '__DATA, __datacoal_nt,coalesced',
  571. '.objc_classlist',
  572. '.objc_nlclasslist',
  573. '.objc_catlist',
  574. '.obcj_nlcatlist',
  575. '.objc_protolist',
  576. '.stack',
  577. '.heap',
  578. '.gcc_except_table',
  579. '.ARM.attributes'
  580. );
  581. const go32v2stub : array[0..2047] of byte=(
  582. $4D,$5A,$00,$00,$04,$00,$00,$00,$20,$00,$27,$00,$FF,$FF,$00,
  583. $00,$60,$07,$00,$00,$54,$00,$00,$00,$00,$00,$00,$00,$0D,$0A,
  584. $73,$74,$75,$62,$2E,$68,$20,$67,$65,$6E,$65,$72,$61,$74,$65,
  585. $64,$20,$66,$72,$6F,$6D,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,
  586. $20,$62,$79,$20,$64,$6A,$61,$73,$6D,$2C,$20,$6F,$6E,$20,$54,
  587. $68,$75,$20,$44,$65,$63,$20,$20,$39,$20,$31,$30,$3A,$35,$39,
  588. $3A,$33,$31,$20,$31,$39,$39,$39,$0D,$0A,$54,$68,$65,$20,$53,
  589. $54,$55,$42,$2E,$45,$58,$45,$20,$73,$74,$75,$62,$20,$6C,$6F,
  590. $61,$64,$65,$72,$20,$69,$73,$20,$43,$6F,$70,$79,$72,$69,$67,
  591. $68,$74,$20,$28,$43,$29,$20,$31,$39,$39,$33,$2D,$31,$39,$39,
  592. $35,$20,$44,$4A,$20,$44,$65,$6C,$6F,$72,$69,$65,$2E,$20,$0D,
  593. $0A,$50,$65,$72,$6D,$69,$73,$73,$69,$6F,$6E,$20,$67,$72,$61,
  594. $6E,$74,$65,$64,$20,$74,$6F,$20,$75,$73,$65,$20,$66,$6F,$72,
  595. $20,$61,$6E,$79,$20,$70,$75,$72,$70,$6F,$73,$65,$20,$70,$72,
  596. $6F,$76,$69,$64,$65,$64,$20,$74,$68,$69,$73,$20,$63,$6F,$70,
  597. $79,$72,$69,$67,$68,$74,$20,$0D,$0A,$72,$65,$6D,$61,$69,$6E,
  598. $73,$20,$70,$72,$65,$73,$65,$6E,$74,$20,$61,$6E,$64,$20,$75,
  599. $6E,$6D,$6F,$64,$69,$66,$69,$65,$64,$2E,$20,$0D,$0A,$54,$68,
  600. $69,$73,$20,$6F,$6E,$6C,$79,$20,$61,$70,$70,$6C,$69,$65,$73,
  601. $20,$74,$6F,$20,$74,$68,$65,$20,$73,$74,$75,$62,$2C,$20,$61,
  602. $6E,$64,$20,$6E,$6F,$74,$20,$6E,$65,$63,$65,$73,$73,$61,$72,
  603. $69,$6C,$79,$20,$74,$68,$65,$20,$77,$68,$6F,$6C,$65,$20,$70,
  604. $72,$6F,$67,$72,$61,$6D,$2E,$0A,$0D,$0A,$24,$49,$64,$3A,$20,
  605. $73,$74,$75,$62,$2E,$61,$73,$6D,$20,$62,$75,$69,$6C,$74,$20,
  606. $31,$32,$2F,$30,$39,$2F,$39,$39,$20,$31,$30,$3A,$35,$39,$3A,
  607. $33,$31,$20,$62,$79,$20,$64,$6A,$61,$73,$6D,$20,$24,$0A,$0D,
  608. $0A,$40,$28,$23,$29,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,$20,
  609. $62,$75,$69,$6C,$74,$20,$31,$32,$2F,$30,$39,$2F,$39,$39,$20,
  610. $31,$30,$3A,$35,$39,$3A,$33,$31,$20,$62,$79,$20,$64,$6A,$61,
  611. $73,$6D,$0A,$0D,$0A,$1A,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  612. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  613. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  614. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  615. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  616. $00,$00,$67,$6F,$33,$32,$73,$74,$75,$62,$2C,$20,$76,$20,$32,
  617. $2E,$30,$32,$54,$00,$00,$00,$00,$00,$08,$00,$00,$00,$00,$00,
  618. $00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  619. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  620. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$43,$57,$53,$44,$50,
  621. $4D,$49,$2E,$45,$58,$45,$00,$00,$00,$00,$00,$0E,$1F,$8C,$1E,
  622. $24,$00,$8C,$06,$60,$07,$FC,$B4,$30,$CD,$21,$3C,$03,$73,$08,
  623. $B0,$6D,$BA,$A7,$05,$E9,$D4,$03,$A2,$69,$08,$BE,$20,$00,$8B,
  624. $04,$09,$C0,$75,$02,$B4,$FE,$BB,$70,$08,$39,$C3,$73,$02,$89,
  625. $C3,$89,$1C,$FE,$C7,$B9,$04,$FF,$D3,$EB,$B4,$4A,$CD,$21,$73,
  626. $08,$D3,$E3,$FE,$CF,$89,$1C,$EB,$D8,$26,$8E,$06,$2C,$00,$31,
  627. $FF,$30,$C0,$A9,$F2,$AE,$26,$81,$3D,$50,$41,$75,$15,$AF,$26,
  628. $81,$3D,$54,$48,$75,$0D,$AF,$26,$80,$3D,$3D,$75,$06,$47,$89,
  629. $3E,$8C,$04,$4F,$AE,$75,$DF,$AF,$B4,$3E,$BB,$13,$00,$CD,$21,
  630. $B4,$3E,$BB,$12,$00,$CD,$21,$06,$57,$31,$C9,$74,$12,$B0,$6E,
  631. $BA,$7E,$05,$E9,$5E,$03,$09,$C9,$75,$F4,$41,$E8,$A1,$03,$72,
  632. $EE,$B8,$87,$16,$CD,$2F,$09,$C0,$75,$ED,$80,$E3,$01,$74,$E8,
  633. $89,$3E,$00,$06,$8C,$06,$02,$06,$89,$36,$04,$06,$5F,$07,$E8,
  634. $D3,$02,$89,$3E,$2A,$00,$89,$36,$62,$07,$80,$3E,$2C,$00,$00,
  635. $74,$23,$B9,$08,$00,$BF,$2C,$00,$8A,$05,$47,$08,$C0,$74,$05,
  636. $88,$07,$43,$E2,$F4,$66,$C7,$07,$2E,$45,$58,$45,$83,$C3,$04,
  637. $C6,$07,$00,$89,$1E,$62,$07,$B8,$00,$3D,$BA,$64,$07,$CD,$21,
  638. $0F,$82,$B3,$02,$A3,$06,$06,$89,$C3,$B9,$06,$00,$BA,$B5,$07,
  639. $B4,$3F,$CD,$21,$31,$D2,$31,$C9,$A1,$B5,$07,$3D,$4C,$01,$74,
  640. $1B,$3D,$4D,$5A,$0F,$85,$98,$02,$8B,$16,$B9,$07,$C1,$E2,$09,
  641. $8B,$1E,$B7,$07,$09,$DB,$74,$05,$80,$EE,$02,$01,$DA,$89,$16,
  642. $BB,$07,$89,$0E,$BD,$07,$B8,$00,$42,$8B,$1E,$06,$06,$CD,$21,
  643. $B9,$A8,$00,$BA,$BF,$07,$B4,$3F,$CD,$21,$3D,$A8,$00,$75,$06,
  644. $81,$3E,$BF,$07,$4C,$01,$0F,$85,$61,$02,$66,$A1,$E3,$07,$66,
  645. $A3,$10,$06,$66,$8B,$0E,$BB,$07,$66,$A1,$03,$08,$66,$01,$C8,
  646. $66,$A3,$08,$06,$66,$A1,$2B,$08,$66,$01,$C8,$66,$A3,$0C,$06,
  647. $66,$8B,$1E,$4B,$08,$66,$A1,$4F,$08,$66,$01,$C3,$66,$B8,$01,
  648. $00,$01,$00,$66,$39,$C3,$73,$03,$66,$89,$C3,$66,$81,$C3,$FF,
  649. $FF,$00,$00,$31,$DB,$66,$89,$1E,$1C,$00,$E8,$F5,$02,$8B,$1E,
  650. $04,$06,$09,$DB,$74,$0A,$B4,$48,$CD,$21,$0F,$82,$15,$02,$8E,
  651. $C0,$E8,$08,$03,$B8,$01,$00,$FF,$1E,$00,$06,$0F,$82,$0F,$02,
  652. $8C,$06,$26,$00,$8C,$0E,$28,$00,$8C,$D8,$A3,$22,$00,$8E,$C0,
  653. $31,$C0,$B9,$01,$00,$CD,$31,$72,$07,$A3,$14,$06,$31,$C0,$CD,
  654. $31,$0F,$82,$F3,$01,$A3,$16,$06,$66,$8B,$0E,$1C,$00,$B8,$01,
  655. $05,$8B,$1E,$1E,$00,$CD,$31,$0F,$82,$E5,$01,$89,$1E,$1A,$06,
  656. $89,$0E,$18,$06,$89,$36,$1A,$00,$89,$3E,$18,$00,$B8,$07,$00,
  657. $8B,$1E,$14,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,$CD,$31,$B8,
  658. $09,$00,$8C,$C9,$83,$E1,$03,$C1,$E1,$05,$51,$81,$C9,$9B,$C0,
  659. $CD,$31,$B8,$08,$00,$8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,
  660. $B8,$07,$00,$8B,$1E,$16,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,
  661. $CD,$31,$B8,$09,$00,$59,$81,$C9,$93,$C0,$CD,$31,$B8,$08,$00,
  662. $8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,$B8,$00,$01,$BB,$00,
  663. $0F,$CD,$31,$73,$10,$3D,$08,$00,$0F,$85,$73,$01,$B8,$00,$01,
  664. $CD,$31,$0F,$82,$6A,$01,$A3,$1C,$06,$89,$16,$1E,$06,$C1,$E3,
  665. $04,$89,$1E,$20,$06,$66,$8B,$36,$08,$06,$66,$8B,$3E,$FB,$07,
  666. $66,$8B,$0E,$FF,$07,$E8,$49,$00,$66,$8B,$36,$0C,$06,$66,$8B,
  667. $3E,$23,$08,$66,$8B,$0E,$27,$08,$E8,$37,$00,$8E,$06,$16,$06,
  668. $66,$8B,$3E,$4B,$08,$66,$8B,$0E,$4F,$08,$66,$31,$C0,$66,$C1,
  669. $E9,$02,$67,$F3,$66,$AB,$B4,$3E,$8B,$1E,$06,$06,$CD,$21,$B8,
  670. $01,$01,$8B,$16,$1E,$06,$CD,$31,$1E,$0F,$A1,$8E,$1E,$16,$06,
  671. $66,$64,$FF,$2E,$10,$06,$66,$89,$F0,$66,$25,$FF,$01,$00,$00,
  672. $66,$01,$C1,$29,$C6,$66,$29,$C7,$66,$89,$0E,$26,$06,$66,$89,
  673. $3E,$22,$06,$E8,$0F,$01,$89,$36,$3E,$06,$66,$C1,$EE,$10,$89,
  674. $36,$42,$06,$8B,$1E,$06,$06,$89,$1E,$3A,$06,$C7,$06,$46,$06,
  675. $00,$42,$E8,$03,$01,$A1,$1C,$06,$A3,$4E,$06,$C7,$06,$3E,$06,
  676. $00,$00,$C6,$06,$47,$06,$3F,$A1,$28,$06,$09,$C0,$75,$09,$A1,
  677. $26,$06,$3B,$06,$20,$06,$76,$03,$A1,$20,$06,$A3,$42,$06,$E8,
  678. $D9,$00,$66,$31,$C9,$8B,$0E,$46,$06,$66,$8B,$3E,$22,$06,$66,
  679. $01,$0E,$22,$06,$66,$29,$0E,$26,$06,$66,$31,$F6,$C1,$E9,$02,
  680. $1E,$06,$8E,$06,$16,$06,$8E,$1E,$1E,$06,$67,$F3,$66,$A5,$07,
  681. $1F,$66,$03,$0E,$26,$06,$75,$AF,$C3,$3C,$3A,$74,$06,$3C,$2F,
  682. $74,$02,$3C,$5C,$C3,$BE,$64,$07,$89,$F3,$26,$8A,$05,$47,$88,
  683. $04,$38,$E0,$74,$0E,$08,$C0,$74,$0A,$46,$E8,$DE,$FF,$75,$EC,
  684. $89,$F3,$74,$E8,$C3,$B0,$66,$BA,$48,$05,$EB,$0C,$B0,$67,$BA,
  685. $55,$05,$EB,$05,$B0,$68,$BA,$5F,$05,$52,$8B,$1E,$62,$07,$C6,
  686. $07,$24,$BB,$64,$07,$EB,$28,$E8,$F5,$00,$B0,$69,$BA,$99,$05,
  687. $EB,$1A,$B0,$6A,$BA,$B2,$05,$EB,$13,$B0,$6B,$BA,$C4,$05,$EB,
  688. $0C,$B0,$6C,$BA,$D6,$05,$EB,$05,$B0,$69,$BA,$99,$05,$52,$BB,
  689. $3B,$05,$E8,$15,$00,$5B,$E8,$11,$00,$BB,$67,$04,$E8,$0B,$00,
  690. $B4,$4C,$CD,$21,$43,$50,$B4,$02,$CD,$21,$58,$8A,$17,$80,$FA,
  691. $24,$75,$F2,$C3,$0D,$0A,$24,$50,$51,$57,$31,$C0,$BF,$2A,$06,
  692. $B9,$19,$00,$F3,$AB,$5F,$59,$58,$C3,$B8,$00,$03,$BB,$21,$00,
  693. $31,$C9,$66,$BF,$2A,$06,$00,$00,$CD,$31,$C3,$00,$00,$30,$E4,
  694. $E8,$4E,$FF,$89,$DE,$8B,$3E,$8C,$04,$EB,$17,$B4,$3B,$E8,$41,
  695. $FF,$81,$FE,$64,$07,$74,$12,$8A,$44,$FF,$E8,$2A,$FF,$74,$04,
  696. $C6,$04,$5C,$46,$E8,$03,$00,$72,$E4,$C3,$E8,$34,$00,$BB,$44,
  697. $00,$8A,$07,$88,$04,$43,$46,$08,$C0,$75,$F6,$06,$57,$1E,$07,
  698. $E8,$9B,$FF,$BB,$2A,$06,$8C,$5F,$04,$89,$5F,$02,$BA,$64,$07,
  699. $B8,$00,$4B,$CD,$21,$5F,$07,$72,$09,$B4,$4D,$CD,$21,$2D,$00,
  700. $03,$F7,$D8,$EB,$28,$80,$3E,$69,$08,$05,$72,$20,$B8,$00,$58,
  701. $CD,$21,$A2,$67,$08,$B8,$02,$58,$CD,$21,$A2,$68,$08,$B8,$01,
  702. $58,$BB,$80,$00,$CD,$21,$B8,$03,$58,$BB,$01,$00,$CD,$21,$C3,
  703. $9C,$80,$3E,$69,$08,$05,$72,$1A,$50,$53,$B8,$03,$58,$8A,$1E,
  704. $68,$08,$30,$FF,$CD,$21,$B8,$01,$58,$8A,$1E,$67,$08,$30,$FF,
  705. $CD,$21,$5B,$58,$9D,$C3,$4C,$6F,$61,$64,$20,$65,$72,$72,$6F,
  706. $72,$3A,$20,$24,$3A,$20,$63,$61,$6E,$27,$74,$20,$6F,$70,$65,
  707. $6E,$24,$3A,$20,$6E,$6F,$74,$20,$45,$58,$45,$24,$3A,$20,$6E,
  708. $6F,$74,$20,$43,$4F,$46,$46,$20,$28,$43,$68,$65,$63,$6B,$20,
  709. $66,$6F,$72,$20,$76,$69,$72,$75,$73,$65,$73,$29,$24,$6E,$6F,
  710. $20,$44,$50,$4D,$49,$20,$2D,$20,$47,$65,$74,$20,$63,$73,$64,
  711. $70,$6D,$69,$2A,$62,$2E,$7A,$69,$70,$24,$6E,$6F,$20,$44,$4F,
  712. $53,$20,$6D,$65,$6D,$6F,$72,$79,$24,$6E,$65,$65,$64,$20,$44,
  713. $4F,$53,$20,$33,$24,$63,$61,$6E,$27,$74,$20,$73,$77,$69,$74,
  714. $63,$68,$20,$6D,$6F,$64,$65,$24,$6E,$6F,$20,$44,$50,$4D,$49,
  715. $20,$73,$65,$6C,$65,$63,$74,$6F,$72,$73,$24,$6E,$6F,$20,$44,
  716. $50,$4D,$49,$20,$6D,$65,$6D,$6F,$72,$79,$24,$90,$90,$90,$90,
  717. $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,
  718. $90,$90,$90,$90,$90,$90,$90,$90);
  719. const win32stub : array[0..127] of byte=(
  720. $4D,$5A,$90,$00,$03,$00,$00,$00,$04,$00,$00,$00,$FF,$FF,$00,$00,
  721. $B8,$00,$00,$00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,
  722. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  723. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$80,$00,$00,$00,
  724. $0E,$1F,$BA,$0E,$00,$B4,$09,$CD,$21,$B8,$01,$4C,$CD,$21,$54,$68,
  725. $69,$73,$20,$70,$72,$6F,$67,$72,$61,$6D,$20,$63,$61,$6E,$6E,$6F,
  726. $74,$20,$62,$65,$20,$72,$75,$6E,$20,$69,$6E,$20,$44,$4F,$53,$20,
  727. $6D,$6F,$64,$65,$2E,$0D,$0D,$0A,$24,$00,$00,$00,$00,$00,$00,$00);
  728. const pemagic : array[0..3] of byte = (
  729. $50,$45,$00,$00);
  730. {****************************************************************************
  731. Helpers
  732. ****************************************************************************}
  733. function djencodesechdrflags(aoptions:TObjSectionOptions):longword;
  734. begin
  735. if (oso_load in aoptions) then
  736. begin
  737. if oso_executable in aoptions then
  738. result:=COFF_STYP_TEXT
  739. else if not(oso_data in aoptions) then
  740. result:=COFF_STYP_BSS
  741. else
  742. result:=COFF_STYP_DATA;
  743. end
  744. else if oso_debug in aoptions then
  745. result:=COFF_STYP_INFO
  746. else
  747. result:=COFF_STYP_REG;
  748. end;
  749. function djdecodesechdrflags(const aname:string;flags:longword):TObjSectionOptions;
  750. begin
  751. result:=[];
  752. if flags and COFF_STYP_TEXT<>0 then
  753. result:=[oso_data,oso_load,oso_executable]
  754. else if flags and COFF_STYP_BSS<>0 then
  755. result:=[oso_load]
  756. else if flags and COFF_STYP_DATA<>0 then
  757. result:=[oso_data,oso_load]
  758. else if flags and COFF_STYP_INFO<>0 then
  759. result:=[oso_data,oso_debug]
  760. else
  761. result:=[oso_data]
  762. end;
  763. function peencodesechdrflags(aoptions:TObjSectionOptions;aalign:longint):longword;
  764. begin
  765. if oso_executable in aoptions then
  766. result:=PE_SCN_CNT_CODE or PE_SCN_MEM_EXECUTE
  767. else
  768. begin
  769. if (oso_data in aoptions) then
  770. result:=PE_SCN_CNT_INITIALIZED_DATA
  771. else
  772. result:=PE_SCN_CNT_UNINITIALIZED_DATA;
  773. end;
  774. if oso_write in aoptions then
  775. result:=result or PE_SCN_MEM_WRITE or PE_SCN_MEM_READ
  776. else
  777. result:=result or PE_SCN_MEM_READ;
  778. if not (oso_load in aoptions) then
  779. result:=result or PE_SCN_MEM_DISCARDABLE;
  780. case aalign of
  781. 1 : result:=result or PE_SCN_ALIGN_1BYTES;
  782. 2 : result:=result or PE_SCN_ALIGN_2BYTES;
  783. 4 : result:=result or PE_SCN_ALIGN_4BYTES;
  784. 8 : result:=result or PE_SCN_ALIGN_8BYTES;
  785. 16 : result:=result or PE_SCN_ALIGN_16BYTES;
  786. 32 : result:=result or PE_SCN_ALIGN_32BYTES;
  787. 64 : result:=result or PE_SCN_ALIGN_64BYTES;
  788. 128 : result:=result or PE_SCN_ALIGN_128BYTES;
  789. 256 : result:=result or PE_SCN_ALIGN_256BYTES;
  790. 512 : result:=result or PE_SCN_ALIGN_512BYTES;
  791. 1024 : result:=result or PE_SCN_ALIGN_1024BYTES;
  792. 2048 : result:=result or PE_SCN_ALIGN_2048BYTES;
  793. 4096 : result:=result or PE_SCN_ALIGN_4096BYTES;
  794. 8192 : result:=result or PE_SCN_ALIGN_8192BYTES;
  795. else result:=result or PE_SCN_ALIGN_16BYTES;
  796. end;
  797. end;
  798. procedure pedecodesechdrflags(const aname:string;flags:longword;out aoptions:TObjSectionOptions;out aalign:longint);
  799. var
  800. alignflag : longword;
  801. begin
  802. aoptions:=[];
  803. if flags and PE_SCN_CNT_CODE<>0 then
  804. include(aoptions,oso_executable);
  805. if flags and PE_SCN_MEM_DISCARDABLE<>0 then
  806. include(aoptions,oso_debug);
  807. if flags and PE_SCN_CNT_UNINITIALIZED_DATA=0 then
  808. include(aoptions,oso_data);
  809. if (flags and (PE_SCN_LNK_REMOVE or PE_SCN_MEM_DISCARDABLE)=0) then
  810. include(aoptions,oso_load);
  811. if flags and PE_SCN_LNK_COMDAT<>0 then
  812. include(aoptions,oso_comdat);
  813. { read/write }
  814. if flags and PE_SCN_MEM_WRITE<>0 then
  815. include(aoptions,oso_write);
  816. { alignment }
  817. alignflag:=flags and PE_SCN_ALIGN_MASK;
  818. if alignflag=PE_SCN_ALIGN_8192BYTES then
  819. aalign:=8192
  820. else if alignflag=PE_SCN_ALIGN_4096BYTES then
  821. aalign:=4096
  822. else if alignflag=PE_SCN_ALIGN_2048BYTES then
  823. aalign:=2048
  824. else if alignflag=PE_SCN_ALIGN_1024BYTES then
  825. aalign:=1024
  826. else if alignflag=PE_SCN_ALIGN_512BYTES then
  827. aalign:=512
  828. else if alignflag=PE_SCN_ALIGN_256BYTES then
  829. aalign:=256
  830. else if alignflag=PE_SCN_ALIGN_128BYTES then
  831. aalign:=128
  832. else if alignflag=PE_SCN_ALIGN_64BYTES then
  833. aalign:=64
  834. else if alignflag=PE_SCN_ALIGN_32BYTES then
  835. aalign:=32
  836. else if alignflag=PE_SCN_ALIGN_16BYTES then
  837. aalign:=16
  838. else if alignflag=PE_SCN_ALIGN_8BYTES then
  839. aalign:=8
  840. else if alignflag=PE_SCN_ALIGN_4BYTES then
  841. aalign:=4
  842. else if alignflag=PE_SCN_ALIGN_2BYTES then
  843. aalign:=2
  844. else if alignflag=PE_SCN_ALIGN_1BYTES then
  845. aalign:=1
  846. else if alignflag=0 then
  847. aalign:=0
  848. else
  849. Internalerror(2009050401);
  850. end;
  851. function encodeBase64(p:aword):string;
  852. const
  853. alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
  854. 'abcdefghijklmnopqrstuvwxyz' +
  855. '0123456789+/';
  856. var
  857. i,
  858. idx,
  859. rem : longint;
  860. begin
  861. setlength(result,6);
  862. idx := 6;
  863. for i:=0 to 5 do
  864. begin
  865. rem:=p mod 64;
  866. p:=p div 64;
  867. result[idx]:=alphabet[rem+1];
  868. dec(idx);
  869. end;
  870. if p<>0 then
  871. internalerror(2020091601);
  872. end;
  873. function decodeBase64(const s:string;out p:longint):boolean;
  874. var
  875. i : longint;
  876. v : aword;
  877. begin
  878. if length(s)>6 then
  879. exit(false);
  880. p:=0;
  881. for i:=1 to length(s) do
  882. begin
  883. v:=0;
  884. if (s[i]>='A') and (s[i]<='Z') then // 0..25
  885. v:=Ord(s[i])-Ord('A')
  886. else if (s[i]>='a') and (s[i]<='z') then // 26..51
  887. v:=Ord(s[i])-Ord('a')+26
  888. else if (s[i]>='0') and (s[i]<='9') then // 52..61
  889. v:=Ord(s[i])-Ord('0')+52
  890. else if s[i]='+' then // 62
  891. v:=62
  892. else if s[i]='/' then // 63
  893. v:=63
  894. else
  895. exit(false);
  896. p:=(p*64)+v;
  897. end;
  898. result:=true;
  899. end;
  900. {****************************************************************************
  901. TCoffObjSection
  902. ****************************************************************************}
  903. constructor TCoffObjSection.create(AList:TFPHashObjectList;const aname:string;aalign:longint;aoptions:TObjSectionOptions);
  904. begin
  905. inherited create(AList,aname,aalign,aoptions);
  906. end;
  907. procedure TCoffObjSection.writereloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  908. begin
  909. AddSectionReloc(size,aTarget,reltype);
  910. write(offset,len);
  911. end;
  912. { We don't want overflow nor range checks here,
  913. wrapping is accepted in the address computation below }
  914. {$r-}
  915. {$q-}
  916. procedure TCoffExeOutput.DoRelocationFixup(objsec:TObjSection);
  917. var
  918. i,zero,address_size : longint;
  919. objreloc : TObjRelocation;
  920. address,
  921. relocval : aint;
  922. {$if defined(arm) or defined(aarch64)}
  923. addend : aint;
  924. {$endif arm or aarch64}
  925. relocsec : TObjSection;
  926. {$ifdef cpu64bitaddr}
  927. s : string;
  928. {$endif cpu64bitaddr}
  929. data : TDynamicArray;
  930. begin
  931. data:=objsec.data;
  932. for i:=0 to objsec.ObjRelocations.Count-1 do
  933. begin
  934. objreloc:=TObjRelocation(objsec.ObjRelocations[i]);
  935. address_size:=4;
  936. case objreloc.typ of
  937. RELOC_NONE:
  938. continue;
  939. RELOC_ZERO:
  940. begin
  941. data.Seek(objreloc.dataoffset);
  942. zero:=0;
  943. data.Write(zero,4);
  944. continue;
  945. end;
  946. {$ifdef cpu64bitaddr}
  947. RELOC_ABSOLUTE:
  948. address_size:=8;
  949. {$endif cpu64bitaddr}
  950. else
  951. ;
  952. end;
  953. address:=0;
  954. data.Seek(objreloc.dataoffset);
  955. data.Read(address,address_size);
  956. if assigned(objreloc.symbol) then
  957. begin
  958. relocsec:=objreloc.symbol.objsection;
  959. relocval:=objreloc.symbol.address;
  960. end
  961. else
  962. if assigned(objreloc.objsection) then
  963. begin
  964. relocsec:=objreloc.objsection;
  965. relocval:=objreloc.objsection.mempos
  966. end
  967. else
  968. internalerror(200205183);
  969. { Only debug sections are allowed to have relocs pointing to unused sections }
  970. if not relocsec.used and not (oso_debug in objsec.secoptions) then
  971. internalerror(200603061);
  972. if relocsec.used then
  973. case objreloc.typ of
  974. RELOC_RELATIVE :
  975. begin
  976. address:=address-objsec.mempos+relocval;
  977. if win32 then
  978. dec(address,objreloc.dataoffset+4);
  979. end;
  980. RELOC_RVA:
  981. begin
  982. { fixup address when the symbol was known in defined object }
  983. if (relocsec.objdata=objsec.objdata) then
  984. dec(address,TCoffObjSection(relocsec).orgmempos);
  985. {$ifdef arm}
  986. if (relocsec.objdata=objsec.objdata) and not TCoffObjData(objsec.objdata).eVCobj then
  987. inc(address, relocsec.MemPos)
  988. else
  989. {$endif arm}
  990. inc(address,relocval);
  991. end;
  992. RELOC_SECREL32 :
  993. begin
  994. { fixup address when the symbol was known in defined object }
  995. if (relocsec.objdata=objsec.objdata) then
  996. dec(address,relocsec.ExeSection.MemPos);
  997. inc(address,relocval);
  998. end;
  999. {$ifdef arm}
  1000. RELOC_RELATIVE_24,
  1001. RELOC_RELATIVE_CALL:
  1002. begin
  1003. addend:=sarlongint(((address and $ffffff) shl 8),6); // Sign-extend while shifting left twice
  1004. relocval:=longint(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  1005. address:=(address and $ff000000) or (relocval and $ffffff);
  1006. relocval:=relocval shr 24;
  1007. if (relocval<>$3f) and (relocval<>0) then
  1008. internalerror(200606085); { offset overflow }
  1009. end;
  1010. RELOC_RELATIVE_24_THUMB,
  1011. RELOC_RELATIVE_CALL_THUMB:
  1012. begin
  1013. addend:=sarlongint(((address and $ffffff) shl 8),6); // Sign-extend while shifting left twice, the assembler never sets the H bit
  1014. relocval:=longint(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 1;
  1015. address:=(address and $ff000000) or ((relocval shr 1) and $ffffff) or ((relocval and 1) shl 24);
  1016. relocval:=relocval shr 25;
  1017. if (relocval<>$3f) and (relocval<>0) then
  1018. internalerror(2006060801); { offset overflow }
  1019. end;
  1020. {$endif arm}
  1021. {$ifdef aarch64}
  1022. RELOC_RELATIVE_26:
  1023. begin
  1024. addend:=sarint64(((address and $3ffffff) shl 38),36); // Sign-extend while shifting left twice
  1025. relocval:=int64(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  1026. address:=(address and $fc000000) or (relocval and $3ffffff);
  1027. relocval:=relocval shr 58;
  1028. if (relocval<>$f) and (relocval<>0) then
  1029. internalerror(2020032202); { offset overflow }
  1030. end;
  1031. RELOC_RELATIVE_19:
  1032. begin
  1033. addend:=sarint64(((address and $7ffe0) shl 45),43); // Sign-extend while shifting left twice
  1034. relocval:=int64(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  1035. address:=(address and $fff80000) or (relocval and $7ffff);
  1036. relocval:=relocval shr 51;
  1037. if (relocval<>$3f) and (relocval<>0) then
  1038. internalerror(2020032203); { offset overflow }
  1039. end;
  1040. RELOC_ADR_PREL_LO21:
  1041. begin
  1042. addend:=((address shr 29) and $3) or (((address shr 5) and $7ffff) shl 2);
  1043. { sign extend the value if necessary }
  1044. if (addend and (1 shl 21)) <> 0 then
  1045. addend:=addend or sarint64(1 shl 63,12);
  1046. relocval:=relocval and $1fffff;
  1047. relocval:=int64(relocval-objsec.mempos-objreloc.dataoffset+addend);
  1048. address:=address and ($3 shl 29) and ($7ffff shl 5);
  1049. address:=address or ((relocval and $3) shl 29) or (((relocval shr 2) and $7ffff) shl 5);
  1050. end;
  1051. RELOC_ADR_PREL_PG_HI21:
  1052. begin
  1053. addend:=((address shr 29) and $3) or (((address shr 5) and $7ffff) shl 2);
  1054. { sign extend the value if necessary }
  1055. if (addend and (1 shl 21)) <> 0 then
  1056. addend:=addend or sarint64(1 shl 63, 12);
  1057. relocval:=relocval shr 12;
  1058. relocval:=int64((relocval-(objsec.mempos+objreloc.dataoffset) shr 12)+addend);
  1059. address:=address and not (($3 shl 29) or ($7ffff shl 5));
  1060. address:=address or ((relocval and $3) shl 29) or (((relocval shr 2) and $7ffff) shl 5);
  1061. end;
  1062. RELOC_LDST8_ABS_LO12,
  1063. RELOC_ADD_ABS_LO12:
  1064. begin
  1065. addend:=(address shr 10) and $fff;
  1066. relocval:=(relocval + addend) and $fff;
  1067. address:=address and not ($fff shl 10);
  1068. address:=address or (relocval shl 10);
  1069. end;
  1070. {$endif aarch64}
  1071. {$ifdef x86_64}
  1072. { 64 bit coff only }
  1073. RELOC_RELATIVE_1:
  1074. begin
  1075. address:=address-objsec.mempos+relocval;
  1076. dec(address,objreloc.dataoffset+5);
  1077. end;
  1078. RELOC_RELATIVE_2:
  1079. begin
  1080. address:=address-objsec.mempos+relocval;
  1081. dec(address,objreloc.dataoffset+6);
  1082. end;
  1083. RELOC_RELATIVE_3:
  1084. begin
  1085. address:=address-objsec.mempos+relocval;
  1086. dec(address,objreloc.dataoffset+7);
  1087. end;
  1088. RELOC_RELATIVE_4:
  1089. begin
  1090. address:=address-objsec.mempos+relocval;
  1091. dec(address,objreloc.dataoffset+8);
  1092. end;
  1093. RELOC_RELATIVE_5:
  1094. begin
  1095. address:=address-objsec.mempos+relocval;
  1096. dec(address,objreloc.dataoffset+9);
  1097. end;
  1098. {$endif x86_64}
  1099. {$ifdef cpu64bitaddr}
  1100. RELOC_ABSOLUTE32,
  1101. {$endif cpu64bitaddr}
  1102. RELOC_ABSOLUTE :
  1103. begin
  1104. if (not win32) and assigned(objreloc.symbol) and
  1105. (objreloc.symbol.bind=AB_COMMON) then
  1106. begin
  1107. dec(address,objreloc.symbol.size);
  1108. end
  1109. else
  1110. begin
  1111. { fixup address when the symbol was known in defined object }
  1112. if (relocsec.objdata=objsec.objdata) then
  1113. dec(address,TCoffObjSection(relocsec).orgmempos);
  1114. end;
  1115. {$ifdef arm}
  1116. if (relocsec.objdata=objsec.objdata) and not TCoffObjData(objsec.objdata).eVCobj then
  1117. inc(address, relocsec.MemPos)
  1118. else
  1119. {$endif arm}
  1120. inc(address,relocval);
  1121. inc(address,imagebase);
  1122. end;
  1123. else
  1124. internalerror(200604014);
  1125. end
  1126. else
  1127. address:=0; { Relocation in debug section points to unused section, which is eliminated by linker }
  1128. data.Seek(objreloc.dataoffset);
  1129. data.Write(address,address_size);
  1130. {$ifdef cpu64bitaddr}
  1131. if (objreloc.typ = RELOC_ABSOLUTE32) and (objsec.name <> '.stab') then
  1132. begin
  1133. if assigned(objreloc.symbol) then
  1134. s:=objreloc.symbol.Name
  1135. else
  1136. s:=objreloc.objsection.Name;
  1137. Message2(link_w_32bit_absolute_reloc, objsec.ObjData.Name, s);
  1138. end;
  1139. {$endif cpu64bitaddr}
  1140. end;
  1141. end;
  1142. {****************************************************************************
  1143. TCoffObjData
  1144. ****************************************************************************}
  1145. constructor TCoffObjData.createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  1146. begin
  1147. inherited create(n);
  1148. CObjSection:=ACObjSection;
  1149. win32:=awin32;
  1150. end;
  1151. function TCoffObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1152. var
  1153. sep : string[3];
  1154. secname : string;
  1155. begin
  1156. { section type user gives the user full controll on the section name }
  1157. if atype=sec_user then
  1158. result:=aname
  1159. else
  1160. begin
  1161. { non-PECOFF targets lack rodata support }
  1162. if (atype in [sec_rodata,sec_rodata_norel]) and
  1163. not (target_info.system in systems_all_windows) then
  1164. atype:=sec_data;
  1165. secname:=coffsecnames[atype];
  1166. if create_smartlink_sections and
  1167. (aname<>'') then
  1168. begin
  1169. case aorder of
  1170. secorder_begin :
  1171. sep:='.b_';
  1172. secorder_end :
  1173. sep:='.z_';
  1174. else
  1175. sep:='.n_';
  1176. end;
  1177. result:=secname+sep+aname
  1178. end
  1179. else
  1180. result:=secname;
  1181. end;
  1182. end;
  1183. procedure TCoffObjData.CreateDebugSections;
  1184. begin
  1185. if target_dbg.id=dbg_stabs then
  1186. begin
  1187. stabssec:=createsection(sec_stab);
  1188. stabstrsec:=createsection(sec_stabstr);
  1189. end;
  1190. end;
  1191. procedure TCoffObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reloctype:TObjRelocationType);
  1192. var
  1193. curraddr,
  1194. symaddr : aword;
  1195. begin
  1196. if CurrObjSec=nil then
  1197. internalerror(2004030701);
  1198. if assigned(p) then
  1199. begin
  1200. { current address }
  1201. curraddr:=CurrObjSec.mempos+CurrObjSec.Size;
  1202. { external/common symbols don't have a fixed memory position yet }
  1203. if (p.bind=AB_COMMON) then
  1204. begin
  1205. { For go32v2 we need to use the size as address }
  1206. if not win32 then
  1207. symaddr:=p.size
  1208. else
  1209. symaddr:=0;
  1210. end
  1211. else
  1212. symaddr:=p.address;
  1213. { no symbol relocation need inside a section }
  1214. if (p.objsection=CurrObjSec) and
  1215. (p.bind<>AB_COMMON) then
  1216. begin
  1217. case reloctype of
  1218. RELOC_ABSOLUTE :
  1219. begin
  1220. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE);
  1221. inc(data,symaddr);
  1222. end;
  1223. {$ifdef cpu64bitaddr}
  1224. RELOC_ABSOLUTE32 :
  1225. begin
  1226. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE32);
  1227. inc(data,symaddr);
  1228. end;
  1229. {$endif cpu64bitaddr}
  1230. RELOC_RELATIVE :
  1231. begin
  1232. //inc(data,symaddr-len-CurrObjSec.Size);
  1233. data:=data+symaddr-len-CurrObjSec.Size;
  1234. end;
  1235. {$ifdef ARM}
  1236. RELOC_RELATIVE_24,
  1237. RELOC_RELATIVE_CALL:
  1238. begin
  1239. data:=(data and $ff000000) or (((((data and $ffffff) shl 2)+(symaddr-CurrObjSec.Size)) shr 2) and $FFFFFF); // TODO: Check overflow
  1240. end;
  1241. {$endif ARM}
  1242. RELOC_RVA,
  1243. RELOC_SECREL32 :
  1244. begin
  1245. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,reloctype);
  1246. inc(data,symaddr);
  1247. end;
  1248. else
  1249. internalerror(200604013);
  1250. end;
  1251. end
  1252. else
  1253. begin
  1254. if (p.objsection<>nil) and
  1255. (p.bind<>AB_COMMON) then
  1256. CurrObjSec.addsectionreloc(curraddr,p.objsection,reloctype)
  1257. else
  1258. CurrObjSec.addsymreloc(curraddr,p,reloctype);
  1259. if (not win32) or
  1260. (p.objsection<>nil) then
  1261. inc(data,symaddr);
  1262. if reloctype=RELOC_RELATIVE then
  1263. begin
  1264. if win32 then
  1265. dec(data,len-4)
  1266. else
  1267. dec(data,len+CurrObjSec.Size);
  1268. end;
  1269. end;
  1270. end
  1271. else
  1272. begin
  1273. if reloctype=RELOC_RVA then
  1274. internalerror(200603033);
  1275. end;
  1276. CurrObjSec.write(data,len);
  1277. end;
  1278. {****************************************************************************
  1279. TDJCoffObjData
  1280. ****************************************************************************}
  1281. constructor TDJCoffObjData.create(const n:string);
  1282. begin
  1283. inherited createcoff(n,false,TCoffObjSection);
  1284. end;
  1285. {****************************************************************************
  1286. TPECoffObjData
  1287. ****************************************************************************}
  1288. constructor TPECoffObjData.create(const n:string);
  1289. begin
  1290. inherited createcoff(n,true,TCoffObjSection);
  1291. end;
  1292. {****************************************************************************
  1293. TCoffObjOutput
  1294. ****************************************************************************}
  1295. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1296. begin
  1297. inherited create(AWriter);
  1298. win32:=awin32;
  1299. end;
  1300. destructor TCoffObjOutput.destroy;
  1301. begin
  1302. FCoffSyms.free;
  1303. FCoffStrs.free;
  1304. inherited destroy;
  1305. end;
  1306. procedure TCoffObjOutput.write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  1307. var
  1308. sym : coffsymbol;
  1309. bosym : coffbigobjsymbol;
  1310. strpos : longword;
  1311. begin
  1312. { symbolname }
  1313. if length(name)>8 then
  1314. begin
  1315. strpos:=FCoffStrs.size+4;
  1316. FCoffStrs.writestr(name);
  1317. FCoffStrs.writestr(#0);
  1318. end
  1319. else
  1320. strpos:=0;
  1321. if bigobj then
  1322. begin
  1323. fillchar(bosym,sizeof(bosym),0);
  1324. if length(name)>8 then
  1325. bosym.name.offset.offset:=strpos
  1326. else
  1327. move(name[1],bosym.name.shortname,length(name));
  1328. bosym.value:=value;
  1329. bosym.SectionNumber:=longword(section);
  1330. bosym.StorageClass:=typ;
  1331. bosym.NumberOfAuxSymbols:=aux;
  1332. inc(symidx);
  1333. FCoffSyms.write(bosym,sizeof(bosym));
  1334. end
  1335. else
  1336. begin
  1337. if section>$7fff then
  1338. internalerror(2017020302);
  1339. FillChar(sym,sizeof(sym),0);
  1340. if length(name)>8 then
  1341. sym.strpos:=strpos
  1342. else
  1343. move(name[1],sym.name,length(name));
  1344. sym.value:=value;
  1345. sym.section:=section;
  1346. sym.typ:=typ;
  1347. sym.aux:=aux;
  1348. inc(symidx);
  1349. FCoffSyms.write(sym,sizeof(sym));
  1350. end;
  1351. end;
  1352. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1353. var
  1354. secrec : coffsectionrec;
  1355. padding : word;
  1356. begin
  1357. with TCoffObjSection(p) do
  1358. begin
  1359. Inc(plongword(arg)^);
  1360. index:=plongword(arg)^;
  1361. secsymidx:=symidx;
  1362. { Both GNU and Microsoft toolchains write section symbols using
  1363. storage class 3 (STATIC).
  1364. No reason to use COFF_SYM_SECTION, it is silently converted to 3 by
  1365. PE binutils and causes warnings with DJGPP binutils. }
  1366. write_symbol(name,mempos,index,COFF_SYM_LOCAL,1);
  1367. { AUX }
  1368. fillchar(secrec,sizeof(secrec),0);
  1369. secrec.len:=Size;
  1370. if ObjRelocations.count<65535 then
  1371. secrec.nrelocs:=ObjRelocations.count
  1372. else
  1373. secrec.nrelocs:=65535;
  1374. inc(symidx);
  1375. FCoffSyms.write(secrec,sizeof(secrec));
  1376. { aux recs have the same size as symbols, so we need to add two
  1377. Byte of padding in case of a Big Obj Coff }
  1378. padding:=0;
  1379. if bigobj then
  1380. FCoffSyms.write(padding,sizeof(padding));
  1381. end;
  1382. end;
  1383. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1384. var
  1385. i : longint;
  1386. rel : coffreloc;
  1387. objreloc : TObjRelocation;
  1388. begin
  1389. if (TObjSection(p).ObjRelocations.Count>65535) then
  1390. begin
  1391. rel.address:=TObjSection(p).ObjRelocations.Count+1;
  1392. rel.sym:=0;
  1393. rel.reloctype:=0;
  1394. FWriter.Write(rel,sizeof(rel));
  1395. end;
  1396. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1397. begin
  1398. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1399. rel.address:=objreloc.dataoffset;
  1400. if assigned(objreloc.symbol) then
  1401. begin
  1402. if (objreloc.symbol.bind=AB_LOCAL) then
  1403. rel.sym:=objreloc.symbol.objsection.secsymidx
  1404. else
  1405. begin
  1406. if objreloc.symbol.symidx=-1 then
  1407. internalerror(200602233);
  1408. rel.sym:=objreloc.symbol.symidx;
  1409. end;
  1410. end
  1411. else
  1412. begin
  1413. if objreloc.objsection<>nil then
  1414. rel.sym:=objreloc.objsection.secsymidx
  1415. else
  1416. rel.sym:=0;
  1417. end;
  1418. case objreloc.typ of
  1419. {$ifdef arm}
  1420. RELOC_ABSOLUTE :
  1421. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1422. { I've no idea if this is correct (FK):
  1423. RELOC_RELATIVE :
  1424. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1425. }
  1426. RELOC_RVA :
  1427. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1428. RELOC_SECREL32 :
  1429. rel.reloctype:=IMAGE_REL_ARM_SECREL;
  1430. RELOC_RELATIVE_24 :
  1431. rel.reloctype:=IMAGE_REL_ARM_BRANCH24;
  1432. RELOC_RELATIVE_CALL :
  1433. rel.reloctype:=IMAGE_REL_ARM_BLX24;
  1434. RELOC_RELATIVE_24_THUMB:
  1435. rel.reloctype:=IMAGE_REL_ARM_BLX23T;
  1436. {$endif arm}
  1437. {$ifdef i386}
  1438. RELOC_RELATIVE :
  1439. rel.reloctype:=IMAGE_REL_I386_PCRLONG;
  1440. RELOC_ABSOLUTE :
  1441. rel.reloctype:=IMAGE_REL_I386_DIR32;
  1442. RELOC_RVA :
  1443. rel.reloctype:=IMAGE_REL_I386_IMAGEBASE;
  1444. RELOC_SECREL32 :
  1445. rel.reloctype:=IMAGE_REL_I386_SECREL32;
  1446. {$endif i386}
  1447. {$ifdef x86_64}
  1448. RELOC_NONE :
  1449. rel.reloctype:=IMAGE_REL_AMD64_ABSOLUTE;
  1450. RELOC_RELATIVE :
  1451. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1452. RELOC_ABSOLUTE32 :
  1453. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1454. RELOC_ABSOLUTE :
  1455. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1456. RELOC_RVA :
  1457. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1458. RELOC_RELATIVE_1 :
  1459. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1460. RELOC_RELATIVE_2 :
  1461. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1462. RELOC_RELATIVE_3 :
  1463. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1464. RELOC_RELATIVE_4 :
  1465. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1466. RELOC_RELATIVE_5 :
  1467. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1468. RELOC_SECREL32 :
  1469. rel.reloctype:=IMAGE_REL_AMD64_SECREL;
  1470. {$endif x86_64}
  1471. {$ifdef aarch64}
  1472. RELOC_NONE :
  1473. rel.reloctype:=IMAGE_REL_ARM64_ABSOLUTE;
  1474. RELOC_ABSOLUTE32 :
  1475. rel.reloctype:=IMAGE_REL_ARM64_ADDR32;
  1476. RELOC_RVA :
  1477. rel.reloctype:=IMAGE_REL_ARM64_ADDR32NB;
  1478. RELOC_RELATIVE_26 :
  1479. rel.reloctype:=IMAGE_REL_ARM64_BRANCH26;
  1480. RELOC_ADR_PREL_PG_HI21 :
  1481. rel.reloctype:=IMAGE_REL_ARM64_PAGEBASE_REL21;
  1482. RELOC_ADR_PREL_LO21 :
  1483. rel.reloctype:=IMAGE_REL_ARM64_REL21;
  1484. RELOC_ADD_ABS_LO12:
  1485. rel.reloctype:=IMAGE_REL_ARM64_PAGEOFFSET_12A;
  1486. RELOC_LDST8_ABS_LO12:
  1487. rel.reloctype:=IMAGE_REL_ARM64_PAGEOFFSET_12L;
  1488. RELOC_SECREL32:
  1489. rel.reloctype:=IMAGE_REL_ARM64_SECREL;
  1490. {IMAGE_REL_ARM64_SECREL_LOW12A
  1491. IMAGE_REL_ARM64_SECREL_HIGH12A
  1492. IMAGE_REL_ARM64_SECREL_LOW12L
  1493. IMAGE_REL_ARM64_TOKEN
  1494. IMAGE_REL_ARM64_SECTION}
  1495. RELOC_ABSOLUTE:
  1496. rel.reloctype:=IMAGE_REL_ARM64_ADDR64;
  1497. RELOC_RELATIVE_19:
  1498. rel.reloctype:=IMAGE_REL_ARM64_BRANCH19;
  1499. {$endif aarch64}
  1500. else
  1501. internalerror(200905071);
  1502. end;
  1503. FWriter.write(rel,sizeof(rel));
  1504. end;
  1505. end;
  1506. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1507. var
  1508. filename : string[20];
  1509. filenamelen : longint;
  1510. sectionval : word;
  1511. globalval : byte;
  1512. i : longint;
  1513. value : aword;
  1514. objsym : TObjSymbol;
  1515. secidx : longword;
  1516. begin
  1517. with TCoffObjData(data) do
  1518. begin
  1519. symidx:=0;
  1520. { The `.file' record, and the file name auxiliary record }
  1521. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1522. fillchar(filename,sizeof(filename),0);
  1523. filename:=ExtractFileName(current_module.mainsource);
  1524. inc(symidx);
  1525. if bigobj then
  1526. filenamelen:=sizeof(coffbigobjsymbol)
  1527. else
  1528. filenamelen:=sizeof(coffsymbol);
  1529. FCoffSyms.write(filename[1],filenamelen);
  1530. { Sections }
  1531. secidx:=0;
  1532. ObjSectionList.ForEachCall(@section_write_symbol,@secidx);
  1533. { ObjSymbols }
  1534. for i:=0 to ObjSymbolList.Count-1 do
  1535. begin
  1536. objsym:=TObjSymbol(ObjSymbolList[i]);
  1537. if (objsym.bind=AB_LOCAL) then
  1538. continue;
  1539. case objsym.bind of
  1540. AB_GLOBAL,
  1541. AB_PRIVATE_EXTERN:
  1542. begin
  1543. globalval:=COFF_SYM_GLOBAL;
  1544. sectionval:=objsym.objsection.index;
  1545. value:=objsym.address;
  1546. end;
  1547. AB_LOCAL :
  1548. begin
  1549. globalval:=COFF_SYM_LOCAL;
  1550. sectionval:=objsym.objsection.index;
  1551. value:=objsym.address;
  1552. end;
  1553. else
  1554. begin
  1555. globalval:=COFF_SYM_GLOBAL;
  1556. sectionval:=0;
  1557. value:=objsym.size;
  1558. end;
  1559. end;
  1560. { symbolname }
  1561. objsym.symidx:=symidx;
  1562. write_symbol(objsym.name,value,sectionval,globalval,0);
  1563. end;
  1564. end;
  1565. end;
  1566. procedure TCoffObjOutput.section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  1567. begin
  1568. p.coffrelocpos:=datapos;
  1569. inc(datapos,sizeof(coffreloc)*p.ObjRelocations.count);
  1570. if p.ObjRelocations.count>65535 then
  1571. begin
  1572. if win32 then
  1573. inc(datapos,sizeof(coffreloc))
  1574. else
  1575. Message1(asmw_f_too_many_relocations,p.fullname);
  1576. end;
  1577. end;
  1578. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1579. var
  1580. sechdr : tcoffsechdr;
  1581. s : string;
  1582. strpos : aword;
  1583. begin
  1584. with TCoffObjSection(p) do
  1585. begin
  1586. fillchar(sechdr,sizeof(sechdr),0);
  1587. s:=name;
  1588. if length(s)>8 then
  1589. begin
  1590. strpos:=FCoffStrs.size+4;
  1591. FCoffStrs.writestr(s);
  1592. FCoffStrs.writestr(#0);
  1593. if strpos>=10000000 then
  1594. s:='//'+encodeBase64(strpos)
  1595. else
  1596. s:='/'+ToStr(strpos);
  1597. if length(s)>8 then
  1598. internalerror(2020091501);
  1599. end;
  1600. move(s[1],sechdr.name,length(s));
  1601. if not win32 then
  1602. begin
  1603. sechdr.rvaofs:=mempos;
  1604. sechdr.vsize:=mempos;
  1605. end
  1606. else
  1607. begin
  1608. if not(oso_data in secoptions) then
  1609. sechdr.vsize:=Size;
  1610. end;
  1611. sechdr.DataSize:=size;
  1612. if (Size>0) and
  1613. (oso_data in secoptions) then
  1614. sechdr.datapos:=datapos;
  1615. if ObjRelocations.count<65535 then
  1616. sechdr.nrelocs:=ObjRelocations.count
  1617. else
  1618. sechdr.nrelocs:=65535;
  1619. sechdr.relocpos:=coffrelocpos;
  1620. if win32 then
  1621. begin
  1622. sechdr.flags:=peencodesechdrflags(secoptions,secalign);
  1623. if ObjRelocations.count>65535 then
  1624. sechdr.flags:=sechdr.flags or PE_SCN_LNK_NRELOC_OVFL;
  1625. end
  1626. else
  1627. sechdr.flags:=djencodesechdrflags(secoptions);
  1628. FWriter.write(sechdr,sizeof(sechdr));
  1629. end;
  1630. end;
  1631. function TCoffObjOutput.writedata(data:TObjData):boolean;
  1632. var
  1633. datapos,
  1634. sympos : aword;
  1635. i : longint;
  1636. header : tcoffheader;
  1637. boheader : tcoffbigobjheader;
  1638. begin
  1639. result:=false;
  1640. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  1641. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  1642. with TCoffObjData(data) do
  1643. begin
  1644. bigobj:=(ObjSectionList.Count>$7fff) and win32;
  1645. { Create Symbol Table }
  1646. create_symbols(data);
  1647. { Calculate the filepositions }
  1648. if bigobj then
  1649. datapos:=sizeof(tcoffbigobjheader)
  1650. else
  1651. datapos:=sizeof(tcoffheader);
  1652. datapos:=datapos+sizeof(tcoffsechdr)*ObjSectionList.Count;
  1653. { Sections first }
  1654. layoutsections(datapos);
  1655. { relocs }
  1656. for i:=0 to ObjSectionList.Count-1 do
  1657. section_set_reloc_datapos(TCoffObjSection(ObjSectionList[i]),datapos);
  1658. { Symbols }
  1659. sympos:=datapos;
  1660. { Generate COFF header }
  1661. if bigobj then
  1662. begin
  1663. fillchar(boheader,sizeof(boheader),0);
  1664. boheader.Sig1:=0;
  1665. boheader.Sig2:=$ffff;
  1666. boheader.Machine:=COFF_MAGIC;
  1667. boheader.Version:=COFF_BIG_OBJ_VERSION;
  1668. boheader.NumberOfSections:=longword(ObjSectionList.Count);
  1669. boheader.NumberOfSymbols:=longword(symidx);
  1670. boheader.PointerToSymbolTable:=sympos;
  1671. Move(COFF_BIG_OBJ_MAGIC,boheader.UUID,length(boheader.UUID));
  1672. FWriter.write(boheader,sizeof(boheader));
  1673. end
  1674. else
  1675. begin
  1676. fillchar(header,sizeof(tcoffheader),0);
  1677. header.mach:=COFF_MAGIC;
  1678. header.nsects:=ObjSectionList.Count;
  1679. header.sympos:=sympos;
  1680. header.syms:=symidx;
  1681. if win32 then
  1682. begin
  1683. {$ifndef cpu64bitaddr}
  1684. header.flag:=PE_FILE_32BIT_MACHINE or
  1685. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1686. {$else cpu64bitaddr}
  1687. header.flag:=PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1688. {$endif cpu64bitaddr}
  1689. end
  1690. else
  1691. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  1692. FWriter.write(header,sizeof(header));
  1693. end;
  1694. { Section headers }
  1695. ObjSectionList.ForEachCall(@section_write_header,nil);
  1696. { ObjSections }
  1697. WriteSectionContent(data);
  1698. { Relocs }
  1699. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  1700. { ObjSymbols }
  1701. if Sympos<>FWriter.ObjSize then
  1702. internalerror(200603051);
  1703. FWriter.writearray(FCoffSyms);
  1704. { Strings }
  1705. i:=FCoffStrs.size+4;
  1706. FWriter.write(i,4);
  1707. FWriter.writearray(FCoffStrs);
  1708. end;
  1709. FCoffStrs.Free;
  1710. FCoffStrs:=nil;
  1711. FCoffSyms.Free;
  1712. FCoffSyms:=nil;
  1713. end;
  1714. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  1715. begin
  1716. inherited createcoff(AWriter,false);
  1717. cobjdata:=TDJCoffObjData;
  1718. end;
  1719. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  1720. begin
  1721. inherited createcoff(AWriter,true);
  1722. cobjdata:=TPECoffObjData;
  1723. end;
  1724. {****************************************************************************
  1725. TCoffObjInput
  1726. ****************************************************************************}
  1727. constructor TCoffObjInput.createcoff(awin32:boolean);
  1728. begin
  1729. inherited create;
  1730. win32:=awin32;
  1731. bigobj:=false;
  1732. FSymTbl:=nil;
  1733. end;
  1734. destructor TCoffObjInput.destroy;
  1735. begin
  1736. FCoffSyms.free;
  1737. if assigned(FCoffStrs) then
  1738. freemem(FCoffStrs);
  1739. if assigned(FSymTbl) then
  1740. freemem(FSymTbl);
  1741. if assigned(FSecTbl) then
  1742. freemem(FSecTbl);
  1743. inherited destroy;
  1744. end;
  1745. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  1746. begin
  1747. result:=nil;
  1748. if (secidx<1) or (secidx>FSecCount) then
  1749. begin
  1750. InputError('Failed reading coff file, invalid section index');
  1751. exit;
  1752. end;
  1753. result:=FSecTbl^[secidx];
  1754. end;
  1755. function TCoffObjInput.Read_str(strpos:longword):string;
  1756. begin
  1757. if (FCoffStrs=nil) or (strpos>=FCoffStrSize) or (FCoffStrs[strpos]=#0) then
  1758. Internalerror(200205172);
  1759. result:=string(PChar(@FCoffStrs[strpos]));
  1760. end;
  1761. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  1762. var
  1763. rel : coffreloc;
  1764. rel_type : TObjRelocationType;
  1765. i : longint;
  1766. p : TObjSymbol;
  1767. begin
  1768. if s.coffrelocs=high(aword) then
  1769. begin
  1770. { If number of relocations exceeds 65535, it is stored in address field
  1771. of the first record, and includes this first fake relocation. }
  1772. FReader.read(rel,sizeof(rel));
  1773. s.coffrelocs:=rel.address-1;
  1774. if s.coffrelocs<=65535 then
  1775. InternalError(2013012503);
  1776. end;
  1777. for i:=1 to s.coffrelocs do
  1778. begin
  1779. FReader.read(rel,sizeof(rel));
  1780. case rel.reloctype of
  1781. {$ifdef arm}
  1782. IMAGE_REL_ARM_ABSOLUTE:
  1783. rel_type:=RELOC_NONE;
  1784. IMAGE_REL_ARM_ADDR32:
  1785. rel_type:=RELOC_ABSOLUTE;
  1786. IMAGE_REL_ARM_ADDR32NB:
  1787. rel_type:=RELOC_RVA;
  1788. IMAGE_REL_ARM_BRANCH24:
  1789. rel_type:=RELOC_RELATIVE_24;
  1790. IMAGE_REL_ARM_BLX24:
  1791. rel_type:=RELOC_RELATIVE_CALL;
  1792. IMAGE_REL_ARM_SECREL:
  1793. rel_type:=RELOC_SECREL32;
  1794. IMAGE_REL_ARM_BLX23T:
  1795. rel_type:=RELOC_RELATIVE_24_THUMB;
  1796. {$endif arm}
  1797. {$ifdef i386}
  1798. IMAGE_REL_I386_PCRLONG :
  1799. rel_type:=RELOC_RELATIVE;
  1800. IMAGE_REL_I386_DIR32 :
  1801. rel_type:=RELOC_ABSOLUTE;
  1802. IMAGE_REL_I386_IMAGEBASE :
  1803. rel_type:=RELOC_RVA;
  1804. IMAGE_REL_I386_SECREL32 :
  1805. rel_type:=RELOC_SECREL32;
  1806. {$endif i386}
  1807. {$ifdef x86_64}
  1808. IMAGE_REL_AMD64_ABSOLUTE:
  1809. rel_type:=RELOC_NONE;
  1810. IMAGE_REL_AMD64_REL32:
  1811. rel_type:=RELOC_RELATIVE;
  1812. IMAGE_REL_AMD64_ADDR32,
  1813. R_X86_64_32S:
  1814. rel_type:=RELOC_ABSOLUTE32;
  1815. IMAGE_REL_AMD64_ADDR64:
  1816. rel_type:=RELOC_ABSOLUTE;
  1817. IMAGE_REL_AMD64_ADDR32NB:
  1818. rel_type:=RELOC_RVA;
  1819. IMAGE_REL_AMD64_REL32_1:
  1820. rel_type:=RELOC_RELATIVE_1;
  1821. IMAGE_REL_AMD64_REL32_2:
  1822. rel_type:=RELOC_RELATIVE_2;
  1823. IMAGE_REL_AMD64_REL32_3:
  1824. rel_type:=RELOC_RELATIVE_3;
  1825. IMAGE_REL_AMD64_REL32_4:
  1826. rel_type:=RELOC_RELATIVE_4;
  1827. IMAGE_REL_AMD64_REL32_5:
  1828. rel_type:=RELOC_RELATIVE_5;
  1829. IMAGE_REL_AMD64_SECREL:
  1830. rel_type:=RELOC_SECREL32;
  1831. {$endif x86_64}
  1832. {$ifdef aarch64}
  1833. IMAGE_REL_ARM64_ABSOLUTE:
  1834. rel_type:=RELOC_NONE;
  1835. IMAGE_REL_ARM64_ADDR32:
  1836. rel_type:=RELOC_ABSOLUTE32;
  1837. IMAGE_REL_ARM64_ADDR32NB:
  1838. rel_type:=RELOC_RVA;
  1839. IMAGE_REL_ARM64_BRANCH26:
  1840. rel_type:=RELOC_RELATIVE_26;
  1841. IMAGE_REL_ARM64_PAGEBASE_REL21:
  1842. rel_type:=RELOC_ADR_PREL_PG_HI21;
  1843. IMAGE_REL_ARM64_REL21:
  1844. rel_type:=RELOC_ADR_PREL_LO21;
  1845. IMAGE_REL_ARM64_PAGEOFFSET_12A:
  1846. rel_type:=RELOC_ADD_ABS_LO12;
  1847. IMAGE_REL_ARM64_PAGEOFFSET_12L:
  1848. rel_type:=RELOC_LDST8_ABS_LO12;
  1849. IMAGE_REL_ARM64_SECREL:
  1850. rel_type:=RELOC_SECREL32;
  1851. //IMAGE_REL_ARM64_SECREL_LOW12A
  1852. //IMAGE_REL_ARM64_SECREL_HIGH12A
  1853. //IMAGE_REL_ARM64_SECREL_LOW12L
  1854. //IMAGE_REL_ARM64_TOKEN
  1855. //IMAGE_REL_ARM64_SECTION
  1856. IMAGE_REL_ARM64_ADDR64:
  1857. rel_type:=RELOC_ABSOLUTE;
  1858. //IMAGE_REL_ARM64_BRANCH19
  1859. {$endif aarch64}
  1860. else
  1861. begin
  1862. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  1863. exit;
  1864. end;
  1865. end;
  1866. p:=FSymTbl^[rel.sym];
  1867. if assigned(p) then
  1868. s.addsymreloc(rel.address-s.mempos,p,rel_type)
  1869. else
  1870. begin
  1871. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  1872. exit;
  1873. end;
  1874. end;
  1875. end;
  1876. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  1877. var
  1878. size,
  1879. address,
  1880. nsyms,
  1881. symidx : aint;
  1882. i : longint;
  1883. sym : coffsymbol;
  1884. bosym : coffbigobjsymbol;
  1885. objsym : TObjSymbol;
  1886. bind : Tasmsymbind;
  1887. strname : string;
  1888. auxrec : array[0..sizeof(coffsymbol)-1] of byte;
  1889. boauxrec : array[0..sizeof(coffbigobjsymbol)-1] of byte;
  1890. secrec : pcoffsectionrec;
  1891. objsec : TObjSection;
  1892. secidx : longint;
  1893. symvalue : longword;
  1894. auxcount : byte;
  1895. symcls : byte;
  1896. comdatsel : TObjSectionComdatSelection;
  1897. { keeps string manipulations out of main routine }
  1898. procedure UnsupportedSymbolType;
  1899. begin
  1900. Comment(V_Fatal,'Unsupported COFF symbol type '+tostr(symcls)+' at index '+tostr(symidx)+' while reading '+InputFileName);
  1901. end;
  1902. begin
  1903. with TCoffObjData(objdata) do
  1904. begin
  1905. if bigobj then
  1906. nsyms:=FCoffSyms.Size div sizeof(coffbigobjsymbol)
  1907. else
  1908. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1909. { Allocate memory for symidx -> TObjSymbol table }
  1910. FSymTbl:=AllocMem(nsyms*sizeof(TObjSymbol));
  1911. { Load the Symbols }
  1912. FCoffSyms.Seek(0);
  1913. symidx:=0;
  1914. while (symidx<nsyms) do
  1915. begin
  1916. if bigobj then
  1917. begin
  1918. FCoffSyms.Read(bosym,sizeof(bosym));
  1919. if bosym.Name.Offset.Zeroes<>0 then
  1920. begin
  1921. move(bosym.Name.ShortName,strname[1],8);
  1922. strname[9]:=#0;
  1923. strname[0]:=chr(strlen(@strname[1]));
  1924. if strname='' then
  1925. internalerror(2017020301);
  1926. end
  1927. else
  1928. strname:=Read_str(bosym.Name.Offset.Offset);
  1929. secidx:=longint(bosym.SectionNumber);
  1930. symvalue:=bosym.Value;
  1931. auxcount:=bosym.NumberOfAuxSymbols;
  1932. symcls:=bosym.StorageClass;
  1933. end
  1934. else
  1935. begin
  1936. FCoffSyms.Read(sym,sizeof(sym));
  1937. if plongint(@sym.name)^<>0 then
  1938. begin
  1939. move(sym.name,strname[1],8);
  1940. strname[9]:=#0;
  1941. strname[0]:=chr(strlen(@strname[1]));
  1942. if strname='' then
  1943. Internalerror(200205171);
  1944. end
  1945. else
  1946. strname:=Read_str(sym.strpos);
  1947. secidx:=sym.section;
  1948. symvalue:=sym.value;
  1949. auxcount:=sym.aux;
  1950. symcls:=sym.typ;
  1951. end;
  1952. bind:=AB_EXTERNAL;
  1953. size:=0;
  1954. address:=0;
  1955. objsym:=nil;
  1956. objsec:=nil;
  1957. case symcls of
  1958. COFF_SYM_GLOBAL :
  1959. begin
  1960. if secidx=0 then
  1961. begin
  1962. if symvalue=0 then
  1963. bind:=AB_EXTERNAL
  1964. else
  1965. begin
  1966. bind:=AB_COMMON;
  1967. size:=symvalue;
  1968. end;
  1969. end
  1970. else
  1971. begin
  1972. bind:=AB_GLOBAL;
  1973. objsec:=GetSection(secidx);
  1974. if symvalue>=objsec.mempos then
  1975. address:=symvalue-objsec.mempos;
  1976. end;
  1977. objsym:=CreateSymbol(strname);
  1978. objsym.bind:=bind;
  1979. objsym.typ:=AT_FUNCTION;
  1980. objsym.objsection:=objsec;
  1981. objsym.offset:=address;
  1982. objsym.size:=size;
  1983. end;
  1984. COFF_SYM_LABEL,
  1985. COFF_SYM_LOCAL :
  1986. begin
  1987. { do not add constants (section=-1) }
  1988. if secidx<>-1 then
  1989. begin
  1990. objsec:=GetSection(secidx);
  1991. if symvalue>=objsec.mempos then
  1992. address:=symvalue-objsec.mempos;
  1993. objsym:=CreateSymbol(strname);
  1994. objsym.bind:=AB_LOCAL;
  1995. objsym.typ:=AT_FUNCTION;
  1996. objsym.objsection:=objsec;
  1997. objsym.offset:=address;
  1998. objsym.size:=size;
  1999. end;
  2000. end;
  2001. COFF_SYM_SECTION :
  2002. begin
  2003. { GetSection checks that index is in range }
  2004. objsec:=GetSection(secidx);
  2005. if assigned(objsec) then
  2006. begin
  2007. if symvalue>=objsec.mempos then
  2008. address:=symvalue-objsec.mempos;
  2009. objsym:=CreateSymbol(strname);
  2010. objsym.bind:=AB_LOCAL;
  2011. objsym.typ:=AT_FUNCTION;
  2012. objsym.objsection:=objsec;
  2013. objsym.offset:=address;
  2014. objsym.size:=size;
  2015. end;
  2016. end;
  2017. COFF_SYM_FUNCTION,
  2018. COFF_SYM_FILE :
  2019. ;
  2020. else
  2021. UnsupportedSymbolType;
  2022. end;
  2023. FSymTbl^[symidx]:=objsym;
  2024. { read aux records }
  2025. { handle COMDAT symbols }
  2026. if (symcls=COFF_SYM_LOCAL) and (auxcount=1) and (symvalue=0) and (oso_comdat in objsym.objsection.SecOptions) then
  2027. begin
  2028. if bigobj then
  2029. begin
  2030. FCoffSyms.Read(boauxrec,sizeof(boauxrec));
  2031. secrec:=pcoffsectionrec(@boauxrec[0]);
  2032. end
  2033. else
  2034. begin
  2035. FCoffSyms.Read(auxrec,sizeof(auxrec));
  2036. secrec:=pcoffsectionrec(@auxrec);
  2037. end;
  2038. case secrec^.select of
  2039. IMAGE_COMDAT_SELECT_NODUPLICATES:
  2040. comdatsel:=oscs_none;
  2041. IMAGE_COMDAT_SELECT_ANY:
  2042. comdatsel:=oscs_any;
  2043. IMAGE_COMDAT_SELECT_SAME_SIZE:
  2044. comdatsel:=oscs_same_size;
  2045. IMAGE_COMDAT_SELECT_EXACT_MATCH:
  2046. comdatsel:=oscs_exact_match;
  2047. IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  2048. comdatsel:=oscs_associative;
  2049. IMAGE_COMDAT_SELECT_LARGEST:
  2050. comdatsel:=oscs_largest;
  2051. else begin
  2052. comdatsel:=oscs_none;
  2053. Message2(link_e_comdat_select_unsupported,inttostr(secrec^.select),objsym.objsection.name);
  2054. end;
  2055. end;
  2056. if comdatsel in [oscs_associative,oscs_exact_match] then
  2057. { only temporary }
  2058. Comment(V_Error,'Associative or exact match COMDAT sections are not yet supported (symbol: '+objsym.objsection.Name+')')
  2059. else if (comdatsel=oscs_associative) and (secrec^.assoc=0) then
  2060. Message1(link_e_comdat_associative_section_expected,objsym.objsection.name)
  2061. else if (objsym.objsection.ComdatSelection<>oscs_none) and (comdatsel<>oscs_none) and (objsym.objsection.ComdatSelection<>comdatsel) then
  2062. Message2(link_e_comdat_not_matching,objsym.objsection.Name,objsym.Name)
  2063. else
  2064. begin
  2065. objsym.objsection.ComdatSelection:=comdatsel;
  2066. if (secrec^.assoc<>0) and not assigned(objsym.objsection.AssociativeSection) then
  2067. begin
  2068. objsym.objsection.AssociativeSection:=GetSection(secrec^.assoc);
  2069. if not assigned(objsym.objsection.AssociativeSection) then
  2070. Message1(link_e_comdat_associative_section_not_found,objsym.objsection.Name);
  2071. end;
  2072. end;
  2073. dec(auxcount);
  2074. inc(symidx);
  2075. end;
  2076. for i:=1 to auxcount do
  2077. begin
  2078. if bigobj then
  2079. FCoffSyms.Read(boauxrec,sizeof(boauxrec))
  2080. else
  2081. FCoffSyms.Read(auxrec,sizeof(auxrec));
  2082. inc(symidx);
  2083. end;
  2084. inc(symidx);
  2085. end;
  2086. end;
  2087. end;
  2088. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  2089. begin
  2090. with TCoffObjSection(p) do
  2091. begin
  2092. { Skip debug sections }
  2093. if (oso_debug in secoptions) and
  2094. (cs_link_strip in current_settings.globalswitches) and
  2095. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  2096. exit;
  2097. if coffrelocs>0 then
  2098. begin
  2099. FReader.Seek(coffrelocpos);
  2100. read_relocs(TCoffObjSection(p));
  2101. end;
  2102. end;
  2103. end;
  2104. function TCoffObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  2105. var
  2106. secalign : longint;
  2107. secofs,
  2108. strpos,
  2109. i : longint;
  2110. sympos,
  2111. symcount,
  2112. symsize,
  2113. code : longint;
  2114. objsec : TCoffObjSection;
  2115. secoptions : TObjSectionOptions;
  2116. header : tcoffheader;
  2117. boheader : tcoffbigobjheader;
  2118. sechdr : tcoffsechdr;
  2119. secname : string;
  2120. secnamebuf : array[0..15] of char;
  2121. begin
  2122. FReader:=AReader;
  2123. InputFileName:=AReader.FileName;
  2124. objdata:=CObjData.Create(InputFileName);
  2125. result:=false;
  2126. boheader:=default(tcoffbigobjheader);
  2127. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  2128. with TCoffObjData(objdata) do
  2129. begin
  2130. { Read COFF header }
  2131. if not AReader.read(header,sizeof(tcoffheader)) then
  2132. begin
  2133. InputError('Can''t read COFF Header');
  2134. exit;
  2135. end;
  2136. if (header.mach=0) and (header.nsects=$ffff) then
  2137. begin
  2138. { either a library or big obj COFF }
  2139. AReader.seek(0);
  2140. if not AReader.read(boheader,sizeof(boheader)) then
  2141. begin
  2142. InputError('Can''t read Big Obj COFF Header');
  2143. exit;
  2144. end;
  2145. if CompareByte(boheader.UUID,COFF_BIG_OBJ_MAGIC,length(boheader.uuid))<>0 then
  2146. begin
  2147. { ToDo: this should be treated as a library }
  2148. InputError('Illegal Big Obj COFF Magic');
  2149. exit;
  2150. end;
  2151. if boheader.Version<>COFF_BIG_OBJ_VERSION then
  2152. begin
  2153. InputError('Illegal Big Obj COFF Version');
  2154. exit;
  2155. end;
  2156. if boheader.Machine<>COFF_MAGIC then
  2157. begin
  2158. InputError('Illegal COFF Machine type');
  2159. exit;
  2160. end;
  2161. bigobj:=true;
  2162. end
  2163. else if header.mach<>COFF_MAGIC then
  2164. begin
  2165. InputError('Illegal COFF Magic');
  2166. exit;
  2167. end;
  2168. {$ifdef arm}
  2169. eVCobj:=header.flag=$100;
  2170. {$endif arm}
  2171. { ObjSymbols }
  2172. if bigobj then
  2173. begin
  2174. sympos:=longint(boheader.PointerToSymbolTable);
  2175. symcount:=longint(boheader.NumberOfSymbols);
  2176. symsize:=sizeof(CoffBigObjSymbol);
  2177. end
  2178. else
  2179. begin
  2180. sympos:=longint(header.sympos);
  2181. symcount:=longint(header.syms);
  2182. symsize:=sizeof(CoffSymbol);
  2183. end;
  2184. AReader.Seek(sympos);
  2185. if not AReader.ReadArray(FCoffSyms,symcount*symsize) then
  2186. begin
  2187. InputError('Error reading coff symbol table');
  2188. exit;
  2189. end;
  2190. { Strings }
  2191. if not AReader.Read(FCoffStrSize,4) then
  2192. begin
  2193. InputError('Error reading COFF string table');
  2194. exit;
  2195. end;
  2196. if (FCoffStrSize>4) then
  2197. begin
  2198. { allocate an extra byte and null-terminate }
  2199. GetMem(FCoffStrs,FCoffStrSize+1);
  2200. FCoffStrs[FCoffStrSize]:=#0;
  2201. for i:=0 to 3 do
  2202. FCoffStrs[i]:=#0;
  2203. if not AReader.Read(FCoffStrs[4],FCoffStrSize-4) then
  2204. begin
  2205. InputError('Error reading COFF string table');
  2206. exit;
  2207. end;
  2208. end;
  2209. { Section headers }
  2210. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  2211. if bigobj then
  2212. FSecCount:=longint(boheader.NumberOfSections)
  2213. else
  2214. FSecCount:=header.nsects;
  2215. FSecTbl:=AllocMem((FSecCount+1)*sizeof(TObjSection));
  2216. if bigobj then
  2217. secofs:=sizeof(tcoffbigobjheader)
  2218. else
  2219. secofs:=sizeof(tcoffheader)+header.opthdr;
  2220. AReader.Seek(secofs);
  2221. for i:=1 to FSecCount do
  2222. begin
  2223. if not AReader.read(sechdr,sizeof(sechdr)) then
  2224. begin
  2225. InputError('Error reading COFF Section Headers');
  2226. exit;
  2227. end;
  2228. move(sechdr.name,secnamebuf,8);
  2229. secnamebuf[8]:=#0;
  2230. secname:=strpas(secnamebuf);
  2231. if secname[1]='/' then
  2232. begin
  2233. if secname[2]='/' then
  2234. begin
  2235. if not decodeBase64(copy(secname,3,8),strpos) then
  2236. begin
  2237. InputError('Error reading COFF Section Headers');
  2238. secname:='error';
  2239. end
  2240. else
  2241. secname:=Read_str(strpos);
  2242. end
  2243. else
  2244. begin
  2245. Val(Copy(secname,2,8),strpos,code);
  2246. if code=0 then
  2247. secname:=Read_str(strpos)
  2248. else
  2249. begin
  2250. InputError('Error reading COFF Section Headers');
  2251. secname:='error';
  2252. end;
  2253. end;
  2254. end;
  2255. if win32 then
  2256. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  2257. else
  2258. begin
  2259. secoptions:=djdecodesechdrflags(secname,sechdr.flags);
  2260. secalign:=sizeof(pint);
  2261. end;
  2262. if (Length(secname)>3) and (secname[2] in ['e','f','i','p','r']) then
  2263. begin
  2264. if (Pos('.edata',secname)=1) or
  2265. (Pos('.rsrc',secname)=1) or
  2266. ((target_info.system=system_arm_wince) and (Pos('.pdata',secname)=1)) or
  2267. (Pos('.fpc',secname)=1) then
  2268. include(secoptions,oso_keep);
  2269. if (Pos('.idata',secname)=1) then
  2270. begin
  2271. { TODO: idata keep can maybe replaced with grouping of text and idata}
  2272. include(secoptions,oso_keep);
  2273. secname:=secname + '.' + ExtractFileName(InputFileName);
  2274. end;
  2275. end;
  2276. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions,false));
  2277. FSecTbl^[i]:=objsec;
  2278. if not win32 then
  2279. objsec.mempos:=sechdr.rvaofs;
  2280. objsec.orgmempos:=sechdr.rvaofs;
  2281. objsec.coffrelocs:=sechdr.nrelocs;
  2282. if win32 then
  2283. begin
  2284. if (sechdr.flags and PE_SCN_LNK_NRELOC_OVFL)<>0 then
  2285. objsec.coffrelocs:=high(aword);
  2286. end;
  2287. objsec.coffrelocpos:=sechdr.relocpos;
  2288. objsec.datapos:=sechdr.datapos;
  2289. objsec.Size:=sechdr.dataSize;
  2290. end;
  2291. { Insert all ObjSymbols }
  2292. read_symbols(objdata);
  2293. { Section Data }
  2294. ReadSectionContent(objdata);
  2295. { Relocs }
  2296. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  2297. end;
  2298. if assigned(FCoffStrs) then
  2299. freemem(FCoffStrs);
  2300. FCoffStrs:=nil;
  2301. FCoffSyms.Free;
  2302. FCoffSyms:=nil;
  2303. result:=true;
  2304. end;
  2305. constructor TDJCoffObjInput.create;
  2306. begin
  2307. inherited createcoff(false);
  2308. cobjdata:=TDJCoffObjData;
  2309. end;
  2310. constructor TPECoffObjInput.create;
  2311. begin
  2312. inherited createcoff(true);
  2313. cobjdata:=TPECoffObjData;
  2314. end;
  2315. {****************************************************************************
  2316. TCoffexeoutput
  2317. ****************************************************************************}
  2318. constructor TCoffexeoutput.createcoff(awin32:boolean);
  2319. begin
  2320. inherited create;
  2321. win32:=awin32;
  2322. if target_info.system in [system_x86_64_win64,system_aarch64_win64] then
  2323. MaxMemPos:=$FFFFFFFF
  2324. else
  2325. if target_info.system in systems_wince then
  2326. MaxMemPos:=$1FFFFFF
  2327. else
  2328. MaxMemPos:=$7FFFFFFF;
  2329. end;
  2330. procedure TCoffexeoutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  2331. var
  2332. sym : coffsymbol;
  2333. begin
  2334. FillChar(sym,sizeof(sym),0);
  2335. if length(name)>8 then
  2336. begin
  2337. sym.strpos:=FCoffStrs.size+4;
  2338. FCoffStrs.writestr(name);
  2339. FCoffStrs.writestr(#0);
  2340. end
  2341. else
  2342. move(name[1],sym.name,length(name));
  2343. sym.value:=value;
  2344. sym.section:=section;
  2345. sym.typ:=typ;
  2346. sym.aux:=aux;
  2347. FWriter.write(sym,sizeof(sym));
  2348. end;
  2349. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  2350. var
  2351. secval,
  2352. value : aint;
  2353. globalval : byte;
  2354. exesec : TExeSection;
  2355. begin
  2356. if not assigned(texesymbol(p).objsymbol) then
  2357. internalerror(2006030505);
  2358. with texesymbol(p).objsymbol do
  2359. begin
  2360. if assigned(objsection) then
  2361. exesec:=TExeSection(objsection.exesection)
  2362. else
  2363. exesec:=nil;
  2364. { There is no exesection defined for special internal symbols
  2365. like __image_base__ }
  2366. if assigned(exesec) then
  2367. begin
  2368. secval:=exesec.secsymidx;
  2369. if win32 then
  2370. value:=address-exesec.mempos
  2371. else
  2372. value:=address;
  2373. end
  2374. else
  2375. begin
  2376. secval:=-1;
  2377. value:=address;
  2378. end;
  2379. if bind=AB_LOCAL then
  2380. globalval:=COFF_SYM_LOCAL
  2381. else
  2382. globalval:=COFF_SYM_GLOBAL;
  2383. { reloctype address to the section in the executable }
  2384. write_symbol(name,value,secval,globalval,0);
  2385. end;
  2386. end;
  2387. procedure TCoffexeoutput.ExeSectionList_write_header(p:TObject;arg:pointer);
  2388. var
  2389. sechdr : tcoffsechdr;
  2390. s : string;
  2391. strpos : aword;
  2392. begin
  2393. with tExeSection(p) do
  2394. begin
  2395. fillchar(sechdr,sizeof(sechdr),0);
  2396. s:=name;
  2397. if length(s)>8 then
  2398. begin
  2399. strpos:=FCoffStrs.size+4;
  2400. FCoffStrs.writestr(s);
  2401. FCoffStrs.writestr(#0);
  2402. s:='/'+ToStr(strpos);
  2403. end;
  2404. move(s[1],sechdr.name,length(s));
  2405. if win32 then
  2406. begin
  2407. sechdr.rvaofs:=mempos;
  2408. sechdr.vsize:=Size;
  2409. { sechdr.dataSize is size of initialized data, rounded up to FileAlignment
  2410. (so it can be greater than VirtualSize). Must be zero for sections that
  2411. do not contain initialized data. }
  2412. if (oso_data in SecOptions) then
  2413. sechdr.datasize:=Align(Size,SectionDataAlign);
  2414. end
  2415. else
  2416. begin
  2417. if not (oso_debug in SecOptions) then
  2418. begin
  2419. sechdr.rvaofs:=mempos;
  2420. sechdr.vsize:=mempos;
  2421. end;
  2422. sechdr.datasize:=Size;
  2423. end;
  2424. if (Size>0) then
  2425. sechdr.datapos:=datapos-datapos_offset;
  2426. sechdr.nrelocs:=0;
  2427. sechdr.relocpos:=0;
  2428. if win32 then
  2429. begin
  2430. if (target_info.system in systems_nativent) and
  2431. (apptype = app_native) then
  2432. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign) or PE_SCN_MEM_NOT_PAGED
  2433. else
  2434. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign);
  2435. { some flags are invalid in executables, reset them }
  2436. sechdr.flags:=sechdr.flags and
  2437. not(PE_SCN_LNK_INFO or PE_SCN_LNK_REMOVE or
  2438. PE_SCN_LNK_COMDAT or PE_SCN_ALIGN_MASK);
  2439. end
  2440. else
  2441. sechdr.flags:=djencodesechdrflags(SecOptions);
  2442. FWriter.write(sechdr,sizeof(sechdr));
  2443. end;
  2444. end;
  2445. procedure TCoffexeoutput.ExeSectionList_pass2_header(p:TObject;arg:pointer);
  2446. begin
  2447. with TExeSection(p) do
  2448. begin
  2449. { The debuginfo sections should already be stripped }
  2450. { if (ExeWriteMode=ewm_exeonly) and
  2451. (oso_debug in SecOptions) then
  2452. internalerror(200801161); }
  2453. inc(plongint(arg)^);
  2454. secsymidx:=plongint(arg)^;
  2455. end;
  2456. end;
  2457. function tcoffexeoutput.totalheadersize:longword;
  2458. var
  2459. stubsize,
  2460. optheadersize : longword;
  2461. begin
  2462. if win32 then
  2463. begin
  2464. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  2465. optheadersize:=sizeof(tcoffpeoptheader);
  2466. end
  2467. else
  2468. begin
  2469. stubsize:=sizeof(go32v2stub);
  2470. optheadersize:=sizeof(coffdjoptheader);
  2471. end;
  2472. result:=stubsize+sizeof(tcoffheader)+optheadersize;
  2473. end;
  2474. procedure tcoffexeoutput.MemPos_Header;
  2475. begin
  2476. { calculate start positions after the headers }
  2477. currmempos:=totalheadersize+sizeof(tcoffsechdr)*longword(ExeSectionList.Count-2);
  2478. end;
  2479. procedure tcoffexeoutput.DataPos_Header;
  2480. begin
  2481. { retrieve amount of sections }
  2482. nsects:=0;
  2483. ExeSectionList.ForEachCall(@ExeSectionList_pass2_header,@nsects);
  2484. { calculate start positions after the headers }
  2485. currdatapos:=totalheadersize+longword(nsects)*sizeof(tcoffsechdr);
  2486. end;
  2487. procedure tcoffexeoutput.DataPos_Symbols;
  2488. begin
  2489. inherited DataPos_Symbols;
  2490. { Calculating symbols position and size }
  2491. nsyms:=ExeSymbolList.Count;
  2492. sympos:=Align(CurrDataPos,SectionDataAlign);
  2493. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  2494. end;
  2495. function TCoffexeoutput.writedata:boolean;
  2496. var
  2497. i : longword;
  2498. header : tcoffheader;
  2499. djoptheader : coffdjoptheader;
  2500. peoptheader : tcoffpeoptheader;
  2501. textExeSec,
  2502. dataExeSec,
  2503. bssExeSec,
  2504. idataExeSec : TExeSection;
  2505. hassymbols,
  2506. writeDbgStrings : boolean;
  2507. procedure UpdateDataDir(const secname:string;idx:longint);
  2508. var
  2509. exesec : TExeSection;
  2510. begin
  2511. exesec:=FindExeSection(secname);
  2512. if assigned(exesec) then
  2513. begin
  2514. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  2515. peoptheader.DataDirectory[idx].size:=exesec.Size;
  2516. end;
  2517. end;
  2518. procedure UpdateImports;
  2519. var
  2520. exesec: TExeSection;
  2521. objsec, iat_start, iat_end, ilt_start: TObjSection;
  2522. i: longint;
  2523. begin
  2524. exesec:=FindExeSection('.idata');
  2525. if exesec=nil then
  2526. exit;
  2527. iat_start:=nil;
  2528. iat_end:=nil;
  2529. ilt_start:=nil;
  2530. for i:=0 to exesec.ObjSectionList.Count-1 do
  2531. begin
  2532. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2533. if (ilt_start=nil) and (Pos('.idata$4',objsec.Name)=1) then
  2534. ilt_start:=objsec;
  2535. if Pos('.idata$5',objsec.Name)=1 then
  2536. begin
  2537. if iat_start=nil then
  2538. iat_start:=objsec;
  2539. end
  2540. else
  2541. if Assigned(iat_start) then
  2542. begin
  2543. iat_end:=objsec;
  2544. Break;
  2545. end;
  2546. end;
  2547. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=exesec.mempos;
  2548. if Assigned(ilt_start) then
  2549. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=ilt_start.mempos-exesec.mempos
  2550. else { should not happen }
  2551. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=exesec.Size;
  2552. if Assigned(iat_start) and Assigned(iat_end) then
  2553. begin
  2554. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].vaddr:=iat_start.mempos;
  2555. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].size:=iat_end.mempos-iat_start.mempos;
  2556. end;
  2557. end;
  2558. procedure UpdateTlsDataDir;
  2559. var
  2560. {callbacksection : TExeSection;}
  2561. tlsexesymbol: TExeSymbol;
  2562. tlssymbol: TObjSymbol;
  2563. callbackexesymbol: TExeSymbol;
  2564. //callbacksymbol: TObjSymbol;
  2565. begin
  2566. { according to GNU ld,
  2567. the callback routines should be placed into .CRT$XL*
  2568. sections, and the thread local variables in .tls
  2569. __tls_start__ and __tls_end__ symbols
  2570. should be used for the initialized part,
  2571. which we do not support yet. }
  2572. { For now, we only pass the address of the __tls_used
  2573. asm symbol into PE_DATADIR_TLS with the correct
  2574. size of this table (different for win32/win64 }
  2575. tlsexesymbol:=texesymbol(ExeSymbolList.Find(
  2576. target_info.Cprefix+'_tls_used'));
  2577. if assigned(tlsexesymbol) then
  2578. begin
  2579. tlssymbol:=tlsexesymbol.ObjSymbol;
  2580. peoptheader.DataDirectory[PE_DATADIR_TLS].vaddr:=tlssymbol.address;
  2581. { sizeof(TlsDirectory) is different on host and target when cross-compiling }
  2582. peoptheader.DataDirectory[PE_DATADIR_TLS].size:=TLSDIR_SIZE;
  2583. if IsSharedLibrary then
  2584. begin
  2585. { Here we should reset __FPC_tls_callbacks value to nil }
  2586. callbackexesymbol:=texesymbol(ExeSymbolList.Find(
  2587. '__FPC_tls_callbacks'));
  2588. if assigned (callbackexesymbol) then
  2589. begin
  2590. //callbacksymbol:=callbackexesymbol.ObjSymbol;
  2591. end;
  2592. end;
  2593. end;
  2594. end;
  2595. begin
  2596. result:=false;
  2597. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  2598. textExeSec:=FindExeSection('.text');
  2599. dataExeSec:=FindExeSection('.data');
  2600. bssExeSec:=FindExeSection('.bss');
  2601. if not assigned(TextExeSec) or
  2602. not assigned(DataExeSec) then
  2603. internalerror(2006022302);
  2604. { do we need to write symbols? }
  2605. hassymbols:=(ExeWriteMode=ewm_dbgonly) or
  2606. (
  2607. (ExeWriteMode=ewm_exefull) and
  2608. not(cs_link_strip in current_settings.globalswitches)
  2609. );
  2610. writeDbgStrings:=hassymbols or ((ExeWriteMode=ewm_exeonly) and (cs_link_separate_dbg_file in current_settings.globalswitches));
  2611. { Stub }
  2612. if win32 then
  2613. begin
  2614. FWriter.write(win32stub,sizeof(win32stub));
  2615. FWriter.write(pemagic,sizeof(pemagic));
  2616. end
  2617. else
  2618. FWriter.write(go32v2stub,sizeof(go32v2stub));
  2619. { Initial header, will be updated later }
  2620. fillchar(header,sizeof(header),0);
  2621. header.mach:=COFF_MAGIC;
  2622. header.nsects:=nsects;
  2623. if writeDbgStrings then
  2624. header.sympos:=sympos-datapos_offset;
  2625. if hassymbols then
  2626. header.syms:=nsyms;
  2627. if win32 then
  2628. header.opthdr:=sizeof(tcoffpeoptheader)
  2629. else
  2630. header.opthdr:=sizeof(coffdjoptheader);
  2631. if win32 then
  2632. begin
  2633. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  2634. if target_info.system in [system_x86_64_win64,system_aarch64_win64] then
  2635. header.flag:=header.flag or PE_FILE_LARGE_ADDRESS_AWARE
  2636. else
  2637. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  2638. if IsSharedLibrary then
  2639. header.flag:=header.flag or PE_FILE_DLL;
  2640. if FindExeSection('.reloc')=nil then
  2641. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  2642. if (FindExeSection('.stab')=nil) and
  2643. (FindExeSection('.debug_info')=nil) and
  2644. (FindExeSection('.gnu_debuglink')=nil) then
  2645. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  2646. if not hassymbols then
  2647. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  2648. if SetPEFlagsSetExplicity then
  2649. header.flag:=header.flag or peflags;
  2650. end
  2651. else
  2652. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  2653. FWriter.write(header,sizeof(header));
  2654. { Optional COFF Header }
  2655. if win32 then
  2656. begin
  2657. fillchar(peoptheader,sizeof(peoptheader),0);
  2658. peoptheader.magic:=COFF_OPT_MAGIC;
  2659. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  2660. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  2661. peoptheader.tsize:=TextExeSec.Size;
  2662. peoptheader.dsize:=DataExeSec.Size;
  2663. if assigned(BSSExeSec) then
  2664. peoptheader.bsize:=BSSExeSec.Size;
  2665. peoptheader.text_start:=TextExeSec.mempos;
  2666. {$ifndef cpu64bitaddr}
  2667. peoptheader.data_start:=DataExeSec.mempos;
  2668. {$endif cpu64bitaddr}
  2669. peoptheader.entry:=EntrySym.Address;
  2670. peoptheader.ImageBase:=ImageBase;
  2671. peoptheader.SectionAlignment:=SectionMemAlign;
  2672. peoptheader.FileAlignment:=SectionDataAlign;
  2673. if SetPEOSVersionSetExplicitely then
  2674. begin
  2675. peoptheader.MajorOperatingSystemVersion:=peosversionmajor;
  2676. peoptheader.MinorOperatingSystemVersion:=peosversionminor;
  2677. end
  2678. else
  2679. begin
  2680. peoptheader.MajorOperatingSystemVersion:=4;
  2681. peoptheader.MinorOperatingSystemVersion:=0;
  2682. end;
  2683. if SetPEUserVersionSetExplicitely then
  2684. begin
  2685. peoptheader.MajorImageVersion:=peuserversionmajor;
  2686. peoptheader.MinorImageVersion:=peuserversionminor;
  2687. end
  2688. else
  2689. begin
  2690. peoptheader.MajorImageVersion:=dllmajor;
  2691. peoptheader.MinorImageVersion:=dllminor;
  2692. end;
  2693. if SetPESubSysVersionSetExplicitely then
  2694. begin
  2695. peoptheader.MajorSubsystemVersion:=pesubsysversionmajor;
  2696. peoptheader.MinorSubsystemVersion:=pesubsysversionminor;
  2697. end
  2698. else
  2699. begin
  2700. if target_info.system in systems_wince then
  2701. peoptheader.MajorSubsystemVersion:=3
  2702. else
  2703. peoptheader.MajorSubsystemVersion:=4;
  2704. peoptheader.MinorSubsystemVersion:=0;
  2705. end;
  2706. peoptheader.Win32Version:=0;
  2707. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  2708. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  2709. peoptheader.CheckSum:=0;
  2710. if (target_info.system in systems_nativent) and (not IsSharedLibrary or (apptype = app_native)) then
  2711. { Although I did not really test this, it seems that Subsystem is
  2712. not checked in DLLs except for maybe drivers}
  2713. peoptheader.Subsystem:=PE_SUBSYSTEM_NATIVE
  2714. else
  2715. if target_info.system in systems_wince then
  2716. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  2717. else
  2718. if apptype=app_gui then
  2719. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  2720. else
  2721. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  2722. if target_info.system in [system_aarch64_win64] then
  2723. peoptheader.DllCharacteristics:=PE_DLLCHARACTERISTICS_DYNAMIC_BASE or
  2724. PE_DLLCHARACTERISTICS_NX_COMPAT or
  2725. PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA
  2726. else
  2727. peoptheader.DllCharacteristics:=0;
  2728. if SetPEOptFlagsSetExplicity then
  2729. peoptheader.DllCharacteristics:=peoptheader.DllCharacteristics or peoptflags;
  2730. peoptheader.SizeOfStackReserve:=stacksize;
  2731. peoptheader.SizeOfStackCommit:=$1000;
  2732. if MinStackSizeSetExplicity then
  2733. peoptheader.SizeOfStackCommit:=minstacksize;
  2734. if MaxStackSizeSetExplicity then
  2735. peoptheader.SizeOfStackReserve:=maxstacksize;
  2736. peoptheader.SizeOfHeapReserve:=$100000;
  2737. peoptheader.SizeOfHeapCommit:=$1000;
  2738. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  2739. UpdateImports;
  2740. UpdateTlsDataDir;
  2741. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  2742. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  2743. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  2744. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  2745. FWriter.write(peoptheader,sizeof(peoptheader));
  2746. end
  2747. else
  2748. begin
  2749. fillchar(djoptheader,sizeof(djoptheader),0);
  2750. djoptheader.magic:=COFF_OPT_MAGIC;
  2751. djoptheader.tsize:=TextExeSec.Size;
  2752. djoptheader.dsize:=DataExeSec.Size;
  2753. if assigned(BSSExeSec) then
  2754. djoptheader.bsize:=BSSExeSec.Size;
  2755. djoptheader.text_start:=TextExeSec.mempos;
  2756. djoptheader.data_start:=DataExeSec.mempos;
  2757. djoptheader.entry:=EntrySym.Address;
  2758. FWriter.write(djoptheader,sizeof(djoptheader));
  2759. end;
  2760. { For some unknown reason WM 6.1 requires .idata section to be read only.
  2761. Otherwise it refuses to load DLLs greater than 64KB.
  2762. Earlier versions of WinCE load DLLs regardless of .idata flags. }
  2763. if target_info.system in systems_wince then
  2764. begin
  2765. idataExeSec:=FindExeSection('.idata');
  2766. if idataExeSec<>nil then
  2767. idataExeSec.SecOptions:=idataExeSec.SecOptions - [oso_write];
  2768. end;
  2769. { Section headers }
  2770. ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
  2771. { Section data }
  2772. WriteExeSectionContent;
  2773. { Align after the last section }
  2774. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  2775. { Optional Symbols }
  2776. if SymPos<>FWriter.Size then
  2777. internalerror(200602252);
  2778. if hassymbols then
  2779. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  2780. if writeDbgStrings then
  2781. begin
  2782. { Strings }
  2783. i:=FCoffStrs.size+4;
  2784. FWriter.write(i,4);
  2785. FWriter.writearray(FCoffStrs);
  2786. end;
  2787. { Release }
  2788. FCoffStrs.Free;
  2789. result:=true;
  2790. end;
  2791. function IdataObjSectionCompare(Item1, Item2: Pointer): Integer;
  2792. var
  2793. I1 : TObjSection absolute Item1;
  2794. I2 : TObjSection absolute Item2;
  2795. begin
  2796. Result:=CompareStr(I1.Name,I2.Name);
  2797. end;
  2798. procedure TCoffexeoutput.Order_ObjSectionList(ObjSectionList: TFPObjectList;const aPattern:string);
  2799. begin
  2800. { Sort sections having '$' in the name, that's how PECOFF documentation
  2801. tells to handle them. However, look for '$' in the pattern, not in section
  2802. names, because the latter often get superfluous '$' due to mangling. }
  2803. if Pos('$',aPattern)>0 then
  2804. ObjSectionList.Sort(@IdataObjSectionCompare);
  2805. end;
  2806. constructor TDJCoffexeoutput.create;
  2807. begin
  2808. inherited createcoff(false);
  2809. datapos_offset:=sizeof(go32v2stub);
  2810. CExeSection:=TExeSection;
  2811. CObjData:=TDJCoffObjData;
  2812. end;
  2813. procedure TDJCoffexeoutput.MemPos_Header;
  2814. begin
  2815. { Headers are not loaded, first 4K page is reserved }
  2816. CurrMemPos:=$1000;
  2817. end;
  2818. constructor TPECoffexeoutput.create;
  2819. begin
  2820. inherited createcoff(true);
  2821. CExeSection:=TExeSection;
  2822. CObjData:=TPECoffObjData;
  2823. end;
  2824. procedure TPECoffexeoutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  2825. var
  2826. exesec:TExeSection;
  2827. objsec,textsec:TObjSection;
  2828. objreloc:TObjRelocation;
  2829. i,j:longint;
  2830. begin
  2831. if not (target_info.system in [system_x86_64_win64,system_aarch64_win64]) then
  2832. exit;
  2833. exesec:=FindExeSection('.pdata');
  2834. if exesec=nil then
  2835. exit;
  2836. for i:=0 to exesec.ObjSectionList.Count-1 do
  2837. begin
  2838. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2839. if objsec.Used then
  2840. continue;
  2841. j:=0;
  2842. while j<objsec.ObjRelocations.Count do
  2843. begin
  2844. objreloc:=TObjRelocation(objsec.ObjRelocations[j]);
  2845. if objreloc.symbol=nil then
  2846. InternalError(2013041201);
  2847. textsec:=objreloc.symbol.objsection;
  2848. if textsec.used then
  2849. begin
  2850. WorkList.Add(objsec);
  2851. objsec.used:=true;
  2852. { The exact algorithm for non-smartlinked .pdata sections
  2853. is subject for refinement. Extreme cases are:
  2854. - several disjoint .pdata entries for a function, if function
  2855. is complex or if compiler splits it into chunks,
  2856. - single .pdata section referencing several .text sections,
  2857. may need to remove irrelevant parts like BFD does for
  2858. .eh_frame sections. }
  2859. break;
  2860. end;
  2861. if target_info.system=system_aarch64_win64 then
  2862. inc(j,2)
  2863. else
  2864. inc(j,3);
  2865. end;
  2866. end;
  2867. end;
  2868. procedure TPECoffexeoutput.AfterUnusedSectionRemoval;
  2869. var
  2870. basedllname : string;
  2871. procedure StartImport(const dllname:string);
  2872. begin
  2873. if assigned(exemap) then
  2874. begin
  2875. exemap.Add('');
  2876. exemap.Add('Importing from DLL '+dllname);
  2877. end;
  2878. basedllname:=ExtractFileName(dllname);
  2879. { idata2 }
  2880. idata2objsection.writereloc_internal(idata4objsection,idata4objsection.size,sizeof(longint),RELOC_RVA);
  2881. idata2objsection.writezeros(2*sizeof(longint));
  2882. idata2objsection.writereloc_internal(idata7objsection,idata7objsection.size,sizeof(longint),RELOC_RVA);
  2883. idata2objsection.writereloc_internal(idata5objsection,idata5objsection.size,sizeof(longint),RELOC_RVA);
  2884. { idata7 }
  2885. idata7objsection.writestr(basedllname);
  2886. end;
  2887. procedure EndImport;
  2888. begin
  2889. { idata4 }
  2890. idata4objsection.writezeros(sizeof(longint));
  2891. if target_info.system in systems_peoptplus then
  2892. idata4objsection.writezeros(sizeof(longint));
  2893. { idata5 }
  2894. idata5objsection.writezeros(sizeof(longint));
  2895. if target_info.system in systems_peoptplus then
  2896. idata5objsection.writezeros(sizeof(longint));
  2897. end;
  2898. function AddImport(const afuncname,amangledname:string; AOrdNr:longint;isvar:boolean):TObjSymbol;
  2899. const
  2900. {$if defined(arm)}
  2901. jmpopcode : array[0..7] of byte = (
  2902. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  2903. $00,$f0,$9c,$e5 // ldr pc, [ip]
  2904. );
  2905. {$elseif defined(aarch64)}
  2906. jmpopcode : array[0..11] of byte = (
  2907. $70,$00,$00,$58, // ldr ip0, .+12
  2908. $10,$02,$40,$F9, // ldr ip0, [ip0]
  2909. $00,$02,$1F,$D6 // br ip0
  2910. );
  2911. {$else}
  2912. jmpopcode : array[0..1] of byte = (
  2913. $ff,$25
  2914. );
  2915. {$endif}
  2916. nopopcodes : array[0..1] of byte = (
  2917. $90,$90
  2918. );
  2919. var
  2920. ordint: dword;
  2921. procedure WriteTableEntry(objsec:TObjSection);
  2922. begin
  2923. if AOrdNr <= 0 then
  2924. begin
  2925. objsec.writereloc_internal(idata6objsection,idata6objsection.size,sizeof(longint),RELOC_RVA);
  2926. if target_info.system in systems_peoptplus then
  2927. objsec.writezeros(sizeof(longint));
  2928. end
  2929. else
  2930. begin
  2931. { import by ordinal }
  2932. ordint:=AOrdNr;
  2933. if target_info.system in systems_peoptplus then
  2934. begin
  2935. objsec.write(ordint,sizeof(ordint));
  2936. ordint:=$80000000;
  2937. objsec.write(ordint,sizeof(ordint));
  2938. end
  2939. else
  2940. begin
  2941. ordint:=ordint or $80000000;
  2942. objsec.write(ordint,sizeof(ordint));
  2943. end;
  2944. end;
  2945. end;
  2946. begin
  2947. result:=nil;
  2948. if assigned(exemap) then
  2949. begin
  2950. if AOrdNr <= 0 then
  2951. exemap.Add(' Importing Function '+afuncname)
  2952. else
  2953. exemap.Add(' Importing Function '+afuncname+' (OrdNr='+tostr(AOrdNr)+')');
  2954. end;
  2955. { idata4, import lookup table }
  2956. WriteTableEntry(idata4objsection);
  2957. { idata5, import address table }
  2958. internalobjdata.SetSection(idata5objsection);
  2959. if isvar then
  2960. result:=internalobjdata.SymbolDefine(amangledname,AB_GLOBAL,AT_DATA)
  2961. else
  2962. begin
  2963. internalobjdata.SetSection(textobjsection);
  2964. textobjsection.writezeros(align_aword(textobjsection.size,16)-textobjsection.size);
  2965. result:=internalobjdata.SymbolDefine('_'+amangledname,AB_GLOBAL,AT_FUNCTION);
  2966. textobjsection.write(jmpopcode,sizeof(jmpopcode));
  2967. {$if defined(x86_64)}
  2968. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_RELATIVE);
  2969. {$elseif defined(aarch64)}
  2970. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,8,RELOC_ABSOLUTE);
  2971. {$else}
  2972. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_ABSOLUTE32);
  2973. {$endif x86_64 or aarch64}
  2974. textobjsection.write(nopopcodes,align(textobjsection.size,qword(sizeof(nopopcodes)))-textobjsection.size);
  2975. end;
  2976. { idata5 section data }
  2977. WriteTableEntry(idata5objsection);
  2978. if (AOrdNr<=0) then
  2979. begin
  2980. { index hint, function name, null terminator and align }
  2981. ordint:=abs(AOrdNr);
  2982. idata6objsection.write(ordint,2);
  2983. idata6objsection.writestr(afuncname);
  2984. idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
  2985. end;
  2986. end;
  2987. var
  2988. i,j : longint;
  2989. ImportLibrary : TImportLibrary;
  2990. ImportSymbol : TImportSymbol;
  2991. exesym : TExeSymbol;
  2992. newdll : boolean;
  2993. begin
  2994. for i:=0 to FImports.Count-1 do
  2995. begin
  2996. ImportLibrary:=TImportLibrary(FImports[i]);
  2997. newdll:=False;
  2998. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  2999. begin
  3000. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  3001. exesym:=ImportSymbol.CachedExeSymbol;
  3002. if assigned(exesym) and
  3003. exesym.Used then
  3004. begin
  3005. if (not newdll) then
  3006. StartImport(ImportLibrary.Name);
  3007. newdll:=True;
  3008. exesym.objsymbol:=AddImport(ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  3009. end;
  3010. end;
  3011. if newdll then
  3012. EndImport;
  3013. end;
  3014. FixupSymbols;
  3015. end;
  3016. procedure TPECoffexeoutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  3017. var
  3018. i,j: longint;
  3019. ImportLibrary: TImportLibrary;
  3020. ImportSymbol: TImportSymbol;
  3021. exesym: TExeSymbol;
  3022. begin
  3023. { Here map import symbols to exe symbols and create necessary sections.
  3024. Actual import generation is done after unused sections (and symbols) are removed. }
  3025. FImports:=ImportLibraryList;
  3026. textobjsection:=internalObjData.CreateSection(sec_code);
  3027. textobjsection.SecOptions:=[oso_keep];
  3028. idata2objsection:=internalObjData.CreateSection(sec_idata2);
  3029. idata2objsection.SecOptions:=[oso_keep];
  3030. idata4objsection:=internalObjData.CreateSection(sec_idata4);
  3031. idata4objsection.SecOptions:=[oso_keep];
  3032. idata5objsection:=internalObjData.CreateSection(sec_idata5);
  3033. idata5objsection.SecOptions:=[oso_keep];
  3034. idata6objsection:=internalObjData.CreateSection(sec_idata6);
  3035. idata6objsection.SecOptions:=[oso_keep];
  3036. idata7objsection:=internalObjData.CreateSection(sec_idata7);
  3037. idata7objsection.SecOptions:=[oso_keep];
  3038. for i:=0 to ImportLibraryList.Count-1 do
  3039. begin
  3040. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  3041. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  3042. begin
  3043. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  3044. exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
  3045. if assigned(exesym) and
  3046. (exesym.State<>symstate_defined) then
  3047. begin
  3048. ImportSymbol.CachedExeSymbol:=exesym;
  3049. exesym.State:=symstate_defined;
  3050. end;
  3051. end;
  3052. end;
  3053. PackUnresolvedExeSymbols('after DLL imports');
  3054. end;
  3055. procedure TPECoffexeoutput.GenerateRelocs;
  3056. var
  3057. pgaddr, hdrpos : longword;
  3058. procedure FinishBlock;
  3059. var
  3060. p,len : longint;
  3061. begin
  3062. if hdrpos = longword(-1) then
  3063. exit;
  3064. p:=0;
  3065. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  3066. p:=internalObjData.CurrObjSec.Data.Pos;
  3067. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  3068. len:=p-hdrpos;
  3069. internalObjData.CurrObjSec.Data.write(len,4);
  3070. internalObjData.CurrObjSec.Data.seek(p);
  3071. hdrpos:=longword(-1);
  3072. end;
  3073. var
  3074. exesec : TExeSection;
  3075. objsec : TObjSection;
  3076. objreloc : TObjRelocation;
  3077. i,j,k : longint;
  3078. offset : longword;
  3079. w: word;
  3080. begin
  3081. if not RelocSection or FRelocsGenerated then
  3082. exit;
  3083. exesec:=FindExeSection('.reloc');
  3084. if exesec=nil then
  3085. exit;
  3086. objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_keep]);
  3087. exesec.AddObjSection(objsec);
  3088. pgaddr:=longword(-1);
  3089. hdrpos:=longword(-1);
  3090. for i:=0 to ExeSectionList.Count-1 do
  3091. begin
  3092. exesec:=TExeSection(ExeSectionList[i]);
  3093. for j:=0 to exesec.ObjSectionList.count-1 do
  3094. begin
  3095. objsec:=TObjSection(exesec.ObjSectionList[j]);
  3096. { create relocs only for sections which are loaded in memory }
  3097. if not (oso_load in objsec.SecOptions) then
  3098. continue;
  3099. for k:=0 to objsec.ObjRelocations.Count-1 do
  3100. begin
  3101. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  3102. if not (objreloc.typ in [{$ifdef cpu64bitaddr}RELOC_ABSOLUTE32,{$endif cpu64bitaddr}RELOC_ABSOLUTE]) then
  3103. continue;
  3104. offset:=objsec.MemPos+objreloc.dataoffset;
  3105. if (offset<pgaddr) and (pgaddr<>longword(-1)) then
  3106. Internalerror(2007062701);
  3107. if (offset-pgaddr>=4096) or (pgaddr=longword(-1)) then
  3108. begin
  3109. FinishBlock;
  3110. pgaddr:=(offset div 4096)*4096;
  3111. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  3112. internalObjData.writebytes(pgaddr,4);
  3113. { Reserving space for block size. The size will be written later in FinishBlock }
  3114. internalObjData.writebytes(k,4);
  3115. end;
  3116. {$ifdef cpu64bitaddr}
  3117. if objreloc.typ = RELOC_ABSOLUTE then
  3118. w:=IMAGE_REL_BASED_DIR64
  3119. else
  3120. {$endif cpu64bitaddr}
  3121. w:=IMAGE_REL_BASED_HIGHLOW;
  3122. w:=(w shl 12) or (offset-pgaddr);
  3123. internalObjData.writebytes(w,2);
  3124. end;
  3125. end;
  3126. end;
  3127. FinishBlock;
  3128. FRelocsGenerated:=true;
  3129. end;
  3130. procedure TPECoffexeoutput.MemPos_Start;
  3131. var
  3132. exesec : TExeSection;
  3133. begin
  3134. if RelocSection then
  3135. begin
  3136. exesec:=FindExeSection('.reloc');
  3137. if exesec=nil then
  3138. InternalError(2012072403);
  3139. exesec.Disabled:=false;
  3140. end;
  3141. inherited;
  3142. end;
  3143. procedure TPECoffexeoutput.MemPos_ExeSection(const aname:string);
  3144. begin
  3145. if aname='.reloc' then
  3146. GenerateRelocs;
  3147. inherited;
  3148. end;
  3149. {****************************************************************************
  3150. TDJCoffAssembler
  3151. ****************************************************************************}
  3152. constructor TDJCoffAssembler.Create(info: pasminfo; smart:boolean);
  3153. begin
  3154. inherited;
  3155. CObjOutput:=TDJCoffObjOutput;
  3156. CInternalAr:=tarobjectwriter;
  3157. end;
  3158. {****************************************************************************
  3159. TPECoffAssembler
  3160. ****************************************************************************}
  3161. constructor TPECoffAssembler.Create(info: pasminfo; smart:boolean);
  3162. begin
  3163. inherited;
  3164. CObjOutput:=TPECoffObjOutput;
  3165. CInternalAr:=tarobjectwriter;
  3166. end;
  3167. {*****************************************************************************
  3168. DLLReader
  3169. *****************************************************************************}
  3170. {$ifdef win32}
  3171. var
  3172. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  3173. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  3174. {$endif win32}
  3175. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  3176. type
  3177. TPECoffExpDir=packed record
  3178. flag,
  3179. stamp : cardinal;
  3180. Major,
  3181. Minor : word;
  3182. Name,
  3183. Base,
  3184. NumFuncs,
  3185. NumNames,
  3186. AddrFuncs,
  3187. AddrNames,
  3188. AddrOrds : cardinal;
  3189. end;
  3190. var
  3191. DLLReader : TObjectReader;
  3192. DosHeader : array[0..$7f] of byte;
  3193. PEMagic : array[0..3] of byte;
  3194. Header : TCoffHeader;
  3195. peheader : tcoffpeoptheader;
  3196. NameOfs,
  3197. newheaderofs : longword;
  3198. FuncName : string;
  3199. expdir : TPECoffExpDir;
  3200. i : longint;
  3201. found : boolean;
  3202. sechdr : tCoffSecHdr;
  3203. {$ifdef win32}
  3204. p : pointer;
  3205. {$endif win32}
  3206. begin
  3207. result:=false;
  3208. fillchar(sechdr,sizeof(sechdr),0);
  3209. {$ifdef win32}
  3210. if (target_info.system=system_x86_64_win64) and
  3211. assigned(Wow64DisableWow64FsRedirection) then
  3212. Wow64DisableWow64FsRedirection(p);
  3213. {$endif win32}
  3214. DLLReader:=TObjectReader.Create;
  3215. DLLReader.OpenFile(dllname);
  3216. {$ifdef win32}
  3217. if (target_info.system=system_x86_64_win64) and
  3218. assigned(Wow64RevertWow64FsRedirection) then
  3219. Wow64RevertWow64FsRedirection(p);
  3220. {$endif win32}
  3221. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  3222. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  3223. begin
  3224. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  3225. exit;
  3226. end;
  3227. newheaderofs:=cardinal(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  3228. DLLReader.Seek(newheaderofs);
  3229. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  3230. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  3231. begin
  3232. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  3233. exit;
  3234. end;
  3235. if not DLLReader.Read(Header,sizeof(TCoffHeader)) or
  3236. (Header.mach<>COFF_MAGIC) or
  3237. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  3238. begin
  3239. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  3240. exit;
  3241. end;
  3242. { Read optheader }
  3243. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  3244. { Section headers }
  3245. found:=false;
  3246. for i:=1 to header.nsects do
  3247. begin
  3248. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  3249. begin
  3250. Comment(V_Error,'Error reading coff file '+DLLName);
  3251. exit;
  3252. end;
  3253. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  3254. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  3255. begin
  3256. found:=true;
  3257. break;
  3258. end;
  3259. end;
  3260. if not found then
  3261. begin
  3262. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  3263. exit;
  3264. end;
  3265. { Process edata }
  3266. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  3267. DLLReader.Read(expdir,sizeof(expdir));
  3268. for i:=0 to expdir.NumNames-1 do
  3269. begin
  3270. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  3271. DLLReader.Read(NameOfs,4);
  3272. Dec(NameOfs,sechdr.rvaofs);
  3273. if {(NameOfs<0) or}
  3274. (NameOfs>sechdr.vsize) then
  3275. begin
  3276. Comment(V_Error,'DLL does contains invalid exports');
  3277. break;
  3278. end;
  3279. { Read Function name from DLL, prepend _ and terminate with #0 }
  3280. DLLReader.Seek(sechdr.datapos+NameOfs);
  3281. DLLReader.Read((@FuncName[1])^,sizeof(FuncName)-3);
  3282. FuncName[sizeof(FuncName)-1]:=#0;
  3283. FuncName[0]:=chr(Strlen(@FuncName[1]));
  3284. readdllproc(DLLName,FuncName);
  3285. end;
  3286. DLLReader.Free;
  3287. end;
  3288. {$ifdef arm}
  3289. function COFF_MAGIC: word;
  3290. begin
  3291. if GenerateThumb2Code and (current_settings.cputype>=cpu_armv7) then
  3292. COFF_MAGIC:=$1c4 // IMAGE_FILE_MACHINE_ARMNT
  3293. else
  3294. COFF_MAGIC:=$1c0; // IMAGE_FILE_MACHINE_ARM
  3295. end;
  3296. {$endif arm}
  3297. {*****************************************************************************
  3298. Initialize
  3299. *****************************************************************************}
  3300. {$ifdef i386}
  3301. const
  3302. as_i386_coff_info : tasminfo =
  3303. (
  3304. id : as_i386_coff;
  3305. idtxt : 'COFF';
  3306. asmbin : '';
  3307. asmcmd : '';
  3308. supported_targets : [system_i386_go32v2];
  3309. flags : [af_outputbinary,af_smartlink_sections];
  3310. labelprefix : '.L';
  3311. labelmaxlen : -1;
  3312. comment : '';
  3313. dollarsign: '$';
  3314. );
  3315. as_i386_pecoff_info : tasminfo =
  3316. (
  3317. id : as_i386_pecoff;
  3318. idtxt : 'PECOFF';
  3319. asmbin : '';
  3320. asmcmd : '';
  3321. supported_targets : [system_i386_win32,system_i386_nativent];
  3322. flags : [af_outputbinary,af_smartlink_sections];
  3323. labelprefix : '.L';
  3324. labelmaxlen : -1;
  3325. comment : '';
  3326. dollarsign: '$';
  3327. );
  3328. as_i386_pecoffwdosx_info : tasminfo =
  3329. (
  3330. id : as_i386_pecoffwdosx;
  3331. idtxt : 'PEWDOSX';
  3332. asmbin : '';
  3333. asmcmd : '';
  3334. supported_targets : [system_i386_wdosx];
  3335. flags : [af_outputbinary];
  3336. labelprefix : '.L';
  3337. labelmaxlen : -1;
  3338. comment : '';
  3339. dollarsign: '$';
  3340. );
  3341. as_i386_pecoffwince_info : tasminfo =
  3342. (
  3343. id : as_i386_pecoffwince;
  3344. idtxt : 'PECOFFWINCE';
  3345. asmbin : '';
  3346. asmcmd : '';
  3347. supported_targets : [system_i386_wince];
  3348. flags : [af_outputbinary,af_smartlink_sections];
  3349. labelprefix : '.L';
  3350. labelmaxlen : -1;
  3351. comment : '';
  3352. dollarsign: '$';
  3353. );
  3354. {$endif i386}
  3355. {$ifdef x86_64}
  3356. const
  3357. as_x86_64_pecoff_info : tasminfo =
  3358. (
  3359. id : as_x86_64_pecoff;
  3360. idtxt : 'PECOFF';
  3361. asmbin : '';
  3362. asmcmd : '';
  3363. supported_targets : [system_x86_64_win64];
  3364. flags : [af_outputbinary,af_smartlink_sections];
  3365. labelprefix : '.L';
  3366. labelmaxlen : -1;
  3367. comment : '';
  3368. dollarsign: '$';
  3369. );
  3370. {$endif x86_64}
  3371. {$ifdef arm}
  3372. const
  3373. as_arm_pecoffwince_info : tasminfo =
  3374. (
  3375. id : as_arm_pecoffwince;
  3376. idtxt : 'PECOFFWINCE';
  3377. asmbin : '';
  3378. asmcmd : '';
  3379. supported_targets : [system_arm_wince];
  3380. flags : [af_outputbinary,af_smartlink_sections];
  3381. labelprefix : '.L';
  3382. labelmaxlen : -1;
  3383. comment : '';
  3384. dollarsign: '$';
  3385. );
  3386. {$endif arm}
  3387. {$ifdef win32}
  3388. procedure SetupProcVars;
  3389. var
  3390. hinstLib : THandle;
  3391. begin
  3392. Wow64DisableWow64FsRedirection:=nil;
  3393. Wow64RevertWow64FsRedirection:=nil;
  3394. hinstLib:=LoadLibrary('kernel32.dll');
  3395. if hinstLib<>0 then
  3396. begin
  3397. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  3398. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  3399. FreeLibrary(hinstLib);
  3400. end;
  3401. end;
  3402. {$endif win32}
  3403. initialization
  3404. {$ifdef i386}
  3405. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  3406. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  3407. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  3408. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  3409. {$endif i386}
  3410. {$ifdef x86_64}
  3411. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  3412. {$endif x86_64}
  3413. {$ifdef arm}
  3414. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  3415. {$endif arm}
  3416. {$ifdef win32}
  3417. SetupProcVars;
  3418. {$endif win32}
  3419. end.