ogcoff.pas 85 KB

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