ogcoff.pas 110 KB

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