ogcoff.pas 113 KB

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