ogcoff.pas 98 KB

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