ogcoff.pas 99 KB

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