ogcoff.pas 112 KB

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