ogcoff.pas 101 KB

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