ogcoff.pas 101 KB

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