ogcoff.pas 114 KB

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