ogcoff.pas 103 KB

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