ogcoff.pas 104 KB

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