ogcoff.pas 107 KB

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