ogcoff.pas 113 KB

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