ogcoff.pas 112 KB

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