ogcoff.pas 92 KB

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