ogcoff.pas 113 KB

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