2
0

ogcoff.pas 114 KB

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