ogcoff.pas 86 KB

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