ogcoff.pas 92 KB

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