ogcoff.pas 84 KB

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