ogcoff.pas 82 KB

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