ogcoff.pas 109 KB

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