ogcoff.pas 113 KB

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