ogcoff.pas 102 KB

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