ogcoff.pas 82 KB

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