ogcoff.pas 99 KB

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