ogcoff.pas 93 KB

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