ogcoff.pas 107 KB

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