ogcoff.pas 101 KB

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