ogcoff.pas 108 KB

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