ogcoff.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  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. type
  31. TCoffObjSection = class(TObjSection)
  32. private
  33. orgmempos,
  34. coffrelocs,
  35. coffrelocpos : aint;
  36. public
  37. secidx : longint;
  38. flags : longword;
  39. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  40. procedure addsymsizereloc(ofs:aint;p:TObjSymbol;symsize:aint;reloctype:TObjRelocationType);
  41. procedure fixuprelocs;override;
  42. end;
  43. TDJCoffObjSection = class(TCoffObjSection)
  44. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  45. end;
  46. TPECoffObjSection = class(TCoffObjSection)
  47. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  48. end;
  49. TCoffObjData = class(TObjData)
  50. private
  51. win32 : boolean;
  52. public
  53. constructor createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  54. destructor destroy;override;
  55. procedure CreateDebugSections;override;
  56. function sectionname(atype:TAsmSectiontype;const aname:string):string;override;
  57. procedure writereloc(data,len:aint;p:TObjSymbol;reloctype:TObjRelocationType);override;
  58. procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
  59. procedure afteralloc;override;
  60. end;
  61. TDJCoffObjData = class(TCoffObjData)
  62. constructor create(const n:string);override;
  63. end;
  64. TPECoffObjData = class(TCoffObjData)
  65. constructor create(const n:string);override;
  66. end;
  67. TCoffObjOutput = class(tObjOutput)
  68. private
  69. win32 : boolean;
  70. symidx : longint;
  71. FCoffSyms,
  72. FCoffStrs : tdynamicarray;
  73. procedure write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  74. procedure section_write_symbol(p:TObject;arg:pointer);
  75. procedure section_write_relocs(p:TObject;arg:pointer);
  76. procedure create_symbols(data:TObjData);
  77. procedure section_set_datapos(p:TObject;arg:pointer);
  78. procedure section_set_reloc_datapos(p:TObject;arg:pointer);
  79. procedure section_write_header(p:TObject;arg:pointer);
  80. procedure section_write_data(p:TObject;arg:pointer);
  81. protected
  82. function writedata(data:TObjData):boolean;override;
  83. public
  84. constructor createcoff(AWriter:TObjectWriter;awin32:boolean);
  85. destructor destroy;override;
  86. end;
  87. TDJCoffObjOutput = class(TCoffObjOutput)
  88. constructor create(AWriter:TObjectWriter);override;
  89. end;
  90. TPECoffObjOutput = class(TCoffObjOutput)
  91. constructor create(AWriter:TObjectWriter);override;
  92. end;
  93. TCoffObjInput = class(tObjInput)
  94. private
  95. FCoffsyms,
  96. FCoffStrs : tdynamicarray;
  97. { Convert symidx -> TObjSymbol }
  98. FSymTbl : ^TObjSymbolArray;
  99. { Convert secidx -> TObjSection }
  100. FSecCount : smallint;
  101. FSecTbl : ^TObjSectionArray;
  102. win32 : boolean;
  103. function GetSection(secidx:longint):TObjSection;
  104. function Read_str(strpos:longint):string;
  105. procedure read_relocs(s:TCoffObjSection);
  106. procedure read_symbols(objdata:TObjData);
  107. procedure ObjSections_read_data(p:TObject;arg:pointer);
  108. procedure ObjSections_read_relocs(p:TObject;arg:pointer);
  109. protected
  110. function readObjData(objdata:TObjData):boolean;override;
  111. public
  112. constructor createcoff(awin32:boolean);
  113. destructor destroy;override;
  114. end;
  115. TDJCoffObjInput = class(TCoffObjInput)
  116. constructor create;override;
  117. end;
  118. TPECoffObjInput = class(TCoffObjInput)
  119. constructor create;override;
  120. end;
  121. TCoffExeSection = class(TExeSection)
  122. private
  123. win32 : boolean;
  124. public
  125. constructor createcoff(AList:TFPHashObjectList;const n:string;awin32:boolean);
  126. end;
  127. TDJCoffExeSection = class(TCoffExeSection)
  128. constructor create(AList:TFPHashObjectList;const n:string);override;
  129. end;
  130. TPECoffExeSection = class(TCoffExeSection)
  131. constructor create(AList:TFPHashObjectList;const n:string);override;
  132. end;
  133. TCoffexeoutput = class(texeoutput)
  134. private
  135. FCoffsyms,
  136. FCoffStrs : tdynamicarray;
  137. win32 : boolean;
  138. nsects : smallint;
  139. nsyms,
  140. sympos : aint;
  141. procedure ExeSections_pass2_header(p:TObject;arg:pointer);
  142. procedure write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  143. procedure globalsyms_write_symbol(p:TObject;arg:pointer);
  144. procedure ExeSections_write_header(p:TObject;arg:pointer);
  145. procedure ExeSections_write_data(p:TObject;arg:pointer);
  146. protected
  147. procedure CalcPos_Header;override;
  148. procedure CalcPos_Symbols;override;
  149. function writedata:boolean;override;
  150. public
  151. constructor createcoff(awin32:boolean);
  152. end;
  153. TDJCoffexeoutput = class(TCoffexeoutput)
  154. constructor create;override;
  155. end;
  156. TPECoffexeoutput = class(TCoffexeoutput)
  157. private
  158. idatalabnr : longint;
  159. public
  160. constructor create;override;
  161. procedure GenerateLibraryImports(ExternalLibraryList:TFPHashObjectList);override;
  162. end;
  163. TObjSymbolrec = record
  164. sym : TObjSymbol;
  165. orgsize : aint;
  166. end;
  167. TObjSymbolArray = array[0..high(word)] of TObjSymbolrec;
  168. TObjSectionArray = array[0..high(smallint)] of TObjSection;
  169. TDJCoffAssembler = class(tinternalassembler)
  170. constructor create(smart:boolean);override;
  171. end;
  172. TPECoffassembler = class(tinternalassembler)
  173. constructor create(smart:boolean);override;
  174. end;
  175. TDJCofflinker = class(tinternallinker)
  176. constructor create;override;
  177. procedure DefaultLinkScript;override;
  178. end;
  179. TPECofflinker = class(tinternallinker)
  180. constructor create;override;
  181. procedure DefaultLinkScript;override;
  182. end;
  183. type
  184. Treaddllproc = procedure(const dllname,funcname:string) of object;
  185. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  186. implementation
  187. uses
  188. {$ifdef win32}
  189. windows,
  190. {$endif win32}
  191. cutils,verbose,globals,
  192. fmodule,aasmtai,aasmdata,
  193. ogmap
  194. ;
  195. const
  196. {$ifdef i386}
  197. COFF_MAGIC = $14c;
  198. COFF_OPT_MAGIC = $10b;
  199. {$endif i386}
  200. {$ifdef arm}
  201. COFF_MAGIC = $1c0;
  202. COFF_OPT_MAGIC = $10b;
  203. {$endif arm}
  204. {$ifdef x86_64}
  205. COFF_MAGIC = $8664;
  206. COFF_OPT_MAGIC = $20b;
  207. {$endif x86_64}
  208. COFF_FLAG_NORELOCS = $0001;
  209. COFF_FLAG_EXE = $0002;
  210. COFF_FLAG_NOLINES = $0004;
  211. COFF_FLAG_NOLSYMS = $0008;
  212. COFF_FLAG_AR16WR = $0080; { 16bit little endian }
  213. COFF_FLAG_AR32WR = $0100; { 32bit little endian }
  214. COFF_FLAG_AR32W = $0200; { 32bit big endian }
  215. COFF_FLAG_DLL = $2000;
  216. COFF_SYM_GLOBAL = 2;
  217. COFF_SYM_LOCAL = 3;
  218. COFF_SYM_LABEL = 6;
  219. COFF_SYM_FUNCTION = 101;
  220. COFF_SYM_FILE = 103;
  221. COFF_SYM_SECTION = 104;
  222. COFF_STYP_REG = $0000; { "regular": allocated, relocated, loaded }
  223. COFF_STYP_DSECT = $0001; { "dummy": relocated only }
  224. COFF_STYP_NOLOAD = $0002; { "noload": allocated, relocated, not loaded }
  225. COFF_STYP_GROUP = $0004; { "grouped": formed of input sections }
  226. COFF_STYP_PAD = $0008;
  227. COFF_STYP_COPY = $0010;
  228. COFF_STYP_TEXT = $0020;
  229. COFF_STYP_DATA = $0040;
  230. COFF_STYP_BSS = $0080;
  231. PE_SUBSYSTEM_WINDOWS_GUI = 2;
  232. PE_SUBSYSTEM_WINDOWS_CUI = 3;
  233. PE_SUBSYSTEM_WINDOWS_CE_GUI = 9;
  234. PE_FILE_RELOCS_STRIPPED = $0001;
  235. PE_FILE_EXECUTABLE_IMAGE = $0002;
  236. PE_FILE_LINE_NUMS_STRIPPED = $0004;
  237. PE_FILE_LOCAL_SYMS_STRIPPED = $0008;
  238. PE_FILE_AGGRESSIVE_WS_TRIM = $0010;
  239. PE_FILE_LARGE_ADDRESS_AWARE = $0020;
  240. PE_FILE_16BIT_MACHINE = $0040;
  241. PE_FILE_BYTES_REVERSED_LO = $0080;
  242. PE_FILE_32BIT_MACHINE = $0100;
  243. PE_FILE_DEBUG_STRIPPED = $0200;
  244. PE_FILE_REMOVABLE_RUN_FROM_SWAP = $0400;
  245. PE_FILE_NET_RUN_FROM_SWAP = $0800;
  246. PE_FILE_SYSTEM = $1000;
  247. PE_FILE_DLL = $2000;
  248. PE_FILE_UP_SYSTEM_ONLY = $4000;
  249. PE_FILE_BYTES_REVERSED_HI = $8000;
  250. PE_SCN_CNT_CODE = $00000020; { Section contains code. }
  251. PE_SCN_CNT_INITIALIZED_DATA = $00000040; { Section contains initialized data. }
  252. PE_SCN_CNT_UNINITIALIZED_DATA = $00000080; { Section contains uninitialized data. }
  253. PE_SCN_LNK_OTHER = $00000100; { Reserved. }
  254. PE_SCN_LNK_INFO = $00000200; { Section contains comments or some other type of information. }
  255. PE_SCN_LNK_REMOVE = $00000800; { Section contents will not become part of image. }
  256. PE_SCN_LNK_COMDAT = $00001000; { Section contents comdat. }
  257. PE_SCN_MEM_FARDATA = $00008000;
  258. PE_SCN_MEM_PURGEABLE = $00020000;
  259. PE_SCN_MEM_16BIT = $00020000;
  260. PE_SCN_MEM_LOCKED = $00040000;
  261. PE_SCN_MEM_PRELOAD = $00080000;
  262. PE_SCN_ALIGN_MASK = $00f00000;
  263. PE_SCN_ALIGN_1BYTES = $00100000;
  264. PE_SCN_ALIGN_2BYTES = $00200000;
  265. PE_SCN_ALIGN_4BYTES = $00300000;
  266. PE_SCN_ALIGN_8BYTES = $00400000;
  267. PE_SCN_ALIGN_16BYTES = $00500000; { Default alignment if no others are specified. }
  268. PE_SCN_ALIGN_32BYTES = $00600000;
  269. PE_SCN_ALIGN_64BYTES = $00700000;
  270. PE_SCN_LNK_NRELOC_OVFL = $01000000; { Section contains extended relocations. }
  271. PE_SCN_MEM_NOT_CACHED = $04000000; { Section is not cachable. }
  272. PE_SCN_MEM_NOT_PAGED = $08000000; { Section is not pageable. }
  273. PE_SCN_MEM_SHARED = $10000000; { Section is shareable. }
  274. PE_SCN_MEM_DISCARDABLE = $02000000;
  275. PE_SCN_MEM_EXECUTE = $20000000;
  276. PE_SCN_MEM_READ = $40000000;
  277. PE_SCN_MEM_WRITE = $80000000;
  278. PE_DATADIR_ENTRIES = 16;
  279. PE_DATADIR_EDATA = 0;
  280. PE_DATADIR_IDATA = 1;
  281. PE_DATADIR_RSRC = 2;
  282. PE_DATADIR_PDATA = 3;
  283. PE_DATADIR_SECURITY = 4;
  284. PE_DATADIR_RELOC = 5;
  285. PE_DATADIR_DEBUG = 6;
  286. PE_DATADIR_DESCRIPTION = 7;
  287. PE_DATADIR_SPECIAL = 8;
  288. PE_DATADIR_TLS = 9;
  289. PE_DATADIR_LOADCFG = 10;
  290. PE_DATADIR_BOUNDIMPORT = 11;
  291. PE_DATADIR_IMPORTADDRESSTABLE = 12;
  292. PE_DATADIR_DELAYIMPORT = 13;
  293. {$ifdef x86_64}
  294. IMAGE_REL_AMD64_ABSOLUTE = $0000; { Reference is absolute, no relocation is necessary }
  295. IMAGE_REL_AMD64_ADDR64 = $0001; { 64-bit address (VA). }
  296. IMAGE_REL_AMD64_ADDR32 = $0002; { 32-bit address (VA). }
  297. IMAGE_REL_AMD64_ADDR32NB = $0003; { 32-bit address w/o image base (RVA). }
  298. IMAGE_REL_AMD64_REL32 = $0004; { 32-bit relative address from byte following reloc }
  299. IMAGE_REL_AMD64_REL32_1 = $0005; { 32-bit relative address from byte distance 1 from reloc }
  300. IMAGE_REL_AMD64_REL32_2 = $0006; { 32-bit relative address from byte distance 2 from reloc }
  301. IMAGE_REL_AMD64_REL32_3 = $0007; { 32-bit relative address from byte distance 3 from reloc }
  302. IMAGE_REL_AMD64_REL32_4 = $0008; { 32-bit relative address from byte distance 4 from reloc }
  303. IMAGE_REL_AMD64_REL32_5 = $0009; { 32-bit relative address from byte distance 5 from reloc }
  304. IMAGE_REL_AMD64_SECTION = $000A; { Section index }
  305. IMAGE_REL_AMD64_SECREL = $000B; { 32 bit offset from base of section containing target }
  306. IMAGE_REL_AMD64_SECREL7 = $000C; { 7 bit unsigned offset from base of section containing target }
  307. IMAGE_REL_AMD64_TOKEN = $000D; { 32 bit metadata token }
  308. IMAGE_REL_AMD64_SREL32 = $000E; { 32 bit signed span-dependent value emitted into object }
  309. IMAGE_REL_AMD64_PAIR = $000F;
  310. IMAGE_REL_AMD64_SSPAN32 = $0010; { 32 bit signed span-dependent value applied at link time }
  311. {$endif x86_64}
  312. {$ifdef arm}
  313. IMAGE_REL_ARM_ABSOLUTE = $0000; { No relocation required }
  314. IMAGE_REL_ARM_ADDR32 = $0001; { 32 bit address }
  315. IMAGE_REL_ARM_ADDR32NB = $0002; { 32 bit address w/o image base }
  316. IMAGE_REL_ARM_BRANCH24 = $0003; { 24 bit offset << 2 & sign ext. }
  317. IMAGE_REL_ARM_BRANCH11 = $0004; { Thumb: 2 11 bit offsets }
  318. IMAGE_REL_ARM_TOKEN = $0005; { clr token }
  319. IMAGE_REL_ARM_GPREL12 = $0006; { GP-relative addressing (ARM) }
  320. IMAGE_REL_ARM_GPREL7 = $0007; { GP-relative addressing (Thumb) }
  321. IMAGE_REL_ARM_BLX24 = $0008;
  322. IMAGE_REL_ARM_BLX11 = $0009;
  323. IMAGE_REL_ARM_SECTION = $000E; { Section table index }
  324. IMAGE_REL_ARM_SECREL = $000F; { Offset within section }
  325. {$endif arm}
  326. R_DIR32 = 6;
  327. R_IMAGEBASE = 7;
  328. R_PCRLONG = 20;
  329. type
  330. { Structures which are written directly to the output file }
  331. coffheader=packed record
  332. mach : word;
  333. nsects : smallint;
  334. time : longint;
  335. sympos : longint;
  336. syms : longint;
  337. opthdr : word;
  338. flag : word;
  339. end;
  340. coffdjoptheader=packed record
  341. magic : word;
  342. vstamp : word;
  343. tsize : longint;
  344. dsize : longint;
  345. bsize : longint;
  346. entry : longint;
  347. text_start : longint;
  348. data_start : longint;
  349. end;
  350. coffpedatadir=packed record
  351. vaddr : longint;
  352. size : longint;
  353. end;
  354. coffpeoptheader=packed record
  355. Magic : word;
  356. MajorLinkerVersion : byte;
  357. MinorLinkerVersion : byte;
  358. tsize : longint;
  359. dsize : longint;
  360. bsize : longint;
  361. entry : longint;
  362. text_start : longint;
  363. {$ifndef x86_64}
  364. data_start : longint;
  365. {$endif x86_64}
  366. ImageBase : aint;
  367. SectionAlignment : longint;
  368. FileAlignment : longint;
  369. MajorOperatingSystemVersion : word;
  370. MinorOperatingSystemVersion : word;
  371. MajorImageVersion : word;
  372. MinorImageVersion : word;
  373. MajorSubsystemVersion : word;
  374. MinorSubsystemVersion : word;
  375. Win32Version : longint;
  376. SizeOfImage : longint;
  377. SizeOfHeaders : longint;
  378. CheckSum : longint;
  379. Subsystem : word;
  380. DllCharacteristics : word;
  381. SizeOfStackReserve : aint;
  382. SizeOfStackCommit : aint;
  383. SizeOfHeapReserve : aint;
  384. SizeOfHeapCommit : aint;
  385. LoaderFlags : longint;
  386. NumberOfRvaAndSizes : longint;
  387. DataDirectory : array[0..PE_DATADIR_ENTRIES-1] of coffpedatadir;
  388. end;
  389. coffsechdr=packed record
  390. name : array[0..7] of char;
  391. vsize : longint;
  392. rvaofs : longint;
  393. datasize : longint;
  394. datapos : longint;
  395. relocpos : longint;
  396. lineno1 : longint;
  397. nrelocs : word;
  398. lineno2 : word;
  399. flags : longword;
  400. end;
  401. coffsectionrec=packed record
  402. len : longint;
  403. nrelocs : word;
  404. empty : array[0..11] of char;
  405. end;
  406. coffreloc=packed record
  407. address : longint;
  408. sym : longint;
  409. reloctype : word;
  410. end;
  411. coffsymbol=packed record
  412. name : array[0..3] of char; { real is [0..7], which overlaps the strpos ! }
  413. strpos : longint;
  414. value : longint;
  415. section : smallint;
  416. empty : word;
  417. typ : byte;
  418. aux : byte;
  419. end;
  420. const
  421. symbolresize = 200*sizeof(coffsymbol);
  422. strsresize = 8192;
  423. coffsecnames : array[TAsmSectiontype] of string[17] = ('',
  424. '.text','.data','.data','.bss','.tls',
  425. '.text',
  426. '.stab','.stabstr',
  427. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  428. '.eh_frame',
  429. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  430. '.fpc',
  431. ''
  432. );
  433. const go32v2stub : array[0..2047] of byte=(
  434. $4D,$5A,$00,$00,$04,$00,$00,$00,$20,$00,$27,$00,$FF,$FF,$00,
  435. $00,$60,$07,$00,$00,$54,$00,$00,$00,$00,$00,$00,$00,$0D,$0A,
  436. $73,$74,$75,$62,$2E,$68,$20,$67,$65,$6E,$65,$72,$61,$74,$65,
  437. $64,$20,$66,$72,$6F,$6D,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,
  438. $20,$62,$79,$20,$64,$6A,$61,$73,$6D,$2C,$20,$6F,$6E,$20,$54,
  439. $68,$75,$20,$44,$65,$63,$20,$20,$39,$20,$31,$30,$3A,$35,$39,
  440. $3A,$33,$31,$20,$31,$39,$39,$39,$0D,$0A,$54,$68,$65,$20,$53,
  441. $54,$55,$42,$2E,$45,$58,$45,$20,$73,$74,$75,$62,$20,$6C,$6F,
  442. $61,$64,$65,$72,$20,$69,$73,$20,$43,$6F,$70,$79,$72,$69,$67,
  443. $68,$74,$20,$28,$43,$29,$20,$31,$39,$39,$33,$2D,$31,$39,$39,
  444. $35,$20,$44,$4A,$20,$44,$65,$6C,$6F,$72,$69,$65,$2E,$20,$0D,
  445. $0A,$50,$65,$72,$6D,$69,$73,$73,$69,$6F,$6E,$20,$67,$72,$61,
  446. $6E,$74,$65,$64,$20,$74,$6F,$20,$75,$73,$65,$20,$66,$6F,$72,
  447. $20,$61,$6E,$79,$20,$70,$75,$72,$70,$6F,$73,$65,$20,$70,$72,
  448. $6F,$76,$69,$64,$65,$64,$20,$74,$68,$69,$73,$20,$63,$6F,$70,
  449. $79,$72,$69,$67,$68,$74,$20,$0D,$0A,$72,$65,$6D,$61,$69,$6E,
  450. $73,$20,$70,$72,$65,$73,$65,$6E,$74,$20,$61,$6E,$64,$20,$75,
  451. $6E,$6D,$6F,$64,$69,$66,$69,$65,$64,$2E,$20,$0D,$0A,$54,$68,
  452. $69,$73,$20,$6F,$6E,$6C,$79,$20,$61,$70,$70,$6C,$69,$65,$73,
  453. $20,$74,$6F,$20,$74,$68,$65,$20,$73,$74,$75,$62,$2C,$20,$61,
  454. $6E,$64,$20,$6E,$6F,$74,$20,$6E,$65,$63,$65,$73,$73,$61,$72,
  455. $69,$6C,$79,$20,$74,$68,$65,$20,$77,$68,$6F,$6C,$65,$20,$70,
  456. $72,$6F,$67,$72,$61,$6D,$2E,$0A,$0D,$0A,$24,$49,$64,$3A,$20,
  457. $73,$74,$75,$62,$2E,$61,$73,$6D,$20,$62,$75,$69,$6C,$74,$20,
  458. $31,$32,$2F,$30,$39,$2F,$39,$39,$20,$31,$30,$3A,$35,$39,$3A,
  459. $33,$31,$20,$62,$79,$20,$64,$6A,$61,$73,$6D,$20,$24,$0A,$0D,
  460. $0A,$40,$28,$23,$29,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,$20,
  461. $62,$75,$69,$6C,$74,$20,$31,$32,$2F,$30,$39,$2F,$39,$39,$20,
  462. $31,$30,$3A,$35,$39,$3A,$33,$31,$20,$62,$79,$20,$64,$6A,$61,
  463. $73,$6D,$0A,$0D,$0A,$1A,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  464. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  465. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  466. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  467. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  468. $00,$00,$67,$6F,$33,$32,$73,$74,$75,$62,$2C,$20,$76,$20,$32,
  469. $2E,$30,$32,$54,$00,$00,$00,$00,$00,$08,$00,$00,$00,$00,$00,
  470. $00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  471. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  472. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$43,$57,$53,$44,$50,
  473. $4D,$49,$2E,$45,$58,$45,$00,$00,$00,$00,$00,$0E,$1F,$8C,$1E,
  474. $24,$00,$8C,$06,$60,$07,$FC,$B4,$30,$CD,$21,$3C,$03,$73,$08,
  475. $B0,$6D,$BA,$A7,$05,$E9,$D4,$03,$A2,$69,$08,$BE,$20,$00,$8B,
  476. $04,$09,$C0,$75,$02,$B4,$FE,$BB,$70,$08,$39,$C3,$73,$02,$89,
  477. $C3,$89,$1C,$FE,$C7,$B9,$04,$FF,$D3,$EB,$B4,$4A,$CD,$21,$73,
  478. $08,$D3,$E3,$FE,$CF,$89,$1C,$EB,$D8,$26,$8E,$06,$2C,$00,$31,
  479. $FF,$30,$C0,$A9,$F2,$AE,$26,$81,$3D,$50,$41,$75,$15,$AF,$26,
  480. $81,$3D,$54,$48,$75,$0D,$AF,$26,$80,$3D,$3D,$75,$06,$47,$89,
  481. $3E,$8C,$04,$4F,$AE,$75,$DF,$AF,$B4,$3E,$BB,$13,$00,$CD,$21,
  482. $B4,$3E,$BB,$12,$00,$CD,$21,$06,$57,$31,$C9,$74,$12,$B0,$6E,
  483. $BA,$7E,$05,$E9,$5E,$03,$09,$C9,$75,$F4,$41,$E8,$A1,$03,$72,
  484. $EE,$B8,$87,$16,$CD,$2F,$09,$C0,$75,$ED,$80,$E3,$01,$74,$E8,
  485. $89,$3E,$00,$06,$8C,$06,$02,$06,$89,$36,$04,$06,$5F,$07,$E8,
  486. $D3,$02,$89,$3E,$2A,$00,$89,$36,$62,$07,$80,$3E,$2C,$00,$00,
  487. $74,$23,$B9,$08,$00,$BF,$2C,$00,$8A,$05,$47,$08,$C0,$74,$05,
  488. $88,$07,$43,$E2,$F4,$66,$C7,$07,$2E,$45,$58,$45,$83,$C3,$04,
  489. $C6,$07,$00,$89,$1E,$62,$07,$B8,$00,$3D,$BA,$64,$07,$CD,$21,
  490. $0F,$82,$B3,$02,$A3,$06,$06,$89,$C3,$B9,$06,$00,$BA,$B5,$07,
  491. $B4,$3F,$CD,$21,$31,$D2,$31,$C9,$A1,$B5,$07,$3D,$4C,$01,$74,
  492. $1B,$3D,$4D,$5A,$0F,$85,$98,$02,$8B,$16,$B9,$07,$C1,$E2,$09,
  493. $8B,$1E,$B7,$07,$09,$DB,$74,$05,$80,$EE,$02,$01,$DA,$89,$16,
  494. $BB,$07,$89,$0E,$BD,$07,$B8,$00,$42,$8B,$1E,$06,$06,$CD,$21,
  495. $B9,$A8,$00,$BA,$BF,$07,$B4,$3F,$CD,$21,$3D,$A8,$00,$75,$06,
  496. $81,$3E,$BF,$07,$4C,$01,$0F,$85,$61,$02,$66,$A1,$E3,$07,$66,
  497. $A3,$10,$06,$66,$8B,$0E,$BB,$07,$66,$A1,$03,$08,$66,$01,$C8,
  498. $66,$A3,$08,$06,$66,$A1,$2B,$08,$66,$01,$C8,$66,$A3,$0C,$06,
  499. $66,$8B,$1E,$4B,$08,$66,$A1,$4F,$08,$66,$01,$C3,$66,$B8,$01,
  500. $00,$01,$00,$66,$39,$C3,$73,$03,$66,$89,$C3,$66,$81,$C3,$FF,
  501. $FF,$00,$00,$31,$DB,$66,$89,$1E,$1C,$00,$E8,$F5,$02,$8B,$1E,
  502. $04,$06,$09,$DB,$74,$0A,$B4,$48,$CD,$21,$0F,$82,$15,$02,$8E,
  503. $C0,$E8,$08,$03,$B8,$01,$00,$FF,$1E,$00,$06,$0F,$82,$0F,$02,
  504. $8C,$06,$26,$00,$8C,$0E,$28,$00,$8C,$D8,$A3,$22,$00,$8E,$C0,
  505. $31,$C0,$B9,$01,$00,$CD,$31,$72,$07,$A3,$14,$06,$31,$C0,$CD,
  506. $31,$0F,$82,$F3,$01,$A3,$16,$06,$66,$8B,$0E,$1C,$00,$B8,$01,
  507. $05,$8B,$1E,$1E,$00,$CD,$31,$0F,$82,$E5,$01,$89,$1E,$1A,$06,
  508. $89,$0E,$18,$06,$89,$36,$1A,$00,$89,$3E,$18,$00,$B8,$07,$00,
  509. $8B,$1E,$14,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,$CD,$31,$B8,
  510. $09,$00,$8C,$C9,$83,$E1,$03,$C1,$E1,$05,$51,$81,$C9,$9B,$C0,
  511. $CD,$31,$B8,$08,$00,$8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,
  512. $B8,$07,$00,$8B,$1E,$16,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,
  513. $CD,$31,$B8,$09,$00,$59,$81,$C9,$93,$C0,$CD,$31,$B8,$08,$00,
  514. $8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,$B8,$00,$01,$BB,$00,
  515. $0F,$CD,$31,$73,$10,$3D,$08,$00,$0F,$85,$73,$01,$B8,$00,$01,
  516. $CD,$31,$0F,$82,$6A,$01,$A3,$1C,$06,$89,$16,$1E,$06,$C1,$E3,
  517. $04,$89,$1E,$20,$06,$66,$8B,$36,$08,$06,$66,$8B,$3E,$FB,$07,
  518. $66,$8B,$0E,$FF,$07,$E8,$49,$00,$66,$8B,$36,$0C,$06,$66,$8B,
  519. $3E,$23,$08,$66,$8B,$0E,$27,$08,$E8,$37,$00,$8E,$06,$16,$06,
  520. $66,$8B,$3E,$4B,$08,$66,$8B,$0E,$4F,$08,$66,$31,$C0,$66,$C1,
  521. $E9,$02,$67,$F3,$66,$AB,$B4,$3E,$8B,$1E,$06,$06,$CD,$21,$B8,
  522. $01,$01,$8B,$16,$1E,$06,$CD,$31,$1E,$0F,$A1,$8E,$1E,$16,$06,
  523. $66,$64,$FF,$2E,$10,$06,$66,$89,$F0,$66,$25,$FF,$01,$00,$00,
  524. $66,$01,$C1,$29,$C6,$66,$29,$C7,$66,$89,$0E,$26,$06,$66,$89,
  525. $3E,$22,$06,$E8,$0F,$01,$89,$36,$3E,$06,$66,$C1,$EE,$10,$89,
  526. $36,$42,$06,$8B,$1E,$06,$06,$89,$1E,$3A,$06,$C7,$06,$46,$06,
  527. $00,$42,$E8,$03,$01,$A1,$1C,$06,$A3,$4E,$06,$C7,$06,$3E,$06,
  528. $00,$00,$C6,$06,$47,$06,$3F,$A1,$28,$06,$09,$C0,$75,$09,$A1,
  529. $26,$06,$3B,$06,$20,$06,$76,$03,$A1,$20,$06,$A3,$42,$06,$E8,
  530. $D9,$00,$66,$31,$C9,$8B,$0E,$46,$06,$66,$8B,$3E,$22,$06,$66,
  531. $01,$0E,$22,$06,$66,$29,$0E,$26,$06,$66,$31,$F6,$C1,$E9,$02,
  532. $1E,$06,$8E,$06,$16,$06,$8E,$1E,$1E,$06,$67,$F3,$66,$A5,$07,
  533. $1F,$66,$03,$0E,$26,$06,$75,$AF,$C3,$3C,$3A,$74,$06,$3C,$2F,
  534. $74,$02,$3C,$5C,$C3,$BE,$64,$07,$89,$F3,$26,$8A,$05,$47,$88,
  535. $04,$38,$E0,$74,$0E,$08,$C0,$74,$0A,$46,$E8,$DE,$FF,$75,$EC,
  536. $89,$F3,$74,$E8,$C3,$B0,$66,$BA,$48,$05,$EB,$0C,$B0,$67,$BA,
  537. $55,$05,$EB,$05,$B0,$68,$BA,$5F,$05,$52,$8B,$1E,$62,$07,$C6,
  538. $07,$24,$BB,$64,$07,$EB,$28,$E8,$F5,$00,$B0,$69,$BA,$99,$05,
  539. $EB,$1A,$B0,$6A,$BA,$B2,$05,$EB,$13,$B0,$6B,$BA,$C4,$05,$EB,
  540. $0C,$B0,$6C,$BA,$D6,$05,$EB,$05,$B0,$69,$BA,$99,$05,$52,$BB,
  541. $3B,$05,$E8,$15,$00,$5B,$E8,$11,$00,$BB,$67,$04,$E8,$0B,$00,
  542. $B4,$4C,$CD,$21,$43,$50,$B4,$02,$CD,$21,$58,$8A,$17,$80,$FA,
  543. $24,$75,$F2,$C3,$0D,$0A,$24,$50,$51,$57,$31,$C0,$BF,$2A,$06,
  544. $B9,$19,$00,$F3,$AB,$5F,$59,$58,$C3,$B8,$00,$03,$BB,$21,$00,
  545. $31,$C9,$66,$BF,$2A,$06,$00,$00,$CD,$31,$C3,$00,$00,$30,$E4,
  546. $E8,$4E,$FF,$89,$DE,$8B,$3E,$8C,$04,$EB,$17,$B4,$3B,$E8,$41,
  547. $FF,$81,$FE,$64,$07,$74,$12,$8A,$44,$FF,$E8,$2A,$FF,$74,$04,
  548. $C6,$04,$5C,$46,$E8,$03,$00,$72,$E4,$C3,$E8,$34,$00,$BB,$44,
  549. $00,$8A,$07,$88,$04,$43,$46,$08,$C0,$75,$F6,$06,$57,$1E,$07,
  550. $E8,$9B,$FF,$BB,$2A,$06,$8C,$5F,$04,$89,$5F,$02,$BA,$64,$07,
  551. $B8,$00,$4B,$CD,$21,$5F,$07,$72,$09,$B4,$4D,$CD,$21,$2D,$00,
  552. $03,$F7,$D8,$EB,$28,$80,$3E,$69,$08,$05,$72,$20,$B8,$00,$58,
  553. $CD,$21,$A2,$67,$08,$B8,$02,$58,$CD,$21,$A2,$68,$08,$B8,$01,
  554. $58,$BB,$80,$00,$CD,$21,$B8,$03,$58,$BB,$01,$00,$CD,$21,$C3,
  555. $9C,$80,$3E,$69,$08,$05,$72,$1A,$50,$53,$B8,$03,$58,$8A,$1E,
  556. $68,$08,$30,$FF,$CD,$21,$B8,$01,$58,$8A,$1E,$67,$08,$30,$FF,
  557. $CD,$21,$5B,$58,$9D,$C3,$4C,$6F,$61,$64,$20,$65,$72,$72,$6F,
  558. $72,$3A,$20,$24,$3A,$20,$63,$61,$6E,$27,$74,$20,$6F,$70,$65,
  559. $6E,$24,$3A,$20,$6E,$6F,$74,$20,$45,$58,$45,$24,$3A,$20,$6E,
  560. $6F,$74,$20,$43,$4F,$46,$46,$20,$28,$43,$68,$65,$63,$6B,$20,
  561. $66,$6F,$72,$20,$76,$69,$72,$75,$73,$65,$73,$29,$24,$6E,$6F,
  562. $20,$44,$50,$4D,$49,$20,$2D,$20,$47,$65,$74,$20,$63,$73,$64,
  563. $70,$6D,$69,$2A,$62,$2E,$7A,$69,$70,$24,$6E,$6F,$20,$44,$4F,
  564. $53,$20,$6D,$65,$6D,$6F,$72,$79,$24,$6E,$65,$65,$64,$20,$44,
  565. $4F,$53,$20,$33,$24,$63,$61,$6E,$27,$74,$20,$73,$77,$69,$74,
  566. $63,$68,$20,$6D,$6F,$64,$65,$24,$6E,$6F,$20,$44,$50,$4D,$49,
  567. $20,$73,$65,$6C,$65,$63,$74,$6F,$72,$73,$24,$6E,$6F,$20,$44,
  568. $50,$4D,$49,$20,$6D,$65,$6D,$6F,$72,$79,$24,$90,$90,$90,$90,
  569. $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,
  570. $90,$90,$90,$90,$90,$90,$90,$90);
  571. const win32stub : array[0..131] of byte=(
  572. $4D,$5A,$90,$00,$03,$00,$00,$00,$04,$00,$00,$00,$FF,$FF,$00,$00,
  573. $B8,$00,$00,$00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,
  574. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  575. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$80,$00,$00,$00,
  576. $0E,$1F,$BA,$0E,$00,$B4,$09,$CD,$21,$B8,$01,$4C,$CD,$21,$54,$68,
  577. $69,$73,$20,$70,$72,$6F,$67,$72,$61,$6D,$20,$63,$61,$6E,$6E,$6F,
  578. $74,$20,$62,$65,$20,$72,$75,$6E,$20,$69,$6E,$20,$44,$4F,$53,$20,
  579. $6D,$6F,$64,$65,$2E,$0D,$0D,$0A,$24,$00,$00,$00,$00,$00,$00,$00,
  580. $50,$45,$00,$00);
  581. {****************************************************************************
  582. Helpers
  583. ****************************************************************************}
  584. function djencodesechdrflags(aoptions:TObjSectionOptions):longword;
  585. begin
  586. if (oso_load in aoptions) then
  587. begin
  588. if oso_executable in aoptions then
  589. result:=COFF_STYP_TEXT
  590. else if not(oso_data in aoptions) then
  591. result:=COFF_STYP_BSS
  592. else
  593. result:=COFF_STYP_DATA;
  594. end
  595. else
  596. result:=COFF_STYP_REG;
  597. end;
  598. function djdecodesechdrflags(const aname:string;flags:longword):TObjSectionOptions;
  599. begin
  600. result:=[];
  601. if flags and COFF_STYP_TEXT<>0 then
  602. result:=[oso_data,oso_load,oso_executable]
  603. else if flags and COFF_STYP_BSS<>0 then
  604. result:=[oso_load]
  605. else if flags and COFF_STYP_DATA<>0 then
  606. result:=[oso_data,oso_load]
  607. else
  608. result:=[oso_data]
  609. end;
  610. function peencodesechdrflags(aoptions:TObjSectionOptions;aalign:shortint):longword;
  611. begin
  612. result:=0;
  613. if (oso_load in aoptions) then
  614. begin
  615. if oso_executable in aoptions then
  616. result:=result or PE_SCN_CNT_CODE or PE_SCN_MEM_EXECUTE
  617. else
  618. begin
  619. if (oso_data in aoptions) then
  620. result:=result or PE_SCN_CNT_INITIALIZED_DATA
  621. else
  622. result:=result or PE_SCN_CNT_UNINITIALIZED_DATA;
  623. end;
  624. if oso_write in aoptions then
  625. result:=result or PE_SCN_MEM_WRITE or PE_SCN_MEM_READ
  626. else
  627. result:=result or PE_SCN_MEM_READ;
  628. end
  629. else
  630. result:=result or PE_SCN_MEM_DISCARDABLE;
  631. case aalign of
  632. 1 : result:=result or PE_SCN_ALIGN_1BYTES;
  633. 2 : result:=result or PE_SCN_ALIGN_2BYTES;
  634. 4 : result:=result or PE_SCN_ALIGN_4BYTES;
  635. 8 : result:=result or PE_SCN_ALIGN_8BYTES;
  636. 16 : result:=result or PE_SCN_ALIGN_16BYTES;
  637. 32 : result:=result or PE_SCN_ALIGN_32BYTES;
  638. 64 : result:=result or PE_SCN_ALIGN_64BYTES;
  639. else result:=result or PE_SCN_ALIGN_16BYTES;
  640. end;
  641. end;
  642. procedure pedecodesechdrflags(const aname:string;flags:longword;out aoptions:TObjSectionOptions;out aalign:shortint);
  643. var
  644. alignflag : longword;
  645. begin
  646. aoptions:=[];
  647. aalign:=sizeof(aint);
  648. if flags and PE_SCN_CNT_CODE<>0 then
  649. include(aoptions,oso_executable);
  650. if flags and PE_SCN_MEM_DISCARDABLE<>0 then
  651. include(aoptions,oso_debug);
  652. if flags and PE_SCN_CNT_UNINITIALIZED_DATA=0 then
  653. include(aoptions,oso_data);
  654. if (flags and PE_SCN_LNK_REMOVE<>0) or
  655. (flags and PE_SCN_MEM_DISCARDABLE<>0) then
  656. include(aoptions,oso_noload)
  657. else
  658. include(aoptions,oso_load);
  659. { read/write }
  660. if flags and PE_SCN_MEM_WRITE<>0 then
  661. include(aoptions,oso_write)
  662. else
  663. include(aoptions,oso_readonly);
  664. { alignment }
  665. alignflag:=flags and PE_SCN_ALIGN_MASK;
  666. if alignflag=PE_SCN_ALIGN_64BYTES then
  667. aalign:=64
  668. else if alignflag=PE_SCN_ALIGN_32BYTES then
  669. aalign:=32
  670. else if alignflag=PE_SCN_ALIGN_16BYTES then
  671. aalign:=16
  672. else if alignflag=PE_SCN_ALIGN_8BYTES then
  673. aalign:=8
  674. else if alignflag=PE_SCN_ALIGN_4BYTES then
  675. aalign:=4
  676. else if alignflag=PE_SCN_ALIGN_2BYTES then
  677. aalign:=2
  678. else if alignflag=PE_SCN_ALIGN_1BYTES then
  679. aalign:=1;
  680. end;
  681. {****************************************************************************
  682. TCoffObjSection
  683. ****************************************************************************}
  684. constructor TCoffObjSection.create(AList:TFPHashObjectList;const aname:string;aalign:shortint;aoptions:TObjSectionOptions);
  685. begin
  686. inherited create(AList,aname,aalign,aoptions);
  687. end;
  688. procedure TCoffObjSection.addsymsizereloc(ofs:aint;p:TObjSymbol;symsize:aint;reloctype:TObjRelocationType);
  689. begin
  690. ObjRelocations.Add(TObjRelocation.createsymbolsize(ofs,p,symsize,reloctype));
  691. end;
  692. procedure TCoffObjSection.fixuprelocs;
  693. var
  694. i : longint;
  695. objreloc : TObjRelocation;
  696. address,
  697. relocval : aint;
  698. relocsec : TObjSection;
  699. begin
  700. if (ObjRelocations.Count>0) and
  701. not assigned(data) then
  702. internalerror(200205183);
  703. for i:=0 to ObjRelocations.Count-1 do
  704. begin
  705. objreloc:=TObjRelocation(ObjRelocations[i]);
  706. if objreloc.typ=RELOC_ZERO then
  707. begin
  708. data.Seek(objreloc.dataoffset);
  709. data.Write(0,4);
  710. continue;
  711. end;
  712. data.Seek(objreloc.dataoffset);
  713. data.Read(address,4);
  714. if assigned(objreloc.symbol) then
  715. begin
  716. relocsec:=objreloc.symbol.objsection;
  717. relocval:=objreloc.symbol.address;
  718. end
  719. else
  720. if assigned(objreloc.objsection) then
  721. begin
  722. relocsec:=objreloc.objsection;
  723. relocval:=objreloc.objsection.mempos
  724. end
  725. else
  726. internalerror(200205183);
  727. { Only debug section are allowed to have }
  728. if not relocsec.used and
  729. not(oso_debug in secoptions) then
  730. internalerror(200603061);
  731. case objreloc.typ of
  732. RELOC_RELATIVE :
  733. begin
  734. address:=address-mempos+relocval;
  735. if TCoffObjData(objdata).win32 then
  736. dec(address,objreloc.dataoffset+4);
  737. end;
  738. RELOC_RVA :
  739. begin
  740. { fixup address when the symbol was known in defined object }
  741. if (relocsec.objdata=objdata) then
  742. dec(address,TCoffObjSection(relocsec).orgmempos);
  743. inc(address,relocval);
  744. end;
  745. {$ifdef x86_64}
  746. { 64 bit coff only }
  747. RELOC_RELATIVE_1:
  748. begin
  749. address:=address-mempos+relocval;
  750. dec(address,objreloc.dataoffset+1);
  751. end;
  752. RELOC_RELATIVE_2:
  753. begin
  754. address:=address-mempos+relocval;
  755. dec(address,objreloc.dataoffset+2);
  756. end;
  757. RELOC_RELATIVE_3:
  758. begin
  759. address:=address-mempos+relocval;
  760. dec(address,objreloc.dataoffset+3);
  761. end;
  762. RELOC_RELATIVE_4:
  763. begin
  764. address:=address-mempos+relocval;
  765. dec(address,objreloc.dataoffset+4);
  766. end;
  767. RELOC_RELATIVE_5:
  768. begin
  769. address:=address-mempos+relocval;
  770. dec(address,objreloc.dataoffset+5);
  771. end;
  772. RELOC_ABSOLUTE32,
  773. {$endif x86_64}
  774. RELOC_ABSOLUTE :
  775. begin
  776. if oso_common in relocsec.secoptions then
  777. dec(address,objreloc.orgsize)
  778. else
  779. begin
  780. { fixup address when the symbol was known in defined object }
  781. if (relocsec.objdata=objdata) then
  782. dec(address,TCoffObjSection(relocsec).orgmempos);
  783. end;
  784. inc(address,relocval);
  785. inc(address,relocsec.objdata.imagebase);
  786. end;
  787. else
  788. internalerror(200604014);
  789. end;
  790. data.Seek(objreloc.dataoffset);
  791. data.Write(address,4);
  792. end;
  793. end;
  794. {****************************************************************************
  795. TDJCoffObjSection
  796. ****************************************************************************}
  797. constructor TDJCoffObjSection.create(AList:TFPHashObjectList;const aname:string;aalign:shortint;aoptions:TObjSectionOptions);
  798. begin
  799. inherited create(alist,aname,aalign,aoptions);
  800. end;
  801. {****************************************************************************
  802. TPECoffObjSection
  803. ****************************************************************************}
  804. constructor TPECoffObjSection.create(AList:TFPHashObjectList;const aname:string;aalign:shortint;aoptions:TObjSectionOptions);
  805. begin
  806. inherited create(alist,aname,aalign,aoptions);
  807. end;
  808. {****************************************************************************
  809. TCoffObjData
  810. ****************************************************************************}
  811. constructor TCoffObjData.createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  812. begin
  813. inherited create(n);
  814. CObjSection:=ACObjSection;
  815. win32:=awin32;
  816. { we need at least the following 3 ObjSections }
  817. createsection(sec_code,'');
  818. createsection(sec_data,'');
  819. createsection(sec_bss,'');
  820. end;
  821. destructor TCoffObjData.destroy;
  822. begin
  823. inherited destroy;
  824. end;
  825. function TCoffObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
  826. var
  827. secname : string;
  828. begin
  829. secname:=coffsecnames[atype];
  830. if use_smartlink_section and
  831. (aname<>'') then
  832. result:=secname+'.'+aname
  833. else
  834. result:=secname;
  835. end;
  836. procedure TCoffObjData.CreateDebugSections;
  837. begin
  838. if target_dbg.id=dbg_stabs then
  839. begin
  840. stabssec:=createsection(sec_stab,'');
  841. stabstrsec:=createsection(sec_stabstr,'');
  842. end;
  843. end;
  844. procedure TCoffObjData.writereloc(data,len:aint;p:TObjSymbol;reloctype:TObjRelocationType);
  845. var
  846. curraddr,
  847. symaddr : aint;
  848. begin
  849. if CurrObjSec=nil then
  850. internalerror(200403072);
  851. if assigned(p) then
  852. begin
  853. { current address }
  854. curraddr:=CurrObjSec.mempos+CurrObjSec.Size;
  855. { external/common symbols don't have a fixed memory position yet }
  856. if (p.bind=AB_COMMON) then
  857. begin
  858. { For go32v2 we need to use the size as address }
  859. if not win32 then
  860. symaddr:=p.size
  861. else
  862. symaddr:=0;
  863. end
  864. else
  865. symaddr:=p.address;
  866. { no symbol relocation need inside a section }
  867. if (p.objsection=CurrObjSec) and
  868. (p.bind<>AB_COMMON) then
  869. begin
  870. case reloctype of
  871. RELOC_ABSOLUTE :
  872. begin
  873. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE);
  874. inc(data,symaddr);
  875. end;
  876. RELOC_RELATIVE :
  877. begin
  878. inc(data,symaddr-len-CurrObjSec.Size);
  879. end;
  880. RELOC_RVA :
  881. begin
  882. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_RVA);
  883. inc(data,symaddr);
  884. end;
  885. else
  886. internalerror(200604013);
  887. end;
  888. end
  889. else
  890. begin
  891. if (p.objsection<>nil) and
  892. (p.bind<>AB_COMMON) and
  893. (reloctype<>RELOC_RELATIVE) then
  894. CurrObjSec.addsectionreloc(curraddr,p.objsection,reloctype)
  895. else
  896. CurrObjSec.addsymreloc(curraddr,p,reloctype);
  897. if (not win32) or
  898. ((reloctype<>RELOC_RELATIVE) and (p.objsection<>nil)) then
  899. inc(data,symaddr);
  900. if reloctype=RELOC_RELATIVE then
  901. begin
  902. if win32 then
  903. dec(data,len-4)
  904. else
  905. dec(data,len+CurrObjSec.Size);
  906. end;
  907. end;
  908. end
  909. else
  910. begin
  911. if reloctype=RELOC_RVA then
  912. internalerror(200603033);
  913. end;
  914. CurrObjSec.write(data,len);
  915. end;
  916. procedure TCoffObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  917. const
  918. N_SourceFile = $64;
  919. N_IncludeFile = $84;
  920. var
  921. stab : TObjStabEntry;
  922. stabstrlen : longint;
  923. curraddr : aint;
  924. {$ifdef optimizestabs}
  925. hs : string;
  926. {$endif optimizestabs}
  927. begin
  928. if not assigned(StabsSec) then
  929. internalerror(200602256);
  930. { Win32 does not need an offset if a symbol relocation is used }
  931. if win32 and
  932. assigned(ps) and
  933. (ps.bind<>AB_LOCAL) then
  934. offset:=0;
  935. if assigned(p) and (p[0]<>#0) then
  936. begin
  937. stabstrlen:=strlen(p);
  938. {$ifdef optimizestabs}
  939. StabStrEntry:=nil;
  940. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  941. begin
  942. hs:=strpas(p);
  943. StabstrEntry:=StabStrDict.Search(hs);
  944. if not assigned(StabstrEntry) then
  945. begin
  946. StabstrEntry:=TStabStrEntry.Create(hs);
  947. StabstrEntry:=StabStrSec.Size;
  948. StabStrDict.Insert(StabstrEntry);
  949. { generate new stab }
  950. StabstrEntry:=nil;
  951. end;
  952. end;
  953. if assigned(StabstrEntry) then
  954. stab.strpos:=StabstrEntry.strpos
  955. else
  956. {$endif optimizestabs}
  957. begin
  958. stab.strpos:=StabStrSec.Size;
  959. StabStrSec.write(p^,stabstrlen+1);
  960. end;
  961. end
  962. else
  963. stab.strpos:=0;
  964. stab.ntype:=nidx;
  965. stab.ndesc:=ndesc;
  966. stab.nother:=nother;
  967. stab.nvalue:=offset;
  968. StabsSec.write(stab,sizeof(stab));
  969. if assigned(ps) then
  970. begin
  971. { current address }
  972. curraddr:=StabsSec.mempos+StabsSec.Size;
  973. if DLLSource and RelocSection then
  974. { avoid relocation in the .stab section
  975. because it ends up in the .reloc section instead }
  976. StabsSec.addsymreloc(curraddr-4,ps,RELOC_RVA)
  977. else
  978. StabsSec.addsymreloc(curraddr-4,ps,RELOC_ABSOLUTE);
  979. end;
  980. end;
  981. procedure TCoffObjData.afteralloc;
  982. var
  983. mempos : aint;
  984. i : longint;
  985. begin
  986. inherited afteralloc;
  987. { DJ Coff requires mempositions }
  988. if not win32 then
  989. begin
  990. mempos:=0;
  991. for i:=0 to ObjSectionList.Count-1 do
  992. TObjSection(ObjSectionList[i]).setmempos(mempos);
  993. end;
  994. end;
  995. {****************************************************************************
  996. TDJCoffObjData
  997. ****************************************************************************}
  998. constructor TDJCoffObjData.create(const n:string);
  999. begin
  1000. inherited createcoff(n,false,TDJCoffObjSection);
  1001. end;
  1002. {****************************************************************************
  1003. TPECoffObjData
  1004. ****************************************************************************}
  1005. constructor TPECoffObjData.create(const n:string);
  1006. begin
  1007. inherited createcoff(n,true,TPECoffObjSection);
  1008. end;
  1009. {****************************************************************************
  1010. TCoffObjOutput
  1011. ****************************************************************************}
  1012. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1013. begin
  1014. inherited create(AWriter);
  1015. win32:=awin32;
  1016. end;
  1017. destructor TCoffObjOutput.destroy;
  1018. begin
  1019. if assigned(FCoffSyms) then
  1020. FCoffSyms.free;
  1021. if assigned(FCoffStrs) then
  1022. FCoffStrs.free;
  1023. inherited destroy;
  1024. end;
  1025. procedure TCoffObjOutput.write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  1026. var
  1027. sym : coffsymbol;
  1028. begin
  1029. FillChar(sym,sizeof(sym),0);
  1030. { symbolname }
  1031. if length(name)>8 then
  1032. begin
  1033. sym.strpos:=FCoffStrs.size+4;
  1034. FCoffStrs.writestr(name);
  1035. FCoffStrs.writestr(#0);
  1036. end
  1037. else
  1038. move(name[1],sym.name,length(name));
  1039. sym.value:=value;
  1040. sym.section:=section;
  1041. sym.typ:=typ;
  1042. sym.aux:=aux;
  1043. inc(symidx);
  1044. FCoffSyms.write(sym,sizeof(sym));
  1045. end;
  1046. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1047. var
  1048. secrec : coffsectionrec;
  1049. begin
  1050. with TCoffObjSection(p) do
  1051. begin
  1052. secidx:=symidx div 2;
  1053. secsymidx:=symidx;
  1054. write_symbol(name,mempos,secidx,COFF_SYM_SECTION,1);
  1055. { AUX }
  1056. fillchar(secrec,sizeof(secrec),0);
  1057. secrec.len:=Size;
  1058. secrec.nrelocs:=ObjRelocations.count;
  1059. inc(symidx);
  1060. FCoffSyms.write(secrec,sizeof(secrec));
  1061. end;
  1062. end;
  1063. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1064. var
  1065. i : longint;
  1066. rel : coffreloc;
  1067. objreloc : TObjRelocation;
  1068. begin
  1069. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1070. begin
  1071. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1072. rel.address:=objreloc.dataoffset;
  1073. if assigned(objreloc.symbol) then
  1074. begin
  1075. if (objreloc.symbol.bind=AB_LOCAL) then
  1076. rel.sym:=objreloc.symbol.objsection.secsymidx
  1077. else
  1078. begin
  1079. if objreloc.symbol.symidx=-1 then
  1080. internalerror(200602233);
  1081. rel.sym:=objreloc.symbol.symidx;
  1082. end;
  1083. end
  1084. else
  1085. begin
  1086. if objreloc.objsection<>nil then
  1087. rel.sym:=objreloc.objsection.secsymidx
  1088. else
  1089. rel.sym:=0;
  1090. end;
  1091. case objreloc.typ of
  1092. {$ifdef arm}
  1093. RELOC_ABSOLUTE :
  1094. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1095. { I've no idea if this is correct (FK):
  1096. RELOC_RELATIVE :
  1097. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1098. }
  1099. RELOC_RVA :
  1100. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1101. {$endif arm}
  1102. {$ifdef i386}
  1103. RELOC_RELATIVE :
  1104. rel.reloctype:=R_PCRLONG;
  1105. RELOC_ABSOLUTE :
  1106. rel.reloctype:=R_DIR32;
  1107. RELOC_RVA :
  1108. rel.reloctype:=R_IMAGEBASE;
  1109. {$endif i386}
  1110. {$ifdef x86_64}
  1111. RELOC_RELATIVE :
  1112. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1113. RELOC_ABSOLUTE32 :
  1114. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1115. RELOC_ABSOLUTE :
  1116. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1117. RELOC_RVA :
  1118. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1119. RELOC_RELATIVE_1 :
  1120. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1121. RELOC_RELATIVE_2 :
  1122. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1123. RELOC_RELATIVE_3 :
  1124. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1125. RELOC_RELATIVE_4 :
  1126. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1127. RELOC_RELATIVE_5 :
  1128. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1129. {$endif x86_64}
  1130. else
  1131. internalerror(200603312);
  1132. end;
  1133. FWriter.write(rel,sizeof(rel));
  1134. end;
  1135. end;
  1136. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1137. var
  1138. filename : string[18];
  1139. sectionval : word;
  1140. globalval : byte;
  1141. i : longint;
  1142. value : aint;
  1143. objsym : TObjSymbol;
  1144. begin
  1145. with TCoffObjData(data) do
  1146. begin
  1147. symidx:=0;
  1148. { The `.file' record, and the file name auxiliary record }
  1149. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1150. fillchar(filename,sizeof(filename),0);
  1151. filename:=SplitFileName(current_module.mainsource^);
  1152. inc(symidx);
  1153. FCoffSyms.write(filename[1],sizeof(filename)-1);
  1154. { Sections }
  1155. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  1156. { ObjSymbols }
  1157. for i:=0 to ObjSymbolList.Count-1 do
  1158. begin
  1159. objsym:=TObjSymbol(ObjSymbolList[i]);
  1160. if (objsym.typ=AT_LABEL) and (objsym.bind=AB_LOCAL) then
  1161. continue;
  1162. case objsym.bind of
  1163. AB_GLOBAL :
  1164. begin
  1165. globalval:=2;
  1166. sectionval:=TCoffObjSection(objsym.objsection).secidx;
  1167. value:=objsym.address;
  1168. end;
  1169. AB_LOCAL :
  1170. begin
  1171. globalval:=3;
  1172. sectionval:=TCoffObjSection(objsym.objsection).secidx;
  1173. value:=objsym.address;
  1174. end;
  1175. else
  1176. begin
  1177. globalval:=2;
  1178. sectionval:=0;
  1179. value:=objsym.size;
  1180. end;
  1181. end;
  1182. { symbolname }
  1183. objsym.symidx:=symidx;
  1184. write_symbol(objsym.name,value,sectionval,globalval,0);
  1185. end;
  1186. end;
  1187. end;
  1188. procedure TCoffObjOutput.section_set_datapos(p:TObject;arg:pointer);
  1189. begin
  1190. TObjSection(p).setdatapos(paint(arg)^);
  1191. end;
  1192. procedure TCoffObjOutput.section_set_reloc_datapos(p:TObject;arg:pointer);
  1193. begin
  1194. TCoffObjSection(p).coffrelocpos:=paint(arg)^;
  1195. inc(paint(arg)^,sizeof(coffreloc)*TObjSection(p).ObjRelocations.count);
  1196. end;
  1197. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1198. var
  1199. sechdr : coffsechdr;
  1200. s : string;
  1201. strpos : Aint;
  1202. begin
  1203. with TCoffObjSection(p) do
  1204. begin
  1205. fillchar(sechdr,sizeof(sechdr),0);
  1206. s:=name;
  1207. if length(s)>8 then
  1208. begin
  1209. strpos:=FCoffStrs.size+4;
  1210. FCoffStrs.writestr(s);
  1211. FCoffStrs.writestr(#0);
  1212. s:='/'+ToStr(strpos);
  1213. end;
  1214. move(s[1],sechdr.name,length(s));
  1215. if not win32 then
  1216. begin
  1217. sechdr.rvaofs:=mempos;
  1218. sechdr.vsize:=mempos;
  1219. end
  1220. else
  1221. begin
  1222. if not(oso_data in secoptions) then
  1223. sechdr.vsize:=Size;
  1224. end;
  1225. sechdr.DataSize:=size;
  1226. if (Size>0) and
  1227. (oso_data in secoptions) then
  1228. sechdr.datapos:=datapos;
  1229. sechdr.nrelocs:=ObjRelocations.count;
  1230. sechdr.relocpos:=coffrelocpos;
  1231. if win32 then
  1232. sechdr.flags:=peencodesechdrflags(secoptions,secalign)
  1233. else
  1234. sechdr.flags:=djencodesechdrflags(secoptions);
  1235. FWriter.write(sechdr,sizeof(sechdr));
  1236. end;
  1237. end;
  1238. procedure TCoffObjOutput.section_write_data(p:TObject;arg:pointer);
  1239. begin
  1240. with TObjSection(p) do
  1241. begin
  1242. if assigned(data) then
  1243. begin
  1244. FWriter.writezeros(dataalignbytes);
  1245. if Datapos<>FWriter.ObjSize then
  1246. internalerror(200603052);
  1247. FWriter.writearray(data);
  1248. end;
  1249. end;
  1250. end;
  1251. function TCoffObjOutput.writedata(data:TObjData):boolean;
  1252. var
  1253. orgdatapos,
  1254. datapos,
  1255. sympos : aint;
  1256. i : longint;
  1257. gotreloc : boolean;
  1258. header : coffheader;
  1259. begin
  1260. result:=false;
  1261. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1262. FCoffStrs:=TDynamicArray.Create(strsresize);
  1263. with TCoffObjData(data) do
  1264. begin
  1265. { Create Symbol Table }
  1266. create_symbols(data);
  1267. { Calculate the filepositions }
  1268. datapos:=sizeof(coffheader)+sizeof(coffsechdr)*ObjSectionList.Count;
  1269. { Sections first }
  1270. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  1271. { relocs }
  1272. orgdatapos:=datapos;
  1273. ObjSectionList.ForEachCall(@section_set_reloc_datapos,@datapos);
  1274. gotreloc:=(orgdatapos<>datapos);
  1275. { Symbols }
  1276. sympos:=datapos;
  1277. { Generate COFF header }
  1278. fillchar(header,sizeof(coffheader),0);
  1279. header.mach:=COFF_MAGIC;
  1280. header.nsects:=ObjSectionList.Count;
  1281. header.sympos:=sympos;
  1282. header.syms:=symidx;
  1283. if win32 then
  1284. begin
  1285. {$ifdef arm}
  1286. header.flag:=PE_FILE_32BIT_MACHINE or
  1287. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1288. {$else arm}
  1289. header.flag:=PE_FILE_BYTES_REVERSED_LO or PE_FILE_32BIT_MACHINE or
  1290. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1291. {$endif arm}
  1292. if not gotreloc then
  1293. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  1294. end
  1295. else
  1296. begin
  1297. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  1298. if not gotreloc then
  1299. header.flag:=header.flag or COFF_FLAG_NORELOCS;
  1300. end;
  1301. FWriter.write(header,sizeof(header));
  1302. { Section headers }
  1303. ObjSectionList.ForEachCall(@section_write_header,nil);
  1304. { ObjSections }
  1305. ObjSectionList.ForEachCall(@section_write_data,nil);
  1306. { Relocs }
  1307. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  1308. { ObjSymbols }
  1309. if Sympos<>FWriter.ObjSize then
  1310. internalerror(200603051);
  1311. FWriter.writearray(FCoffSyms);
  1312. { Strings }
  1313. i:=FCoffStrs.size+4;
  1314. FWriter.write(i,4);
  1315. FWriter.writearray(FCoffStrs);
  1316. end;
  1317. FCoffStrs.Free;
  1318. FCoffStrs:=nil;
  1319. FCoffSyms.Free;
  1320. FCoffSyms:=nil;
  1321. end;
  1322. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  1323. begin
  1324. inherited createcoff(AWriter,false);
  1325. cobjdata:=TDJCoffObjData;
  1326. end;
  1327. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  1328. begin
  1329. inherited createcoff(AWriter,true);
  1330. cobjdata:=TPECoffObjData;
  1331. end;
  1332. {****************************************************************************
  1333. TCoffObjInput
  1334. ****************************************************************************}
  1335. constructor TCoffObjInput.createcoff(awin32:boolean);
  1336. begin
  1337. inherited create;
  1338. win32:=awin32;
  1339. FSymTbl:=nil;
  1340. end;
  1341. destructor TCoffObjInput.destroy;
  1342. begin
  1343. if assigned(FCoffSyms) then
  1344. FCoffSyms.free;
  1345. if assigned(FCoffStrs) then
  1346. FCoffStrs.free;
  1347. if assigned(FSymTbl) then
  1348. freemem(FSymTbl);
  1349. if assigned(FSecTbl) then
  1350. freemem(FSecTbl);
  1351. inherited destroy;
  1352. end;
  1353. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  1354. begin
  1355. result:=nil;
  1356. if (secidx<1) or (secidx>FSecCount) then
  1357. begin
  1358. InputError('Failed reading coff file, invalid section index');
  1359. exit;
  1360. end;
  1361. result:=FSecTbl^[secidx];
  1362. end;
  1363. function TCoffObjInput.Read_str(strpos:longint):string;
  1364. begin
  1365. FCoffStrs.Seek(strpos-4);
  1366. FCoffStrs.Read(result[1],255);
  1367. result[255]:=#0;
  1368. result[0]:=chr(strlen(@result[1]));
  1369. if result='' then
  1370. Internalerror(200205172);
  1371. end;
  1372. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  1373. var
  1374. rel : coffreloc;
  1375. rel_type : TObjRelocationType;
  1376. i : longint;
  1377. p : TObjSymbol;
  1378. extradist : longint;
  1379. begin
  1380. for i:=1 to s.coffrelocs do
  1381. begin
  1382. FReader.read(rel,sizeof(rel));
  1383. case rel.reloctype of
  1384. {$ifdef arm}
  1385. R_IMAGEBASE :
  1386. ;
  1387. {$endif arm}
  1388. {$ifdef i386}
  1389. R_PCRLONG :
  1390. rel_type:=RELOC_RELATIVE;
  1391. R_DIR32 :
  1392. rel_type:=RELOC_ABSOLUTE;
  1393. R_IMAGEBASE :
  1394. rel_type:=RELOC_RVA;
  1395. {$endif i386}
  1396. {$ifdef x86_64}
  1397. IMAGE_REL_AMD64_REL32:
  1398. rel_type:=RELOC_RELATIVE;
  1399. IMAGE_REL_AMD64_ADDR32:
  1400. rel_type:=RELOC_ABSOLUTE32;
  1401. IMAGE_REL_AMD64_ADDR64:
  1402. rel_type:=RELOC_ABSOLUTE;
  1403. IMAGE_REL_AMD64_ADDR32NB:
  1404. rel_type:=RELOC_RVA;
  1405. IMAGE_REL_AMD64_REL32_1:
  1406. rel_type:=RELOC_RELATIVE_1;
  1407. IMAGE_REL_AMD64_REL32_2:
  1408. rel_type:=RELOC_RELATIVE_2;
  1409. IMAGE_REL_AMD64_REL32_3:
  1410. rel_type:=RELOC_RELATIVE_3;
  1411. IMAGE_REL_AMD64_REL32_4:
  1412. rel_type:=RELOC_RELATIVE_4;
  1413. IMAGE_REL_AMD64_REL32_5:
  1414. rel_type:=RELOC_RELATIVE_5;
  1415. {$endif x86_64}
  1416. else
  1417. begin
  1418. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  1419. exit;
  1420. end;
  1421. end;
  1422. p:=FSymTbl^[rel.sym].sym;
  1423. if assigned(p) then
  1424. s.addsymsizereloc(rel.address-s.mempos,p,FSymTbl^[rel.sym].orgsize,rel_type)
  1425. else
  1426. begin
  1427. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  1428. exit;
  1429. end;
  1430. end;
  1431. end;
  1432. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  1433. var
  1434. size,
  1435. address,
  1436. nsyms,
  1437. symidx : aint;
  1438. i : longint;
  1439. sym : coffsymbol;
  1440. objsym : TObjSymbol;
  1441. bind : Tasmsymbind;
  1442. strname : string;
  1443. auxrec : array[0..17] of byte;
  1444. objsec : TObjSection;
  1445. begin
  1446. with TCoffObjData(objdata) do
  1447. begin
  1448. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1449. { Allocate memory for symidx -> TObjSymbol table }
  1450. GetMem(FSymTbl,nsyms*sizeof(TObjSymbolrec));
  1451. FillChar(FSymTbl^,nsyms*sizeof(TObjSymbolrec),0);
  1452. { Load the Symbols }
  1453. FCoffSyms.Seek(0);
  1454. symidx:=0;
  1455. while (symidx<nsyms) do
  1456. begin
  1457. FCoffSyms.Read(sym,sizeof(sym));
  1458. if plongint(@sym.name)^<>0 then
  1459. begin
  1460. move(sym.name,strname[1],8);
  1461. strname[9]:=#0;
  1462. strname[0]:=chr(strlen(@strname[1]));
  1463. if strname='' then
  1464. Internalerror(200205171);
  1465. end
  1466. else
  1467. strname:=Read_str(sym.strpos);
  1468. bind:=AB_EXTERNAL;
  1469. size:=0;
  1470. address:=0;
  1471. objsym:=nil;
  1472. objsec:=nil;
  1473. case sym.typ of
  1474. COFF_SYM_GLOBAL :
  1475. begin
  1476. if sym.section=0 then
  1477. begin
  1478. if sym.value=0 then
  1479. bind:=AB_EXTERNAL
  1480. else
  1481. begin
  1482. bind:=AB_COMMON;
  1483. size:=sym.value;
  1484. end;
  1485. end
  1486. else
  1487. begin
  1488. bind:=AB_GLOBAL;
  1489. objsec:=GetSection(sym.section);
  1490. if sym.value>=objsec.mempos then
  1491. address:=sym.value-objsec.mempos;
  1492. end;
  1493. objsym:=CreateSymbol(strname);
  1494. objsym.bind:=bind;
  1495. objsym.typ:=AT_FUNCTION;
  1496. objsym.objsection:=objsec;
  1497. objsym.offset:=address;
  1498. objsym.size:=size;
  1499. { Register in ObjSection }
  1500. if assigned(objsec) then
  1501. objsec.AddSymbolDefine(objsym);
  1502. end;
  1503. COFF_SYM_LABEL,
  1504. COFF_SYM_LOCAL :
  1505. begin
  1506. { do not add constants (section=-1) }
  1507. if sym.section<>-1 then
  1508. begin
  1509. bind:=AB_LOCAL;
  1510. objsec:=GetSection(sym.section);
  1511. if sym.value>=objsec.mempos then
  1512. address:=sym.value-objsec.mempos;
  1513. objsym:=CreateSymbol(strname);
  1514. objsym.bind:=bind;
  1515. objsym.typ:=AT_FUNCTION;
  1516. objsym.objsection:=objsec;
  1517. objsym.offset:=address;
  1518. objsym.size:=size;
  1519. end;
  1520. end;
  1521. COFF_SYM_SECTION :
  1522. begin
  1523. if sym.section=0 then
  1524. InputError('Failed reading coff file, illegal section');
  1525. objsec:=GetSection(sym.section);
  1526. if sym.value>=objsec.mempos then
  1527. address:=sym.value-objsec.mempos;
  1528. objsym:=CreateSymbol(strname);
  1529. objsym.bind:=AB_LOCAL;
  1530. objsym.typ:=AT_FUNCTION;
  1531. objsym.objsection:=objsec;
  1532. objsym.offset:=address;
  1533. objsym.size:=size;
  1534. end;
  1535. COFF_SYM_FUNCTION,
  1536. COFF_SYM_FILE :
  1537. ;
  1538. else
  1539. internalerror(200602232);
  1540. end;
  1541. FSymTbl^[symidx].sym:=objsym;
  1542. FSymTbl^[symidx].orgsize:=size;
  1543. { read aux records }
  1544. for i:=1 to sym.aux do
  1545. begin
  1546. FCoffSyms.Read(auxrec,sizeof(auxrec));
  1547. inc(symidx);
  1548. end;
  1549. inc(symidx);
  1550. end;
  1551. end;
  1552. end;
  1553. procedure TCoffObjInput.ObjSections_read_data(p:TObject;arg:pointer);
  1554. begin
  1555. with TCoffObjSection(p) do
  1556. begin
  1557. { Skip debug sections }
  1558. if (cs_link_strip in aktglobalswitches) and
  1559. (oso_debug in secoptions) then
  1560. exit;
  1561. if assigned(data) then
  1562. begin
  1563. Reader.Seek(datapos);
  1564. if not Reader.ReadArray(data,Size) then
  1565. begin
  1566. Comment(V_Error,'Error reading coff file, can''t read object data');
  1567. exit;
  1568. end;
  1569. end;
  1570. end;
  1571. end;
  1572. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  1573. begin
  1574. with TCoffObjSection(p) do
  1575. begin
  1576. { Skip debug sections }
  1577. if (cs_link_strip in aktglobalswitches) and
  1578. (oso_debug in secoptions) then
  1579. exit;
  1580. if coffrelocs>0 then
  1581. begin
  1582. Reader.Seek(coffrelocpos);
  1583. read_relocs(TCoffObjSection(p));
  1584. end;
  1585. end;
  1586. end;
  1587. function TCoffObjInput.readObjData(objdata:TObjData):boolean;
  1588. var
  1589. secalign : shortint;
  1590. strsize,
  1591. strpos,
  1592. i : longint;
  1593. code : longint;
  1594. objsec : TCoffObjSection;
  1595. secoptions : TObjSectionOptions;
  1596. header : coffheader;
  1597. sechdr : coffsechdr;
  1598. secname : string;
  1599. secnamebuf : array[0..15] of char;
  1600. begin
  1601. result:=false;
  1602. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1603. FCoffStrs:=TDynamicArray.Create(strsresize);
  1604. with TCoffObjData(objdata) do
  1605. begin
  1606. { Read COFF header }
  1607. if not reader.read(header,sizeof(coffheader)) then
  1608. begin
  1609. Comment(V_Error,'Error reading coff file, can''t read header: '+reader.filename);
  1610. exit;
  1611. end;
  1612. if header.mach<>COFF_MAGIC then
  1613. begin
  1614. Comment(V_Error,'Not a coff file, illegal magic: '+reader.filename);
  1615. exit;
  1616. end;
  1617. { Strings }
  1618. Reader.Seek(header.sympos+header.syms*sizeof(CoffSymbol));
  1619. if not Reader.Read(strsize,4) then
  1620. begin
  1621. Comment(V_Error,'Error reading coff file');
  1622. exit;
  1623. end;
  1624. if strsize<4 then
  1625. begin
  1626. Comment(V_Error,'Error reading coff file');
  1627. exit;
  1628. end;
  1629. if not Reader.ReadArray(FCoffStrs,Strsize-4) then
  1630. begin
  1631. Comment(V_Error,'Error reading coff file: '+reader.filename);
  1632. exit;
  1633. end;
  1634. { Section headers }
  1635. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  1636. FSecCount:=header.nsects;
  1637. GetMem(FSecTbl,(header.nsects+1)*sizeof(TObjSection));
  1638. FillChar(FSecTbl^,(header.nsects+1)*sizeof(TObjSection),0);
  1639. reader.Seek(sizeof(coffheader)+header.opthdr);
  1640. for i:=1 to header.nsects do
  1641. begin
  1642. if not reader.read(sechdr,sizeof(sechdr)) then
  1643. begin
  1644. Comment(V_Error,'Error reading coff file, can''t read section header: '+reader.filename);
  1645. exit;
  1646. end;
  1647. move(sechdr.name,secnamebuf,8);
  1648. secnamebuf[8]:=#0;
  1649. secname:=strpas(secnamebuf);
  1650. if secname[1]='/' then
  1651. begin
  1652. Val(Copy(secname,2,8),strpos,code);
  1653. if code=0 then
  1654. secname:=Read_str(strpos)
  1655. else
  1656. begin
  1657. Comment(V_Error,'Error reading section headers coff file');
  1658. secname:='error';
  1659. end;
  1660. end;
  1661. if win32 then
  1662. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  1663. else
  1664. begin
  1665. djdecodesechdrflags(secname,sechdr.flags);
  1666. secalign:=sizeof(aint);
  1667. end;
  1668. {$warning TODO idata keep can maybe replaced with grouping of text and idata}
  1669. if (Copy(secname,1,6)='.idata') or
  1670. (Copy(secname,1,6)='.rsrc') then
  1671. include(secoptions,oso_keep);
  1672. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions));
  1673. FSecTbl^[i]:=objsec;
  1674. if not win32 then
  1675. objsec.mempos:=sechdr.rvaofs;
  1676. objsec.orgmempos:=sechdr.rvaofs;
  1677. objsec.coffrelocs:=sechdr.nrelocs;
  1678. objsec.coffrelocpos:=sechdr.relocpos;
  1679. objsec.datapos:=sechdr.datapos;
  1680. objsec.Size:=sechdr.dataSize;
  1681. end;
  1682. { ObjSymbols }
  1683. Reader.Seek(header.sympos);
  1684. if not Reader.ReadArray(FCoffSyms,header.syms*sizeof(CoffSymbol)) then
  1685. begin
  1686. Comment(V_Error,'Error reading coff file');
  1687. exit;
  1688. end;
  1689. { Insert all ObjSymbols }
  1690. read_symbols(objdata);
  1691. { Section Data }
  1692. ObjSectionList.ForEachCall(@objsections_read_data,nil);
  1693. { Relocs }
  1694. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  1695. end;
  1696. FCoffStrs.Free;
  1697. FCoffStrs:=nil;
  1698. FCoffSyms.Free;
  1699. FCoffSyms:=nil;
  1700. result:=true;
  1701. end;
  1702. constructor TDJCoffObjInput.create;
  1703. begin
  1704. inherited createcoff(false);
  1705. cobjdata:=TDJCoffObjData;
  1706. end;
  1707. constructor TPECoffObjInput.create;
  1708. begin
  1709. inherited createcoff(true);
  1710. cobjdata:=TPECoffObjData;
  1711. end;
  1712. {****************************************************************************
  1713. TCoffexesection
  1714. ****************************************************************************}
  1715. constructor TCoffExeSection.createcoff(AList:TFPHashObjectList;const n:string;awin32:boolean);
  1716. begin
  1717. inherited create(AList,n);
  1718. win32:=awin32;
  1719. end;
  1720. constructor TDJCoffExeSection.create(AList:TFPHashObjectList;const n:string);
  1721. begin
  1722. inherited createcoff(AList,n,false);
  1723. end;
  1724. constructor TPECoffExeSection.create(AList:TFPHashObjectList;const n:string);
  1725. begin
  1726. inherited createcoff(AList,n,false);
  1727. end;
  1728. {****************************************************************************
  1729. TCoffexeoutput
  1730. ****************************************************************************}
  1731. constructor TCoffexeoutput.createcoff(awin32:boolean);
  1732. begin
  1733. inherited create;
  1734. win32:=awin32;
  1735. if win32 then
  1736. imagebase:=$400000;
  1737. end;
  1738. procedure TCoffexeoutput.write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  1739. var
  1740. sym : coffsymbol;
  1741. begin
  1742. FillChar(sym,sizeof(sym),0);
  1743. if length(name)>8 then
  1744. begin
  1745. sym.strpos:=FCoffStrs.size+4;
  1746. FCoffStrs.writestr(name);
  1747. FCoffStrs.writestr(#0);
  1748. end
  1749. else
  1750. move(name[1],sym.name,length(name));
  1751. sym.value:=value;
  1752. sym.section:=section;
  1753. sym.typ:=typ;
  1754. sym.aux:=aux;
  1755. FWriter.write(sym,sizeof(sym));
  1756. end;
  1757. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  1758. var
  1759. value : aint;
  1760. globalval : byte;
  1761. exesec : TExeSection;
  1762. begin
  1763. if not assigned(texesymbol(p).objsymbol) then
  1764. internalerror(200603053);
  1765. with texesymbol(p).objsymbol do
  1766. begin
  1767. exesec:=TExeSection(objsection.exesection);
  1768. if not assigned(exesec) then
  1769. internalerror(200602255);
  1770. if bind=AB_LOCAL then
  1771. globalval:=3
  1772. else
  1773. globalval:=2;
  1774. { reloctype address to the section in the executable }
  1775. value:=address-exesec.mempos;
  1776. write_symbol(name,value,exesec.secsymidx,globalval,0);
  1777. end;
  1778. end;
  1779. procedure TCoffexeoutput.ExeSections_write_header(p:TObject;arg:pointer);
  1780. var
  1781. sechdr : coffsechdr;
  1782. begin
  1783. with tExeSection(p) do
  1784. begin
  1785. fillchar(sechdr,sizeof(sechdr),0);
  1786. move(name[1],sechdr.name,length(name));
  1787. sechdr.rvaofs:=mempos;
  1788. sechdr.vsize:=mempos;
  1789. if win32 then
  1790. begin
  1791. sechdr.rvaofs:=mempos;
  1792. sechdr.vsize:=align(Size,$100);
  1793. end
  1794. else
  1795. begin
  1796. sechdr.rvaofs:=mempos;
  1797. sechdr.vsize:=mempos;
  1798. end;
  1799. if oso_data in SecOptions then
  1800. begin
  1801. sechdr.dataSize:=Size;
  1802. sechdr.datapos:=datapos;
  1803. end
  1804. else
  1805. sechdr.dataSize:=Size;
  1806. sechdr.nrelocs:=0;
  1807. sechdr.relocpos:=0;
  1808. if win32 then
  1809. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign)
  1810. else
  1811. sechdr.flags:=djencodesechdrflags(SecOptions);
  1812. FWriter.write(sechdr,sizeof(sechdr));
  1813. end;
  1814. end;
  1815. procedure TCoffexeoutput.ExeSections_pass2_header(p:TObject;arg:pointer);
  1816. begin
  1817. with TExeSection(p) do
  1818. begin
  1819. inc(plongint(arg)^);
  1820. secsymidx:=plongint(arg)^;
  1821. end;
  1822. end;
  1823. procedure Tcoffexeoutput.ExeSections_write_Data(p:TObject;arg:pointer);
  1824. var
  1825. objsec : TObjSection;
  1826. i : longint;
  1827. begin
  1828. with texesection(p) do
  1829. begin
  1830. if oso_data in secoptions then
  1831. begin
  1832. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  1833. for i:=0 to ObjSectionList.Count-1 do
  1834. begin
  1835. objsec:=TObjSection(ObjSectionList[i]);
  1836. if oso_data in objsec.secoptions then
  1837. begin
  1838. if not assigned(objsec.data) then
  1839. internalerror(200603042);
  1840. FWriter.writezeros(objsec.dataalignbytes);
  1841. if objsec.DataPos<>FWriter.Size then
  1842. internalerror(200602251);
  1843. FWriter.writearray(objsec.data);
  1844. end;
  1845. end;
  1846. end;
  1847. end;
  1848. end;
  1849. procedure tcoffexeoutput.CalcPos_Header;
  1850. var
  1851. stubsize,
  1852. optheadersize : longint;
  1853. begin
  1854. if win32 then
  1855. begin
  1856. stubsize:=sizeof(win32stub);
  1857. optheadersize:=sizeof(coffpeoptheader);
  1858. end
  1859. else
  1860. begin
  1861. stubsize:=sizeof(go32v2stub);
  1862. optheadersize:=sizeof(coffdjoptheader);
  1863. end;
  1864. { retrieve amount of ObjSections }
  1865. nsects:=0;
  1866. ExeSections.ForEachCall(@ExeSections_pass2_header,@nsects);
  1867. { calculate start positions after the headers }
  1868. currdatapos:=stubsize+optheadersize+sizeof(coffsechdr)*nsects;
  1869. currmempos:=stubsize+optheadersize+sizeof(coffsechdr)*nsects;
  1870. end;
  1871. procedure tcoffexeoutput.CalcPos_Symbols;
  1872. begin
  1873. nsyms:=0;
  1874. sympos:=0;
  1875. if not(cs_link_strip in aktglobalswitches) then
  1876. begin
  1877. nsyms:=ExeSymbolList.Count;
  1878. sympos:=CurrDataPos;
  1879. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  1880. end;
  1881. end;
  1882. function TCoffexeoutput.writedata:boolean;
  1883. var
  1884. i : longint;
  1885. header : coffheader;
  1886. djoptheader : coffdjoptheader;
  1887. peoptheader : coffpeoptheader;
  1888. rsrcexesec,
  1889. idataexesec,
  1890. textExeSec,
  1891. dataExeSec,
  1892. bssExeSec : TExeSection;
  1893. begin
  1894. result:=false;
  1895. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1896. FCoffStrs:=TDynamicArray.Create(strsresize);
  1897. textExeSec:=FindExeSection('.text');
  1898. dataExeSec:=FindExeSection('.data');
  1899. bssExeSec:=FindExeSection('.bss');
  1900. if not assigned(TextExeSec) or
  1901. not assigned(DataExeSec) or
  1902. not assigned(BSSExeSec) then
  1903. internalerror(200602231);
  1904. { Stub }
  1905. if win32 then
  1906. FWriter.write(win32stub,sizeof(win32stub))
  1907. else
  1908. FWriter.write(go32v2stub,sizeof(go32v2stub));
  1909. { COFF header }
  1910. fillchar(header,sizeof(header),0);
  1911. header.mach:=COFF_MAGIC;
  1912. header.nsects:=nsects;
  1913. header.sympos:=sympos;
  1914. header.syms:=nsyms;
  1915. if win32 then
  1916. header.opthdr:=sizeof(coffpeoptheader)
  1917. else
  1918. header.opthdr:=sizeof(coffdjoptheader);
  1919. if win32 then
  1920. begin
  1921. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_RELOCS_STRIPPED or
  1922. {PE_FILE_BYTES_REVERSED_LO or }
  1923. PE_FILE_LINE_NUMS_STRIPPED;
  1924. if FindExeSection('.stab')=nil then
  1925. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  1926. if (cs_link_strip in aktglobalswitches) then
  1927. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  1928. end
  1929. else
  1930. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  1931. if target_info.system in [system_i386_win32,system_arm_wince,system_i386_wince] then
  1932. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  1933. FWriter.write(header,sizeof(header));
  1934. { Optional COFF Header }
  1935. if win32 then
  1936. begin
  1937. fillchar(peoptheader,sizeof(peoptheader),0);
  1938. peoptheader.magic:=COFF_OPT_MAGIC;
  1939. peoptheader.tsize:=TextExeSec.Size;
  1940. peoptheader.dsize:=DataExeSec.Size;
  1941. peoptheader.bsize:=BSSExeSec.Size;
  1942. peoptheader.text_start:=TextExeSec.mempos;
  1943. {$ifndef x86_64}
  1944. peoptheader.data_start:=DataExeSec.mempos;
  1945. {$endif x86_64}
  1946. peoptheader.entry:=EntrySym.Address;
  1947. peoptheader.ImageBase:=ImageBase;
  1948. peoptheader.SectionAlignment:=SectionMemAlign;
  1949. peoptheader.FileAlignment:=SectionDataAlign;
  1950. peoptheader.MajorOperatingSystemVersion:=4;
  1951. peoptheader.MinorOperatingSystemVersion:=0;
  1952. peoptheader.MajorImageVersion:=dllmajor;
  1953. peoptheader.MinorImageVersion:=dllminor;
  1954. peoptheader.MajorSubsystemVersion:=4;
  1955. peoptheader.MinorSubsystemVersion:=0;
  1956. peoptheader.Win32Version:=0;
  1957. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  1958. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  1959. peoptheader.CheckSum:=0;
  1960. if target_info.system in [system_arm_wince,system_i386_wince] then
  1961. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  1962. else
  1963. if apptype=app_gui then
  1964. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  1965. else
  1966. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  1967. peoptheader.DllCharacteristics:=0;
  1968. peoptheader.SizeOfStackReserve:=stacksize;
  1969. peoptheader.SizeOfStackCommit:=$1000;
  1970. peoptheader.SizeOfHeapReserve:=$100000;
  1971. peoptheader.SizeOfHeapCommit:=$1000;
  1972. peoptheader.LoaderFlags:=0;
  1973. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  1974. idataexesec:=FindExeSection('.idata');
  1975. if assigned(idataexesec) then
  1976. begin
  1977. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=idataexesec.mempos;
  1978. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=idataexesec.Size;
  1979. end;
  1980. rsrcexesec:=FindExeSection('.rsrc');
  1981. if assigned(rsrcexesec) then
  1982. begin
  1983. peoptheader.DataDirectory[PE_DATADIR_RSRC].vaddr:=rsrcexesec.mempos;
  1984. peoptheader.DataDirectory[PE_DATADIR_RSRC].size:=rsrcexesec.Size;
  1985. end;
  1986. FWriter.write(peoptheader,sizeof(peoptheader));
  1987. end
  1988. else
  1989. begin
  1990. fillchar(djoptheader,sizeof(djoptheader),0);
  1991. djoptheader.magic:=COFF_OPT_MAGIC;
  1992. djoptheader.tsize:=TextExeSec.Size;
  1993. djoptheader.dsize:=DataExeSec.Size;
  1994. djoptheader.bsize:=BSSExeSec.Size;
  1995. djoptheader.text_start:=TextExeSec.mempos;
  1996. djoptheader.data_start:=DataExeSec.mempos;
  1997. djoptheader.entry:=EntrySym.offset;
  1998. FWriter.write(djoptheader,sizeof(djoptheader));
  1999. end;
  2000. { Section headers }
  2001. ExeSections.ForEachCall(@ExeSections_write_header,nil);
  2002. { Section data }
  2003. ExeSections.ForEachCall(@ExeSections_write_data,nil);
  2004. { Optional ObjSymbols }
  2005. if not(cs_link_strip in aktglobalswitches) then
  2006. begin
  2007. if SymPos<>FWriter.Size then
  2008. internalerror(200602252);
  2009. { ObjSymbols }
  2010. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  2011. { Strings }
  2012. i:=FCoffStrs.size+4;
  2013. FWriter.write(i,4);
  2014. FWriter.writearray(FCoffStrs);
  2015. end;
  2016. { Release }
  2017. FCoffStrs.Free;
  2018. FCoffSyms.Free;
  2019. result:=true;
  2020. end;
  2021. constructor TDJCoffexeoutput.create;
  2022. begin
  2023. inherited createcoff(false);
  2024. CExeSection:=TDJCoffExeSection;
  2025. CObjData:=TPECoffObjData;
  2026. end;
  2027. constructor TPECoffexeoutput.create;
  2028. begin
  2029. inherited createcoff(true);
  2030. CExeSection:=TPECoffExeSection;
  2031. CObjData:=TPECoffObjData;
  2032. end;
  2033. procedure TPECoffexeoutput.GenerateLibraryImports(ExternalLibraryList:TFPHashObjectList);
  2034. var
  2035. textobjsection,
  2036. idata2objsection,
  2037. idata4objsection,
  2038. idata5objsection,
  2039. idata6objsection,
  2040. idata7objsection : TObjSection;
  2041. procedure StartImport(const dllname:string);
  2042. var
  2043. idata4label,
  2044. idata5label,
  2045. idata7label : TObjSymbol;
  2046. emptyint : longint;
  2047. basedllname : string;
  2048. begin
  2049. if assigned(exemap) then
  2050. begin
  2051. exemap.Add('');
  2052. exemap.Add('Importing from DLL '+dllname);
  2053. end;
  2054. emptyint:=0;
  2055. basedllname:=splitfilename(dllname);
  2056. textobjsection:=internalobjdata.createsection(sec_code,'');
  2057. idata2objsection:=internalobjdata.createsection(sec_idata2,'');
  2058. idata4objsection:=internalobjdata.createsection(sec_idata4,'');
  2059. idata4label:=internalobjdata.SymbolDefine('__imp_names_'+basedllname,AB_LOCAL,AT_DATA);
  2060. idata5objsection:=internalobjdata.createsection(sec_idata5,'');
  2061. idata5label:=internalobjdata.SymbolDefine('__imp_fixup_'+basedllname,AB_LOCAL,AT_DATA);
  2062. idata6objsection:=internalobjdata.createsection(sec_idata6,'');
  2063. idata7objsection:=internalobjdata.createsection(sec_idata7,'');
  2064. idata7label:=internalobjdata.SymbolDefine('__imp_dll_'+basedllname,AB_LOCAL,AT_DATA);
  2065. { idata2 }
  2066. internalobjdata.SetSection(idata2objsection);
  2067. internalobjdata.writereloc(0,sizeof(longint),idata4label,RELOC_RVA);
  2068. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2069. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2070. internalobjdata.writereloc(0,sizeof(longint),idata7label,RELOC_RVA);
  2071. internalobjdata.writereloc(0,sizeof(longint),idata5label,RELOC_RVA);
  2072. { idata7 }
  2073. internalobjdata.SetSection(idata7objsection);
  2074. internalobjdata.writebytes(basedllname[1],length(basedllname));
  2075. internalobjdata.writebytes(emptyint,1);
  2076. end;
  2077. procedure EndImport;
  2078. var
  2079. emptyint : longint;
  2080. begin
  2081. emptyint:=0;
  2082. { idata4 }
  2083. internalobjdata.SetSection(idata4objsection);
  2084. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2085. if target_info.system=system_x86_64_win64 then
  2086. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2087. { idata5 }
  2088. internalobjdata.SetSection(idata5objsection);
  2089. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2090. if target_info.system=system_x86_64_win64 then
  2091. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2092. end;
  2093. function AddProcImport(const afuncname:string):TObjSymbol;
  2094. const
  2095. {$ifdef x86_64}
  2096. jmpopcode : array[0..2] of byte = (
  2097. $ff,$24,$25
  2098. );
  2099. {$else x86_64}
  2100. jmpopcode : array[0..1] of byte = (
  2101. $ff,$25
  2102. );
  2103. {$endif x86_64}
  2104. nopopcodes : array[0..1] of byte = (
  2105. $90,$90
  2106. );
  2107. var
  2108. idata5label,
  2109. idata6label : TObjSymbol;
  2110. emptyint : longint;
  2111. begin
  2112. result:=nil;
  2113. emptyint:=0;
  2114. if assigned(exemap) then
  2115. exemap.Add(' Importing Function '+afuncname);
  2116. { idata6, import data (ordnr+name) }
  2117. internalobjdata.SetSection(idata6objsection);
  2118. inc(idatalabnr);
  2119. idata6label:=internalobjdata.SymbolDefine('__imp_'+tostr(idatalabnr),AB_LOCAL,AT_DATA);
  2120. internalobjdata.writebytes(emptyint,2);
  2121. internalobjdata.writebytes(afuncname[1],length(afuncname));
  2122. internalobjdata.writebytes(emptyint,1);
  2123. internalobjdata.writebytes(emptyint,align(internalobjdata.CurrObjSec.size,2)-internalobjdata.CurrObjSec.size);
  2124. { idata4, import lookup table }
  2125. internalobjdata.SetSection(idata4objsection);
  2126. internalobjdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  2127. if target_info.system=system_x86_64_win64 then
  2128. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2129. { idata5, import address table }
  2130. internalobjdata.SetSection(idata5objsection);
  2131. idata5label:=internalobjdata.SymbolDefine('__imp_'+afuncname,AB_LOCAL,AT_DATA);
  2132. internalobjdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  2133. if target_info.system=system_x86_64_win64 then
  2134. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2135. { text, jmp }
  2136. internalobjdata.SetSection(textobjsection);
  2137. result:=internalobjdata.SymbolDefine('_'+afuncname,AB_GLOBAL,AT_FUNCTION);
  2138. internalobjdata.writebytes(jmpopcode,sizeof(jmpopcode));
  2139. internalobjdata.writereloc(0,sizeof(longint),idata5label,RELOC_ABSOLUTE32);
  2140. internalobjdata.writebytes(nopopcodes,align(internalobjdata.CurrObjSec.size,sizeof(nopopcodes))-internalobjdata.CurrObjSec.size);
  2141. end;
  2142. var
  2143. i,j : longint;
  2144. ExtLibrary : TExternalLibrary;
  2145. ExtSymbol : TFPHashObject;
  2146. exesym : TExeSymbol;
  2147. begin
  2148. for i:=0 to ExternalLibraryList.Count-1 do
  2149. begin
  2150. ExtLibrary:=TExternalLibrary(ExternalLibraryList[i]);
  2151. idata2objsection:=nil;
  2152. idata4objsection:=nil;
  2153. idata5objsection:=nil;
  2154. idata6objsection:=nil;
  2155. idata7objsection:=nil;
  2156. StartImport(ExtLibrary.Name);
  2157. for j:=0 to ExtLibrary.ExternalSymbolList.Count-1 do
  2158. begin
  2159. ExtSymbol:=TFPHashObject(ExtLibrary.ExternalSymbolList[j]);
  2160. exesym:=TExeSymbol(ExeSymbolList.Find(ExtSymbol.Name));
  2161. if assigned(exesym) and
  2162. not assigned(exesym.objsymbol) then
  2163. exesym.objsymbol:=AddProcImport(ExtSymbol.Name);
  2164. end;
  2165. EndImport;
  2166. end;
  2167. end;
  2168. {****************************************************************************
  2169. TDJCoffAssembler
  2170. ****************************************************************************}
  2171. constructor TDJCoffAssembler.Create(smart:boolean);
  2172. begin
  2173. inherited Create(smart);
  2174. CObjOutput:=TDJCoffObjOutput;
  2175. end;
  2176. {****************************************************************************
  2177. TPECoffAssembler
  2178. ****************************************************************************}
  2179. constructor TPECoffAssembler.Create(smart:boolean);
  2180. begin
  2181. inherited Create(smart);
  2182. CObjOutput:=TPECoffObjOutput;
  2183. end;
  2184. {****************************************************************************
  2185. TCoffLinker
  2186. ****************************************************************************}
  2187. constructor TDJCoffLinker.Create;
  2188. begin
  2189. inherited Create;
  2190. CExeoutput:=TDJCoffexeoutput;
  2191. CObjInput:=TDJCoffObjInput;
  2192. end;
  2193. procedure TDJCoffLinker.DefaultLinkScript;
  2194. begin
  2195. end;
  2196. constructor TPECoffLinker.Create;
  2197. begin
  2198. inherited Create;
  2199. CExeoutput:=TPECoffexeoutput;
  2200. CObjInput:=TPECoffObjInput;
  2201. end;
  2202. procedure TPECoffLinker.DefaultLinkScript;
  2203. begin
  2204. with LinkScript do
  2205. begin
  2206. Concat('READUNITOBJECTS');
  2207. if apptype=app_gui then
  2208. Concat('ENTRYNAME _WinMainCRTStartup')
  2209. else
  2210. Concat('ENTRYNAME _mainCRTStartup');
  2211. Concat('HEADER');
  2212. Concat('EXESECTION .text');
  2213. Concat(' OBJSECTION .text*');
  2214. Concat(' SYMBOL etext');
  2215. Concat('ENDEXESECTION');
  2216. Concat('EXESECTION .data');
  2217. Concat(' SYMBOL __data_start__');
  2218. Concat(' OBJSECTION .data*');
  2219. Concat(' SYMBOL edata');
  2220. Concat(' SYMBOL __data_end__');
  2221. Concat('ENDEXESECTION');
  2222. Concat('EXESECTION .bss');
  2223. Concat(' SYMBOL __bss_start__');
  2224. Concat(' OBJSECTION .bss*');
  2225. Concat(' SYMBOL __bss_end__');
  2226. Concat('ENDEXESECTION');
  2227. Concat('EXESECTION .idata');
  2228. Concat(' OBJSECTION .idata$2');
  2229. Concat(' OBJSECTION .idata$3');
  2230. Concat(' ZEROS 20');
  2231. Concat(' OBJSECTION .idata$4');
  2232. Concat(' OBJSECTION .idata$5');
  2233. Concat(' OBJSECTION .idata$6');
  2234. Concat(' OBJSECTION .idata$7');
  2235. Concat('ENDEXESECTION');
  2236. Concat('EXESECTION .rsrc');
  2237. Concat(' OBJSECTION .rsrc*');
  2238. Concat('ENDEXESECTION');
  2239. Concat('EXESECTION .stab');
  2240. Concat(' OBJSECTION .stab');
  2241. Concat('ENDEXESECTION');
  2242. Concat('EXESECTION .stabstr');
  2243. Concat(' OBJSECTION .stabstr');
  2244. Concat('ENDEXESECTION');
  2245. Concat('STABS');
  2246. Concat('SYMBOLS');
  2247. end;
  2248. end;
  2249. {*****************************************************************************
  2250. DLLReader
  2251. *****************************************************************************}
  2252. {$ifdef win32}
  2253. var
  2254. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  2255. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  2256. {$endif win32}
  2257. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  2258. type
  2259. TPECoffExpDir=packed record
  2260. flag,
  2261. stamp : cardinal;
  2262. Major,
  2263. Minor : word;
  2264. Name,
  2265. Base,
  2266. NumFuncs,
  2267. NumNames,
  2268. AddrFuncs,
  2269. AddrNames,
  2270. AddrOrds : cardinal;
  2271. end;
  2272. var
  2273. DLLReader : TObjectReader;
  2274. DosHeader : array[0..$7f] of byte;
  2275. PEMagic : array[0..3] of byte;
  2276. Header : CoffHeader;
  2277. peheader : coffpeoptheader;
  2278. NameOfs,
  2279. newheaderofs : longint;
  2280. FuncName : string;
  2281. expdir : TPECoffExpDir;
  2282. i : longint;
  2283. found : boolean;
  2284. sechdr : CoffSecHdr;
  2285. {$ifdef win32}
  2286. p : pointer;
  2287. {$endif win32}
  2288. begin
  2289. result:=false;
  2290. {$ifdef win32}
  2291. if (target_info.system=system_x86_64_win64) and
  2292. assigned(Wow64DisableWow64FsRedirection) then
  2293. Wow64DisableWow64FsRedirection(p);
  2294. {$endif win32}
  2295. DLLReader:=TObjectReader.Create;
  2296. DLLReader.OpenFile(dllname);
  2297. {$ifdef win32}
  2298. if (target_info.system=system_x86_64_win64) and
  2299. assigned(Wow64RevertWow64FsRedirection) then
  2300. Wow64RevertWow64FsRedirection(p);
  2301. {$endif win32}
  2302. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  2303. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  2304. begin
  2305. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  2306. exit;
  2307. end;
  2308. newheaderofs:=longint(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  2309. DLLReader.Seek(newheaderofs);
  2310. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  2311. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  2312. begin
  2313. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  2314. exit;
  2315. end;
  2316. if not DLLReader.Read(Header,sizeof(CoffHeader)) or
  2317. (Header.mach<>COFF_MAGIC) or
  2318. (Header.opthdr<>sizeof(coffpeoptheader)) then
  2319. begin
  2320. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  2321. exit;
  2322. end;
  2323. { Read optheader }
  2324. DLLreader.Read(peheader,sizeof(coffpeoptheader));
  2325. { Section headers }
  2326. found:=false;
  2327. for i:=1 to header.nsects do
  2328. begin
  2329. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  2330. begin
  2331. Comment(V_Error,'Error reading coff file '+DLLName);
  2332. exit;
  2333. end;
  2334. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  2335. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  2336. begin
  2337. found:=true;
  2338. break;
  2339. end;
  2340. end;
  2341. if not found then
  2342. begin
  2343. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  2344. exit;
  2345. end;
  2346. { Process edata }
  2347. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  2348. DLLReader.Read(expdir,sizeof(expdir));
  2349. for i:=0 to expdir.NumNames-1 do
  2350. begin
  2351. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  2352. DLLReader.Read(NameOfs,4);
  2353. Dec(NameOfs,sechdr.rvaofs);
  2354. if (NameOfs<0) or
  2355. (NameOfs>sechdr.vsize) then
  2356. begin
  2357. Comment(V_Error,'DLL does contains invalid exports');
  2358. break;
  2359. end;
  2360. { Read Function name from DLL, prepend _ and terminate with #0 }
  2361. DLLReader.Seek(sechdr.datapos+NameOfs);
  2362. DLLReader.Read(FuncName[1],sizeof(FuncName)-3);
  2363. FuncName[sizeof(FuncName)-1]:=#0;
  2364. FuncName[0]:=chr(Strlen(@FuncName[1]));
  2365. readdllproc(DLLName,FuncName);
  2366. end;
  2367. DLLReader.Free;
  2368. end;
  2369. {*****************************************************************************
  2370. Initialize
  2371. *****************************************************************************}
  2372. {$ifdef i386}
  2373. const
  2374. as_i386_coff_info : tasminfo =
  2375. (
  2376. id : as_i386_coff;
  2377. idtxt : 'COFF';
  2378. asmbin : '';
  2379. asmcmd : '';
  2380. supported_target : system_i386_go32v2;
  2381. flags : [af_outputbinary];
  2382. labelprefix : '.L';
  2383. comment : '';
  2384. );
  2385. as_i386_pecoff_info : tasminfo =
  2386. (
  2387. id : as_i386_pecoff;
  2388. idtxt : 'PECOFF';
  2389. asmbin : '';
  2390. asmcmd : '';
  2391. supported_target : system_i386_win32;
  2392. flags : [af_outputbinary,af_smartlink_sections];
  2393. labelprefix : '.L';
  2394. comment : '';
  2395. );
  2396. as_i386_pecoffwdosx_info : tasminfo =
  2397. (
  2398. id : as_i386_pecoffwdosx;
  2399. idtxt : 'PEWDOSX';
  2400. asmbin : '';
  2401. asmcmd : '';
  2402. supported_target : system_i386_wdosx;
  2403. flags : [af_outputbinary];
  2404. labelprefix : '.L';
  2405. comment : '';
  2406. );
  2407. as_i386_pecoffwince_info : tasminfo =
  2408. (
  2409. id : as_i386_pecoffwince;
  2410. idtxt : 'PECOFFWINCE';
  2411. asmbin : '';
  2412. asmcmd : '';
  2413. supported_target : system_i386_wince;
  2414. flags : [af_outputbinary];
  2415. labelprefix : '.L';
  2416. comment : '';
  2417. );
  2418. {$endif i386}
  2419. {$ifdef x86_64}
  2420. const
  2421. as_x86_64_pecoff_info : tasminfo =
  2422. (
  2423. id : as_x86_64_pecoff;
  2424. idtxt : 'PECOFF';
  2425. asmbin : '';
  2426. asmcmd : '';
  2427. supported_target : system_x86_64_win64;
  2428. flags : [af_outputbinary,af_smartlink_sections];
  2429. labelprefix : '.L';
  2430. comment : '';
  2431. );
  2432. {$endif x86_64}
  2433. {$ifdef arm}
  2434. const
  2435. as_arm_pecoffwince_info : tasminfo =
  2436. (
  2437. id : as_arm_pecoffwince;
  2438. idtxt : 'PECOFFWINCE';
  2439. asmbin : '';
  2440. asmcmd : '';
  2441. supported_target : system_arm_wince;
  2442. flags : [af_outputbinary];
  2443. labelprefix : '.L';
  2444. comment : '';
  2445. );
  2446. {$endif arm}
  2447. {$ifdef win32}
  2448. procedure SetupProcVars;
  2449. var
  2450. hinstLib : THandle;
  2451. begin
  2452. Wow64DisableWow64FsRedirection:=nil;
  2453. Wow64RevertWow64FsRedirection:=nil;
  2454. hinstLib:=LoadLibrary('kernel32.dll');
  2455. if hinstLib<>0 then
  2456. begin
  2457. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  2458. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  2459. FreeLibrary(hinstLib);
  2460. end;
  2461. end;
  2462. {$endif win32}
  2463. initialization
  2464. {$ifdef i386}
  2465. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  2466. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  2467. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  2468. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  2469. {$endif i386}
  2470. {$ifdef x86_64}
  2471. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  2472. {$endif x86_64}
  2473. {$ifdef arm}
  2474. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  2475. {$endif arm}
  2476. {$ifdef win32}
  2477. SetupProcVars;
  2478. {$endif win32}
  2479. end.