ogcoff.pas 98 KB

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