ogcoff.pas 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  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. end;
  116. TDJCoffObjData = class(TCoffObjData)
  117. constructor create(const n:string);override;
  118. end;
  119. TPECoffObjData = class(TCoffObjData)
  120. constructor create(const n:string);override;
  121. end;
  122. TCoffObjOutput = class(tObjOutput)
  123. private
  124. win32 : boolean;
  125. symidx : longint;
  126. FCoffSyms,
  127. FCoffStrs : tdynamicarray;
  128. procedure write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  129. procedure section_write_symbol(p:TObject;arg:pointer);
  130. procedure section_write_relocs(p:TObject;arg:pointer);
  131. procedure create_symbols(data:TObjData);
  132. procedure section_set_reloc_datapos(p:TObject;arg:pointer);
  133. procedure section_write_header(p:TObject;arg:pointer);
  134. protected
  135. function writedata(data:TObjData):boolean;override;
  136. public
  137. constructor createcoff(AWriter:TObjectWriter;awin32:boolean);
  138. destructor destroy;override;
  139. end;
  140. TDJCoffObjOutput = class(TCoffObjOutput)
  141. constructor create(AWriter:TObjectWriter);override;
  142. end;
  143. TPECoffObjOutput = class(TCoffObjOutput)
  144. constructor create(AWriter:TObjectWriter);override;
  145. end;
  146. TCoffObjInput = class(tObjInput)
  147. private
  148. FCoffsyms : tdynamicarray;
  149. FCoffStrs : PChar;
  150. FCoffStrSize: longword;
  151. { Convert symidx -> TObjSymbol }
  152. FSymTbl : ^TObjSymbolArray;
  153. { Convert secidx -> TObjSection }
  154. FSecCount : smallint;
  155. FSecTbl : ^TObjSectionArray;
  156. win32 : boolean;
  157. function GetSection(secidx:longint):TObjSection;
  158. function Read_str(strpos:longword):string;
  159. procedure read_relocs(s:TCoffObjSection);
  160. procedure read_symbols(objdata:TObjData);
  161. procedure ObjSections_read_relocs(p:TObject;arg:pointer);
  162. public
  163. constructor createcoff(awin32:boolean);
  164. destructor destroy;override;
  165. function ReadObjData(AReader:TObjectreader;objdata:TObjData):boolean;override;
  166. end;
  167. TDJCoffObjInput = class(TCoffObjInput)
  168. constructor create;override;
  169. end;
  170. TPECoffObjInput = class(TCoffObjInput)
  171. constructor create;override;
  172. end;
  173. TCoffexeoutput = class(texeoutput)
  174. private
  175. FCoffStrs : tdynamicarray;
  176. win32 : boolean;
  177. nsects : word;
  178. nsyms,
  179. sympos : aword;
  180. datapos_offset: longword;
  181. function totalheadersize:longword;
  182. procedure ExeSectionList_pass2_header(p:TObject;arg:pointer);
  183. procedure write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  184. procedure globalsyms_write_symbol(p:TObject;arg:pointer);
  185. procedure ExeSectionList_write_header(p:TObject;arg:pointer);
  186. protected
  187. function writedata:boolean;override;
  188. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList;const aPattern:string);override;
  189. public
  190. constructor createcoff(awin32:boolean);
  191. procedure MemPos_Header;override;
  192. procedure DataPos_Header;override;
  193. procedure DataPos_Symbols;override;
  194. end;
  195. TDJCoffexeoutput = class(TCoffexeoutput)
  196. constructor create;override;
  197. procedure MemPos_Header;override;
  198. end;
  199. TPECoffexeoutput = class(TCoffexeoutput)
  200. private
  201. idatalabnr : longword;
  202. FRelocsGenerated : boolean;
  203. procedure GenerateRelocs;
  204. public
  205. constructor create;override;
  206. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  207. procedure Order_End;override;
  208. procedure MemPos_ExeSection(const aname:string);override;
  209. end;
  210. TObjSymbolArray = array[0..high(word)] of TObjSymbol;
  211. TObjSectionArray = array[0..high(smallint)] of TObjSection;
  212. TDJCoffAssembler = class(tinternalassembler)
  213. constructor create(smart:boolean);override;
  214. end;
  215. TPECoffassembler = class(tinternalassembler)
  216. constructor create(smart:boolean);override;
  217. end;
  218. type
  219. Treaddllproc = procedure(const dllname,funcname:string) of object;
  220. const
  221. {$ifdef i386}
  222. COFF_MAGIC = $14c;
  223. COFF_OPT_MAGIC = $10b;
  224. TLSDIR_SIZE = $18;
  225. {$endif i386}
  226. {$ifdef arm}
  227. COFF_MAGIC = $1c0;
  228. COFF_OPT_MAGIC = $10b;
  229. TLSDIR_SIZE = $18;
  230. {$endif arm}
  231. {$ifdef x86_64}
  232. COFF_MAGIC = $8664;
  233. COFF_OPT_MAGIC = $20b;
  234. TLSDIR_SIZE = $28;
  235. {$endif x86_64}
  236. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  237. implementation
  238. uses
  239. {$ifdef win32}
  240. Windows,
  241. {$endif win32}
  242. SysUtils,
  243. cutils,verbose,globals,
  244. fmodule,aasmtai,aasmdata,
  245. ogmap,
  246. version
  247. ;
  248. const
  249. COFF_FLAG_NORELOCS = $0001;
  250. COFF_FLAG_EXE = $0002;
  251. COFF_FLAG_NOLINES = $0004;
  252. COFF_FLAG_NOLSYMS = $0008;
  253. COFF_FLAG_AR16WR = $0080; { 16bit little endian }
  254. COFF_FLAG_AR32WR = $0100; { 32bit little endian }
  255. COFF_FLAG_AR32W = $0200; { 32bit big endian }
  256. COFF_FLAG_DLL = $2000;
  257. COFF_SYM_GLOBAL = 2;
  258. COFF_SYM_LOCAL = 3;
  259. COFF_SYM_LABEL = 6;
  260. COFF_SYM_FUNCTION = 101;
  261. COFF_SYM_FILE = 103;
  262. COFF_SYM_SECTION = 104;
  263. COFF_STYP_REG = $0000; { "regular": allocated, relocated, loaded }
  264. COFF_STYP_DSECT = $0001; { "dummy": relocated only }
  265. COFF_STYP_NOLOAD = $0002; { "noload": allocated, relocated, not loaded }
  266. COFF_STYP_GROUP = $0004; { "grouped": formed of input sections }
  267. COFF_STYP_PAD = $0008;
  268. COFF_STYP_COPY = $0010;
  269. COFF_STYP_TEXT = $0020;
  270. COFF_STYP_DATA = $0040;
  271. COFF_STYP_BSS = $0080;
  272. COFF_STYP_INFO = $0200;
  273. COFF_STYP_OVER = $0400;
  274. COFF_STYP_LIB = $0800;
  275. PE_SUBSYSTEM_NATIVE = 1;
  276. PE_SUBSYSTEM_WINDOWS_GUI = 2;
  277. PE_SUBSYSTEM_WINDOWS_CUI = 3;
  278. PE_SUBSYSTEM_WINDOWS_CE_GUI = 9;
  279. PE_FILE_RELOCS_STRIPPED = $0001;
  280. PE_FILE_EXECUTABLE_IMAGE = $0002;
  281. PE_FILE_LINE_NUMS_STRIPPED = $0004;
  282. PE_FILE_LOCAL_SYMS_STRIPPED = $0008;
  283. PE_FILE_AGGRESSIVE_WS_TRIM = $0010;
  284. PE_FILE_LARGE_ADDRESS_AWARE = $0020;
  285. PE_FILE_16BIT_MACHINE = $0040;
  286. PE_FILE_BYTES_REVERSED_LO = $0080;
  287. PE_FILE_32BIT_MACHINE = $0100;
  288. PE_FILE_DEBUG_STRIPPED = $0200;
  289. PE_FILE_REMOVABLE_RUN_FROM_SWAP = $0400;
  290. PE_FILE_NET_RUN_FROM_SWAP = $0800;
  291. PE_FILE_SYSTEM = $1000;
  292. PE_FILE_DLL = $2000;
  293. PE_FILE_UP_SYSTEM_ONLY = $4000;
  294. PE_FILE_BYTES_REVERSED_HI = $8000;
  295. PE_SCN_CNT_CODE = $00000020; { Section contains code. }
  296. PE_SCN_CNT_INITIALIZED_DATA = $00000040; { Section contains initialized data. }
  297. PE_SCN_CNT_UNINITIALIZED_DATA = $00000080; { Section contains uninitialized data. }
  298. PE_SCN_LNK_OTHER = $00000100; { Reserved. }
  299. PE_SCN_LNK_INFO = $00000200; { Section contains comments or some other type of information. }
  300. PE_SCN_LNK_REMOVE = $00000800; { Section contents will not become part of image. }
  301. PE_SCN_LNK_COMDAT = $00001000; { Section contents comdat. }
  302. PE_SCN_MEM_FARDATA = $00008000;
  303. PE_SCN_MEM_PURGEABLE = $00020000;
  304. PE_SCN_MEM_16BIT = $00020000;
  305. PE_SCN_MEM_LOCKED = $00040000;
  306. PE_SCN_MEM_PRELOAD = $00080000;
  307. PE_SCN_ALIGN_MASK = $00f00000;
  308. PE_SCN_ALIGN_1BYTES = $00100000;
  309. PE_SCN_ALIGN_2BYTES = $00200000;
  310. PE_SCN_ALIGN_4BYTES = $00300000;
  311. PE_SCN_ALIGN_8BYTES = $00400000;
  312. PE_SCN_ALIGN_16BYTES = $00500000; { Default alignment if no others are specified. }
  313. PE_SCN_ALIGN_32BYTES = $00600000;
  314. PE_SCN_ALIGN_64BYTES = $00700000;
  315. PE_SCN_LNK_NRELOC_OVFL = $01000000; { Section contains extended relocations. }
  316. PE_SCN_MEM_NOT_CACHED = $04000000; { Section is not cachable. }
  317. PE_SCN_MEM_NOT_PAGED = $08000000; { Section is not pageable. }
  318. PE_SCN_MEM_SHARED = $10000000; { Section is shareable. }
  319. PE_SCN_MEM_DISCARDABLE = $02000000;
  320. PE_SCN_MEM_EXECUTE = $20000000;
  321. PE_SCN_MEM_READ = $40000000;
  322. PE_SCN_MEM_WRITE = $80000000;
  323. PE_DATADIR_EDATA = 0;
  324. PE_DATADIR_IDATA = 1;
  325. PE_DATADIR_RSRC = 2;
  326. PE_DATADIR_PDATA = 3;
  327. PE_DATADIR_SECURITY = 4;
  328. PE_DATADIR_RELOC = 5;
  329. PE_DATADIR_DEBUG = 6;
  330. PE_DATADIR_DESCRIPTION = 7;
  331. PE_DATADIR_SPECIAL = 8;
  332. PE_DATADIR_TLS = 9;
  333. PE_DATADIR_LOADCFG = 10;
  334. PE_DATADIR_BOUNDIMPORT = 11;
  335. PE_DATADIR_IMPORTADDRESSTABLE = 12;
  336. PE_DATADIR_DELAYIMPORT = 13;
  337. {$ifdef x86_64}
  338. IMAGE_REL_AMD64_ABSOLUTE = $0000; { Reference is absolute, no relocation is necessary }
  339. IMAGE_REL_AMD64_ADDR64 = $0001; { 64-bit address (VA). }
  340. IMAGE_REL_AMD64_ADDR32 = $0002; { 32-bit address (VA). }
  341. IMAGE_REL_AMD64_ADDR32NB = $0003; { 32-bit address w/o image base (RVA). }
  342. IMAGE_REL_AMD64_REL32 = $0004; { 32-bit relative address from byte following reloc }
  343. IMAGE_REL_AMD64_REL32_1 = $0005; { 32-bit relative address from byte distance 1 from reloc }
  344. IMAGE_REL_AMD64_REL32_2 = $0006; { 32-bit relative address from byte distance 2 from reloc }
  345. IMAGE_REL_AMD64_REL32_3 = $0007; { 32-bit relative address from byte distance 3 from reloc }
  346. IMAGE_REL_AMD64_REL32_4 = $0008; { 32-bit relative address from byte distance 4 from reloc }
  347. IMAGE_REL_AMD64_REL32_5 = $0009; { 32-bit relative address from byte distance 5 from reloc }
  348. IMAGE_REL_AMD64_SECTION = $000A; { Section index }
  349. IMAGE_REL_AMD64_SECREL = $000B; { 32 bit offset from base of section containing target }
  350. IMAGE_REL_AMD64_SECREL7 = $000C; { 7 bit unsigned offset from base of section containing target }
  351. IMAGE_REL_AMD64_TOKEN = $000D; { 32 bit metadata token }
  352. IMAGE_REL_AMD64_SREL32 = $000E; { 32 bit signed span-dependent value emitted into object }
  353. IMAGE_REL_AMD64_PAIR = $000F;
  354. IMAGE_REL_AMD64_SSPAN32 = $0010; { 32 bit signed span-dependent value applied at link time }
  355. { Direct 32 bit sign extended,
  356. win64 mingw GNU compiler
  357. also generates this type
  358. inside coff objects
  359. We assume they are equivalent to
  360. IMAGE_REL_AMD64_ADDR32 PM 2010-11-27 }
  361. R_X86_64_32S = $11;
  362. {$endif x86_64}
  363. {$ifdef arm}
  364. IMAGE_REL_ARM_ABSOLUTE = $0000; { No relocation required }
  365. IMAGE_REL_ARM_ADDR32 = $0001; { 32 bit address }
  366. IMAGE_REL_ARM_ADDR32NB = $0002; { 32 bit address w/o image base }
  367. IMAGE_REL_ARM_BRANCH24 = $0003; { 24 bit offset << 2 & sign ext. }
  368. IMAGE_REL_ARM_BRANCH11 = $0004; { Thumb: 2 11 bit offsets }
  369. IMAGE_REL_ARM_TOKEN = $0005; { clr token }
  370. IMAGE_REL_ARM_GPREL12 = $0006; { GP-relative addressing (ARM) }
  371. IMAGE_REL_ARM_GPREL7 = $0007; { GP-relative addressing (Thumb) }
  372. IMAGE_REL_ARM_BLX24 = $0008;
  373. IMAGE_REL_ARM_BLX11 = $0009;
  374. IMAGE_REL_ARM_SECTION = $000E; { Section table index }
  375. IMAGE_REL_ARM_SECREL = $000F; { Offset within section }
  376. {$endif arm}
  377. {$ifdef i386}
  378. IMAGE_REL_I386_DIR32 = 6;
  379. IMAGE_REL_I386_IMAGEBASE = 7;
  380. IMAGE_REL_I386_SECREL32 = 11;
  381. IMAGE_REL_I386_PCRLONG = 20;
  382. {$endif i386}
  383. { .reloc section fixup types }
  384. IMAGE_REL_BASED_HIGHLOW = 3; { Applies the delta to the 32-bit field at Offset. }
  385. IMAGE_REL_BASED_DIR64 = 10; { Applies the delta to the 64-bit field at Offset. }
  386. { values for coffsectionrec.select }
  387. IMAGE_COMDAT_SELECT_NODUPLICATES = 1;
  388. IMAGE_COMDAT_SELECT_ANY = 2;
  389. IMAGE_COMDAT_SELECT_SAME_SIZE = 3;
  390. IMAGE_COMDAT_SELECT_EXACT_MATCH = 4;
  391. IMAGE_COMDAT_SELECT_ASSOCIATIVE = 5;
  392. IMAGE_COMDAT_SELECT_LARGEST = 6;
  393. type
  394. coffdjoptheader=packed record
  395. magic : word;
  396. vstamp : word;
  397. tsize : longint;
  398. dsize : longint;
  399. bsize : longint;
  400. entry : longint;
  401. text_start : longint;
  402. data_start : longint;
  403. end;
  404. coffsectionrec=packed record
  405. len : longword;
  406. nrelocs : word;
  407. nlines : word;
  408. checksum: longword;
  409. assoc : word;
  410. select : byte;
  411. empty : array[0..2] of char;
  412. end;
  413. coffreloc=packed record
  414. address : longword;
  415. sym : longword;
  416. reloctype : word;
  417. end;
  418. coffsymbol=packed record
  419. name : array[0..3] of char; { real is [0..7], which overlaps the strpos ! }
  420. strpos : longword;
  421. value : longword;
  422. section : smallint;
  423. empty : word; { actually type, $20: function, 0: not a function }
  424. typ : byte;
  425. aux : byte;
  426. end;
  427. { This is defined in rtl/win/sysos.inc source }
  428. tlsdirectory=packed record
  429. data_start, data_end : PUInt;
  430. index_pointer, callbacks_pointer : PUInt;
  431. zero_fill_size : dword;
  432. flags : dword;
  433. end;
  434. const
  435. SymbolMaxGrow = 200*sizeof(coffsymbol);
  436. StrsMaxGrow = 8192;
  437. coffsecnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  438. '.text','.data','.data','.data','.bss','.tls',
  439. '.pdata',{pdata}
  440. '.text', {stub}
  441. '.data',
  442. '.data',
  443. '.data',
  444. '.data',
  445. '.stab','.stabstr',
  446. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  447. '.eh_frame',
  448. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  449. '.fpc',
  450. '.rtti',
  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. {****************************************************************************
  1046. TDJCoffObjData
  1047. ****************************************************************************}
  1048. constructor TDJCoffObjData.create(const n:string);
  1049. begin
  1050. inherited createcoff(n,false,TCoffObjSection);
  1051. end;
  1052. {****************************************************************************
  1053. TPECoffObjData
  1054. ****************************************************************************}
  1055. constructor TPECoffObjData.create(const n:string);
  1056. begin
  1057. inherited createcoff(n,true,TCoffObjSection);
  1058. end;
  1059. {****************************************************************************
  1060. TCoffObjOutput
  1061. ****************************************************************************}
  1062. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1063. begin
  1064. inherited create(AWriter);
  1065. win32:=awin32;
  1066. end;
  1067. destructor TCoffObjOutput.destroy;
  1068. begin
  1069. FCoffSyms.free;
  1070. FCoffStrs.free;
  1071. inherited destroy;
  1072. end;
  1073. procedure TCoffObjOutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  1074. var
  1075. sym : coffsymbol;
  1076. begin
  1077. FillChar(sym,sizeof(sym),0);
  1078. { symbolname }
  1079. if length(name)>8 then
  1080. begin
  1081. sym.strpos:=FCoffStrs.size+4;
  1082. FCoffStrs.writestr(name);
  1083. FCoffStrs.writestr(#0);
  1084. end
  1085. else
  1086. move(name[1],sym.name,length(name));
  1087. sym.value:=value;
  1088. sym.section:=section;
  1089. sym.typ:=typ;
  1090. sym.aux:=aux;
  1091. inc(symidx);
  1092. FCoffSyms.write(sym,sizeof(sym));
  1093. end;
  1094. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1095. var
  1096. secrec : coffsectionrec;
  1097. begin
  1098. with TCoffObjSection(p) do
  1099. begin
  1100. Inc(plongword(arg)^);
  1101. secidx:=plongword(arg)^;
  1102. secsymidx:=symidx;
  1103. { Both GNU and Microsoft toolchains write section symbols using
  1104. storage class 3 (STATIC).
  1105. No reason to use COFF_SYM_SECTION, it is silently converted to 3 by
  1106. PE binutils and causes warnings with DJGPP binutils. }
  1107. write_symbol(name,mempos,secidx,COFF_SYM_LOCAL,1);
  1108. { AUX }
  1109. fillchar(secrec,sizeof(secrec),0);
  1110. secrec.len:=Size;
  1111. secrec.nrelocs:=ObjRelocations.count;
  1112. inc(symidx);
  1113. FCoffSyms.write(secrec,sizeof(secrec));
  1114. end;
  1115. end;
  1116. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1117. var
  1118. i : longint;
  1119. rel : coffreloc;
  1120. objreloc : TObjRelocation;
  1121. begin
  1122. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1123. begin
  1124. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1125. rel.address:=objreloc.dataoffset;
  1126. if assigned(objreloc.symbol) then
  1127. begin
  1128. if (objreloc.symbol.bind=AB_LOCAL) then
  1129. rel.sym:=objreloc.symbol.objsection.secsymidx
  1130. else
  1131. begin
  1132. if objreloc.symbol.symidx=-1 then
  1133. internalerror(200602233);
  1134. rel.sym:=objreloc.symbol.symidx;
  1135. end;
  1136. end
  1137. else
  1138. begin
  1139. if objreloc.objsection<>nil then
  1140. rel.sym:=objreloc.objsection.secsymidx
  1141. else
  1142. rel.sym:=0;
  1143. end;
  1144. case objreloc.typ of
  1145. {$ifdef arm}
  1146. RELOC_ABSOLUTE :
  1147. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1148. { I've no idea if this is correct (FK):
  1149. RELOC_RELATIVE :
  1150. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1151. }
  1152. RELOC_RVA :
  1153. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1154. RELOC_SECREL32 :
  1155. rel.reloctype:=IMAGE_REL_ARM_SECREL;
  1156. {$endif arm}
  1157. {$ifdef i386}
  1158. RELOC_RELATIVE :
  1159. rel.reloctype:=IMAGE_REL_I386_PCRLONG;
  1160. RELOC_ABSOLUTE :
  1161. rel.reloctype:=IMAGE_REL_I386_DIR32;
  1162. RELOC_RVA :
  1163. rel.reloctype:=IMAGE_REL_I386_IMAGEBASE;
  1164. RELOC_SECREL32 :
  1165. rel.reloctype:=IMAGE_REL_I386_SECREL32;
  1166. {$endif i386}
  1167. {$ifdef x86_64}
  1168. RELOC_NONE :
  1169. rel.reloctype:=IMAGE_REL_AMD64_ABSOLUTE;
  1170. RELOC_RELATIVE :
  1171. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1172. RELOC_ABSOLUTE32 :
  1173. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1174. RELOC_ABSOLUTE :
  1175. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1176. RELOC_RVA :
  1177. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1178. RELOC_RELATIVE_1 :
  1179. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1180. RELOC_RELATIVE_2 :
  1181. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1182. RELOC_RELATIVE_3 :
  1183. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1184. RELOC_RELATIVE_4 :
  1185. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1186. RELOC_RELATIVE_5 :
  1187. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1188. RELOC_SECREL32 :
  1189. rel.reloctype:=IMAGE_REL_AMD64_SECREL;
  1190. {$endif x86_64}
  1191. else
  1192. internalerror(200905071);
  1193. end;
  1194. FWriter.write(rel,sizeof(rel));
  1195. end;
  1196. end;
  1197. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1198. var
  1199. filename : string[18];
  1200. sectionval : word;
  1201. globalval : byte;
  1202. i : longint;
  1203. value : aword;
  1204. objsym : TObjSymbol;
  1205. secidx : longword;
  1206. begin
  1207. with TCoffObjData(data) do
  1208. begin
  1209. symidx:=0;
  1210. { The `.file' record, and the file name auxiliary record }
  1211. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1212. fillchar(filename,sizeof(filename),0);
  1213. filename:=ExtractFileName(current_module.mainsource);
  1214. inc(symidx);
  1215. FCoffSyms.write(filename[1],sizeof(filename)-1);
  1216. { Sections }
  1217. secidx:=0;
  1218. ObjSectionList.ForEachCall(@section_write_symbol,@secidx);
  1219. { ObjSymbols }
  1220. for i:=0 to ObjSymbolList.Count-1 do
  1221. begin
  1222. objsym:=TObjSymbol(ObjSymbolList[i]);
  1223. if (objsym.bind=AB_LOCAL) then
  1224. continue;
  1225. case objsym.bind of
  1226. AB_GLOBAL :
  1227. begin
  1228. globalval:=COFF_SYM_GLOBAL;
  1229. sectionval:=TCoffObjSection(objsym.objsection).secidx;
  1230. value:=objsym.address;
  1231. end;
  1232. AB_LOCAL :
  1233. begin
  1234. globalval:=COFF_SYM_LOCAL;
  1235. sectionval:=TCoffObjSection(objsym.objsection).secidx;
  1236. value:=objsym.address;
  1237. end;
  1238. else
  1239. begin
  1240. globalval:=COFF_SYM_GLOBAL;
  1241. sectionval:=0;
  1242. value:=objsym.size;
  1243. end;
  1244. end;
  1245. { symbolname }
  1246. objsym.symidx:=symidx;
  1247. write_symbol(objsym.name,value,sectionval,globalval,0);
  1248. end;
  1249. end;
  1250. end;
  1251. procedure TCoffObjOutput.section_set_reloc_datapos(p:TObject;arg:pointer);
  1252. begin
  1253. TCoffObjSection(p).coffrelocpos:=paint(arg)^;
  1254. inc(paint(arg)^,sizeof(coffreloc)*TObjSection(p).ObjRelocations.count);
  1255. end;
  1256. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1257. var
  1258. sechdr : tcoffsechdr;
  1259. s : string;
  1260. strpos : aword;
  1261. begin
  1262. with TCoffObjSection(p) do
  1263. begin
  1264. fillchar(sechdr,sizeof(sechdr),0);
  1265. s:=name;
  1266. if length(s)>8 then
  1267. begin
  1268. strpos:=FCoffStrs.size+4;
  1269. FCoffStrs.writestr(s);
  1270. FCoffStrs.writestr(#0);
  1271. s:='/'+ToStr(strpos);
  1272. end;
  1273. move(s[1],sechdr.name,length(s));
  1274. if not win32 then
  1275. begin
  1276. sechdr.rvaofs:=mempos;
  1277. sechdr.vsize:=mempos;
  1278. end
  1279. else
  1280. begin
  1281. if not(oso_data in secoptions) then
  1282. sechdr.vsize:=Size;
  1283. end;
  1284. sechdr.DataSize:=size;
  1285. if (Size>0) and
  1286. (oso_data in secoptions) then
  1287. sechdr.datapos:=datapos;
  1288. sechdr.nrelocs:=ObjRelocations.count;
  1289. sechdr.relocpos:=coffrelocpos;
  1290. if win32 then
  1291. sechdr.flags:=peencodesechdrflags(secoptions,secalign)
  1292. else
  1293. sechdr.flags:=djencodesechdrflags(secoptions);
  1294. FWriter.write(sechdr,sizeof(sechdr));
  1295. end;
  1296. end;
  1297. function TCoffObjOutput.writedata(data:TObjData):boolean;
  1298. var
  1299. datapos,
  1300. sympos : aword;
  1301. i : longint;
  1302. header : tcoffheader;
  1303. begin
  1304. result:=false;
  1305. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  1306. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  1307. with TCoffObjData(data) do
  1308. begin
  1309. { Create Symbol Table }
  1310. create_symbols(data);
  1311. { Calculate the filepositions }
  1312. datapos:=sizeof(tcoffheader)+sizeof(tcoffsechdr)*ObjSectionList.Count;
  1313. { Sections first }
  1314. layoutsections(datapos);
  1315. { relocs }
  1316. ObjSectionList.ForEachCall(@section_set_reloc_datapos,@datapos);
  1317. { Symbols }
  1318. sympos:=datapos;
  1319. { Generate COFF header }
  1320. fillchar(header,sizeof(tcoffheader),0);
  1321. header.mach:=COFF_MAGIC;
  1322. header.nsects:=ObjSectionList.Count;
  1323. header.sympos:=sympos;
  1324. header.syms:=symidx;
  1325. if win32 then
  1326. begin
  1327. {$ifndef x86_64}
  1328. header.flag:=PE_FILE_32BIT_MACHINE or
  1329. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1330. {$else x86_64}
  1331. header.flag:=PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1332. {$endif x86_64}
  1333. end
  1334. else
  1335. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  1336. FWriter.write(header,sizeof(header));
  1337. { Section headers }
  1338. ObjSectionList.ForEachCall(@section_write_header,nil);
  1339. { ObjSections }
  1340. WriteSectionContent(data);
  1341. { Relocs }
  1342. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  1343. { ObjSymbols }
  1344. if Sympos<>FWriter.ObjSize then
  1345. internalerror(200603051);
  1346. FWriter.writearray(FCoffSyms);
  1347. { Strings }
  1348. i:=FCoffStrs.size+4;
  1349. FWriter.write(i,4);
  1350. FWriter.writearray(FCoffStrs);
  1351. end;
  1352. FCoffStrs.Free;
  1353. FCoffStrs:=nil;
  1354. FCoffSyms.Free;
  1355. FCoffSyms:=nil;
  1356. end;
  1357. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  1358. begin
  1359. inherited createcoff(AWriter,false);
  1360. cobjdata:=TDJCoffObjData;
  1361. end;
  1362. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  1363. begin
  1364. inherited createcoff(AWriter,true);
  1365. cobjdata:=TPECoffObjData;
  1366. end;
  1367. {****************************************************************************
  1368. TCoffObjInput
  1369. ****************************************************************************}
  1370. constructor TCoffObjInput.createcoff(awin32:boolean);
  1371. begin
  1372. inherited create;
  1373. win32:=awin32;
  1374. FSymTbl:=nil;
  1375. end;
  1376. destructor TCoffObjInput.destroy;
  1377. begin
  1378. FCoffSyms.free;
  1379. if assigned(FCoffStrs) then
  1380. freemem(FCoffStrs);
  1381. if assigned(FSymTbl) then
  1382. freemem(FSymTbl);
  1383. if assigned(FSecTbl) then
  1384. freemem(FSecTbl);
  1385. inherited destroy;
  1386. end;
  1387. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  1388. begin
  1389. result:=nil;
  1390. if (secidx<1) or (secidx>FSecCount) then
  1391. begin
  1392. InputError('Failed reading coff file, invalid section index');
  1393. exit;
  1394. end;
  1395. result:=FSecTbl^[secidx];
  1396. end;
  1397. function TCoffObjInput.Read_str(strpos:longword):string;
  1398. begin
  1399. if (FCoffStrs=nil) or (strpos>=FCoffStrSize) or (FCoffStrs[strpos]=#0) then
  1400. Internalerror(200205172);
  1401. result:=string(PChar(@FCoffStrs[strpos]));
  1402. end;
  1403. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  1404. var
  1405. rel : coffreloc;
  1406. rel_type : TObjRelocationType;
  1407. i : longint;
  1408. p : TObjSymbol;
  1409. begin
  1410. for i:=1 to s.coffrelocs do
  1411. begin
  1412. FReader.read(rel,sizeof(rel));
  1413. case rel.reloctype of
  1414. {$ifdef arm}
  1415. IMAGE_REL_ARM_ABSOLUTE:
  1416. rel_type:=RELOC_NONE;
  1417. IMAGE_REL_ARM_ADDR32:
  1418. rel_type:=RELOC_ABSOLUTE;
  1419. IMAGE_REL_ARM_ADDR32NB:
  1420. rel_type:=RELOC_RVA;
  1421. IMAGE_REL_ARM_BRANCH24:
  1422. rel_type:=RELOC_RELATIVE_24;
  1423. IMAGE_REL_ARM_SECREL:
  1424. rel_type:=RELOC_SECREL32;
  1425. {$endif arm}
  1426. {$ifdef i386}
  1427. IMAGE_REL_I386_PCRLONG :
  1428. rel_type:=RELOC_RELATIVE;
  1429. IMAGE_REL_I386_DIR32 :
  1430. rel_type:=RELOC_ABSOLUTE;
  1431. IMAGE_REL_I386_IMAGEBASE :
  1432. rel_type:=RELOC_RVA;
  1433. IMAGE_REL_I386_SECREL32 :
  1434. rel_type:=RELOC_SECREL32;
  1435. {$endif i386}
  1436. {$ifdef x86_64}
  1437. IMAGE_REL_AMD64_ABSOLUTE:
  1438. rel_type:=RELOC_NONE;
  1439. IMAGE_REL_AMD64_REL32:
  1440. rel_type:=RELOC_RELATIVE;
  1441. IMAGE_REL_AMD64_ADDR32,
  1442. R_X86_64_32S:
  1443. rel_type:=RELOC_ABSOLUTE32;
  1444. IMAGE_REL_AMD64_ADDR64:
  1445. rel_type:=RELOC_ABSOLUTE;
  1446. IMAGE_REL_AMD64_ADDR32NB:
  1447. rel_type:=RELOC_RVA;
  1448. IMAGE_REL_AMD64_REL32_1:
  1449. rel_type:=RELOC_RELATIVE_1;
  1450. IMAGE_REL_AMD64_REL32_2:
  1451. rel_type:=RELOC_RELATIVE_2;
  1452. IMAGE_REL_AMD64_REL32_3:
  1453. rel_type:=RELOC_RELATIVE_3;
  1454. IMAGE_REL_AMD64_REL32_4:
  1455. rel_type:=RELOC_RELATIVE_4;
  1456. IMAGE_REL_AMD64_REL32_5:
  1457. rel_type:=RELOC_RELATIVE_5;
  1458. IMAGE_REL_AMD64_SECREL:
  1459. rel_type:=RELOC_SECREL32;
  1460. {$endif x86_64}
  1461. else
  1462. begin
  1463. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  1464. exit;
  1465. end;
  1466. end;
  1467. p:=FSymTbl^[rel.sym];
  1468. if assigned(p) then
  1469. s.addsymsizereloc(rel.address-s.mempos,p,p.size,rel_type)
  1470. else
  1471. begin
  1472. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  1473. exit;
  1474. end;
  1475. end;
  1476. end;
  1477. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  1478. var
  1479. size,
  1480. address,
  1481. nsyms,
  1482. symidx : aint;
  1483. i : longint;
  1484. sym : coffsymbol;
  1485. objsym : TObjSymbol;
  1486. bind : Tasmsymbind;
  1487. strname : string;
  1488. auxrec : array[0..17] of byte;
  1489. objsec : TObjSection;
  1490. begin
  1491. with TCoffObjData(objdata) do
  1492. begin
  1493. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1494. { Allocate memory for symidx -> TObjSymbol table }
  1495. FSymTbl:=AllocMem(nsyms*sizeof(TObjSymbol));
  1496. { Load the Symbols }
  1497. FCoffSyms.Seek(0);
  1498. symidx:=0;
  1499. while (symidx<nsyms) do
  1500. begin
  1501. FCoffSyms.Read(sym,sizeof(sym));
  1502. if plongint(@sym.name)^<>0 then
  1503. begin
  1504. move(sym.name,strname[1],8);
  1505. strname[9]:=#0;
  1506. strname[0]:=chr(strlen(@strname[1]));
  1507. if strname='' then
  1508. Internalerror(200205171);
  1509. end
  1510. else
  1511. strname:=Read_str(sym.strpos);
  1512. bind:=AB_EXTERNAL;
  1513. size:=0;
  1514. address:=0;
  1515. objsym:=nil;
  1516. objsec:=nil;
  1517. case sym.typ of
  1518. COFF_SYM_GLOBAL :
  1519. begin
  1520. if sym.section=0 then
  1521. begin
  1522. if sym.value=0 then
  1523. bind:=AB_EXTERNAL
  1524. else
  1525. begin
  1526. bind:=AB_COMMON;
  1527. size:=sym.value;
  1528. end;
  1529. end
  1530. else
  1531. begin
  1532. bind:=AB_GLOBAL;
  1533. objsec:=GetSection(sym.section);
  1534. if sym.value>=objsec.mempos then
  1535. address:=sym.value-objsec.mempos;
  1536. end;
  1537. objsym:=CreateSymbol(strname);
  1538. objsym.bind:=bind;
  1539. objsym.typ:=AT_FUNCTION;
  1540. objsym.objsection:=objsec;
  1541. objsym.offset:=address;
  1542. objsym.size:=size;
  1543. end;
  1544. COFF_SYM_LABEL,
  1545. COFF_SYM_LOCAL :
  1546. begin
  1547. { do not add constants (section=-1) }
  1548. if sym.section<>-1 then
  1549. begin
  1550. objsec:=GetSection(sym.section);
  1551. if sym.value>=objsec.mempos then
  1552. address:=sym.value-objsec.mempos;
  1553. objsym:=CreateSymbol(strname);
  1554. objsym.bind:=AB_LOCAL;
  1555. objsym.typ:=AT_FUNCTION;
  1556. objsym.objsection:=objsec;
  1557. objsym.offset:=address;
  1558. objsym.size:=size;
  1559. end;
  1560. end;
  1561. COFF_SYM_SECTION :
  1562. begin
  1563. if sym.section=0 then
  1564. InputError('Failed reading coff file, illegal section');
  1565. objsec:=GetSection(sym.section);
  1566. if assigned(objsec) then
  1567. begin
  1568. if sym.value>=objsec.mempos then
  1569. address:=sym.value-objsec.mempos;
  1570. objsym:=CreateSymbol(strname);
  1571. objsym.bind:=AB_LOCAL;
  1572. objsym.typ:=AT_FUNCTION;
  1573. objsym.objsection:=objsec;
  1574. objsym.offset:=address;
  1575. objsym.size:=size;
  1576. end;
  1577. end;
  1578. COFF_SYM_FUNCTION,
  1579. COFF_SYM_FILE :
  1580. ;
  1581. else
  1582. internalerror(200602232);
  1583. end;
  1584. FSymTbl^[symidx]:=objsym;
  1585. { read aux records }
  1586. for i:=1 to sym.aux do
  1587. begin
  1588. FCoffSyms.Read(auxrec,sizeof(auxrec));
  1589. inc(symidx);
  1590. end;
  1591. inc(symidx);
  1592. end;
  1593. end;
  1594. end;
  1595. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  1596. begin
  1597. with TCoffObjSection(p) do
  1598. begin
  1599. { Skip debug sections }
  1600. if (oso_debug in secoptions) and
  1601. (cs_link_strip in current_settings.globalswitches) and
  1602. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1603. exit;
  1604. if coffrelocs>0 then
  1605. begin
  1606. FReader.Seek(coffrelocpos);
  1607. read_relocs(TCoffObjSection(p));
  1608. end;
  1609. end;
  1610. end;
  1611. function TCoffObjInput.ReadObjData(AReader:TObjectreader;objdata:TObjData):boolean;
  1612. var
  1613. secalign : shortint;
  1614. strpos,
  1615. i : longint;
  1616. code : longint;
  1617. objsec : TCoffObjSection;
  1618. secoptions : TObjSectionOptions;
  1619. header : tcoffheader;
  1620. sechdr : tcoffsechdr;
  1621. secname : string;
  1622. secnamebuf : array[0..15] of char;
  1623. begin
  1624. FReader:=AReader;
  1625. InputFileName:=AReader.FileName;
  1626. result:=false;
  1627. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  1628. with TCoffObjData(objdata) do
  1629. begin
  1630. { Read COFF header }
  1631. if not AReader.read(header,sizeof(tcoffheader)) then
  1632. begin
  1633. InputError('Can''t read COFF Header');
  1634. exit;
  1635. end;
  1636. if header.mach<>COFF_MAGIC then
  1637. begin
  1638. InputError('Illegal COFF Magic');
  1639. exit;
  1640. end;
  1641. {$ifdef arm}
  1642. eVCobj:=header.flag=$100;
  1643. {$endif arm}
  1644. { ObjSymbols }
  1645. AReader.Seek(header.sympos);
  1646. if not AReader.ReadArray(FCoffSyms,header.syms*sizeof(CoffSymbol)) then
  1647. begin
  1648. InputError('Error reading coff symbol table');
  1649. exit;
  1650. end;
  1651. { Strings }
  1652. if not AReader.Read(FCoffStrSize,4) then
  1653. begin
  1654. InputError('Error reading COFF string table');
  1655. exit;
  1656. end;
  1657. if (FCoffStrSize>4) then
  1658. begin
  1659. { allocate an extra byte and null-terminate }
  1660. GetMem(FCoffStrs,FCoffStrSize+1);
  1661. FCoffStrs[FCoffStrSize]:=#0;
  1662. for i:=0 to 3 do
  1663. FCoffStrs[i]:=#0;
  1664. if not AReader.Read(FCoffStrs[4],FCoffStrSize-4) then
  1665. begin
  1666. InputError('Error reading COFF string table');
  1667. exit;
  1668. end;
  1669. end;
  1670. { Section headers }
  1671. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  1672. FSecCount:=header.nsects;
  1673. FSecTbl:=AllocMem((header.nsects+1)*sizeof(TObjSection));
  1674. AReader.Seek(sizeof(tcoffheader)+header.opthdr);
  1675. for i:=1 to header.nsects do
  1676. begin
  1677. if not AReader.read(sechdr,sizeof(sechdr)) then
  1678. begin
  1679. InputError('Error reading COFF Section Headers');
  1680. exit;
  1681. end;
  1682. move(sechdr.name,secnamebuf,8);
  1683. secnamebuf[8]:=#0;
  1684. secname:=strpas(secnamebuf);
  1685. if secname[1]='/' then
  1686. begin
  1687. Val(Copy(secname,2,8),strpos,code);
  1688. if code=0 then
  1689. secname:=Read_str(strpos)
  1690. else
  1691. begin
  1692. InputError('Error reading COFF Section Headers');
  1693. secname:='error';
  1694. end;
  1695. end;
  1696. if win32 then
  1697. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  1698. else
  1699. begin
  1700. secoptions:=djdecodesechdrflags(secname,sechdr.flags);
  1701. secalign:=sizeof(pint);
  1702. end;
  1703. if (Length(secname)>3) and (secname[2] in ['e','f','i','p','r']) then
  1704. begin
  1705. if (Pos('.edata',secname)=1) or
  1706. (Pos('.rsrc',secname)=1) or
  1707. {$ifndef x86_64}
  1708. (Pos('.pdata',secname)=1) or
  1709. {$endif}
  1710. (Pos('.fpc',secname)=1) then
  1711. include(secoptions,oso_keep);
  1712. if (Pos('.idata',secname)=1) then
  1713. begin
  1714. { TODO: idata keep can maybe replaced with grouping of text and idata}
  1715. include(secoptions,oso_keep);
  1716. secname:=secname + '.' + ExtractFileName(InputFileName);
  1717. end;
  1718. end;
  1719. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions,false));
  1720. FSecTbl^[i]:=objsec;
  1721. if not win32 then
  1722. objsec.mempos:=sechdr.rvaofs;
  1723. objsec.orgmempos:=sechdr.rvaofs;
  1724. objsec.coffrelocs:=sechdr.nrelocs;
  1725. objsec.coffrelocpos:=sechdr.relocpos;
  1726. objsec.datapos:=sechdr.datapos;
  1727. objsec.Size:=sechdr.dataSize;
  1728. end;
  1729. { Insert all ObjSymbols }
  1730. read_symbols(objdata);
  1731. { Section Data }
  1732. ReadSectionContent(objdata);
  1733. { Relocs }
  1734. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  1735. end;
  1736. if assigned(FCoffStrs) then
  1737. freemem(FCoffStrs);
  1738. FCoffStrs:=nil;
  1739. FCoffSyms.Free;
  1740. FCoffSyms:=nil;
  1741. result:=true;
  1742. end;
  1743. constructor TDJCoffObjInput.create;
  1744. begin
  1745. inherited createcoff(false);
  1746. cobjdata:=TDJCoffObjData;
  1747. end;
  1748. constructor TPECoffObjInput.create;
  1749. begin
  1750. inherited createcoff(true);
  1751. cobjdata:=TPECoffObjData;
  1752. end;
  1753. {****************************************************************************
  1754. TCoffexeoutput
  1755. ****************************************************************************}
  1756. constructor TCoffexeoutput.createcoff(awin32:boolean);
  1757. begin
  1758. inherited create;
  1759. win32:=awin32;
  1760. if target_info.system in [system_x86_64_win64] then
  1761. MaxMemPos:=$FFFFFFFF
  1762. else
  1763. if target_info.system in systems_wince then
  1764. MaxMemPos:=$1FFFFFF
  1765. else
  1766. MaxMemPos:=$7FFFFFFF;
  1767. end;
  1768. procedure TCoffexeoutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  1769. var
  1770. sym : coffsymbol;
  1771. begin
  1772. FillChar(sym,sizeof(sym),0);
  1773. if length(name)>8 then
  1774. begin
  1775. sym.strpos:=FCoffStrs.size+4;
  1776. FCoffStrs.writestr(name);
  1777. FCoffStrs.writestr(#0);
  1778. end
  1779. else
  1780. move(name[1],sym.name,length(name));
  1781. sym.value:=value;
  1782. sym.section:=section;
  1783. sym.typ:=typ;
  1784. sym.aux:=aux;
  1785. FWriter.write(sym,sizeof(sym));
  1786. end;
  1787. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  1788. var
  1789. secval,
  1790. value : aint;
  1791. globalval : byte;
  1792. exesec : TExeSection;
  1793. begin
  1794. if not assigned(texesymbol(p).objsymbol) then
  1795. internalerror(200603053);
  1796. with texesymbol(p).objsymbol do
  1797. begin
  1798. exesec:=TExeSection(objsection.exesection);
  1799. { There is no exesection defined for special internal symbols
  1800. like __image_base__ }
  1801. if assigned(exesec) then
  1802. begin
  1803. secval:=exesec.secsymidx;
  1804. if win32 then
  1805. value:=address-exesec.mempos
  1806. else
  1807. value:=address;
  1808. end
  1809. else
  1810. begin
  1811. secval:=-1;
  1812. value:=address;
  1813. end;
  1814. if bind=AB_LOCAL then
  1815. globalval:=3
  1816. else
  1817. globalval:=2;
  1818. { reloctype address to the section in the executable }
  1819. write_symbol(name,value,secval,globalval,0);
  1820. end;
  1821. end;
  1822. procedure TCoffexeoutput.ExeSectionList_write_header(p:TObject;arg:pointer);
  1823. var
  1824. sechdr : tcoffsechdr;
  1825. s : string;
  1826. strpos : aword;
  1827. begin
  1828. with tExeSection(p) do
  1829. begin
  1830. fillchar(sechdr,sizeof(sechdr),0);
  1831. s:=name;
  1832. if length(s)>8 then
  1833. begin
  1834. strpos:=FCoffStrs.size+4;
  1835. FCoffStrs.writestr(s);
  1836. FCoffStrs.writestr(#0);
  1837. s:='/'+ToStr(strpos);
  1838. end;
  1839. move(s[1],sechdr.name,length(s));
  1840. if win32 then
  1841. begin
  1842. sechdr.rvaofs:=mempos;
  1843. sechdr.vsize:=Size;
  1844. { sechdr.dataSize is size of initialized data, rounded up to FileAlignment
  1845. (so it can be greater than VirtualSize). Must be zero for sections that
  1846. do not contain initialized data. }
  1847. if (oso_data in SecOptions) then
  1848. sechdr.datasize:=Align(Size,SectionDataAlign);
  1849. end
  1850. else
  1851. begin
  1852. if not (oso_debug in SecOptions) then
  1853. begin
  1854. sechdr.rvaofs:=mempos;
  1855. sechdr.vsize:=mempos;
  1856. end;
  1857. sechdr.datasize:=Size;
  1858. end;
  1859. if (Size>0) then
  1860. sechdr.datapos:=datapos-datapos_offset;
  1861. sechdr.nrelocs:=0;
  1862. sechdr.relocpos:=0;
  1863. if win32 then
  1864. begin
  1865. if (target_info.system in systems_nativent) and
  1866. (apptype = app_native) then
  1867. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign) or PE_SCN_MEM_NOT_PAGED
  1868. else
  1869. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign);
  1870. { some flags are invalid in executables, reset them }
  1871. sechdr.flags:=sechdr.flags and
  1872. not(PE_SCN_LNK_INFO or PE_SCN_LNK_REMOVE or
  1873. PE_SCN_LNK_COMDAT or PE_SCN_ALIGN_MASK);
  1874. end
  1875. else
  1876. sechdr.flags:=djencodesechdrflags(SecOptions);
  1877. FWriter.write(sechdr,sizeof(sechdr));
  1878. end;
  1879. end;
  1880. procedure TCoffexeoutput.ExeSectionList_pass2_header(p:TObject;arg:pointer);
  1881. begin
  1882. with TExeSection(p) do
  1883. begin
  1884. { The debuginfo sections should already be stripped }
  1885. { if (ExeWriteMode=ewm_exeonly) and
  1886. (oso_debug in SecOptions) then
  1887. internalerror(200801161); }
  1888. inc(plongint(arg)^);
  1889. secsymidx:=plongint(arg)^;
  1890. end;
  1891. end;
  1892. function tcoffexeoutput.totalheadersize:longword;
  1893. var
  1894. stubsize,
  1895. optheadersize : longword;
  1896. begin
  1897. if win32 then
  1898. begin
  1899. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  1900. optheadersize:=sizeof(tcoffpeoptheader);
  1901. end
  1902. else
  1903. begin
  1904. stubsize:=sizeof(go32v2stub);
  1905. optheadersize:=sizeof(coffdjoptheader);
  1906. end;
  1907. result:=stubsize+sizeof(tcoffheader)+optheadersize;
  1908. end;
  1909. procedure tcoffexeoutput.MemPos_Header;
  1910. begin
  1911. { calculate start positions after the headers }
  1912. currmempos:=totalheadersize+sizeof(tcoffsechdr)*longword(ExeSectionList.Count-2);
  1913. end;
  1914. procedure tcoffexeoutput.DataPos_Header;
  1915. begin
  1916. { retrieve amount of sections }
  1917. nsects:=0;
  1918. ExeSectionList.ForEachCall(@ExeSectionList_pass2_header,@nsects);
  1919. { calculate start positions after the headers }
  1920. currdatapos:=totalheadersize+longword(nsects)*sizeof(tcoffsechdr);
  1921. end;
  1922. procedure tcoffexeoutput.DataPos_Symbols;
  1923. begin
  1924. inherited DataPos_Symbols;
  1925. { Calculating symbols position and size }
  1926. nsyms:=ExeSymbolList.Count;
  1927. sympos:=Align(CurrDataPos,SectionDataAlign);
  1928. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  1929. end;
  1930. function TCoffexeoutput.writedata:boolean;
  1931. var
  1932. i : longword;
  1933. header : tcoffheader;
  1934. djoptheader : coffdjoptheader;
  1935. peoptheader : tcoffpeoptheader;
  1936. textExeSec,
  1937. dataExeSec,
  1938. bssExeSec,
  1939. idataExeSec,
  1940. tlsExeSec : TExeSection;
  1941. tlsdir : TlsDirectory;
  1942. hassymbols,
  1943. writeDbgStrings : boolean;
  1944. procedure UpdateDataDir(const secname:string;idx:longint);
  1945. var
  1946. exesec : TExeSection;
  1947. begin
  1948. exesec:=FindExeSection(secname);
  1949. if assigned(exesec) then
  1950. begin
  1951. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  1952. peoptheader.DataDirectory[idx].size:=exesec.Size;
  1953. end;
  1954. end;
  1955. procedure UpdateImports;
  1956. var
  1957. exesec: TExeSection;
  1958. objsec, iat_start, iat_end, ilt_start: TObjSection;
  1959. i: longint;
  1960. begin
  1961. exesec:=FindExeSection('.idata');
  1962. if exesec=nil then
  1963. exit;
  1964. iat_start:=nil;
  1965. iat_end:=nil;
  1966. ilt_start:=nil;
  1967. for i:=0 to exesec.ObjSectionList.Count-1 do
  1968. begin
  1969. objsec:=TObjSection(exesec.ObjSectionList[i]);
  1970. if (ilt_start=nil) and (Pos('.idata$4',objsec.Name)=1) then
  1971. ilt_start:=objsec;
  1972. if Pos('.idata$5',objsec.Name)=1 then
  1973. begin
  1974. if iat_start=nil then
  1975. iat_start:=objsec;
  1976. end
  1977. else
  1978. if Assigned(iat_start) then
  1979. begin
  1980. iat_end:=objsec;
  1981. Break;
  1982. end;
  1983. end;
  1984. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=exesec.mempos;
  1985. if Assigned(ilt_start) then
  1986. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=ilt_start.mempos-exesec.mempos
  1987. else { should not happen }
  1988. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=exesec.Size;
  1989. if Assigned(iat_start) and Assigned(iat_end) then
  1990. begin
  1991. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].vaddr:=iat_start.mempos;
  1992. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].size:=iat_end.mempos-iat_start.mempos;
  1993. end;
  1994. end;
  1995. procedure UpdateTlsDataDir;
  1996. var
  1997. {callbacksection : TExeSection;}
  1998. tlsexesymbol: TExeSymbol;
  1999. tlssymbol: TObjSymbol;
  2000. callbackexesymbol: TExeSymbol;
  2001. callbacksymbol: TObjSymbol;
  2002. begin
  2003. { according to GNU ld,
  2004. the callback routines should be placed into .CRT$XL*
  2005. sections, and the thread local variables in .tls
  2006. __tls_start__ and __tls_end__ symbols
  2007. should be used for the initialized part,
  2008. which we do not support yet. }
  2009. { For now, we only pass the address of the __tls_used
  2010. asm symbol into PE_DATADIR_TLS with the correct
  2011. size of this table (different for win32/win64 }
  2012. tlsexesymbol:=texesymbol(ExeSymbolList.Find(
  2013. target_info.Cprefix+'_tls_used'));
  2014. if assigned(tlsexesymbol) then
  2015. begin
  2016. tlssymbol:=tlsexesymbol.ObjSymbol;
  2017. peoptheader.DataDirectory[PE_DATADIR_TLS].vaddr:=tlssymbol.address;
  2018. { sizeof(TlsDirectory) is different on host and target when cross-compiling }
  2019. peoptheader.DataDirectory[PE_DATADIR_TLS].size:=TLSDIR_SIZE;
  2020. if IsSharedLibrary then
  2021. begin
  2022. { Here we should reset __FPC_tls_callbacks value to nil }
  2023. callbackexesymbol:=texesymbol(ExeSymbolList.Find(
  2024. '__FPC_tls_callbacks'));
  2025. if assigned (callbackexesymbol) then
  2026. begin
  2027. callbacksymbol:=callbackexesymbol.ObjSymbol;
  2028. end;
  2029. end;
  2030. end;
  2031. end;
  2032. begin
  2033. result:=false;
  2034. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  2035. textExeSec:=FindExeSection('.text');
  2036. dataExeSec:=FindExeSection('.data');
  2037. bssExeSec:=FindExeSection('.bss');
  2038. tlsExeSec:=FindExeSection('.tls');
  2039. if not assigned(TextExeSec) or
  2040. not assigned(DataExeSec) then
  2041. internalerror(200602231);
  2042. { do we need to write symbols? }
  2043. hassymbols:=(ExeWriteMode=ewm_dbgonly) or
  2044. (
  2045. (ExeWriteMode=ewm_exefull) and
  2046. not(cs_link_strip in current_settings.globalswitches)
  2047. );
  2048. writeDbgStrings:=hassymbols or ((ExeWriteMode=ewm_exeonly) and (cs_link_separate_dbg_file in current_settings.globalswitches));
  2049. { Stub }
  2050. if win32 then
  2051. begin
  2052. FWriter.write(win32stub,sizeof(win32stub));
  2053. FWriter.write(pemagic,sizeof(pemagic));
  2054. end
  2055. else
  2056. FWriter.write(go32v2stub,sizeof(go32v2stub));
  2057. { Initial header, will be updated later }
  2058. fillchar(header,sizeof(header),0);
  2059. header.mach:=COFF_MAGIC;
  2060. header.nsects:=nsects;
  2061. if writeDbgStrings then
  2062. header.sympos:=sympos-datapos_offset;
  2063. if hassymbols then
  2064. header.syms:=nsyms;
  2065. if win32 then
  2066. header.opthdr:=sizeof(tcoffpeoptheader)
  2067. else
  2068. header.opthdr:=sizeof(coffdjoptheader);
  2069. if win32 then
  2070. begin
  2071. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  2072. if target_info.system in [system_x86_64_win64] then
  2073. header.flag:=header.flag or PE_FILE_LARGE_ADDRESS_AWARE
  2074. else
  2075. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  2076. if IsSharedLibrary then
  2077. header.flag:=header.flag or PE_FILE_DLL;
  2078. if FindExeSection('.reloc')=nil then
  2079. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  2080. if (FindExeSection('.stab')=nil) and
  2081. (FindExeSection('.debug_info')=nil) and
  2082. (FindExeSection('.gnu_debuglink')=nil) then
  2083. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  2084. if not hassymbols then
  2085. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  2086. if SetPEFlagsSetExplicity then
  2087. header.flag:=header.flag or peflags;
  2088. end
  2089. else
  2090. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  2091. FWriter.write(header,sizeof(header));
  2092. { Optional COFF Header }
  2093. if win32 then
  2094. begin
  2095. fillchar(peoptheader,sizeof(peoptheader),0);
  2096. peoptheader.magic:=COFF_OPT_MAGIC;
  2097. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  2098. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  2099. peoptheader.tsize:=TextExeSec.Size;
  2100. peoptheader.dsize:=DataExeSec.Size;
  2101. if assigned(BSSExeSec) then
  2102. peoptheader.bsize:=BSSExeSec.Size;
  2103. peoptheader.text_start:=TextExeSec.mempos;
  2104. {$ifndef cpu64bitaddr}
  2105. peoptheader.data_start:=DataExeSec.mempos;
  2106. {$endif cpu64bitaddr}
  2107. peoptheader.entry:=EntrySym.Address;
  2108. peoptheader.ImageBase:=ImageBase;
  2109. peoptheader.SectionAlignment:=SectionMemAlign;
  2110. peoptheader.FileAlignment:=SectionDataAlign;
  2111. peoptheader.MajorOperatingSystemVersion:=4;
  2112. peoptheader.MinorOperatingSystemVersion:=0;
  2113. peoptheader.MajorImageVersion:=dllmajor;
  2114. peoptheader.MinorImageVersion:=dllminor;
  2115. if target_info.system in systems_wince then
  2116. peoptheader.MajorSubsystemVersion:=3
  2117. else
  2118. peoptheader.MajorSubsystemVersion:=4;
  2119. peoptheader.MinorSubsystemVersion:=0;
  2120. peoptheader.Win32Version:=0;
  2121. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  2122. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  2123. peoptheader.CheckSum:=0;
  2124. if (target_info.system in systems_nativent) and (not IsSharedLibrary or (apptype = app_native)) then
  2125. { Although I did not really test this, it seems that Subsystem is
  2126. not checked in DLLs except for maybe drivers}
  2127. peoptheader.Subsystem:=PE_SUBSYSTEM_NATIVE
  2128. else
  2129. if target_info.system in systems_wince then
  2130. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  2131. else
  2132. if apptype=app_gui then
  2133. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  2134. else
  2135. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  2136. peoptheader.DllCharacteristics:=0;
  2137. peoptheader.SizeOfStackReserve:=stacksize;
  2138. peoptheader.SizeOfStackCommit:=$1000;
  2139. if MinStackSizeSetExplicity then
  2140. peoptheader.SizeOfStackCommit:=minstacksize;
  2141. if MaxStackSizeSetExplicity then
  2142. peoptheader.SizeOfStackReserve:=maxstacksize;
  2143. peoptheader.SizeOfHeapReserve:=$100000;
  2144. peoptheader.SizeOfHeapCommit:=$1000;
  2145. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  2146. UpdateImports;
  2147. UpdateTlsDataDir;
  2148. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  2149. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  2150. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  2151. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  2152. FWriter.write(peoptheader,sizeof(peoptheader));
  2153. end
  2154. else
  2155. begin
  2156. fillchar(djoptheader,sizeof(djoptheader),0);
  2157. djoptheader.magic:=COFF_OPT_MAGIC;
  2158. djoptheader.tsize:=TextExeSec.Size;
  2159. djoptheader.dsize:=DataExeSec.Size;
  2160. if assigned(BSSExeSec) then
  2161. djoptheader.bsize:=BSSExeSec.Size;
  2162. djoptheader.text_start:=TextExeSec.mempos;
  2163. djoptheader.data_start:=DataExeSec.mempos;
  2164. djoptheader.entry:=EntrySym.Address;
  2165. FWriter.write(djoptheader,sizeof(djoptheader));
  2166. end;
  2167. { For some unknown reason WM 6.1 requires .idata section to be read only.
  2168. Otherwise it refuses to load DLLs greater than 64KB.
  2169. Earlier versions of WinCE load DLLs regardless of .idata flags. }
  2170. if target_info.system in systems_wince then
  2171. begin
  2172. idataExeSec:=FindExeSection('.idata');
  2173. if idataExeSec<>nil then
  2174. idataExeSec.SecOptions:=idataExeSec.SecOptions - [oso_write] + [oso_readonly];
  2175. end;
  2176. { Section headers }
  2177. ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
  2178. { Section data }
  2179. WriteExeSectionContent;
  2180. { Align after the last section }
  2181. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  2182. { Optional Symbols }
  2183. if SymPos<>FWriter.Size then
  2184. internalerror(200602252);
  2185. if hassymbols then
  2186. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  2187. if writeDbgStrings then
  2188. begin
  2189. { Strings }
  2190. i:=FCoffStrs.size+4;
  2191. FWriter.write(i,4);
  2192. FWriter.writearray(FCoffStrs);
  2193. end;
  2194. { Release }
  2195. FCoffStrs.Free;
  2196. result:=true;
  2197. end;
  2198. function IdataObjSectionCompare(Item1, Item2: Pointer): Integer;
  2199. var
  2200. I1 : TObjSection absolute Item1;
  2201. I2 : TObjSection absolute Item2;
  2202. begin
  2203. Result:=CompareStr(I1.Name,I2.Name);
  2204. end;
  2205. procedure TCoffexeoutput.Order_ObjSectionList(ObjSectionList: TFPObjectList;const aPattern:string);
  2206. begin
  2207. { Sort sections having '$' in the name, that's how PECOFF documentation
  2208. tells to handle them. However, look for '$' in the pattern, not in section
  2209. names, because the latter often get superfluous '$' due to mangling. }
  2210. if Pos('$',aPattern)>0 then
  2211. ObjSectionList.Sort(@IdataObjSectionCompare);
  2212. end;
  2213. constructor TDJCoffexeoutput.create;
  2214. begin
  2215. inherited createcoff(false);
  2216. datapos_offset:=sizeof(go32v2stub);
  2217. CExeSection:=TExeSection;
  2218. CObjData:=TDJCoffObjData;
  2219. end;
  2220. procedure TDJCoffexeoutput.MemPos_Header;
  2221. begin
  2222. { Headers are not loaded, first 4K page is reserved }
  2223. CurrMemPos:=$1000;
  2224. end;
  2225. constructor TPECoffexeoutput.create;
  2226. begin
  2227. inherited createcoff(true);
  2228. CExeSection:=TExeSection;
  2229. CObjData:=TPECoffObjData;
  2230. end;
  2231. procedure TPECoffexeoutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2232. var
  2233. textobjsection,
  2234. idata2objsection,
  2235. idata4objsection,
  2236. idata5objsection,
  2237. idata6objsection,
  2238. idata7objsection : TObjSection;
  2239. idata2label : TObjSymbol;
  2240. basedllname : string;
  2241. procedure StartImport(const dllname:string);
  2242. var
  2243. idata4label,
  2244. idata5label,
  2245. idata7label : TObjSymbol;
  2246. emptyint : longint;
  2247. begin
  2248. if assigned(exemap) then
  2249. begin
  2250. exemap.Add('');
  2251. exemap.Add('Importing from DLL '+dllname);
  2252. end;
  2253. emptyint:=0;
  2254. basedllname:=ExtractFileName(dllname);
  2255. idata2objsection:=internalobjdata.createsection(sec_idata2,basedllname);
  2256. idata2label:=internalobjdata.SymbolDefine('__imp_dir_'+basedllname,AB_LOCAL,AT_DATA);
  2257. idata4objsection:=internalobjdata.createsection(sec_idata4,basedllname);
  2258. idata4label:=internalobjdata.SymbolDefine('__imp_names_'+basedllname,AB_LOCAL,AT_DATA);
  2259. idata5objsection:=internalobjdata.createsection(sec_idata5,basedllname);
  2260. idata5label:=internalobjdata.SymbolDefine('__imp_fixup_'+basedllname,AB_LOCAL,AT_DATA);
  2261. idata7objsection:=internalobjdata.createsection(sec_idata7,basedllname);
  2262. idata7label:=internalobjdata.SymbolDefine('__imp_dll_'+basedllname,AB_LOCAL,AT_DATA);
  2263. { idata2 }
  2264. internalobjdata.SetSection(idata2objsection);
  2265. { dummy links to imports finalization }
  2266. internalobjdata.writereloc(0,0,internalobjdata.SymbolRef('__imp_names_end_'+basedllname),RELOC_NONE);
  2267. internalobjdata.writereloc(0,0,internalobjdata.SymbolRef('__imp_fixup_end_'+basedllname),RELOC_NONE);
  2268. { section data }
  2269. internalobjdata.writereloc(0,sizeof(longint),idata4label,RELOC_RVA);
  2270. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2271. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2272. internalobjdata.writereloc(0,sizeof(longint),idata7label,RELOC_RVA);
  2273. internalobjdata.writereloc(0,sizeof(longint),idata5label,RELOC_RVA);
  2274. { idata7 }
  2275. internalobjdata.SetSection(idata7objsection);
  2276. internalobjdata.writebytes(basedllname[1],length(basedllname));
  2277. internalobjdata.writebytes(emptyint,1);
  2278. end;
  2279. procedure EndImport;
  2280. var
  2281. emptyint : longint;
  2282. begin
  2283. emptyint:=0;
  2284. { These are referenced from idata2, oso_keep is not necessary. }
  2285. idata4objsection:=internalobjdata.createsection(sec_idata4, basedllname+'_z_');
  2286. internalobjdata.SymbolDefine('__imp_names_end_'+basedllname,AB_LOCAL,AT_DATA);
  2287. idata5objsection:=internalobjdata.createsection(sec_idata5, basedllname+'_z_');
  2288. internalobjdata.SymbolDefine('__imp_fixup_end_'+basedllname,AB_LOCAL,AT_DATA);
  2289. { idata4 }
  2290. internalobjdata.SetSection(idata4objsection);
  2291. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2292. if target_info.system=system_x86_64_win64 then
  2293. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2294. { idata5 }
  2295. internalobjdata.SetSection(idata5objsection);
  2296. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2297. if target_info.system=system_x86_64_win64 then
  2298. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2299. end;
  2300. function AddImport(const afuncname,amangledname:string; AOrdNr:longint;isvar:boolean):TObjSymbol;
  2301. const
  2302. {$ifdef x86_64}
  2303. jmpopcode : array[0..1] of byte = (
  2304. $ff,$25 // jmp qword [rip + offset32]
  2305. );
  2306. {$else x86_64}
  2307. {$ifdef arm}
  2308. jmpopcode : array[0..7] of byte = (
  2309. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  2310. $00,$f0,$9c,$e5 // ldr pc, [ip]
  2311. );
  2312. {$else arm}
  2313. jmpopcode : array[0..1] of byte = (
  2314. $ff,$25
  2315. );
  2316. {$endif arm}
  2317. {$endif x86_64}
  2318. nopopcodes : array[0..1] of byte = (
  2319. $90,$90
  2320. );
  2321. var
  2322. idata4label,
  2323. idata5label,
  2324. idata6label : TObjSymbol;
  2325. emptyint : longint;
  2326. secname,
  2327. num : string;
  2328. absordnr: word;
  2329. procedure WriteTableEntry;
  2330. var
  2331. ordint: dword;
  2332. begin
  2333. if AOrdNr <= 0 then
  2334. begin
  2335. { import by name }
  2336. internalobjdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  2337. if target_info.system=system_x86_64_win64 then
  2338. internalobjdata.writebytes(emptyint,sizeof(emptyint));
  2339. end
  2340. else
  2341. begin
  2342. { import by ordinal }
  2343. ordint:=AOrdNr;
  2344. if target_info.system=system_x86_64_win64 then
  2345. begin
  2346. internalobjdata.writebytes(ordint,sizeof(ordint));
  2347. ordint:=$80000000;
  2348. internalobjdata.writebytes(ordint,sizeof(ordint));
  2349. end
  2350. else
  2351. begin
  2352. ordint:=ordint or $80000000;
  2353. internalobjdata.writebytes(ordint,sizeof(ordint));
  2354. end;
  2355. end;
  2356. end;
  2357. begin
  2358. result:=nil;
  2359. emptyint:=0;
  2360. if assigned(exemap) then
  2361. begin
  2362. if AOrdNr <= 0 then
  2363. exemap.Add(' Importing Function '+afuncname)
  2364. else
  2365. exemap.Add(' Importing Function '+afuncname+' (OrdNr='+tostr(AOrdNr)+')');
  2366. end;
  2367. with internalobjdata do
  2368. begin
  2369. secname:=basedllname+'_i_'+amangledname;
  2370. textobjsection:=createsection(sectionname(sec_code,secname,secorder_default),current_settings.alignment.procalign,sectiontype2options(sec_code) - [oso_keep]);
  2371. idata4objsection:=createsection(sec_idata4, secname);
  2372. idata5objsection:=createsection(sec_idata5, secname);
  2373. idata6objsection:=createsection(sec_idata6, secname);
  2374. end;
  2375. { idata6, import data (ordnr+name) }
  2376. internalobjdata.SetSection(idata6objsection);
  2377. inc(idatalabnr);
  2378. num:=tostr(idatalabnr);
  2379. idata6label:=internalobjdata.SymbolDefine('__imp_'+num,AB_LOCAL,AT_DATA);
  2380. absordnr:=Abs(AOrdNr);
  2381. { write index hint }
  2382. internalobjdata.writebytes(absordnr,2);
  2383. if AOrdNr <= 0 then
  2384. internalobjdata.writebytes(afuncname[1],length(afuncname));
  2385. internalobjdata.writebytes(emptyint,1);
  2386. internalobjdata.writebytes(emptyint,align(internalobjdata.CurrObjSec.size,2)-internalobjdata.CurrObjSec.size);
  2387. { idata4, import lookup table }
  2388. internalobjdata.SetSection(idata4objsection);
  2389. idata4label:=internalobjdata.SymbolDefine('__imp_lookup_'+num,AB_LOCAL,AT_DATA);
  2390. WriteTableEntry;
  2391. { idata5, import address table }
  2392. internalobjdata.SetSection(idata5objsection);
  2393. { dummy back links }
  2394. internalobjdata.writereloc(0,0,idata4label,RELOC_NONE);
  2395. internalobjdata.writereloc(0,0,idata2label,RELOC_NONE);
  2396. { section data }
  2397. if isvar then
  2398. result:=internalobjdata.SymbolDefine(amangledname,AB_GLOBAL,AT_DATA)
  2399. else
  2400. idata5label:=internalobjdata.SymbolDefine('__imp_'+amangledname,AB_LOCAL,AT_DATA);
  2401. WriteTableEntry;
  2402. { text, jmp }
  2403. if not isvar then
  2404. begin
  2405. internalobjdata.SetSection(textobjsection);
  2406. result:=internalobjdata.SymbolDefine('_'+amangledname,AB_GLOBAL,AT_FUNCTION);
  2407. internalobjdata.writebytes(jmpopcode,sizeof(jmpopcode));
  2408. {$ifdef x86_64}
  2409. internalobjdata.writereloc(0,4,idata5label,RELOC_RELATIVE);
  2410. {$else}
  2411. internalobjdata.writereloc(0,4,idata5label,RELOC_ABSOLUTE32);
  2412. {$endif x86_64}
  2413. internalobjdata.writebytes(nopopcodes,align(internalobjdata.CurrObjSec.size,sizeof(nopopcodes))-internalobjdata.CurrObjSec.size);
  2414. end;
  2415. end;
  2416. var
  2417. i,j : longint;
  2418. ImportLibrary : TImportLibrary;
  2419. ImportSymbol : TImportSymbol;
  2420. exesym : TExeSymbol;
  2421. begin
  2422. for i:=0 to ImportLibraryList.Count-1 do
  2423. begin
  2424. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  2425. idata2objsection:=nil;
  2426. idata4objsection:=nil;
  2427. idata5objsection:=nil;
  2428. idata6objsection:=nil;
  2429. idata7objsection:=nil;
  2430. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  2431. begin
  2432. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  2433. exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
  2434. if assigned(exesym) and
  2435. (exesym.State<>symstate_defined) then
  2436. begin
  2437. if not assigned(idata2objsection) then
  2438. StartImport(ImportLibrary.Name);
  2439. exesym.objsymbol:=AddImport(ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  2440. exesym.State:=symstate_defined;
  2441. end;
  2442. end;
  2443. if assigned(idata2objsection) then
  2444. EndImport;
  2445. end;
  2446. PackUnresolvedExeSymbols('after DLL imports');
  2447. end;
  2448. procedure TPECoffexeoutput.GenerateRelocs;
  2449. var
  2450. pgaddr, hdrpos : longword;
  2451. procedure FinishBlock;
  2452. var
  2453. p,len : longint;
  2454. begin
  2455. if hdrpos = longword(-1) then
  2456. exit;
  2457. p:=0;
  2458. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  2459. p:=internalObjData.CurrObjSec.Data.Pos;
  2460. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  2461. len:=p-hdrpos;
  2462. internalObjData.CurrObjSec.Data.write(len,4);
  2463. internalObjData.CurrObjSec.Data.seek(p);
  2464. hdrpos:=longword(-1);
  2465. end;
  2466. var
  2467. exesec : TExeSection;
  2468. objsec : TObjSection;
  2469. objreloc : TObjRelocation;
  2470. i,j,k : longint;
  2471. offset : longword;
  2472. w: word;
  2473. begin
  2474. if not RelocSection or FRelocsGenerated then
  2475. exit;
  2476. exesec:=FindExeSection('.reloc');
  2477. if exesec=nil then
  2478. exit;
  2479. objsec:=internalObjData.createsection('.reloc',0,exesec.SecOptions+[oso_data]);
  2480. exesec.AddObjSection(objsec);
  2481. pgaddr:=longword(-1);
  2482. hdrpos:=longword(-1);
  2483. for i:=0 to ExeSectionList.Count-1 do
  2484. begin
  2485. exesec:=TExeSection(ExeSectionList[i]);
  2486. for j:=0 to exesec.ObjSectionList.count-1 do
  2487. begin
  2488. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2489. { create relocs only for sections which are loaded in memory }
  2490. if not (oso_load in objsec.SecOptions) then
  2491. continue;
  2492. for k:=0 to objsec.ObjRelocations.Count-1 do
  2493. begin
  2494. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  2495. if not (objreloc.typ in [{$ifdef cpu64bitaddr}RELOC_ABSOLUTE32,{$endif cpu64bitaddr}RELOC_ABSOLUTE]) then
  2496. continue;
  2497. offset:=objsec.MemPos+objreloc.dataoffset;
  2498. if (offset<pgaddr) and (pgaddr<>longword(-1)) then
  2499. Internalerror(2007062701);
  2500. if (offset-pgaddr>=4096) or (pgaddr=longword(-1)) then
  2501. begin
  2502. FinishBlock;
  2503. pgaddr:=(offset div 4096)*4096;
  2504. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  2505. internalObjData.writebytes(pgaddr,4);
  2506. { Reserving space for block size. The size will be written later in FinishBlock }
  2507. internalObjData.writebytes(k,4);
  2508. end;
  2509. {$ifdef cpu64bitaddr}
  2510. if objreloc.typ = RELOC_ABSOLUTE then
  2511. w:=IMAGE_REL_BASED_DIR64
  2512. else
  2513. {$endif cpu64bitaddr}
  2514. w:=IMAGE_REL_BASED_HIGHLOW;
  2515. w:=(w shl 12) or (offset-pgaddr);
  2516. internalObjData.writebytes(w,2);
  2517. end;
  2518. end;
  2519. end;
  2520. FinishBlock;
  2521. FRelocsGenerated:=true;
  2522. end;
  2523. procedure TPECoffexeoutput.Order_End;
  2524. var
  2525. exesec : TExeSection;
  2526. begin
  2527. inherited;
  2528. if not IsSharedLibrary then
  2529. exit;
  2530. exesec:=FindExeSection('.reloc');
  2531. if exesec=nil then
  2532. exit;
  2533. exesec.SecOptions:=exesec.SecOptions + [oso_Data,oso_keep,oso_load];
  2534. end;
  2535. procedure TPECoffexeoutput.MemPos_ExeSection(const aname:string);
  2536. begin
  2537. if aname='.reloc' then
  2538. GenerateRelocs;
  2539. inherited;
  2540. end;
  2541. {****************************************************************************
  2542. TDJCoffAssembler
  2543. ****************************************************************************}
  2544. constructor TDJCoffAssembler.Create(smart:boolean);
  2545. begin
  2546. inherited Create(smart);
  2547. CObjOutput:=TDJCoffObjOutput;
  2548. end;
  2549. {****************************************************************************
  2550. TPECoffAssembler
  2551. ****************************************************************************}
  2552. constructor TPECoffAssembler.Create(smart:boolean);
  2553. begin
  2554. inherited Create(smart);
  2555. CObjOutput:=TPECoffObjOutput;
  2556. end;
  2557. {*****************************************************************************
  2558. DLLReader
  2559. *****************************************************************************}
  2560. {$ifdef win32}
  2561. var
  2562. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  2563. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  2564. {$endif win32}
  2565. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  2566. type
  2567. TPECoffExpDir=packed record
  2568. flag,
  2569. stamp : cardinal;
  2570. Major,
  2571. Minor : word;
  2572. Name,
  2573. Base,
  2574. NumFuncs,
  2575. NumNames,
  2576. AddrFuncs,
  2577. AddrNames,
  2578. AddrOrds : cardinal;
  2579. end;
  2580. var
  2581. DLLReader : TObjectReader;
  2582. DosHeader : array[0..$7f] of byte;
  2583. PEMagic : array[0..3] of byte;
  2584. Header : TCoffHeader;
  2585. peheader : tcoffpeoptheader;
  2586. NameOfs,
  2587. newheaderofs : longword;
  2588. FuncName : string;
  2589. expdir : TPECoffExpDir;
  2590. i : longint;
  2591. found : boolean;
  2592. sechdr : tCoffSecHdr;
  2593. {$ifdef win32}
  2594. p : pointer;
  2595. {$endif win32}
  2596. begin
  2597. result:=false;
  2598. {$ifdef win32}
  2599. if (target_info.system=system_x86_64_win64) and
  2600. assigned(Wow64DisableWow64FsRedirection) then
  2601. Wow64DisableWow64FsRedirection(p);
  2602. {$endif win32}
  2603. DLLReader:=TObjectReader.Create;
  2604. DLLReader.OpenFile(dllname);
  2605. {$ifdef win32}
  2606. if (target_info.system=system_x86_64_win64) and
  2607. assigned(Wow64RevertWow64FsRedirection) then
  2608. Wow64RevertWow64FsRedirection(p);
  2609. {$endif win32}
  2610. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  2611. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  2612. begin
  2613. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  2614. exit;
  2615. end;
  2616. newheaderofs:=cardinal(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  2617. DLLReader.Seek(newheaderofs);
  2618. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  2619. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  2620. begin
  2621. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  2622. exit;
  2623. end;
  2624. if not DLLReader.Read(Header,sizeof(TCoffHeader)) or
  2625. (Header.mach<>COFF_MAGIC) or
  2626. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  2627. begin
  2628. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  2629. exit;
  2630. end;
  2631. { Read optheader }
  2632. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  2633. { Section headers }
  2634. found:=false;
  2635. for i:=1 to header.nsects do
  2636. begin
  2637. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  2638. begin
  2639. Comment(V_Error,'Error reading coff file '+DLLName);
  2640. exit;
  2641. end;
  2642. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  2643. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  2644. begin
  2645. found:=true;
  2646. break;
  2647. end;
  2648. end;
  2649. if not found then
  2650. begin
  2651. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  2652. exit;
  2653. end;
  2654. { Process edata }
  2655. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  2656. DLLReader.Read(expdir,sizeof(expdir));
  2657. for i:=0 to expdir.NumNames-1 do
  2658. begin
  2659. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  2660. DLLReader.Read(NameOfs,4);
  2661. Dec(NameOfs,sechdr.rvaofs);
  2662. if {(NameOfs<0) or}
  2663. (NameOfs>sechdr.vsize) then
  2664. begin
  2665. Comment(V_Error,'DLL does contains invalid exports');
  2666. break;
  2667. end;
  2668. { Read Function name from DLL, prepend _ and terminate with #0 }
  2669. DLLReader.Seek(sechdr.datapos+NameOfs);
  2670. DLLReader.Read((@FuncName[1])^,sizeof(FuncName)-3);
  2671. FuncName[sizeof(FuncName)-1]:=#0;
  2672. FuncName[0]:=chr(Strlen(@FuncName[1]));
  2673. readdllproc(DLLName,FuncName);
  2674. end;
  2675. DLLReader.Free;
  2676. end;
  2677. {*****************************************************************************
  2678. Initialize
  2679. *****************************************************************************}
  2680. {$ifdef i386}
  2681. const
  2682. as_i386_coff_info : tasminfo =
  2683. (
  2684. id : as_i386_coff;
  2685. idtxt : 'COFF';
  2686. asmbin : '';
  2687. asmcmd : '';
  2688. supported_targets : [system_i386_go32v2];
  2689. flags : [af_outputbinary,af_smartlink_sections];
  2690. labelprefix : '.L';
  2691. comment : '';
  2692. dollarsign: '$';
  2693. );
  2694. as_i386_pecoff_info : tasminfo =
  2695. (
  2696. id : as_i386_pecoff;
  2697. idtxt : 'PECOFF';
  2698. asmbin : '';
  2699. asmcmd : '';
  2700. supported_targets : [system_i386_win32,system_i386_nativent];
  2701. flags : [af_outputbinary,af_smartlink_sections];
  2702. labelprefix : '.L';
  2703. comment : '';
  2704. dollarsign: '$';
  2705. );
  2706. as_i386_pecoffwdosx_info : tasminfo =
  2707. (
  2708. id : as_i386_pecoffwdosx;
  2709. idtxt : 'PEWDOSX';
  2710. asmbin : '';
  2711. asmcmd : '';
  2712. supported_targets : [system_i386_wdosx];
  2713. flags : [af_outputbinary];
  2714. labelprefix : '.L';
  2715. comment : '';
  2716. dollarsign: '$';
  2717. );
  2718. as_i386_pecoffwince_info : tasminfo =
  2719. (
  2720. id : as_i386_pecoffwince;
  2721. idtxt : 'PECOFFWINCE';
  2722. asmbin : '';
  2723. asmcmd : '';
  2724. supported_targets : [system_i386_wince];
  2725. flags : [af_outputbinary,af_smartlink_sections];
  2726. labelprefix : '.L';
  2727. comment : '';
  2728. dollarsign: '$';
  2729. );
  2730. {$endif i386}
  2731. {$ifdef x86_64}
  2732. const
  2733. as_x86_64_pecoff_info : tasminfo =
  2734. (
  2735. id : as_x86_64_pecoff;
  2736. idtxt : 'PECOFF';
  2737. asmbin : '';
  2738. asmcmd : '';
  2739. supported_targets : [system_x86_64_win64];
  2740. flags : [af_outputbinary,af_smartlink_sections];
  2741. labelprefix : '.L';
  2742. comment : '';
  2743. dollarsign: '$';
  2744. );
  2745. {$endif x86_64}
  2746. {$ifdef arm}
  2747. const
  2748. as_arm_pecoffwince_info : tasminfo =
  2749. (
  2750. id : as_arm_pecoffwince;
  2751. idtxt : 'PECOFFWINCE';
  2752. asmbin : '';
  2753. asmcmd : '';
  2754. supported_targets : [system_arm_wince];
  2755. flags : [af_outputbinary];
  2756. labelprefix : '.L';
  2757. comment : '';
  2758. dollarsign: '$';
  2759. );
  2760. {$endif arm}
  2761. {$ifdef win32}
  2762. procedure SetupProcVars;
  2763. var
  2764. hinstLib : THandle;
  2765. begin
  2766. Wow64DisableWow64FsRedirection:=nil;
  2767. Wow64RevertWow64FsRedirection:=nil;
  2768. hinstLib:=LoadLibrary('kernel32.dll');
  2769. if hinstLib<>0 then
  2770. begin
  2771. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  2772. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  2773. FreeLibrary(hinstLib);
  2774. end;
  2775. end;
  2776. {$endif win32}
  2777. initialization
  2778. {$ifdef i386}
  2779. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  2780. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  2781. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  2782. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  2783. {$endif i386}
  2784. {$ifdef x86_64}
  2785. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  2786. {$endif x86_64}
  2787. {$ifdef arm}
  2788. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  2789. {$endif arm}
  2790. {$ifdef win32}
  2791. SetupProcVars;
  2792. {$endif win32}
  2793. end.