ogcoff.pas 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973
  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;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;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. result:=false;
  1635. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  1636. with TCoffObjData(objdata) do
  1637. begin
  1638. { Read COFF header }
  1639. if not AReader.read(header,sizeof(tcoffheader)) then
  1640. begin
  1641. InputError('Can''t read COFF Header');
  1642. exit;
  1643. end;
  1644. if header.mach<>COFF_MAGIC then
  1645. begin
  1646. InputError('Illegal COFF Magic');
  1647. exit;
  1648. end;
  1649. {$ifdef arm}
  1650. eVCobj:=header.flag=$100;
  1651. {$endif arm}
  1652. { ObjSymbols }
  1653. AReader.Seek(header.sympos);
  1654. if not AReader.ReadArray(FCoffSyms,header.syms*sizeof(CoffSymbol)) then
  1655. begin
  1656. InputError('Error reading coff symbol table');
  1657. exit;
  1658. end;
  1659. { Strings }
  1660. if not AReader.Read(FCoffStrSize,4) then
  1661. begin
  1662. InputError('Error reading COFF string table');
  1663. exit;
  1664. end;
  1665. if (FCoffStrSize>4) then
  1666. begin
  1667. { allocate an extra byte and null-terminate }
  1668. GetMem(FCoffStrs,FCoffStrSize+1);
  1669. FCoffStrs[FCoffStrSize]:=#0;
  1670. for i:=0 to 3 do
  1671. FCoffStrs[i]:=#0;
  1672. if not AReader.Read(FCoffStrs[4],FCoffStrSize-4) then
  1673. begin
  1674. InputError('Error reading COFF string table');
  1675. exit;
  1676. end;
  1677. end;
  1678. { Section headers }
  1679. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  1680. FSecCount:=header.nsects;
  1681. FSecTbl:=AllocMem((header.nsects+1)*sizeof(TObjSection));
  1682. AReader.Seek(sizeof(tcoffheader)+header.opthdr);
  1683. for i:=1 to header.nsects do
  1684. begin
  1685. if not AReader.read(sechdr,sizeof(sechdr)) then
  1686. begin
  1687. InputError('Error reading COFF Section Headers');
  1688. exit;
  1689. end;
  1690. move(sechdr.name,secnamebuf,8);
  1691. secnamebuf[8]:=#0;
  1692. secname:=strpas(secnamebuf);
  1693. if secname[1]='/' then
  1694. begin
  1695. Val(Copy(secname,2,8),strpos,code);
  1696. if code=0 then
  1697. secname:=Read_str(strpos)
  1698. else
  1699. begin
  1700. InputError('Error reading COFF Section Headers');
  1701. secname:='error';
  1702. end;
  1703. end;
  1704. if win32 then
  1705. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  1706. else
  1707. begin
  1708. secoptions:=djdecodesechdrflags(secname,sechdr.flags);
  1709. secalign:=sizeof(pint);
  1710. end;
  1711. if (Length(secname)>3) and (secname[2] in ['e','f','i','p','r']) then
  1712. begin
  1713. if (Pos('.edata',secname)=1) or
  1714. (Pos('.rsrc',secname)=1) or
  1715. {$ifndef x86_64}
  1716. (Pos('.pdata',secname)=1) or
  1717. {$endif}
  1718. (Pos('.fpc',secname)=1) then
  1719. include(secoptions,oso_keep);
  1720. if (Pos('.idata',secname)=1) then
  1721. begin
  1722. { TODO: idata keep can maybe replaced with grouping of text and idata}
  1723. include(secoptions,oso_keep);
  1724. secname:=secname + '.' + ExtractFileName(InputFileName);
  1725. end;
  1726. end;
  1727. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions,false));
  1728. FSecTbl^[i]:=objsec;
  1729. if not win32 then
  1730. objsec.mempos:=sechdr.rvaofs;
  1731. objsec.orgmempos:=sechdr.rvaofs;
  1732. objsec.coffrelocs:=sechdr.nrelocs;
  1733. objsec.coffrelocpos:=sechdr.relocpos;
  1734. objsec.datapos:=sechdr.datapos;
  1735. objsec.Size:=sechdr.dataSize;
  1736. end;
  1737. { Insert all ObjSymbols }
  1738. read_symbols(objdata);
  1739. { Section Data }
  1740. ReadSectionContent(objdata);
  1741. { Relocs }
  1742. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  1743. end;
  1744. if assigned(FCoffStrs) then
  1745. freemem(FCoffStrs);
  1746. FCoffStrs:=nil;
  1747. FCoffSyms.Free;
  1748. FCoffSyms:=nil;
  1749. result:=true;
  1750. end;
  1751. constructor TDJCoffObjInput.create;
  1752. begin
  1753. inherited createcoff(false);
  1754. cobjdata:=TDJCoffObjData;
  1755. end;
  1756. constructor TPECoffObjInput.create;
  1757. begin
  1758. inherited createcoff(true);
  1759. cobjdata:=TPECoffObjData;
  1760. end;
  1761. {****************************************************************************
  1762. TCoffexeoutput
  1763. ****************************************************************************}
  1764. constructor TCoffexeoutput.createcoff(awin32:boolean);
  1765. begin
  1766. inherited create;
  1767. win32:=awin32;
  1768. if target_info.system in [system_x86_64_win64] then
  1769. MaxMemPos:=$FFFFFFFF
  1770. else
  1771. if target_info.system in systems_wince then
  1772. MaxMemPos:=$1FFFFFF
  1773. else
  1774. MaxMemPos:=$7FFFFFFF;
  1775. end;
  1776. procedure TCoffexeoutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  1777. var
  1778. sym : coffsymbol;
  1779. begin
  1780. FillChar(sym,sizeof(sym),0);
  1781. if length(name)>8 then
  1782. begin
  1783. sym.strpos:=FCoffStrs.size+4;
  1784. FCoffStrs.writestr(name);
  1785. FCoffStrs.writestr(#0);
  1786. end
  1787. else
  1788. move(name[1],sym.name,length(name));
  1789. sym.value:=value;
  1790. sym.section:=section;
  1791. sym.typ:=typ;
  1792. sym.aux:=aux;
  1793. FWriter.write(sym,sizeof(sym));
  1794. end;
  1795. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  1796. var
  1797. secval,
  1798. value : aint;
  1799. globalval : byte;
  1800. exesec : TExeSection;
  1801. begin
  1802. if not assigned(texesymbol(p).objsymbol) then
  1803. internalerror(200603053);
  1804. with texesymbol(p).objsymbol do
  1805. begin
  1806. exesec:=TExeSection(objsection.exesection);
  1807. { There is no exesection defined for special internal symbols
  1808. like __image_base__ }
  1809. if assigned(exesec) then
  1810. begin
  1811. secval:=exesec.secsymidx;
  1812. if win32 then
  1813. value:=address-exesec.mempos
  1814. else
  1815. value:=address;
  1816. end
  1817. else
  1818. begin
  1819. secval:=-1;
  1820. value:=address;
  1821. end;
  1822. if bind=AB_LOCAL then
  1823. globalval:=COFF_SYM_LOCAL
  1824. else
  1825. globalval:=COFF_SYM_GLOBAL;
  1826. { reloctype address to the section in the executable }
  1827. write_symbol(name,value,secval,globalval,0);
  1828. end;
  1829. end;
  1830. procedure TCoffexeoutput.ExeSectionList_write_header(p:TObject;arg:pointer);
  1831. var
  1832. sechdr : tcoffsechdr;
  1833. s : string;
  1834. strpos : aword;
  1835. begin
  1836. with tExeSection(p) do
  1837. begin
  1838. fillchar(sechdr,sizeof(sechdr),0);
  1839. s:=name;
  1840. if length(s)>8 then
  1841. begin
  1842. strpos:=FCoffStrs.size+4;
  1843. FCoffStrs.writestr(s);
  1844. FCoffStrs.writestr(#0);
  1845. s:='/'+ToStr(strpos);
  1846. end;
  1847. move(s[1],sechdr.name,length(s));
  1848. if win32 then
  1849. begin
  1850. sechdr.rvaofs:=mempos;
  1851. sechdr.vsize:=Size;
  1852. { sechdr.dataSize is size of initialized data, rounded up to FileAlignment
  1853. (so it can be greater than VirtualSize). Must be zero for sections that
  1854. do not contain initialized data. }
  1855. if (oso_data in SecOptions) then
  1856. sechdr.datasize:=Align(Size,SectionDataAlign);
  1857. end
  1858. else
  1859. begin
  1860. if not (oso_debug in SecOptions) then
  1861. begin
  1862. sechdr.rvaofs:=mempos;
  1863. sechdr.vsize:=mempos;
  1864. end;
  1865. sechdr.datasize:=Size;
  1866. end;
  1867. if (Size>0) then
  1868. sechdr.datapos:=datapos-datapos_offset;
  1869. sechdr.nrelocs:=0;
  1870. sechdr.relocpos:=0;
  1871. if win32 then
  1872. begin
  1873. if (target_info.system in systems_nativent) and
  1874. (apptype = app_native) then
  1875. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign) or PE_SCN_MEM_NOT_PAGED
  1876. else
  1877. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign);
  1878. { some flags are invalid in executables, reset them }
  1879. sechdr.flags:=sechdr.flags and
  1880. not(PE_SCN_LNK_INFO or PE_SCN_LNK_REMOVE or
  1881. PE_SCN_LNK_COMDAT or PE_SCN_ALIGN_MASK);
  1882. end
  1883. else
  1884. sechdr.flags:=djencodesechdrflags(SecOptions);
  1885. FWriter.write(sechdr,sizeof(sechdr));
  1886. end;
  1887. end;
  1888. procedure TCoffexeoutput.ExeSectionList_pass2_header(p:TObject;arg:pointer);
  1889. begin
  1890. with TExeSection(p) do
  1891. begin
  1892. { The debuginfo sections should already be stripped }
  1893. { if (ExeWriteMode=ewm_exeonly) and
  1894. (oso_debug in SecOptions) then
  1895. internalerror(200801161); }
  1896. inc(plongint(arg)^);
  1897. secsymidx:=plongint(arg)^;
  1898. end;
  1899. end;
  1900. function tcoffexeoutput.totalheadersize:longword;
  1901. var
  1902. stubsize,
  1903. optheadersize : longword;
  1904. begin
  1905. if win32 then
  1906. begin
  1907. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  1908. optheadersize:=sizeof(tcoffpeoptheader);
  1909. end
  1910. else
  1911. begin
  1912. stubsize:=sizeof(go32v2stub);
  1913. optheadersize:=sizeof(coffdjoptheader);
  1914. end;
  1915. result:=stubsize+sizeof(tcoffheader)+optheadersize;
  1916. end;
  1917. procedure tcoffexeoutput.MemPos_Header;
  1918. begin
  1919. { calculate start positions after the headers }
  1920. currmempos:=totalheadersize+sizeof(tcoffsechdr)*longword(ExeSectionList.Count-2);
  1921. end;
  1922. procedure tcoffexeoutput.DataPos_Header;
  1923. begin
  1924. { retrieve amount of sections }
  1925. nsects:=0;
  1926. ExeSectionList.ForEachCall(@ExeSectionList_pass2_header,@nsects);
  1927. { calculate start positions after the headers }
  1928. currdatapos:=totalheadersize+longword(nsects)*sizeof(tcoffsechdr);
  1929. end;
  1930. procedure tcoffexeoutput.DataPos_Symbols;
  1931. begin
  1932. inherited DataPos_Symbols;
  1933. { Calculating symbols position and size }
  1934. nsyms:=ExeSymbolList.Count;
  1935. sympos:=Align(CurrDataPos,SectionDataAlign);
  1936. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  1937. end;
  1938. function TCoffexeoutput.writedata:boolean;
  1939. var
  1940. i : longword;
  1941. header : tcoffheader;
  1942. djoptheader : coffdjoptheader;
  1943. peoptheader : tcoffpeoptheader;
  1944. textExeSec,
  1945. dataExeSec,
  1946. bssExeSec,
  1947. idataExeSec,
  1948. tlsExeSec : TExeSection;
  1949. tlsdir : TlsDirectory;
  1950. hassymbols,
  1951. writeDbgStrings : boolean;
  1952. procedure UpdateDataDir(const secname:string;idx:longint);
  1953. var
  1954. exesec : TExeSection;
  1955. begin
  1956. exesec:=FindExeSection(secname);
  1957. if assigned(exesec) then
  1958. begin
  1959. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  1960. peoptheader.DataDirectory[idx].size:=exesec.Size;
  1961. end;
  1962. end;
  1963. procedure UpdateImports;
  1964. var
  1965. exesec: TExeSection;
  1966. objsec, iat_start, iat_end, ilt_start: TObjSection;
  1967. i: longint;
  1968. begin
  1969. exesec:=FindExeSection('.idata');
  1970. if exesec=nil then
  1971. exit;
  1972. iat_start:=nil;
  1973. iat_end:=nil;
  1974. ilt_start:=nil;
  1975. for i:=0 to exesec.ObjSectionList.Count-1 do
  1976. begin
  1977. objsec:=TObjSection(exesec.ObjSectionList[i]);
  1978. if (ilt_start=nil) and (Pos('.idata$4',objsec.Name)=1) then
  1979. ilt_start:=objsec;
  1980. if Pos('.idata$5',objsec.Name)=1 then
  1981. begin
  1982. if iat_start=nil then
  1983. iat_start:=objsec;
  1984. end
  1985. else
  1986. if Assigned(iat_start) then
  1987. begin
  1988. iat_end:=objsec;
  1989. Break;
  1990. end;
  1991. end;
  1992. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=exesec.mempos;
  1993. if Assigned(ilt_start) then
  1994. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=ilt_start.mempos-exesec.mempos
  1995. else { should not happen }
  1996. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=exesec.Size;
  1997. if Assigned(iat_start) and Assigned(iat_end) then
  1998. begin
  1999. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].vaddr:=iat_start.mempos;
  2000. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].size:=iat_end.mempos-iat_start.mempos;
  2001. end;
  2002. end;
  2003. procedure UpdateTlsDataDir;
  2004. var
  2005. {callbacksection : TExeSection;}
  2006. tlsexesymbol: TExeSymbol;
  2007. tlssymbol: TObjSymbol;
  2008. callbackexesymbol: TExeSymbol;
  2009. callbacksymbol: TObjSymbol;
  2010. begin
  2011. { according to GNU ld,
  2012. the callback routines should be placed into .CRT$XL*
  2013. sections, and the thread local variables in .tls
  2014. __tls_start__ and __tls_end__ symbols
  2015. should be used for the initialized part,
  2016. which we do not support yet. }
  2017. { For now, we only pass the address of the __tls_used
  2018. asm symbol into PE_DATADIR_TLS with the correct
  2019. size of this table (different for win32/win64 }
  2020. tlsexesymbol:=texesymbol(ExeSymbolList.Find(
  2021. target_info.Cprefix+'_tls_used'));
  2022. if assigned(tlsexesymbol) then
  2023. begin
  2024. tlssymbol:=tlsexesymbol.ObjSymbol;
  2025. peoptheader.DataDirectory[PE_DATADIR_TLS].vaddr:=tlssymbol.address;
  2026. { sizeof(TlsDirectory) is different on host and target when cross-compiling }
  2027. peoptheader.DataDirectory[PE_DATADIR_TLS].size:=TLSDIR_SIZE;
  2028. if IsSharedLibrary then
  2029. begin
  2030. { Here we should reset __FPC_tls_callbacks value to nil }
  2031. callbackexesymbol:=texesymbol(ExeSymbolList.Find(
  2032. '__FPC_tls_callbacks'));
  2033. if assigned (callbackexesymbol) then
  2034. begin
  2035. callbacksymbol:=callbackexesymbol.ObjSymbol;
  2036. end;
  2037. end;
  2038. end;
  2039. end;
  2040. begin
  2041. result:=false;
  2042. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  2043. textExeSec:=FindExeSection('.text');
  2044. dataExeSec:=FindExeSection('.data');
  2045. bssExeSec:=FindExeSection('.bss');
  2046. tlsExeSec:=FindExeSection('.tls');
  2047. if not assigned(TextExeSec) or
  2048. not assigned(DataExeSec) then
  2049. internalerror(200602231);
  2050. { do we need to write symbols? }
  2051. hassymbols:=(ExeWriteMode=ewm_dbgonly) or
  2052. (
  2053. (ExeWriteMode=ewm_exefull) and
  2054. not(cs_link_strip in current_settings.globalswitches)
  2055. );
  2056. writeDbgStrings:=hassymbols or ((ExeWriteMode=ewm_exeonly) and (cs_link_separate_dbg_file in current_settings.globalswitches));
  2057. { Stub }
  2058. if win32 then
  2059. begin
  2060. FWriter.write(win32stub,sizeof(win32stub));
  2061. FWriter.write(pemagic,sizeof(pemagic));
  2062. end
  2063. else
  2064. FWriter.write(go32v2stub,sizeof(go32v2stub));
  2065. { Initial header, will be updated later }
  2066. fillchar(header,sizeof(header),0);
  2067. header.mach:=COFF_MAGIC;
  2068. header.nsects:=nsects;
  2069. if writeDbgStrings then
  2070. header.sympos:=sympos-datapos_offset;
  2071. if hassymbols then
  2072. header.syms:=nsyms;
  2073. if win32 then
  2074. header.opthdr:=sizeof(tcoffpeoptheader)
  2075. else
  2076. header.opthdr:=sizeof(coffdjoptheader);
  2077. if win32 then
  2078. begin
  2079. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  2080. if target_info.system in [system_x86_64_win64] then
  2081. header.flag:=header.flag or PE_FILE_LARGE_ADDRESS_AWARE
  2082. else
  2083. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  2084. if IsSharedLibrary then
  2085. header.flag:=header.flag or PE_FILE_DLL;
  2086. if FindExeSection('.reloc')=nil then
  2087. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  2088. if (FindExeSection('.stab')=nil) and
  2089. (FindExeSection('.debug_info')=nil) and
  2090. (FindExeSection('.gnu_debuglink')=nil) then
  2091. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  2092. if not hassymbols then
  2093. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  2094. if SetPEFlagsSetExplicity then
  2095. header.flag:=header.flag or peflags;
  2096. end
  2097. else
  2098. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  2099. FWriter.write(header,sizeof(header));
  2100. { Optional COFF Header }
  2101. if win32 then
  2102. begin
  2103. fillchar(peoptheader,sizeof(peoptheader),0);
  2104. peoptheader.magic:=COFF_OPT_MAGIC;
  2105. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  2106. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  2107. peoptheader.tsize:=TextExeSec.Size;
  2108. peoptheader.dsize:=DataExeSec.Size;
  2109. if assigned(BSSExeSec) then
  2110. peoptheader.bsize:=BSSExeSec.Size;
  2111. peoptheader.text_start:=TextExeSec.mempos;
  2112. {$ifndef cpu64bitaddr}
  2113. peoptheader.data_start:=DataExeSec.mempos;
  2114. {$endif cpu64bitaddr}
  2115. peoptheader.entry:=EntrySym.Address;
  2116. peoptheader.ImageBase:=ImageBase;
  2117. peoptheader.SectionAlignment:=SectionMemAlign;
  2118. peoptheader.FileAlignment:=SectionDataAlign;
  2119. peoptheader.MajorOperatingSystemVersion:=4;
  2120. peoptheader.MinorOperatingSystemVersion:=0;
  2121. peoptheader.MajorImageVersion:=dllmajor;
  2122. peoptheader.MinorImageVersion:=dllminor;
  2123. if target_info.system in systems_wince then
  2124. peoptheader.MajorSubsystemVersion:=3
  2125. else
  2126. peoptheader.MajorSubsystemVersion:=4;
  2127. peoptheader.MinorSubsystemVersion:=0;
  2128. peoptheader.Win32Version:=0;
  2129. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  2130. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  2131. peoptheader.CheckSum:=0;
  2132. if (target_info.system in systems_nativent) and (not IsSharedLibrary or (apptype = app_native)) then
  2133. { Although I did not really test this, it seems that Subsystem is
  2134. not checked in DLLs except for maybe drivers}
  2135. peoptheader.Subsystem:=PE_SUBSYSTEM_NATIVE
  2136. else
  2137. if target_info.system in systems_wince then
  2138. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  2139. else
  2140. if apptype=app_gui then
  2141. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  2142. else
  2143. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  2144. peoptheader.DllCharacteristics:=0;
  2145. peoptheader.SizeOfStackReserve:=stacksize;
  2146. peoptheader.SizeOfStackCommit:=$1000;
  2147. if MinStackSizeSetExplicity then
  2148. peoptheader.SizeOfStackCommit:=minstacksize;
  2149. if MaxStackSizeSetExplicity then
  2150. peoptheader.SizeOfStackReserve:=maxstacksize;
  2151. peoptheader.SizeOfHeapReserve:=$100000;
  2152. peoptheader.SizeOfHeapCommit:=$1000;
  2153. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  2154. UpdateImports;
  2155. UpdateTlsDataDir;
  2156. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  2157. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  2158. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  2159. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  2160. FWriter.write(peoptheader,sizeof(peoptheader));
  2161. end
  2162. else
  2163. begin
  2164. fillchar(djoptheader,sizeof(djoptheader),0);
  2165. djoptheader.magic:=COFF_OPT_MAGIC;
  2166. djoptheader.tsize:=TextExeSec.Size;
  2167. djoptheader.dsize:=DataExeSec.Size;
  2168. if assigned(BSSExeSec) then
  2169. djoptheader.bsize:=BSSExeSec.Size;
  2170. djoptheader.text_start:=TextExeSec.mempos;
  2171. djoptheader.data_start:=DataExeSec.mempos;
  2172. djoptheader.entry:=EntrySym.Address;
  2173. FWriter.write(djoptheader,sizeof(djoptheader));
  2174. end;
  2175. { For some unknown reason WM 6.1 requires .idata section to be read only.
  2176. Otherwise it refuses to load DLLs greater than 64KB.
  2177. Earlier versions of WinCE load DLLs regardless of .idata flags. }
  2178. if target_info.system in systems_wince then
  2179. begin
  2180. idataExeSec:=FindExeSection('.idata');
  2181. if idataExeSec<>nil then
  2182. idataExeSec.SecOptions:=idataExeSec.SecOptions - [oso_write];
  2183. end;
  2184. { Section headers }
  2185. ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
  2186. { Section data }
  2187. WriteExeSectionContent;
  2188. { Align after the last section }
  2189. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  2190. { Optional Symbols }
  2191. if SymPos<>FWriter.Size then
  2192. internalerror(200602252);
  2193. if hassymbols then
  2194. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  2195. if writeDbgStrings then
  2196. begin
  2197. { Strings }
  2198. i:=FCoffStrs.size+4;
  2199. FWriter.write(i,4);
  2200. FWriter.writearray(FCoffStrs);
  2201. end;
  2202. { Release }
  2203. FCoffStrs.Free;
  2204. result:=true;
  2205. end;
  2206. function IdataObjSectionCompare(Item1, Item2: Pointer): Integer;
  2207. var
  2208. I1 : TObjSection absolute Item1;
  2209. I2 : TObjSection absolute Item2;
  2210. begin
  2211. Result:=CompareStr(I1.Name,I2.Name);
  2212. end;
  2213. procedure TCoffexeoutput.Order_ObjSectionList(ObjSectionList: TFPObjectList;const aPattern:string);
  2214. begin
  2215. { Sort sections having '$' in the name, that's how PECOFF documentation
  2216. tells to handle them. However, look for '$' in the pattern, not in section
  2217. names, because the latter often get superfluous '$' due to mangling. }
  2218. if Pos('$',aPattern)>0 then
  2219. ObjSectionList.Sort(@IdataObjSectionCompare);
  2220. end;
  2221. constructor TDJCoffexeoutput.create;
  2222. begin
  2223. inherited createcoff(false);
  2224. datapos_offset:=sizeof(go32v2stub);
  2225. CExeSection:=TExeSection;
  2226. CObjData:=TDJCoffObjData;
  2227. end;
  2228. procedure TDJCoffexeoutput.MemPos_Header;
  2229. begin
  2230. { Headers are not loaded, first 4K page is reserved }
  2231. CurrMemPos:=$1000;
  2232. end;
  2233. constructor TPECoffexeoutput.create;
  2234. begin
  2235. inherited createcoff(true);
  2236. CExeSection:=TExeSection;
  2237. CObjData:=TPECoffObjData;
  2238. end;
  2239. procedure TPECoffexeoutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2240. var
  2241. textobjsection,
  2242. idata2objsection,
  2243. idata4objsection,
  2244. idata5objsection,
  2245. idata6objsection,
  2246. idata7objsection : TObjSection;
  2247. basedllname : string;
  2248. procedure StartImport(const dllname:string);
  2249. begin
  2250. if assigned(exemap) then
  2251. begin
  2252. exemap.Add('');
  2253. exemap.Add('Importing from DLL '+dllname);
  2254. end;
  2255. basedllname:=ExtractFileName(dllname);
  2256. idata2objsection:=internalobjdata.createsection(sec_idata2,basedllname);
  2257. idata4objsection:=internalobjdata.createsection(sec_idata4,basedllname);
  2258. idata5objsection:=internalobjdata.createsection(sec_idata5,basedllname);
  2259. idata7objsection:=internalobjdata.createsection(sec_idata7,basedllname);
  2260. { idata2 }
  2261. idata2objsection.writereloc_internal(idata4objsection,0,sizeof(longint),RELOC_RVA);
  2262. idata2objsection.writezeros(2*sizeof(longint));
  2263. idata2objsection.writereloc_internal(idata7objsection,0,sizeof(longint),RELOC_RVA);
  2264. idata2objsection.writereloc_internal(idata5objsection,0,sizeof(longint),RELOC_RVA);
  2265. { idata7 }
  2266. idata7objsection.writestr(basedllname);
  2267. end;
  2268. procedure EndImport;
  2269. begin
  2270. { These are referenced from idata2, oso_keep is not necessary. }
  2271. idata4objsection:=internalobjdata.createsection(sec_idata4, basedllname+'_z_');
  2272. idata5objsection:=internalobjdata.createsection(sec_idata5, basedllname+'_z_');
  2273. idata2objsection.writereloc_internal(idata4objsection,0,0,RELOC_NONE);
  2274. idata2objsection.writereloc_internal(idata5objsection,0,0,RELOC_NONE);
  2275. { idata4 }
  2276. idata4objsection.writezeros(sizeof(longint));
  2277. if target_info.system=system_x86_64_win64 then
  2278. idata4objsection.writezeros(sizeof(longint));
  2279. { idata5 }
  2280. idata5objsection.writezeros(sizeof(longint));
  2281. if target_info.system=system_x86_64_win64 then
  2282. idata5objsection.writezeros(sizeof(longint));
  2283. end;
  2284. function AddImport(const afuncname,amangledname:string; AOrdNr:longint;isvar:boolean):TObjSymbol;
  2285. const
  2286. {$ifdef arm}
  2287. jmpopcode : array[0..7] of byte = (
  2288. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  2289. $00,$f0,$9c,$e5 // ldr pc, [ip]
  2290. );
  2291. {$else arm}
  2292. jmpopcode : array[0..1] of byte = (
  2293. $ff,$25
  2294. );
  2295. {$endif arm}
  2296. nopopcodes : array[0..1] of byte = (
  2297. $90,$90
  2298. );
  2299. var
  2300. secname: string;
  2301. procedure WriteTableEntry(objsec:TObjSection);
  2302. var
  2303. ordint: dword;
  2304. begin
  2305. if AOrdNr <= 0 then
  2306. begin
  2307. { import by name }
  2308. if idata6objsection=nil then
  2309. begin
  2310. idata6objsection:=internalobjdata.createsection(sec_idata6,secname);
  2311. ordint:=Abs(AOrdNr);
  2312. { index hint, function name, null terminator and align }
  2313. idata6objsection.write(ordint,2);
  2314. idata6objsection.writestr(afuncname);
  2315. idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
  2316. end;
  2317. objsec.writereloc_internal(idata6objsection,0,sizeof(longint),RELOC_RVA);
  2318. if target_info.system=system_x86_64_win64 then
  2319. objsec.writezeros(sizeof(longint));
  2320. end
  2321. else
  2322. begin
  2323. { import by ordinal }
  2324. ordint:=AOrdNr;
  2325. if target_info.system=system_x86_64_win64 then
  2326. begin
  2327. objsec.write(ordint,sizeof(ordint));
  2328. ordint:=$80000000;
  2329. objsec.write(ordint,sizeof(ordint));
  2330. end
  2331. else
  2332. begin
  2333. ordint:=ordint or $80000000;
  2334. objsec.write(ordint,sizeof(ordint));
  2335. end;
  2336. end;
  2337. end;
  2338. begin
  2339. result:=nil;
  2340. textobjsection:=nil;
  2341. idata6objsection:=nil;
  2342. if assigned(exemap) then
  2343. begin
  2344. if AOrdNr <= 0 then
  2345. exemap.Add(' Importing Function '+afuncname)
  2346. else
  2347. exemap.Add(' Importing Function '+afuncname+' (OrdNr='+tostr(AOrdNr)+')');
  2348. end;
  2349. secname:=basedllname+'_i_'+amangledname;
  2350. idata4objsection:=internalobjdata.createsection(sec_idata4, secname);
  2351. idata5objsection:=internalobjdata.createsection(sec_idata5, secname);
  2352. { idata4, import lookup table }
  2353. WriteTableEntry(idata4objsection);
  2354. { idata5, import address table }
  2355. internalobjdata.SetSection(idata5objsection);
  2356. { dummy back links }
  2357. idata5objsection.writereloc_internal(idata4objsection,0,0,RELOC_NONE);
  2358. idata5objsection.writereloc_internal(idata2objsection,0,0,RELOC_NONE);
  2359. if isvar then
  2360. result:=internalobjdata.SymbolDefine(amangledname,AB_GLOBAL,AT_DATA)
  2361. else
  2362. begin
  2363. textobjsection:=internalobjdata.createsection(internalobjdata.sectionname(sec_code,secname,secorder_default),current_settings.alignment.procalign,
  2364. internalobjdata.sectiontype2options(sec_code) - [oso_keep]);
  2365. result:=internalobjdata.SymbolDefine('_'+amangledname,AB_GLOBAL,AT_FUNCTION);
  2366. textobjsection.write(jmpopcode,sizeof(jmpopcode));
  2367. {$ifdef x86_64}
  2368. textobjsection.writereloc_internal(idata5objsection,0,4,RELOC_RELATIVE);
  2369. {$else}
  2370. textobjsection.writereloc_internal(idata5objsection,0,4,RELOC_ABSOLUTE32);
  2371. {$endif x86_64}
  2372. textobjsection.write(nopopcodes,align(textobjsection.size,sizeof(nopopcodes))-textobjsection.size);
  2373. end;
  2374. { idata5 section data }
  2375. WriteTableEntry(idata5objsection);
  2376. end;
  2377. var
  2378. i,j : longint;
  2379. ImportLibrary : TImportLibrary;
  2380. ImportSymbol : TImportSymbol;
  2381. exesym : TExeSymbol;
  2382. begin
  2383. for i:=0 to ImportLibraryList.Count-1 do
  2384. begin
  2385. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  2386. idata2objsection:=nil;
  2387. idata4objsection:=nil;
  2388. idata5objsection:=nil;
  2389. idata6objsection:=nil;
  2390. idata7objsection:=nil;
  2391. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  2392. begin
  2393. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  2394. exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
  2395. if assigned(exesym) and
  2396. (exesym.State<>symstate_defined) then
  2397. begin
  2398. if not assigned(idata2objsection) then
  2399. StartImport(ImportLibrary.Name);
  2400. exesym.objsymbol:=AddImport(ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  2401. exesym.State:=symstate_defined;
  2402. end;
  2403. end;
  2404. if assigned(idata2objsection) then
  2405. EndImport;
  2406. end;
  2407. PackUnresolvedExeSymbols('after DLL imports');
  2408. end;
  2409. procedure TPECoffexeoutput.GenerateRelocs;
  2410. var
  2411. pgaddr, hdrpos : longword;
  2412. procedure FinishBlock;
  2413. var
  2414. p,len : longint;
  2415. begin
  2416. if hdrpos = longword(-1) then
  2417. exit;
  2418. p:=0;
  2419. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  2420. p:=internalObjData.CurrObjSec.Data.Pos;
  2421. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  2422. len:=p-hdrpos;
  2423. internalObjData.CurrObjSec.Data.write(len,4);
  2424. internalObjData.CurrObjSec.Data.seek(p);
  2425. hdrpos:=longword(-1);
  2426. end;
  2427. var
  2428. exesec : TExeSection;
  2429. objsec : TObjSection;
  2430. objreloc : TObjRelocation;
  2431. i,j,k : longint;
  2432. offset : longword;
  2433. w: word;
  2434. begin
  2435. if not RelocSection or FRelocsGenerated then
  2436. exit;
  2437. exesec:=FindExeSection('.reloc');
  2438. if exesec=nil then
  2439. exit;
  2440. objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_load,oso_keep]);
  2441. exesec.AddObjSection(objsec);
  2442. pgaddr:=longword(-1);
  2443. hdrpos:=longword(-1);
  2444. for i:=0 to ExeSectionList.Count-1 do
  2445. begin
  2446. exesec:=TExeSection(ExeSectionList[i]);
  2447. for j:=0 to exesec.ObjSectionList.count-1 do
  2448. begin
  2449. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2450. { create relocs only for sections which are loaded in memory }
  2451. if not (oso_load in objsec.SecOptions) then
  2452. continue;
  2453. for k:=0 to objsec.ObjRelocations.Count-1 do
  2454. begin
  2455. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  2456. if not (objreloc.typ in [{$ifdef cpu64bitaddr}RELOC_ABSOLUTE32,{$endif cpu64bitaddr}RELOC_ABSOLUTE]) then
  2457. continue;
  2458. offset:=objsec.MemPos+objreloc.dataoffset;
  2459. if (offset<pgaddr) and (pgaddr<>longword(-1)) then
  2460. Internalerror(2007062701);
  2461. if (offset-pgaddr>=4096) or (pgaddr=longword(-1)) then
  2462. begin
  2463. FinishBlock;
  2464. pgaddr:=(offset div 4096)*4096;
  2465. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  2466. internalObjData.writebytes(pgaddr,4);
  2467. { Reserving space for block size. The size will be written later in FinishBlock }
  2468. internalObjData.writebytes(k,4);
  2469. end;
  2470. {$ifdef cpu64bitaddr}
  2471. if objreloc.typ = RELOC_ABSOLUTE then
  2472. w:=IMAGE_REL_BASED_DIR64
  2473. else
  2474. {$endif cpu64bitaddr}
  2475. w:=IMAGE_REL_BASED_HIGHLOW;
  2476. w:=(w shl 12) or (offset-pgaddr);
  2477. internalObjData.writebytes(w,2);
  2478. end;
  2479. end;
  2480. end;
  2481. FinishBlock;
  2482. FRelocsGenerated:=true;
  2483. end;
  2484. procedure TPECoffexeoutput.MemPos_Start;
  2485. var
  2486. exesec : TExeSection;
  2487. begin
  2488. if RelocSection then
  2489. begin
  2490. exesec:=FindExeSection('.reloc');
  2491. if exesec=nil then
  2492. InternalError(2012072401);
  2493. exesec.SecOptions:=exesec.SecOptions-[oso_disabled];
  2494. end;
  2495. inherited;
  2496. end;
  2497. procedure TPECoffexeoutput.MemPos_ExeSection(const aname:string);
  2498. begin
  2499. if aname='.reloc' then
  2500. GenerateRelocs;
  2501. inherited;
  2502. end;
  2503. {****************************************************************************
  2504. TDJCoffAssembler
  2505. ****************************************************************************}
  2506. constructor TDJCoffAssembler.Create(smart:boolean);
  2507. begin
  2508. inherited Create(smart);
  2509. CObjOutput:=TDJCoffObjOutput;
  2510. end;
  2511. {****************************************************************************
  2512. TPECoffAssembler
  2513. ****************************************************************************}
  2514. constructor TPECoffAssembler.Create(smart:boolean);
  2515. begin
  2516. inherited Create(smart);
  2517. CObjOutput:=TPECoffObjOutput;
  2518. end;
  2519. {*****************************************************************************
  2520. DLLReader
  2521. *****************************************************************************}
  2522. {$ifdef win32}
  2523. var
  2524. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  2525. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  2526. {$endif win32}
  2527. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  2528. type
  2529. TPECoffExpDir=packed record
  2530. flag,
  2531. stamp : cardinal;
  2532. Major,
  2533. Minor : word;
  2534. Name,
  2535. Base,
  2536. NumFuncs,
  2537. NumNames,
  2538. AddrFuncs,
  2539. AddrNames,
  2540. AddrOrds : cardinal;
  2541. end;
  2542. var
  2543. DLLReader : TObjectReader;
  2544. DosHeader : array[0..$7f] of byte;
  2545. PEMagic : array[0..3] of byte;
  2546. Header : TCoffHeader;
  2547. peheader : tcoffpeoptheader;
  2548. NameOfs,
  2549. newheaderofs : longword;
  2550. FuncName : string;
  2551. expdir : TPECoffExpDir;
  2552. i : longint;
  2553. found : boolean;
  2554. sechdr : tCoffSecHdr;
  2555. {$ifdef win32}
  2556. p : pointer;
  2557. {$endif win32}
  2558. begin
  2559. result:=false;
  2560. {$ifdef win32}
  2561. if (target_info.system=system_x86_64_win64) and
  2562. assigned(Wow64DisableWow64FsRedirection) then
  2563. Wow64DisableWow64FsRedirection(p);
  2564. {$endif win32}
  2565. DLLReader:=TObjectReader.Create;
  2566. DLLReader.OpenFile(dllname);
  2567. {$ifdef win32}
  2568. if (target_info.system=system_x86_64_win64) and
  2569. assigned(Wow64RevertWow64FsRedirection) then
  2570. Wow64RevertWow64FsRedirection(p);
  2571. {$endif win32}
  2572. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  2573. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  2574. begin
  2575. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  2576. exit;
  2577. end;
  2578. newheaderofs:=cardinal(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  2579. DLLReader.Seek(newheaderofs);
  2580. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  2581. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  2582. begin
  2583. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  2584. exit;
  2585. end;
  2586. if not DLLReader.Read(Header,sizeof(TCoffHeader)) or
  2587. (Header.mach<>COFF_MAGIC) or
  2588. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  2589. begin
  2590. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  2591. exit;
  2592. end;
  2593. { Read optheader }
  2594. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  2595. { Section headers }
  2596. found:=false;
  2597. for i:=1 to header.nsects do
  2598. begin
  2599. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  2600. begin
  2601. Comment(V_Error,'Error reading coff file '+DLLName);
  2602. exit;
  2603. end;
  2604. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  2605. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  2606. begin
  2607. found:=true;
  2608. break;
  2609. end;
  2610. end;
  2611. if not found then
  2612. begin
  2613. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  2614. exit;
  2615. end;
  2616. { Process edata }
  2617. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  2618. DLLReader.Read(expdir,sizeof(expdir));
  2619. for i:=0 to expdir.NumNames-1 do
  2620. begin
  2621. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  2622. DLLReader.Read(NameOfs,4);
  2623. Dec(NameOfs,sechdr.rvaofs);
  2624. if {(NameOfs<0) or}
  2625. (NameOfs>sechdr.vsize) then
  2626. begin
  2627. Comment(V_Error,'DLL does contains invalid exports');
  2628. break;
  2629. end;
  2630. { Read Function name from DLL, prepend _ and terminate with #0 }
  2631. DLLReader.Seek(sechdr.datapos+NameOfs);
  2632. DLLReader.Read((@FuncName[1])^,sizeof(FuncName)-3);
  2633. FuncName[sizeof(FuncName)-1]:=#0;
  2634. FuncName[0]:=chr(Strlen(@FuncName[1]));
  2635. readdllproc(DLLName,FuncName);
  2636. end;
  2637. DLLReader.Free;
  2638. end;
  2639. {*****************************************************************************
  2640. Initialize
  2641. *****************************************************************************}
  2642. {$ifdef i386}
  2643. const
  2644. as_i386_coff_info : tasminfo =
  2645. (
  2646. id : as_i386_coff;
  2647. idtxt : 'COFF';
  2648. asmbin : '';
  2649. asmcmd : '';
  2650. supported_targets : [system_i386_go32v2];
  2651. flags : [af_outputbinary,af_smartlink_sections];
  2652. labelprefix : '.L';
  2653. comment : '';
  2654. dollarsign: '$';
  2655. );
  2656. as_i386_pecoff_info : tasminfo =
  2657. (
  2658. id : as_i386_pecoff;
  2659. idtxt : 'PECOFF';
  2660. asmbin : '';
  2661. asmcmd : '';
  2662. supported_targets : [system_i386_win32,system_i386_nativent];
  2663. flags : [af_outputbinary,af_smartlink_sections];
  2664. labelprefix : '.L';
  2665. comment : '';
  2666. dollarsign: '$';
  2667. );
  2668. as_i386_pecoffwdosx_info : tasminfo =
  2669. (
  2670. id : as_i386_pecoffwdosx;
  2671. idtxt : 'PEWDOSX';
  2672. asmbin : '';
  2673. asmcmd : '';
  2674. supported_targets : [system_i386_wdosx];
  2675. flags : [af_outputbinary];
  2676. labelprefix : '.L';
  2677. comment : '';
  2678. dollarsign: '$';
  2679. );
  2680. as_i386_pecoffwince_info : tasminfo =
  2681. (
  2682. id : as_i386_pecoffwince;
  2683. idtxt : 'PECOFFWINCE';
  2684. asmbin : '';
  2685. asmcmd : '';
  2686. supported_targets : [system_i386_wince];
  2687. flags : [af_outputbinary,af_smartlink_sections];
  2688. labelprefix : '.L';
  2689. comment : '';
  2690. dollarsign: '$';
  2691. );
  2692. {$endif i386}
  2693. {$ifdef x86_64}
  2694. const
  2695. as_x86_64_pecoff_info : tasminfo =
  2696. (
  2697. id : as_x86_64_pecoff;
  2698. idtxt : 'PECOFF';
  2699. asmbin : '';
  2700. asmcmd : '';
  2701. supported_targets : [system_x86_64_win64];
  2702. flags : [af_outputbinary,af_smartlink_sections];
  2703. labelprefix : '.L';
  2704. comment : '';
  2705. dollarsign: '$';
  2706. );
  2707. {$endif x86_64}
  2708. {$ifdef arm}
  2709. const
  2710. as_arm_pecoffwince_info : tasminfo =
  2711. (
  2712. id : as_arm_pecoffwince;
  2713. idtxt : 'PECOFFWINCE';
  2714. asmbin : '';
  2715. asmcmd : '';
  2716. supported_targets : [system_arm_wince];
  2717. flags : [af_outputbinary];
  2718. labelprefix : '.L';
  2719. comment : '';
  2720. dollarsign: '$';
  2721. );
  2722. {$endif arm}
  2723. {$ifdef win32}
  2724. procedure SetupProcVars;
  2725. var
  2726. hinstLib : THandle;
  2727. begin
  2728. Wow64DisableWow64FsRedirection:=nil;
  2729. Wow64RevertWow64FsRedirection:=nil;
  2730. hinstLib:=LoadLibrary('kernel32.dll');
  2731. if hinstLib<>0 then
  2732. begin
  2733. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  2734. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  2735. FreeLibrary(hinstLib);
  2736. end;
  2737. end;
  2738. {$endif win32}
  2739. initialization
  2740. {$ifdef i386}
  2741. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  2742. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  2743. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  2744. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  2745. {$endif i386}
  2746. {$ifdef x86_64}
  2747. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  2748. {$endif x86_64}
  2749. {$ifdef arm}
  2750. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  2751. {$endif arm}
  2752. {$ifdef win32}
  2753. SetupProcVars;
  2754. {$endif win32}
  2755. end.