ogcoff.pas 115 KB

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