ogcoff.pas 99 KB

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