ogcoff.pas 115 KB

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