ogcoff.pas 107 KB

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