ogcoff.pas 97 KB

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