ogcoff.pas 108 KB

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