ogcoff.pas 100 KB

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