ogcoff.pas 111 KB

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