ogcoff.pas 99 KB

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