2
0

ogcoff.pas 109 KB

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