ogcoff.pas 107 KB

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