ogcoff.pas 113 KB

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