ogcoff.pas 107 KB

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