ogcoff.pas 104 KB

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