ogcoff.pas 110 KB

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