ogcoff.pas 100 KB

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