ogcoff.pas 101 KB

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