ogcoff.pas 97 KB

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