ogcoff.pas 90 KB

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