ogcoff.pas 102 KB

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