ogcoff.pas 106 KB

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