ogcoff.pas 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary coff/PE reader and writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogcoff;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. cpuinfo,cpubase,aasmbase,assemble,link,
  27. { output }
  28. ogbase,
  29. owbase;
  30. const
  31. PE_DATADIR_ENTRIES = 16;
  32. type
  33. tcoffpedatadir = packed record
  34. vaddr : longint;
  35. size : longint;
  36. end;
  37. tcoffheader = packed record
  38. mach : word;
  39. nsects : smallint;
  40. time : longint;
  41. sympos : longint;
  42. syms : longint;
  43. opthdr : word;
  44. flag : word;
  45. end;
  46. tcoffpeoptheader = packed record
  47. Magic : word;
  48. MajorLinkerVersion : byte;
  49. MinorLinkerVersion : byte;
  50. tsize : longint;
  51. dsize : longint;
  52. bsize : longint;
  53. entry : longint;
  54. text_start : longint;
  55. {$ifndef x86_64}
  56. data_start : longint;
  57. {$endif x86_64}
  58. ImageBase : aint;
  59. SectionAlignment : longint;
  60. FileAlignment : longint;
  61. MajorOperatingSystemVersion : word;
  62. MinorOperatingSystemVersion : word;
  63. MajorImageVersion : word;
  64. MinorImageVersion : word;
  65. MajorSubsystemVersion : word;
  66. MinorSubsystemVersion : word;
  67. Win32Version : longint;
  68. SizeOfImage : longint;
  69. SizeOfHeaders : longint;
  70. CheckSum : longint;
  71. Subsystem : word;
  72. DllCharacteristics : word;
  73. SizeOfStackReserve : aint;
  74. SizeOfStackCommit : aint;
  75. SizeOfHeapReserve : aint;
  76. SizeOfHeapCommit : aint;
  77. LoaderFlags : longint;
  78. NumberOfRvaAndSizes : longint;
  79. DataDirectory : array[0..PE_DATADIR_ENTRIES-1] of tcoffpedatadir;
  80. end;
  81. tcoffsechdr = packed record
  82. name : array[0..7] of char;
  83. vsize : longint;
  84. rvaofs : longint;
  85. datasize : longint;
  86. datapos : longint;
  87. relocpos : longint;
  88. lineno1 : longint;
  89. nrelocs : word;
  90. lineno2 : word;
  91. flags : longword;
  92. end;
  93. TCoffObjSection = class(TObjSection)
  94. private
  95. orgmempos,
  96. coffrelocs,
  97. coffrelocpos : aint;
  98. public
  99. secidx : longint;
  100. flags : longword;
  101. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  102. procedure addsymsizereloc(ofs:aint;p:TObjSymbol;symsize:aint;reloctype:TObjRelocationType);
  103. procedure fixuprelocs;override;
  104. end;
  105. TDJCoffObjSection = class(TCoffObjSection)
  106. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  107. end;
  108. TPECoffObjSection = class(TCoffObjSection)
  109. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  110. end;
  111. TCoffObjData = class(TObjData)
  112. private
  113. win32 : boolean;
  114. public
  115. constructor createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  116. destructor destroy;override;
  117. procedure CreateDebugSections;override;
  118. function sectionname(atype:TAsmSectiontype;const aname:string):string;override;
  119. procedure writereloc(data,len:aint;p:TObjSymbol;reloctype:TObjRelocationType);override;
  120. procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
  121. procedure afteralloc;override;
  122. end;
  123. TDJCoffObjData = class(TCoffObjData)
  124. constructor create(const n:string);override;
  125. end;
  126. TPECoffObjData = class(TCoffObjData)
  127. constructor create(const n:string);override;
  128. end;
  129. TCoffObjOutput = class(tObjOutput)
  130. private
  131. win32 : boolean;
  132. symidx : longint;
  133. FCoffSyms,
  134. FCoffStrs : tdynamicarray;
  135. procedure write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  136. procedure section_write_symbol(p:TObject;arg:pointer);
  137. procedure section_write_relocs(p:TObject;arg:pointer);
  138. procedure create_symbols(data:TObjData);
  139. procedure section_set_datapos(p:TObject;arg:pointer);
  140. procedure section_set_reloc_datapos(p:TObject;arg:pointer);
  141. procedure section_write_header(p:TObject;arg:pointer);
  142. procedure section_write_data(p:TObject;arg:pointer);
  143. protected
  144. function writedata(data:TObjData):boolean;override;
  145. public
  146. constructor createcoff(AWriter:TObjectWriter;awin32:boolean);
  147. destructor destroy;override;
  148. end;
  149. TDJCoffObjOutput = class(TCoffObjOutput)
  150. constructor create(AWriter:TObjectWriter);override;
  151. end;
  152. TPECoffObjOutput = class(TCoffObjOutput)
  153. constructor create(AWriter:TObjectWriter);override;
  154. end;
  155. TCoffObjInput = class(tObjInput)
  156. private
  157. FCoffsyms,
  158. FCoffStrs : tdynamicarray;
  159. { Convert symidx -> TObjSymbol }
  160. FSymTbl : ^TObjSymbolArray;
  161. { Convert secidx -> TObjSection }
  162. FSecCount : smallint;
  163. FSecTbl : ^TObjSectionArray;
  164. win32 : boolean;
  165. function GetSection(secidx:longint):TObjSection;
  166. function Read_str(strpos:longint):string;
  167. procedure read_relocs(s:TCoffObjSection);
  168. procedure read_symbols(objdata:TObjData);
  169. procedure ObjSections_read_data(p:TObject;arg:pointer);
  170. procedure ObjSections_read_relocs(p:TObject;arg:pointer);
  171. public
  172. constructor createcoff(awin32:boolean);
  173. destructor destroy;override;
  174. function ReadObjData(AReader:TObjectreader;objdata:TObjData):boolean;override;
  175. end;
  176. TDJCoffObjInput = class(TCoffObjInput)
  177. constructor create;override;
  178. end;
  179. TPECoffObjInput = class(TCoffObjInput)
  180. constructor create;override;
  181. end;
  182. TCoffExeSection = class(TExeSection)
  183. private
  184. win32 : boolean;
  185. public
  186. constructor createcoff(AList:TFPHashObjectList;const n:string;awin32:boolean);
  187. end;
  188. TDJCoffExeSection = class(TCoffExeSection)
  189. constructor create(AList:TFPHashObjectList;const n:string);override;
  190. end;
  191. TPECoffExeSection = class(TCoffExeSection)
  192. constructor create(AList:TFPHashObjectList;const n:string);override;
  193. end;
  194. TCoffexeoutput = class(texeoutput)
  195. private
  196. FCoffsyms,
  197. FCoffStrs : tdynamicarray;
  198. win32 : boolean;
  199. nsects : smallint;
  200. nsyms,
  201. sympos : aint;
  202. procedure ExeSections_pass2_header(p:TObject;arg:pointer);
  203. procedure write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  204. procedure globalsyms_write_symbol(p:TObject;arg:pointer);
  205. procedure ExeSections_write_header(p:TObject;arg:pointer);
  206. procedure ExeSections_write_data(p:TObject;arg:pointer);
  207. protected
  208. procedure CalcPos_Header;override;
  209. procedure CalcPos_Symbols;override;
  210. function writedata:boolean;override;
  211. public
  212. constructor createcoff(awin32:boolean);
  213. end;
  214. TDJCoffexeoutput = class(TCoffexeoutput)
  215. constructor create;override;
  216. end;
  217. TPECoffexeoutput = class(TCoffexeoutput)
  218. private
  219. idatalabnr : longint;
  220. procedure GenerateRelocs;
  221. public
  222. constructor create;override;
  223. procedure GenerateLibraryImports(ExternalLibraryList:TFPHashObjectList);override;
  224. procedure Order_End;override;
  225. procedure CalcPos_ExeSection(const aname:string);override;
  226. end;
  227. TObjSymbolrec = record
  228. sym : TObjSymbol;
  229. orgsize : aint;
  230. end;
  231. TObjSymbolArray = array[0..high(word)] of TObjSymbolrec;
  232. TObjSectionArray = array[0..high(smallint)] of TObjSection;
  233. TDJCoffAssembler = class(tinternalassembler)
  234. constructor create(smart:boolean);override;
  235. end;
  236. TPECoffassembler = class(tinternalassembler)
  237. constructor create(smart:boolean);override;
  238. end;
  239. type
  240. Treaddllproc = procedure(const dllname,funcname:string) of object;
  241. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  242. implementation
  243. uses
  244. {$ifdef win32}
  245. windows,
  246. {$endif win32}
  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. symbolresize = 200*sizeof(coffsymbol);
  420. strsresize = 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):string;
  848. var
  849. secname : string;
  850. begin
  851. secname:=coffsecnames[atype];
  852. if use_smartlink_section and
  853. (aname<>'') then
  854. result:=secname+'.'+aname
  855. else
  856. result:=secname;
  857. end;
  858. procedure TCoffObjData.CreateDebugSections;
  859. begin
  860. if target_dbg.id=dbg_stabs then
  861. begin
  862. stabssec:=createsection(sec_stab,'');
  863. stabstrsec:=createsection(sec_stabstr,'');
  864. end;
  865. end;
  866. procedure TCoffObjData.writereloc(data,len:aint;p:TObjSymbol;reloctype:TObjRelocationType);
  867. var
  868. curraddr,
  869. symaddr : aint;
  870. begin
  871. if CurrObjSec=nil then
  872. internalerror(200403072);
  873. if assigned(p) then
  874. begin
  875. { current address }
  876. curraddr:=CurrObjSec.mempos+CurrObjSec.Size;
  877. { external/common symbols don't have a fixed memory position yet }
  878. if (p.bind=AB_COMMON) then
  879. begin
  880. { For go32v2 we need to use the size as address }
  881. if not win32 then
  882. symaddr:=p.size
  883. else
  884. symaddr:=0;
  885. end
  886. else
  887. symaddr:=p.address;
  888. { no symbol relocation need inside a section }
  889. if (p.objsection=CurrObjSec) and
  890. (p.bind<>AB_COMMON) then
  891. begin
  892. case reloctype of
  893. RELOC_ABSOLUTE :
  894. begin
  895. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE);
  896. inc(data,symaddr);
  897. end;
  898. RELOC_RELATIVE :
  899. begin
  900. inc(data,symaddr-len-CurrObjSec.Size);
  901. end;
  902. RELOC_RVA :
  903. begin
  904. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_RVA);
  905. inc(data,symaddr);
  906. end;
  907. else
  908. internalerror(200604013);
  909. end;
  910. end
  911. else
  912. begin
  913. if (p.objsection<>nil) and
  914. (p.bind<>AB_COMMON) and
  915. (reloctype<>RELOC_RELATIVE) then
  916. CurrObjSec.addsectionreloc(curraddr,p.objsection,reloctype)
  917. else
  918. CurrObjSec.addsymreloc(curraddr,p,reloctype);
  919. if (not win32) or
  920. ((reloctype<>RELOC_RELATIVE) and (p.objsection<>nil)) then
  921. inc(data,symaddr);
  922. if reloctype=RELOC_RELATIVE then
  923. begin
  924. if win32 then
  925. dec(data,len-4)
  926. else
  927. dec(data,len+CurrObjSec.Size);
  928. end;
  929. end;
  930. end
  931. else
  932. begin
  933. if reloctype=RELOC_RVA then
  934. internalerror(200603033);
  935. end;
  936. CurrObjSec.write(data,len);
  937. end;
  938. procedure TCoffObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  939. const
  940. N_SourceFile = $64;
  941. N_IncludeFile = $84;
  942. var
  943. stab : TObjStabEntry;
  944. stabstrlen : longint;
  945. curraddr : aint;
  946. {$ifdef optimizestabs}
  947. hs : string;
  948. {$endif optimizestabs}
  949. begin
  950. if not assigned(StabsSec) then
  951. internalerror(200602256);
  952. { Win32 does not need an offset if a symbol relocation is used }
  953. if win32 and
  954. assigned(ps) and
  955. (ps.bind<>AB_LOCAL) then
  956. offset:=0;
  957. if assigned(p) and (p[0]<>#0) then
  958. begin
  959. stabstrlen:=strlen(p);
  960. {$ifdef optimizestabs}
  961. StabStrEntry:=nil;
  962. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  963. begin
  964. hs:=strpas(p);
  965. StabstrEntry:=StabStrDict.Search(hs);
  966. if not assigned(StabstrEntry) then
  967. begin
  968. StabstrEntry:=TStabStrEntry.Create(hs);
  969. StabstrEntry:=StabStrSec.Size;
  970. StabStrDict.Insert(StabstrEntry);
  971. { generate new stab }
  972. StabstrEntry:=nil;
  973. end;
  974. end;
  975. if assigned(StabstrEntry) then
  976. stab.strpos:=StabstrEntry.strpos
  977. else
  978. {$endif optimizestabs}
  979. begin
  980. stab.strpos:=StabStrSec.Size;
  981. StabStrSec.write(p^,stabstrlen+1);
  982. end;
  983. end
  984. else
  985. stab.strpos:=0;
  986. stab.ntype:=nidx;
  987. stab.ndesc:=ndesc;
  988. stab.nother:=nother;
  989. stab.nvalue:=offset;
  990. StabsSec.write(stab,sizeof(stab));
  991. if assigned(ps) then
  992. begin
  993. { current address }
  994. curraddr:=StabsSec.mempos+StabsSec.Size;
  995. if DLLSource and RelocSection then
  996. { avoid relocation in the .stab section
  997. because it ends up in the .reloc section instead }
  998. StabsSec.addsymreloc(curraddr-4,ps,RELOC_RVA)
  999. else
  1000. StabsSec.addsymreloc(curraddr-4,ps,RELOC_ABSOLUTE);
  1001. end;
  1002. end;
  1003. procedure TCoffObjData.afteralloc;
  1004. var
  1005. mempos : aint;
  1006. i : longint;
  1007. begin
  1008. inherited afteralloc;
  1009. { DJ Coff requires mempositions }
  1010. if not win32 then
  1011. begin
  1012. mempos:=0;
  1013. for i:=0 to ObjSectionList.Count-1 do
  1014. TObjSection(ObjSectionList[i]).setmempos(mempos);
  1015. end;
  1016. end;
  1017. {****************************************************************************
  1018. TDJCoffObjData
  1019. ****************************************************************************}
  1020. constructor TDJCoffObjData.create(const n:string);
  1021. begin
  1022. inherited createcoff(n,false,TDJCoffObjSection);
  1023. end;
  1024. {****************************************************************************
  1025. TPECoffObjData
  1026. ****************************************************************************}
  1027. constructor TPECoffObjData.create(const n:string);
  1028. begin
  1029. inherited createcoff(n,true,TPECoffObjSection);
  1030. end;
  1031. {****************************************************************************
  1032. TCoffObjOutput
  1033. ****************************************************************************}
  1034. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1035. begin
  1036. inherited create(AWriter);
  1037. win32:=awin32;
  1038. end;
  1039. destructor TCoffObjOutput.destroy;
  1040. begin
  1041. if assigned(FCoffSyms) then
  1042. FCoffSyms.free;
  1043. if assigned(FCoffStrs) then
  1044. FCoffStrs.free;
  1045. inherited destroy;
  1046. end;
  1047. procedure TCoffObjOutput.write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  1048. var
  1049. sym : coffsymbol;
  1050. begin
  1051. FillChar(sym,sizeof(sym),0);
  1052. { symbolname }
  1053. if length(name)>8 then
  1054. begin
  1055. sym.strpos:=FCoffStrs.size+4;
  1056. FCoffStrs.writestr(name);
  1057. FCoffStrs.writestr(#0);
  1058. end
  1059. else
  1060. move(name[1],sym.name,length(name));
  1061. sym.value:=value;
  1062. sym.section:=section;
  1063. sym.typ:=typ;
  1064. sym.aux:=aux;
  1065. inc(symidx);
  1066. FCoffSyms.write(sym,sizeof(sym));
  1067. end;
  1068. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1069. var
  1070. secrec : coffsectionrec;
  1071. begin
  1072. with TCoffObjSection(p) do
  1073. begin
  1074. secidx:=symidx div 2;
  1075. secsymidx:=symidx;
  1076. write_symbol(name,mempos,secidx,COFF_SYM_SECTION,1);
  1077. { AUX }
  1078. fillchar(secrec,sizeof(secrec),0);
  1079. secrec.len:=Size;
  1080. secrec.nrelocs:=ObjRelocations.count;
  1081. inc(symidx);
  1082. FCoffSyms.write(secrec,sizeof(secrec));
  1083. end;
  1084. end;
  1085. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1086. var
  1087. i : longint;
  1088. rel : coffreloc;
  1089. objreloc : TObjRelocation;
  1090. begin
  1091. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1092. begin
  1093. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1094. rel.address:=objreloc.dataoffset;
  1095. if assigned(objreloc.symbol) then
  1096. begin
  1097. if (objreloc.symbol.bind=AB_LOCAL) then
  1098. rel.sym:=objreloc.symbol.objsection.secsymidx
  1099. else
  1100. begin
  1101. if objreloc.symbol.symidx=-1 then
  1102. internalerror(200602233);
  1103. rel.sym:=objreloc.symbol.symidx;
  1104. end;
  1105. end
  1106. else
  1107. begin
  1108. if objreloc.objsection<>nil then
  1109. rel.sym:=objreloc.objsection.secsymidx
  1110. else
  1111. rel.sym:=0;
  1112. end;
  1113. case objreloc.typ of
  1114. {$ifdef arm}
  1115. RELOC_ABSOLUTE :
  1116. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1117. { I've no idea if this is correct (FK):
  1118. RELOC_RELATIVE :
  1119. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1120. }
  1121. RELOC_RVA :
  1122. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1123. {$endif arm}
  1124. {$ifdef i386}
  1125. RELOC_RELATIVE :
  1126. rel.reloctype:=R_PCRLONG;
  1127. RELOC_ABSOLUTE :
  1128. rel.reloctype:=R_DIR32;
  1129. RELOC_RVA :
  1130. rel.reloctype:=R_IMAGEBASE;
  1131. {$endif i386}
  1132. {$ifdef x86_64}
  1133. RELOC_RELATIVE :
  1134. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1135. RELOC_ABSOLUTE32 :
  1136. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1137. RELOC_ABSOLUTE :
  1138. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1139. RELOC_RVA :
  1140. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1141. RELOC_RELATIVE_1 :
  1142. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1143. RELOC_RELATIVE_2 :
  1144. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1145. RELOC_RELATIVE_3 :
  1146. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1147. RELOC_RELATIVE_4 :
  1148. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1149. RELOC_RELATIVE_5 :
  1150. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1151. {$endif x86_64}
  1152. else
  1153. internalerror(200603312);
  1154. end;
  1155. FWriter.write(rel,sizeof(rel));
  1156. end;
  1157. end;
  1158. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1159. var
  1160. filename : string[18];
  1161. sectionval : word;
  1162. globalval : byte;
  1163. i : longint;
  1164. value : aint;
  1165. objsym : TObjSymbol;
  1166. begin
  1167. with TCoffObjData(data) do
  1168. begin
  1169. symidx:=0;
  1170. { The `.file' record, and the file name auxiliary record }
  1171. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1172. fillchar(filename,sizeof(filename),0);
  1173. filename:=SplitFileName(current_module.mainsource^);
  1174. inc(symidx);
  1175. FCoffSyms.write(filename[1],sizeof(filename)-1);
  1176. { Sections }
  1177. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  1178. { ObjSymbols }
  1179. for i:=0 to ObjSymbolList.Count-1 do
  1180. begin
  1181. objsym:=TObjSymbol(ObjSymbolList[i]);
  1182. if (objsym.typ=AT_LABEL) and (objsym.bind=AB_LOCAL) then
  1183. continue;
  1184. case objsym.bind of
  1185. AB_GLOBAL :
  1186. begin
  1187. globalval:=2;
  1188. sectionval:=TCoffObjSection(objsym.objsection).secidx;
  1189. value:=objsym.address;
  1190. end;
  1191. AB_LOCAL :
  1192. begin
  1193. globalval:=3;
  1194. sectionval:=TCoffObjSection(objsym.objsection).secidx;
  1195. value:=objsym.address;
  1196. end;
  1197. else
  1198. begin
  1199. globalval:=2;
  1200. sectionval:=0;
  1201. value:=objsym.size;
  1202. end;
  1203. end;
  1204. { symbolname }
  1205. objsym.symidx:=symidx;
  1206. write_symbol(objsym.name,value,sectionval,globalval,0);
  1207. end;
  1208. end;
  1209. end;
  1210. procedure TCoffObjOutput.section_set_datapos(p:TObject;arg:pointer);
  1211. begin
  1212. TObjSection(p).setdatapos(paint(arg)^);
  1213. end;
  1214. procedure TCoffObjOutput.section_set_reloc_datapos(p:TObject;arg:pointer);
  1215. begin
  1216. TCoffObjSection(p).coffrelocpos:=paint(arg)^;
  1217. inc(paint(arg)^,sizeof(coffreloc)*TObjSection(p).ObjRelocations.count);
  1218. end;
  1219. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1220. var
  1221. sechdr : tcoffsechdr;
  1222. s : string;
  1223. strpos : Aint;
  1224. begin
  1225. with TCoffObjSection(p) do
  1226. begin
  1227. fillchar(sechdr,sizeof(sechdr),0);
  1228. s:=name;
  1229. if length(s)>8 then
  1230. begin
  1231. strpos:=FCoffStrs.size+4;
  1232. FCoffStrs.writestr(s);
  1233. FCoffStrs.writestr(#0);
  1234. s:='/'+ToStr(strpos);
  1235. end;
  1236. move(s[1],sechdr.name,length(s));
  1237. if not win32 then
  1238. begin
  1239. sechdr.rvaofs:=mempos;
  1240. sechdr.vsize:=mempos;
  1241. end
  1242. else
  1243. begin
  1244. if not(oso_data in secoptions) then
  1245. sechdr.vsize:=Size;
  1246. end;
  1247. sechdr.DataSize:=size;
  1248. if (Size>0) and
  1249. (oso_data in secoptions) then
  1250. sechdr.datapos:=datapos;
  1251. sechdr.nrelocs:=ObjRelocations.count;
  1252. sechdr.relocpos:=coffrelocpos;
  1253. if win32 then
  1254. sechdr.flags:=peencodesechdrflags(secoptions,secalign)
  1255. else
  1256. sechdr.flags:=djencodesechdrflags(secoptions);
  1257. FWriter.write(sechdr,sizeof(sechdr));
  1258. end;
  1259. end;
  1260. procedure TCoffObjOutput.section_write_data(p:TObject;arg:pointer);
  1261. begin
  1262. with TObjSection(p) do
  1263. begin
  1264. if assigned(data) then
  1265. begin
  1266. FWriter.writezeros(dataalignbytes);
  1267. if Datapos<>FWriter.ObjSize then
  1268. internalerror(200603052);
  1269. FWriter.writearray(data);
  1270. end;
  1271. end;
  1272. end;
  1273. function TCoffObjOutput.writedata(data:TObjData):boolean;
  1274. var
  1275. orgdatapos,
  1276. datapos,
  1277. sympos : aint;
  1278. i : longint;
  1279. gotreloc : boolean;
  1280. header : tcoffheader;
  1281. begin
  1282. result:=false;
  1283. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1284. FCoffStrs:=TDynamicArray.Create(strsresize);
  1285. with TCoffObjData(data) do
  1286. begin
  1287. { Create Symbol Table }
  1288. create_symbols(data);
  1289. { Calculate the filepositions }
  1290. datapos:=sizeof(tcoffheader)+sizeof(tcoffsechdr)*ObjSectionList.Count;
  1291. { Sections first }
  1292. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  1293. { relocs }
  1294. orgdatapos:=datapos;
  1295. ObjSectionList.ForEachCall(@section_set_reloc_datapos,@datapos);
  1296. gotreloc:=(orgdatapos<>datapos);
  1297. { Symbols }
  1298. sympos:=datapos;
  1299. { Generate COFF header }
  1300. fillchar(header,sizeof(tcoffheader),0);
  1301. header.mach:=COFF_MAGIC;
  1302. header.nsects:=ObjSectionList.Count;
  1303. header.sympos:=sympos;
  1304. header.syms:=symidx;
  1305. if win32 then
  1306. begin
  1307. {$ifdef arm}
  1308. header.flag:=PE_FILE_32BIT_MACHINE or
  1309. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1310. {$else arm}
  1311. header.flag:=PE_FILE_BYTES_REVERSED_LO or PE_FILE_32BIT_MACHINE or
  1312. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1313. {$endif arm}
  1314. if not gotreloc then
  1315. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  1316. end
  1317. else
  1318. begin
  1319. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  1320. if not gotreloc then
  1321. header.flag:=header.flag or COFF_FLAG_NORELOCS;
  1322. end;
  1323. FWriter.write(header,sizeof(header));
  1324. { Section headers }
  1325. ObjSectionList.ForEachCall(@section_write_header,nil);
  1326. { ObjSections }
  1327. ObjSectionList.ForEachCall(@section_write_data,nil);
  1328. { Relocs }
  1329. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  1330. { ObjSymbols }
  1331. if Sympos<>FWriter.ObjSize then
  1332. internalerror(200603051);
  1333. FWriter.writearray(FCoffSyms);
  1334. { Strings }
  1335. i:=FCoffStrs.size+4;
  1336. FWriter.write(i,4);
  1337. FWriter.writearray(FCoffStrs);
  1338. end;
  1339. FCoffStrs.Free;
  1340. FCoffStrs:=nil;
  1341. FCoffSyms.Free;
  1342. FCoffSyms:=nil;
  1343. end;
  1344. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  1345. begin
  1346. inherited createcoff(AWriter,false);
  1347. cobjdata:=TDJCoffObjData;
  1348. end;
  1349. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  1350. begin
  1351. inherited createcoff(AWriter,true);
  1352. cobjdata:=TPECoffObjData;
  1353. end;
  1354. {****************************************************************************
  1355. TCoffObjInput
  1356. ****************************************************************************}
  1357. constructor TCoffObjInput.createcoff(awin32:boolean);
  1358. begin
  1359. inherited create;
  1360. win32:=awin32;
  1361. FSymTbl:=nil;
  1362. end;
  1363. destructor TCoffObjInput.destroy;
  1364. begin
  1365. if assigned(FCoffSyms) then
  1366. FCoffSyms.free;
  1367. if assigned(FCoffStrs) then
  1368. FCoffStrs.free;
  1369. if assigned(FSymTbl) then
  1370. freemem(FSymTbl);
  1371. if assigned(FSecTbl) then
  1372. freemem(FSecTbl);
  1373. inherited destroy;
  1374. end;
  1375. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  1376. begin
  1377. result:=nil;
  1378. if (secidx<1) or (secidx>FSecCount) then
  1379. begin
  1380. InputError('Failed reading coff file, invalid section index');
  1381. exit;
  1382. end;
  1383. result:=FSecTbl^[secidx];
  1384. end;
  1385. function TCoffObjInput.Read_str(strpos:longint):string;
  1386. begin
  1387. FCoffStrs.Seek(strpos-4);
  1388. FCoffStrs.Read(result[1],255);
  1389. result[255]:=#0;
  1390. result[0]:=chr(strlen(@result[1]));
  1391. if result='' then
  1392. Internalerror(200205172);
  1393. end;
  1394. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  1395. var
  1396. rel : coffreloc;
  1397. rel_type : TObjRelocationType;
  1398. i : longint;
  1399. p : TObjSymbol;
  1400. extradist : longint;
  1401. begin
  1402. for i:=1 to s.coffrelocs do
  1403. begin
  1404. FReader.read(rel,sizeof(rel));
  1405. case rel.reloctype of
  1406. {$ifdef arm}
  1407. IMAGE_REL_ARM_ABSOLUTE:
  1408. rel_type:=RELOC_NONE;
  1409. IMAGE_REL_ARM_ADDR32:
  1410. rel_type:=RELOC_ABSOLUTE;
  1411. IMAGE_REL_ARM_ADDR32NB:
  1412. rel_type:=RELOC_RVA;
  1413. IMAGE_REL_ARM_BRANCH24:
  1414. rel_type:=RELOC_RELATIVE_24;
  1415. {$endif arm}
  1416. {$ifdef i386}
  1417. R_PCRLONG :
  1418. rel_type:=RELOC_RELATIVE;
  1419. R_DIR32 :
  1420. rel_type:=RELOC_ABSOLUTE;
  1421. R_IMAGEBASE :
  1422. rel_type:=RELOC_RVA;
  1423. {$endif i386}
  1424. {$ifdef x86_64}
  1425. IMAGE_REL_AMD64_REL32:
  1426. rel_type:=RELOC_RELATIVE;
  1427. IMAGE_REL_AMD64_ADDR32:
  1428. rel_type:=RELOC_ABSOLUTE32;
  1429. IMAGE_REL_AMD64_ADDR64:
  1430. rel_type:=RELOC_ABSOLUTE;
  1431. IMAGE_REL_AMD64_ADDR32NB:
  1432. rel_type:=RELOC_RVA;
  1433. IMAGE_REL_AMD64_REL32_1:
  1434. rel_type:=RELOC_RELATIVE_1;
  1435. IMAGE_REL_AMD64_REL32_2:
  1436. rel_type:=RELOC_RELATIVE_2;
  1437. IMAGE_REL_AMD64_REL32_3:
  1438. rel_type:=RELOC_RELATIVE_3;
  1439. IMAGE_REL_AMD64_REL32_4:
  1440. rel_type:=RELOC_RELATIVE_4;
  1441. IMAGE_REL_AMD64_REL32_5:
  1442. rel_type:=RELOC_RELATIVE_5;
  1443. {$endif x86_64}
  1444. else
  1445. begin
  1446. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  1447. exit;
  1448. end;
  1449. end;
  1450. p:=FSymTbl^[rel.sym].sym;
  1451. if assigned(p) then
  1452. s.addsymsizereloc(rel.address-s.mempos,p,FSymTbl^[rel.sym].orgsize,rel_type)
  1453. else
  1454. begin
  1455. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  1456. exit;
  1457. end;
  1458. end;
  1459. end;
  1460. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  1461. var
  1462. size,
  1463. address,
  1464. nsyms,
  1465. symidx : aint;
  1466. i : longint;
  1467. sym : coffsymbol;
  1468. objsym : TObjSymbol;
  1469. bind : Tasmsymbind;
  1470. strname : string;
  1471. auxrec : array[0..17] of byte;
  1472. objsec : TObjSection;
  1473. begin
  1474. with TCoffObjData(objdata) do
  1475. begin
  1476. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1477. { Allocate memory for symidx -> TObjSymbol table }
  1478. GetMem(FSymTbl,nsyms*sizeof(TObjSymbolrec));
  1479. FillChar(FSymTbl^,nsyms*sizeof(TObjSymbolrec),0);
  1480. { Load the Symbols }
  1481. FCoffSyms.Seek(0);
  1482. symidx:=0;
  1483. while (symidx<nsyms) do
  1484. begin
  1485. FCoffSyms.Read(sym,sizeof(sym));
  1486. if plongint(@sym.name)^<>0 then
  1487. begin
  1488. move(sym.name,strname[1],8);
  1489. strname[9]:=#0;
  1490. strname[0]:=chr(strlen(@strname[1]));
  1491. if strname='' then
  1492. Internalerror(200205171);
  1493. end
  1494. else
  1495. strname:=Read_str(sym.strpos);
  1496. bind:=AB_EXTERNAL;
  1497. size:=0;
  1498. address:=0;
  1499. objsym:=nil;
  1500. objsec:=nil;
  1501. case sym.typ of
  1502. COFF_SYM_GLOBAL :
  1503. begin
  1504. if sym.section=0 then
  1505. begin
  1506. if sym.value=0 then
  1507. bind:=AB_EXTERNAL
  1508. else
  1509. begin
  1510. bind:=AB_COMMON;
  1511. size:=sym.value;
  1512. end;
  1513. end
  1514. else
  1515. begin
  1516. bind:=AB_GLOBAL;
  1517. objsec:=GetSection(sym.section);
  1518. if sym.value>=objsec.mempos then
  1519. address:=sym.value-objsec.mempos;
  1520. end;
  1521. objsym:=CreateSymbol(strname);
  1522. objsym.bind:=bind;
  1523. objsym.typ:=AT_FUNCTION;
  1524. objsym.objsection:=objsec;
  1525. objsym.offset:=address;
  1526. objsym.size:=size;
  1527. { Register in ObjSection }
  1528. if assigned(objsec) then
  1529. objsec.AddSymbolDefine(objsym);
  1530. end;
  1531. COFF_SYM_LABEL,
  1532. COFF_SYM_LOCAL :
  1533. begin
  1534. { do not add constants (section=-1) }
  1535. if sym.section<>-1 then
  1536. begin
  1537. bind:=AB_LOCAL;
  1538. objsec:=GetSection(sym.section);
  1539. if sym.value>=objsec.mempos then
  1540. address:=sym.value-objsec.mempos;
  1541. objsym:=CreateSymbol(strname);
  1542. objsym.bind:=bind;
  1543. objsym.typ:=AT_FUNCTION;
  1544. objsym.objsection:=objsec;
  1545. objsym.offset:=address;
  1546. objsym.size:=size;
  1547. end;
  1548. end;
  1549. COFF_SYM_SECTION :
  1550. begin
  1551. if sym.section=0 then
  1552. InputError('Failed reading coff file, illegal section');
  1553. objsec:=GetSection(sym.section);
  1554. if assigned(objsec) then
  1555. begin
  1556. if sym.value>=objsec.mempos then
  1557. address:=sym.value-objsec.mempos;
  1558. objsym:=CreateSymbol(strname);
  1559. objsym.bind:=AB_LOCAL;
  1560. objsym.typ:=AT_FUNCTION;
  1561. objsym.objsection:=objsec;
  1562. objsym.offset:=address;
  1563. objsym.size:=size;
  1564. end;
  1565. end;
  1566. COFF_SYM_FUNCTION,
  1567. COFF_SYM_FILE :
  1568. ;
  1569. else
  1570. internalerror(200602232);
  1571. end;
  1572. FSymTbl^[symidx].sym:=objsym;
  1573. FSymTbl^[symidx].orgsize:=size;
  1574. { read aux records }
  1575. for i:=1 to sym.aux do
  1576. begin
  1577. FCoffSyms.Read(auxrec,sizeof(auxrec));
  1578. inc(symidx);
  1579. end;
  1580. inc(symidx);
  1581. end;
  1582. end;
  1583. end;
  1584. procedure TCoffObjInput.ObjSections_read_data(p:TObject;arg:pointer);
  1585. begin
  1586. with TCoffObjSection(p) do
  1587. begin
  1588. { Skip debug sections }
  1589. if (cs_link_strip in aktglobalswitches) and
  1590. (oso_debug in secoptions) then
  1591. exit;
  1592. if assigned(data) then
  1593. begin
  1594. FReader.Seek(datapos);
  1595. if not FReader.ReadArray(data,Size) then
  1596. begin
  1597. Comment(V_Error,'Error reading coff file, can''t read object data');
  1598. exit;
  1599. end;
  1600. end;
  1601. end;
  1602. end;
  1603. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  1604. begin
  1605. with TCoffObjSection(p) do
  1606. begin
  1607. { Skip debug sections }
  1608. if (cs_link_strip in aktglobalswitches) and
  1609. (oso_debug in secoptions) then
  1610. exit;
  1611. if coffrelocs>0 then
  1612. begin
  1613. FReader.Seek(coffrelocpos);
  1614. read_relocs(TCoffObjSection(p));
  1615. end;
  1616. end;
  1617. end;
  1618. function TCoffObjInput.ReadObjData(AReader:TObjectreader;objdata:TObjData):boolean;
  1619. var
  1620. secalign : shortint;
  1621. strsize,
  1622. strpos,
  1623. i : longint;
  1624. code : longint;
  1625. objsec : TCoffObjSection;
  1626. secoptions : TObjSectionOptions;
  1627. header : tcoffheader;
  1628. sechdr : tcoffsechdr;
  1629. secname : string;
  1630. secnamebuf : array[0..15] of char;
  1631. begin
  1632. FReader:=AReader;
  1633. InputFileName:=AReader.FileName;
  1634. result:=false;
  1635. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1636. FCoffStrs:=TDynamicArray.Create(strsresize);
  1637. with TCoffObjData(objdata) do
  1638. begin
  1639. { Read COFF header }
  1640. if not AReader.read(header,sizeof(tcoffheader)) then
  1641. begin
  1642. InputError('Can''t read COFF Header');
  1643. exit;
  1644. end;
  1645. if header.mach<>COFF_MAGIC then
  1646. begin
  1647. InputError('Illegal COFF Magic');
  1648. exit;
  1649. end;
  1650. { Strings }
  1651. AReader.Seek(header.sympos+header.syms*sizeof(CoffSymbol));
  1652. if not AReader.Read(strsize,4) then
  1653. begin
  1654. InputError('Error reading COFF Symtable');
  1655. exit;
  1656. end;
  1657. if strsize<4 then
  1658. begin
  1659. InputError('Error reading COFF Symtable');
  1660. exit;
  1661. end;
  1662. if not AReader.ReadArray(FCoffStrs,Strsize-4) then
  1663. begin
  1664. InputError('Error reading COFF Symtable');
  1665. exit;
  1666. end;
  1667. { Section headers }
  1668. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  1669. FSecCount:=header.nsects;
  1670. GetMem(FSecTbl,(header.nsects+1)*sizeof(TObjSection));
  1671. FillChar(FSecTbl^,(header.nsects+1)*sizeof(TObjSection),0);
  1672. AReader.Seek(sizeof(tcoffheader)+header.opthdr);
  1673. for i:=1 to header.nsects do
  1674. begin
  1675. if not AReader.read(sechdr,sizeof(sechdr)) then
  1676. begin
  1677. InputError('Error reading COFF Section Headers');
  1678. exit;
  1679. end;
  1680. move(sechdr.name,secnamebuf,8);
  1681. secnamebuf[8]:=#0;
  1682. secname:=strpas(secnamebuf);
  1683. if secname[1]='/' then
  1684. begin
  1685. Val(Copy(secname,2,8),strpos,code);
  1686. if code=0 then
  1687. secname:=Read_str(strpos)
  1688. else
  1689. begin
  1690. InputError('Error reading COFF Section Headers');
  1691. secname:='error';
  1692. end;
  1693. end;
  1694. if win32 then
  1695. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  1696. else
  1697. begin
  1698. djdecodesechdrflags(secname,sechdr.flags);
  1699. secalign:=sizeof(aint);
  1700. end;
  1701. {$warning TODO idata keep can maybe replaced with grouping of text and idata}
  1702. if (Copy(secname,1,6)='.idata') or
  1703. (Copy(secname,1,6)='.edata') or
  1704. (Copy(secname,1,5)='.rsrc') or
  1705. (Copy(secname,1,6)='.pdata') then
  1706. include(secoptions,oso_keep);
  1707. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions));
  1708. FSecTbl^[i]:=objsec;
  1709. if not win32 then
  1710. objsec.mempos:=sechdr.rvaofs;
  1711. objsec.orgmempos:=sechdr.rvaofs;
  1712. objsec.coffrelocs:=sechdr.nrelocs;
  1713. objsec.coffrelocpos:=sechdr.relocpos;
  1714. objsec.datapos:=sechdr.datapos;
  1715. objsec.Size:=sechdr.dataSize;
  1716. end;
  1717. { ObjSymbols }
  1718. AReader.Seek(header.sympos);
  1719. if not AReader.ReadArray(FCoffSyms,header.syms*sizeof(CoffSymbol)) then
  1720. begin
  1721. Comment(V_Error,'Error reading coff file');
  1722. exit;
  1723. end;
  1724. { Insert all ObjSymbols }
  1725. read_symbols(objdata);
  1726. { Section Data }
  1727. ObjSectionList.ForEachCall(@objsections_read_data,nil);
  1728. { Relocs }
  1729. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  1730. end;
  1731. FCoffStrs.Free;
  1732. FCoffStrs:=nil;
  1733. FCoffSyms.Free;
  1734. FCoffSyms:=nil;
  1735. result:=true;
  1736. end;
  1737. constructor TDJCoffObjInput.create;
  1738. begin
  1739. inherited createcoff(false);
  1740. cobjdata:=TDJCoffObjData;
  1741. end;
  1742. constructor TPECoffObjInput.create;
  1743. begin
  1744. inherited createcoff(true);
  1745. cobjdata:=TPECoffObjData;
  1746. end;
  1747. {****************************************************************************
  1748. TCoffexesection
  1749. ****************************************************************************}
  1750. constructor TCoffExeSection.createcoff(AList:TFPHashObjectList;const n:string;awin32:boolean);
  1751. begin
  1752. inherited create(AList,n);
  1753. win32:=awin32;
  1754. end;
  1755. constructor TDJCoffExeSection.create(AList:TFPHashObjectList;const n:string);
  1756. begin
  1757. inherited createcoff(AList,n,false);
  1758. end;
  1759. constructor TPECoffExeSection.create(AList:TFPHashObjectList;const n:string);
  1760. begin
  1761. inherited createcoff(AList,n,false);
  1762. end;
  1763. {****************************************************************************
  1764. TCoffexeoutput
  1765. ****************************************************************************}
  1766. constructor TCoffexeoutput.createcoff(awin32:boolean);
  1767. begin
  1768. inherited create;
  1769. win32:=awin32;
  1770. end;
  1771. procedure TCoffexeoutput.write_symbol(const name:string;value:aint;section:smallint;typ,aux:byte);
  1772. var
  1773. sym : coffsymbol;
  1774. begin
  1775. FillChar(sym,sizeof(sym),0);
  1776. if length(name)>8 then
  1777. begin
  1778. sym.strpos:=FCoffStrs.size+4;
  1779. FCoffStrs.writestr(name);
  1780. FCoffStrs.writestr(#0);
  1781. end
  1782. else
  1783. move(name[1],sym.name,length(name));
  1784. sym.value:=value;
  1785. sym.section:=section;
  1786. sym.typ:=typ;
  1787. sym.aux:=aux;
  1788. FWriter.write(sym,sizeof(sym));
  1789. end;
  1790. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  1791. var
  1792. secval,
  1793. value : aint;
  1794. globalval : byte;
  1795. exesec : TExeSection;
  1796. begin
  1797. if not assigned(texesymbol(p).objsymbol) then
  1798. internalerror(200603053);
  1799. with texesymbol(p).objsymbol do
  1800. begin
  1801. exesec:=TExeSection(objsection.exesection);
  1802. { There is no exesection defined for special internal symbols
  1803. like __image_base__ }
  1804. if assigned(exesec) then
  1805. begin
  1806. secval:=exesec.secsymidx;
  1807. value:=address-exesec.mempos;
  1808. end
  1809. else
  1810. begin
  1811. secval:=-1;
  1812. value:=address;
  1813. end;
  1814. if bind=AB_LOCAL then
  1815. globalval:=3
  1816. else
  1817. globalval:=2;
  1818. { reloctype address to the section in the executable }
  1819. write_symbol(name,value,secval,globalval,0);
  1820. end;
  1821. end;
  1822. procedure TCoffexeoutput.ExeSections_write_header(p:TObject;arg:pointer);
  1823. var
  1824. sechdr : tcoffsechdr;
  1825. begin
  1826. with tExeSection(p) do
  1827. begin
  1828. fillchar(sechdr,sizeof(sechdr),0);
  1829. move(name[1],sechdr.name,length(name));
  1830. sechdr.rvaofs:=mempos;
  1831. sechdr.vsize:=mempos;
  1832. if win32 then
  1833. begin
  1834. sechdr.rvaofs:=mempos;
  1835. sechdr.vsize:=align(Size,$100);
  1836. end
  1837. else
  1838. begin
  1839. sechdr.rvaofs:=mempos;
  1840. sechdr.vsize:=mempos;
  1841. end;
  1842. { sechdr.dataSize is size of initilized data. For .bss section it must be zero }
  1843. if Name <> '.bss' then
  1844. if oso_data in SecOptions then
  1845. begin
  1846. sechdr.dataSize:=Size;
  1847. sechdr.datapos:=datapos;
  1848. end
  1849. else
  1850. sechdr.dataSize:=Size;
  1851. sechdr.nrelocs:=0;
  1852. sechdr.relocpos:=0;
  1853. if win32 then
  1854. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign)
  1855. else
  1856. sechdr.flags:=djencodesechdrflags(SecOptions);
  1857. FWriter.write(sechdr,sizeof(sechdr));
  1858. end;
  1859. end;
  1860. procedure TCoffexeoutput.ExeSections_pass2_header(p:TObject;arg:pointer);
  1861. begin
  1862. with TExeSection(p) do
  1863. begin
  1864. inc(plongint(arg)^);
  1865. secsymidx:=plongint(arg)^;
  1866. end;
  1867. end;
  1868. procedure Tcoffexeoutput.ExeSections_write_Data(p:TObject;arg:pointer);
  1869. var
  1870. objsec : TObjSection;
  1871. i : longint;
  1872. begin
  1873. with texesection(p) do
  1874. begin
  1875. if oso_data in secoptions then
  1876. begin
  1877. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  1878. for i:=0 to ObjSectionList.Count-1 do
  1879. begin
  1880. objsec:=TObjSection(ObjSectionList[i]);
  1881. if oso_data in objsec.secoptions then
  1882. begin
  1883. if not assigned(objsec.data) then
  1884. internalerror(200603042);
  1885. FWriter.writezeros(objsec.dataalignbytes);
  1886. if objsec.DataPos<>FWriter.Size then
  1887. internalerror(200602251);
  1888. FWriter.writearray(objsec.data);
  1889. end;
  1890. end;
  1891. end;
  1892. end;
  1893. end;
  1894. procedure tcoffexeoutput.CalcPos_Header;
  1895. var
  1896. stubsize,
  1897. optheadersize : longint;
  1898. begin
  1899. if win32 then
  1900. begin
  1901. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  1902. optheadersize:=sizeof(tcoffpeoptheader);
  1903. end
  1904. else
  1905. begin
  1906. stubsize:=sizeof(go32v2stub);
  1907. optheadersize:=sizeof(coffdjoptheader);
  1908. end;
  1909. { retrieve amount of ObjSections }
  1910. nsects:=0;
  1911. ExeSections.ForEachCall(@ExeSections_pass2_header,@nsects);
  1912. { calculate start positions after the headers }
  1913. currdatapos:=stubsize+optheadersize+sizeof(tcoffsechdr)*nsects;
  1914. currmempos:=stubsize+optheadersize+sizeof(tcoffsechdr)*nsects;
  1915. end;
  1916. procedure tcoffexeoutput.CalcPos_Symbols;
  1917. begin
  1918. nsyms:=0;
  1919. sympos:=0;
  1920. if not(cs_link_strip in aktglobalswitches) then
  1921. begin
  1922. nsyms:=ExeSymbolList.Count;
  1923. sympos:=CurrDataPos;
  1924. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  1925. end;
  1926. end;
  1927. function TCoffexeoutput.writedata:boolean;
  1928. var
  1929. i : longint;
  1930. header : tcoffheader;
  1931. djoptheader : coffdjoptheader;
  1932. peoptheader : tcoffpeoptheader;
  1933. textExeSec,
  1934. dataExeSec,
  1935. bssExeSec : TExeSection;
  1936. procedure UpdateDataDir(const secname:string;idx:longint);
  1937. var
  1938. exesec : TExeSection;
  1939. begin
  1940. exesec:=FindExeSection(secname);
  1941. if assigned(exesec) then
  1942. begin
  1943. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  1944. peoptheader.DataDirectory[idx].size:=exesec.Size;
  1945. end;
  1946. end;
  1947. begin
  1948. result:=false;
  1949. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1950. FCoffStrs:=TDynamicArray.Create(strsresize);
  1951. textExeSec:=FindExeSection('.text');
  1952. dataExeSec:=FindExeSection('.data');
  1953. bssExeSec:=FindExeSection('.bss');
  1954. if not assigned(TextExeSec) or
  1955. not assigned(DataExeSec) or
  1956. not assigned(BSSExeSec) then
  1957. internalerror(200602231);
  1958. { Stub }
  1959. if win32 then
  1960. begin
  1961. FWriter.write(win32stub,sizeof(win32stub));
  1962. FWriter.write(pemagic,sizeof(pemagic));
  1963. end
  1964. else
  1965. FWriter.write(go32v2stub,sizeof(go32v2stub));
  1966. { COFF header }
  1967. fillchar(header,sizeof(header),0);
  1968. header.mach:=COFF_MAGIC;
  1969. header.nsects:=nsects;
  1970. header.sympos:=sympos;
  1971. header.syms:=nsyms;
  1972. if win32 then
  1973. header.opthdr:=sizeof(tcoffpeoptheader)
  1974. else
  1975. header.opthdr:=sizeof(coffdjoptheader);
  1976. if win32 then
  1977. begin
  1978. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  1979. if IsSharedLibrary then
  1980. header.flag:=header.flag or PE_FILE_DLL;
  1981. if FindExeSection('.reloc')=nil then
  1982. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  1983. if FindExeSection('.stab')=nil then
  1984. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  1985. if (cs_link_strip in aktglobalswitches) then
  1986. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  1987. end
  1988. else
  1989. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  1990. if target_info.system in [system_i386_win32,system_arm_wince,system_i386_wince] then
  1991. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  1992. FWriter.write(header,sizeof(header));
  1993. { Optional COFF Header }
  1994. if win32 then
  1995. begin
  1996. fillchar(peoptheader,sizeof(peoptheader),0);
  1997. peoptheader.magic:=COFF_OPT_MAGIC;
  1998. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  1999. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  2000. peoptheader.tsize:=TextExeSec.Size;
  2001. peoptheader.dsize:=DataExeSec.Size;
  2002. peoptheader.bsize:=BSSExeSec.Size;
  2003. peoptheader.text_start:=TextExeSec.mempos;
  2004. {$ifndef x86_64}
  2005. peoptheader.data_start:=DataExeSec.mempos;
  2006. {$endif x86_64}
  2007. peoptheader.entry:=EntrySym.Address;
  2008. peoptheader.ImageBase:=ImageBase;
  2009. peoptheader.SectionAlignment:=SectionMemAlign;
  2010. peoptheader.FileAlignment:=SectionDataAlign;
  2011. peoptheader.MajorOperatingSystemVersion:=4;
  2012. peoptheader.MinorOperatingSystemVersion:=0;
  2013. peoptheader.MajorImageVersion:=dllmajor;
  2014. peoptheader.MinorImageVersion:=dllminor;
  2015. if target_info.system in [system_arm_wince,system_i386_wince] then
  2016. peoptheader.MajorSubsystemVersion:=3
  2017. else
  2018. peoptheader.MajorSubsystemVersion:=4;
  2019. peoptheader.MinorSubsystemVersion:=0;
  2020. peoptheader.Win32Version:=0;
  2021. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  2022. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  2023. peoptheader.CheckSum:=0;
  2024. if target_info.system in [system_arm_wince,system_i386_wince] then
  2025. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  2026. else
  2027. if apptype=app_gui then
  2028. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  2029. else
  2030. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  2031. peoptheader.DllCharacteristics:=0;
  2032. peoptheader.SizeOfStackReserve:=stacksize;
  2033. peoptheader.SizeOfStackCommit:=$1000;
  2034. peoptheader.SizeOfHeapReserve:=$100000;
  2035. peoptheader.SizeOfHeapCommit:=$1000;
  2036. peoptheader.LoaderFlags:=0;
  2037. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  2038. UpdateDataDir('.idata',PE_DATADIR_IDATA);
  2039. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  2040. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  2041. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  2042. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  2043. FWriter.write(peoptheader,sizeof(peoptheader));
  2044. end
  2045. else
  2046. begin
  2047. fillchar(djoptheader,sizeof(djoptheader),0);
  2048. djoptheader.magic:=COFF_OPT_MAGIC;
  2049. djoptheader.tsize:=TextExeSec.Size;
  2050. djoptheader.dsize:=DataExeSec.Size;
  2051. djoptheader.bsize:=BSSExeSec.Size;
  2052. djoptheader.text_start:=TextExeSec.mempos;
  2053. djoptheader.data_start:=DataExeSec.mempos;
  2054. djoptheader.entry:=EntrySym.offset;
  2055. FWriter.write(djoptheader,sizeof(djoptheader));
  2056. end;
  2057. { Section headers }
  2058. ExeSections.ForEachCall(@ExeSections_write_header,nil);
  2059. { Section data }
  2060. ExeSections.ForEachCall(@ExeSections_write_data,nil);
  2061. { Optional ObjSymbols }
  2062. if not(cs_link_strip in aktglobalswitches) then
  2063. begin
  2064. if SymPos<>FWriter.Size then
  2065. internalerror(200602252);
  2066. { ObjSymbols }
  2067. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  2068. { Strings }
  2069. i:=FCoffStrs.size+4;
  2070. FWriter.write(i,4);
  2071. FWriter.writearray(FCoffStrs);
  2072. end;
  2073. { Release }
  2074. FCoffStrs.Free;
  2075. FCoffSyms.Free;
  2076. result:=true;
  2077. end;
  2078. constructor TDJCoffexeoutput.create;
  2079. begin
  2080. inherited createcoff(false);
  2081. CExeSection:=TDJCoffExeSection;
  2082. CObjData:=TPECoffObjData;
  2083. end;
  2084. constructor TPECoffexeoutput.create;
  2085. begin
  2086. inherited createcoff(true);
  2087. CExeSection:=TPECoffExeSection;
  2088. CObjData:=TPECoffObjData;
  2089. end;
  2090. procedure TPECoffexeoutput.GenerateLibraryImports(ExternalLibraryList:TFPHashObjectList);
  2091. var
  2092. textobjsection,
  2093. idata2objsection,
  2094. idata4objsection,
  2095. idata5objsection,
  2096. idata6objsection,
  2097. idata7objsection : TObjSection;
  2098. idata2label : TObjSymbol;
  2099. basedllname : string;
  2100. procedure StartImport(const dllname:string);
  2101. var
  2102. idata4label,
  2103. idata5label,
  2104. idata7label : TObjSymbol;
  2105. emptyint : longint;
  2106. begin
  2107. if assigned(exemap) then
  2108. begin
  2109. exemap.Add('');
  2110. exemap.Add('Importing from DLL '+dllname);
  2111. end;
  2112. emptyint:=0;
  2113. basedllname:=splitfilename(dllname);
  2114. idata2objsection:=internalobjdata.createsection(sec_idata2,basedllname);
  2115. idata2label:=internalobjdata.SymbolDefine('__imp_dir_'+basedllname,AB_LOCAL,AT_DATA);
  2116. idata4objsection:=internalobjdata.createsection(sec_idata4,basedllname);
  2117. idata4label:=internalobjdata.SymbolDefine('__imp_names_'+basedllname,AB_LOCAL,AT_DATA);
  2118. idata5objsection:=internalobjdata.createsection(sec_idata5,basedllname);
  2119. idata5label:=internalobjdata.SymbolDefine('__imp_fixup_'+basedllname,AB_LOCAL,AT_DATA);
  2120. idata7objsection:=internalobjdata.createsection(sec_idata7,basedllname);
  2121. idata7label:=internalobjdata.SymbolDefine('__imp_dll_'+basedllname,AB_LOCAL,AT_DATA);
  2122. { idata2 }
  2123. internalobjdata.SetSection(idata2objsection);
  2124. { dummy links to imports finalization }
  2125. internalobjdata.writereloc(0,0,internalobjdata.SymbolRef('__imp_names_end_'+basedllname),RELOC_NONE);
  2126. internalobjdata.writereloc(0,0,internalobjdata.SymbolRef('__imp_fixup_end_'+basedllname),RELOC_NONE);
  2127. { section data }
  2128. internalobjdata.writereloc(0,sizeof(longint),idata4label,RELOC_RVA);
  2129. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2130. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2131. internalobjdata.writereloc(0,sizeof(longint),idata7label,RELOC_RVA);
  2132. internalobjdata.writereloc(0,sizeof(longint),idata5label,RELOC_RVA);
  2133. { idata7 }
  2134. internalobjdata.SetSection(idata7objsection);
  2135. internalobjdata.writebytes(basedllname[1],length(basedllname));
  2136. internalobjdata.writebytes(emptyint,1);
  2137. end;
  2138. procedure EndImport;
  2139. var
  2140. emptyint : longint;
  2141. begin
  2142. emptyint:=0;
  2143. idata4objsection:=internalobjdata.createsection(sec_idata4, basedllname+'_z_');
  2144. internalobjdata.SymbolDefine('__imp_names_end_'+basedllname,AB_LOCAL,AT_DATA);
  2145. idata5objsection:=internalobjdata.createsection(sec_idata5, basedllname+'_z_');
  2146. internalobjdata.SymbolDefine('__imp_fixup_end_'+basedllname,AB_LOCAL,AT_DATA);
  2147. { idata4 }
  2148. internalobjdata.SetSection(idata4objsection);
  2149. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2150. if target_info.system=system_x86_64_win64 then
  2151. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2152. { idata5 }
  2153. internalobjdata.SetSection(idata5objsection);
  2154. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2155. if target_info.system=system_x86_64_win64 then
  2156. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2157. end;
  2158. function AddProcImport(const afuncname:string; ordnumber:longint):TObjSymbol;
  2159. const
  2160. {$ifdef x86_64}
  2161. jmpopcode : array[0..2] of byte = (
  2162. $ff,$24,$25
  2163. );
  2164. {$else x86_64}
  2165. {$ifdef arm}
  2166. jmpopcode : array[0..7] of byte = (
  2167. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  2168. $00,$f0,$9c,$e5 // ldr pc, [ip]
  2169. );
  2170. {$else arm}
  2171. jmpopcode : array[0..1] of byte = (
  2172. $ff,$25
  2173. );
  2174. {$endif arm}
  2175. {$endif x86_64}
  2176. nopopcodes : array[0..1] of byte = (
  2177. $90,$90
  2178. );
  2179. var
  2180. idata4label,
  2181. idata5label,
  2182. idata6label : TObjSymbol;
  2183. emptyint : longint;
  2184. secname,
  2185. num : string;
  2186. ordnr: word;
  2187. begin
  2188. result:=nil;
  2189. emptyint:=0;
  2190. if assigned(exemap) then
  2191. exemap.Add(' Importing Function '+afuncname);
  2192. with internalobjdata do
  2193. begin
  2194. secname:=basedllname+'_i_'+afuncname;
  2195. textobjsection:=createsection(sectionname(sec_code,secname),sectiontype2align(sec_code),sectiontype2options(sec_code) - [oso_keep]);
  2196. idata4objsection:=createsection(sec_idata4, secname);
  2197. idata5objsection:=createsection(sec_idata5, secname);
  2198. idata6objsection:=createsection(sec_idata6, secname);
  2199. end;
  2200. { idata6, import data (ordnr+name) }
  2201. internalobjdata.SetSection(idata6objsection);
  2202. inc(idatalabnr);
  2203. num:=tostr(idatalabnr);
  2204. idata6label:=internalobjdata.SymbolDefine('__imp_'+num,AB_LOCAL,AT_DATA);
  2205. ordnr:=Abs(ordnumber);
  2206. internalobjdata.writebytes(ordnr,2);
  2207. if ordnumber <= 0 then
  2208. internalobjdata.writebytes(afuncname[1],length(afuncname));
  2209. internalobjdata.writebytes(emptyint,1);
  2210. internalobjdata.writebytes(emptyint,align(internalobjdata.CurrObjSec.size,2)-internalobjdata.CurrObjSec.size);
  2211. { idata4, import lookup table }
  2212. internalobjdata.SetSection(idata4objsection);
  2213. idata4label:=internalobjdata.SymbolDefine('__imp_lookup_'+num,AB_LOCAL,AT_DATA);
  2214. if ordnumber <= 0 then
  2215. begin
  2216. internalobjdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  2217. if target_info.system=system_x86_64_win64 then
  2218. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2219. end
  2220. else
  2221. begin
  2222. emptyint:=ordnumber;
  2223. if target_info.system=system_x86_64_win64 then
  2224. begin
  2225. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2226. emptyint:=longint($80000000);
  2227. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2228. end
  2229. else
  2230. begin
  2231. emptyint:=emptyint or longint($80000000);
  2232. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2233. end;
  2234. emptyint:=0;
  2235. end;
  2236. { idata5, import address table }
  2237. internalobjdata.SetSection(idata5objsection);
  2238. { dummy back links }
  2239. internalobjdata.writereloc(0,0,idata4label,RELOC_NONE);
  2240. internalobjdata.writereloc(0,0,idata2label,RELOC_NONE);
  2241. { section data }
  2242. idata5label:=internalobjdata.SymbolDefine('__imp_'+afuncname,AB_LOCAL,AT_DATA);
  2243. internalobjdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  2244. if target_info.system=system_x86_64_win64 then
  2245. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2246. { text, jmp }
  2247. internalobjdata.SetSection(textobjsection);
  2248. result:=internalobjdata.SymbolDefine('_'+afuncname,AB_GLOBAL,AT_FUNCTION);
  2249. internalobjdata.writebytes(jmpopcode,sizeof(jmpopcode));
  2250. internalobjdata.writereloc(0,sizeof(longint),idata5label,RELOC_ABSOLUTE32);
  2251. internalobjdata.writebytes(nopopcodes,align(internalobjdata.CurrObjSec.size,sizeof(nopopcodes))-internalobjdata.CurrObjSec.size);
  2252. end;
  2253. var
  2254. i,j : longint;
  2255. ExtLibrary : TExternalLibrary;
  2256. ExtSymbol : TExternalSymbol;
  2257. exesym : TExeSymbol;
  2258. begin
  2259. for i:=0 to ExternalLibraryList.Count-1 do
  2260. begin
  2261. ExtLibrary:=TExternalLibrary(ExternalLibraryList[i]);
  2262. idata2objsection:=nil;
  2263. idata4objsection:=nil;
  2264. idata5objsection:=nil;
  2265. idata6objsection:=nil;
  2266. idata7objsection:=nil;
  2267. for j:=0 to ExtLibrary.ExternalSymbolList.Count-1 do
  2268. begin
  2269. ExtSymbol:=TExternalSymbol(ExtLibrary.ExternalSymbolList[j]);
  2270. exesym:=TExeSymbol(ExeSymbolList.Find(ExtSymbol.Name));
  2271. if assigned(exesym) and
  2272. (exesym.State<>symstate_defined) then
  2273. begin
  2274. if not assigned(idata2objsection) then
  2275. StartImport(ExtLibrary.Name);
  2276. exesym.objsymbol:=AddProcImport(ExtSymbol.Name, ExtSymbol.OrdNumber);
  2277. exesym.State:=symstate_defined;
  2278. end;
  2279. end;
  2280. if assigned(idata2objsection) then
  2281. EndImport;
  2282. end;
  2283. PackUnresolvedExeSymbols('after DLL imports');
  2284. end;
  2285. procedure TPECoffexeoutput.GenerateRelocs;
  2286. var
  2287. pgaddr, hdrpos : longint;
  2288. procedure FinishBlock;
  2289. var
  2290. p,len : longint;
  2291. begin
  2292. if hdrpos = -1 then
  2293. exit;
  2294. p:=0;
  2295. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  2296. p:=internalObjData.CurrObjSec.Data.Pos;
  2297. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  2298. len:=p-hdrpos;
  2299. internalObjData.CurrObjSec.Data.write(len,4);
  2300. internalObjData.CurrObjSec.Data.seek(p);
  2301. hdrpos:=-1;
  2302. end;
  2303. var
  2304. exesec : TExeSection;
  2305. objsec : TObjSection;
  2306. objreloc : TObjRelocation;
  2307. i,j,k,offset : longint;
  2308. w: word;
  2309. begin
  2310. if not IsSharedLibrary then
  2311. exit;
  2312. exesec:=FindExeSection('.reloc');
  2313. if exesec=nil then
  2314. exit;
  2315. objsec:=internalObjData.createsection('.reloc',0,exesec.SecOptions);
  2316. exesec.AddObjSection(objsec);
  2317. pgaddr:=-1;
  2318. hdrpos:=-1;
  2319. for i:=0 to ExeSections.Count-1 do
  2320. begin
  2321. exesec:=TExeSection(ExeSections[i]);
  2322. for j:=0 to exesec.ObjSectionList.count-1 do
  2323. begin
  2324. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2325. for k:=0 to objsec.ObjRelocations.Count-1 do
  2326. begin
  2327. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  2328. if not (objreloc.typ in [{$ifdef x86_64}RELOC_ABSOLUTE32,{$endif x86_64}RELOC_ABSOLUTE]) then
  2329. continue;
  2330. offset:=objsec.MemPos+objreloc.dataoffset;
  2331. if (offset-pgaddr>=4096) or (pgaddr=-1) then
  2332. begin
  2333. FinishBlock;
  2334. pgaddr:=(offset div 4096)*4096;
  2335. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  2336. internalObjData.writebytes(pgaddr,4);
  2337. { Reserving space for block size. The size will be written later in FinishBlock }
  2338. internalObjData.writebytes(k,4);
  2339. end;
  2340. w:=(IMAGE_REL_BASED_HIGHLOW shl 12) or (offset-pgaddr);
  2341. internalObjData.writebytes(w,2);
  2342. end;
  2343. end;
  2344. end;
  2345. FinishBlock;
  2346. end;
  2347. procedure TPECoffexeoutput.Order_End;
  2348. var
  2349. exesec : TExeSection;
  2350. begin
  2351. inherited;
  2352. if not IsSharedLibrary then
  2353. exit;
  2354. exesec:=FindExeSection('.reloc');
  2355. if exesec=nil then
  2356. exit;
  2357. exesec.SecOptions:=exesec.SecOptions + [oso_Data,oso_keep];
  2358. end;
  2359. procedure TPECoffexeoutput.CalcPos_ExeSection(const aname:string);
  2360. begin
  2361. if aname='.reloc' then
  2362. GenerateRelocs;
  2363. inherited;
  2364. end;
  2365. {****************************************************************************
  2366. TDJCoffAssembler
  2367. ****************************************************************************}
  2368. constructor TDJCoffAssembler.Create(smart:boolean);
  2369. begin
  2370. inherited Create(smart);
  2371. CObjOutput:=TDJCoffObjOutput;
  2372. end;
  2373. {****************************************************************************
  2374. TPECoffAssembler
  2375. ****************************************************************************}
  2376. constructor TPECoffAssembler.Create(smart:boolean);
  2377. begin
  2378. inherited Create(smart);
  2379. CObjOutput:=TPECoffObjOutput;
  2380. end;
  2381. {*****************************************************************************
  2382. DLLReader
  2383. *****************************************************************************}
  2384. {$ifdef win32}
  2385. var
  2386. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  2387. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  2388. {$endif win32}
  2389. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  2390. type
  2391. TPECoffExpDir=packed record
  2392. flag,
  2393. stamp : cardinal;
  2394. Major,
  2395. Minor : word;
  2396. Name,
  2397. Base,
  2398. NumFuncs,
  2399. NumNames,
  2400. AddrFuncs,
  2401. AddrNames,
  2402. AddrOrds : cardinal;
  2403. end;
  2404. var
  2405. DLLReader : TObjectReader;
  2406. DosHeader : array[0..$7f] of byte;
  2407. PEMagic : array[0..3] of byte;
  2408. Header : TCoffHeader;
  2409. peheader : tcoffpeoptheader;
  2410. NameOfs,
  2411. newheaderofs : longint;
  2412. FuncName : string;
  2413. expdir : TPECoffExpDir;
  2414. i : longint;
  2415. found : boolean;
  2416. sechdr : tCoffSecHdr;
  2417. {$ifdef win32}
  2418. p : pointer;
  2419. {$endif win32}
  2420. begin
  2421. result:=false;
  2422. {$ifdef win32}
  2423. if (target_info.system=system_x86_64_win64) and
  2424. assigned(Wow64DisableWow64FsRedirection) then
  2425. Wow64DisableWow64FsRedirection(p);
  2426. {$endif win32}
  2427. DLLReader:=TObjectReader.Create;
  2428. DLLReader.OpenFile(dllname);
  2429. {$ifdef win32}
  2430. if (target_info.system=system_x86_64_win64) and
  2431. assigned(Wow64RevertWow64FsRedirection) then
  2432. Wow64RevertWow64FsRedirection(p);
  2433. {$endif win32}
  2434. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  2435. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  2436. begin
  2437. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  2438. exit;
  2439. end;
  2440. newheaderofs:=longint(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  2441. DLLReader.Seek(newheaderofs);
  2442. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  2443. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  2444. begin
  2445. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  2446. exit;
  2447. end;
  2448. if not DLLReader.Read(Header,sizeof(TCoffHeader)) or
  2449. (Header.mach<>COFF_MAGIC) or
  2450. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  2451. begin
  2452. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  2453. exit;
  2454. end;
  2455. { Read optheader }
  2456. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  2457. { Section headers }
  2458. found:=false;
  2459. for i:=1 to header.nsects do
  2460. begin
  2461. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  2462. begin
  2463. Comment(V_Error,'Error reading coff file '+DLLName);
  2464. exit;
  2465. end;
  2466. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  2467. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  2468. begin
  2469. found:=true;
  2470. break;
  2471. end;
  2472. end;
  2473. if not found then
  2474. begin
  2475. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  2476. exit;
  2477. end;
  2478. { Process edata }
  2479. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  2480. DLLReader.Read(expdir,sizeof(expdir));
  2481. for i:=0 to expdir.NumNames-1 do
  2482. begin
  2483. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  2484. DLLReader.Read(NameOfs,4);
  2485. Dec(NameOfs,sechdr.rvaofs);
  2486. if (NameOfs<0) or
  2487. (NameOfs>sechdr.vsize) then
  2488. begin
  2489. Comment(V_Error,'DLL does contains invalid exports');
  2490. break;
  2491. end;
  2492. { Read Function name from DLL, prepend _ and terminate with #0 }
  2493. DLLReader.Seek(sechdr.datapos+NameOfs);
  2494. DLLReader.Read(FuncName[1],sizeof(FuncName)-3);
  2495. FuncName[sizeof(FuncName)-1]:=#0;
  2496. FuncName[0]:=chr(Strlen(@FuncName[1]));
  2497. readdllproc(DLLName,FuncName);
  2498. end;
  2499. DLLReader.Free;
  2500. end;
  2501. {*****************************************************************************
  2502. Initialize
  2503. *****************************************************************************}
  2504. {$ifdef i386}
  2505. const
  2506. as_i386_coff_info : tasminfo =
  2507. (
  2508. id : as_i386_coff;
  2509. idtxt : 'COFF';
  2510. asmbin : '';
  2511. asmcmd : '';
  2512. supported_target : system_i386_go32v2;
  2513. flags : [af_outputbinary];
  2514. labelprefix : '.L';
  2515. comment : '';
  2516. );
  2517. as_i386_pecoff_info : tasminfo =
  2518. (
  2519. id : as_i386_pecoff;
  2520. idtxt : 'PECOFF';
  2521. asmbin : '';
  2522. asmcmd : '';
  2523. supported_target : system_i386_win32;
  2524. flags : [af_outputbinary,af_smartlink_sections];
  2525. labelprefix : '.L';
  2526. comment : '';
  2527. );
  2528. as_i386_pecoffwdosx_info : tasminfo =
  2529. (
  2530. id : as_i386_pecoffwdosx;
  2531. idtxt : 'PEWDOSX';
  2532. asmbin : '';
  2533. asmcmd : '';
  2534. supported_target : system_i386_wdosx;
  2535. flags : [af_outputbinary];
  2536. labelprefix : '.L';
  2537. comment : '';
  2538. );
  2539. as_i386_pecoffwince_info : tasminfo =
  2540. (
  2541. id : as_i386_pecoffwince;
  2542. idtxt : 'PECOFFWINCE';
  2543. asmbin : '';
  2544. asmcmd : '';
  2545. supported_target : system_i386_wince;
  2546. flags : [af_outputbinary];
  2547. labelprefix : '.L';
  2548. comment : '';
  2549. );
  2550. {$endif i386}
  2551. {$ifdef x86_64}
  2552. const
  2553. as_x86_64_pecoff_info : tasminfo =
  2554. (
  2555. id : as_x86_64_pecoff;
  2556. idtxt : 'PECOFF';
  2557. asmbin : '';
  2558. asmcmd : '';
  2559. supported_target : system_x86_64_win64;
  2560. flags : [af_outputbinary,af_smartlink_sections];
  2561. labelprefix : '.L';
  2562. comment : '';
  2563. );
  2564. {$endif x86_64}
  2565. {$ifdef arm}
  2566. const
  2567. as_arm_pecoffwince_info : tasminfo =
  2568. (
  2569. id : as_arm_pecoffwince;
  2570. idtxt : 'PECOFFWINCE';
  2571. asmbin : '';
  2572. asmcmd : '';
  2573. supported_target : system_arm_wince;
  2574. flags : [af_outputbinary];
  2575. labelprefix : '.L';
  2576. comment : '';
  2577. );
  2578. {$endif arm}
  2579. {$ifdef win32}
  2580. procedure SetupProcVars;
  2581. var
  2582. hinstLib : THandle;
  2583. begin
  2584. Wow64DisableWow64FsRedirection:=nil;
  2585. Wow64RevertWow64FsRedirection:=nil;
  2586. hinstLib:=LoadLibrary('kernel32.dll');
  2587. if hinstLib<>0 then
  2588. begin
  2589. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  2590. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  2591. FreeLibrary(hinstLib);
  2592. end;
  2593. end;
  2594. {$endif win32}
  2595. initialization
  2596. {$ifdef i386}
  2597. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  2598. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  2599. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  2600. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  2601. {$endif i386}
  2602. {$ifdef x86_64}
  2603. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  2604. {$endif x86_64}
  2605. {$ifdef arm}
  2606. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  2607. {$endif arm}
  2608. {$ifdef win32}
  2609. SetupProcVars;
  2610. {$endif win32}
  2611. end.