ogcoff.pas 108 KB

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