ogcoff.pas 98 KB

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