ogcoff.pas 113 KB

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