ogcoff.pas 106 KB

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