ogcoff.pas 111 KB

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