ogcoff.pas 108 KB

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