ogcoff.pas 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary coff/PE reader and writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogcoff;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. cpuinfo,cpubase,aasmbase,assemble,link,
  27. { output }
  28. ogbase,
  29. owbase;
  30. const
  31. PE_DATADIR_ENTRIES = 16;
  32. type
  33. tcoffpedatadir = packed record
  34. vaddr : longword;
  35. size : longword;
  36. end;
  37. tcoffheader = packed record
  38. mach : word;
  39. nsects : word;
  40. time : longword;
  41. sympos : longword;
  42. syms : longword;
  43. opthdr : word;
  44. flag : word;
  45. end;
  46. tcoffbigobjheader = packed record
  47. Sig1 : word;
  48. Sig2 : word;
  49. Version : word;
  50. Machine : word;
  51. TimeDateStame : longword;
  52. UUID : array[0..15] of byte;
  53. unused : array[0..3] of longword;
  54. NumberOfSections : longword;
  55. PointerToSymbolTable : longword;
  56. NumberOfSymbols : longword;
  57. end;
  58. tcoffpeoptheader = packed record
  59. Magic : word;
  60. MajorLinkerVersion : byte;
  61. MinorLinkerVersion : byte;
  62. tsize : longword;
  63. dsize : longword;
  64. bsize : longword;
  65. entry : longword;
  66. text_start : longword;
  67. {$ifndef cpu64bitaddr}
  68. data_start : longword;
  69. {$endif cpu64bitaddr}
  70. ImageBase : aword;
  71. SectionAlignment : longword;
  72. FileAlignment : longword;
  73. MajorOperatingSystemVersion : word;
  74. MinorOperatingSystemVersion : word;
  75. MajorImageVersion : word;
  76. MinorImageVersion : word;
  77. MajorSubsystemVersion : word;
  78. MinorSubsystemVersion : word;
  79. Win32Version : longword;
  80. SizeOfImage : longword;
  81. SizeOfHeaders : longword;
  82. CheckSum : longword;
  83. Subsystem : word;
  84. DllCharacteristics : word;
  85. SizeOfStackReserve : aword;
  86. SizeOfStackCommit : aword;
  87. SizeOfHeapReserve : aword;
  88. SizeOfHeapCommit : aword;
  89. LoaderFlags : longword; { This field is obsolete }
  90. NumberOfRvaAndSizes : longword;
  91. DataDirectory : array[0..PE_DATADIR_ENTRIES-1] of tcoffpedatadir;
  92. end;
  93. tcoffsechdr = packed record
  94. name : array[0..7] of char;
  95. vsize : longword;
  96. rvaofs : longword;
  97. datasize : longword;
  98. datapos : longword;
  99. relocpos : longword;
  100. lineno1 : longword;
  101. nrelocs : word;
  102. lineno2 : word;
  103. flags : longword;
  104. end;
  105. TCoffObjSection = class(TObjSection)
  106. private
  107. orgmempos,
  108. coffrelocs,
  109. coffrelocpos : aword;
  110. public
  111. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  112. procedure writereloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);override;
  113. end;
  114. TCoffObjData = class(TObjData)
  115. private
  116. win32 : boolean;
  117. {$ifdef arm}
  118. eVCobj : boolean;
  119. {$endif arm}
  120. public
  121. constructor createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  122. procedure CreateDebugSections;override;
  123. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  124. function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;override;
  125. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reloctype:TObjRelocationType);override;
  126. end;
  127. TDJCoffObjData = class(TCoffObjData)
  128. constructor create(const n:string);override;
  129. end;
  130. TPECoffObjData = class(TCoffObjData)
  131. constructor create(const n:string);override;
  132. end;
  133. TCoffObjOutput = class(tObjOutput)
  134. private
  135. win32 : boolean;
  136. bigobj : boolean;
  137. symidx : longint;
  138. FCoffSyms,
  139. FCoffStrs : tdynamicarray;
  140. procedure write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  141. procedure section_write_symbol(p:TObject;arg:pointer);
  142. procedure section_write_relocs(p:TObject;arg:pointer);
  143. procedure create_symbols(data:TObjData);
  144. procedure section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  145. procedure section_write_header(p:TObject;arg:pointer);
  146. protected
  147. function writedata(data:TObjData):boolean;override;
  148. public
  149. constructor createcoff(AWriter:TObjectWriter;awin32:boolean);
  150. destructor destroy;override;
  151. end;
  152. TDJCoffObjOutput = class(TCoffObjOutput)
  153. constructor create(AWriter:TObjectWriter);override;
  154. end;
  155. TPECoffObjOutput = class(TCoffObjOutput)
  156. constructor create(AWriter:TObjectWriter);override;
  157. end;
  158. TCoffObjInput = class(tObjInput)
  159. private
  160. FCoffsyms : tdynamicarray;
  161. FCoffStrs : PChar;
  162. FCoffStrSize: longword;
  163. { Convert symidx -> TObjSymbol }
  164. FSymTbl : ^TObjSymbolArray;
  165. { Convert secidx -> TObjSection }
  166. FSecCount : Longint;
  167. FSecTbl : ^TObjSectionArray;
  168. win32 : boolean;
  169. bigobj : boolean;
  170. function GetSection(secidx:longint):TObjSection;
  171. function Read_str(strpos:longword):string;
  172. procedure read_relocs(s:TCoffObjSection);
  173. procedure read_symbols(objdata:TObjData);
  174. procedure ObjSections_read_relocs(p:TObject;arg:pointer);
  175. public
  176. constructor createcoff(awin32:boolean);
  177. destructor destroy;override;
  178. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  179. end;
  180. TDJCoffObjInput = class(TCoffObjInput)
  181. constructor create;override;
  182. end;
  183. TPECoffObjInput = class(TCoffObjInput)
  184. constructor create;override;
  185. end;
  186. TCoffexeoutput = class(texeoutput)
  187. private
  188. FCoffStrs : tdynamicarray;
  189. win32 : boolean;
  190. nsects : word;
  191. nsyms,
  192. sympos : aword;
  193. datapos_offset: longword;
  194. function totalheadersize:longword;
  195. procedure ExeSectionList_pass2_header(p:TObject;arg:pointer);
  196. procedure write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  197. procedure globalsyms_write_symbol(p:TObject;arg:pointer);
  198. procedure ExeSectionList_write_header(p:TObject;arg:pointer);
  199. protected
  200. function writedata:boolean;override;
  201. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList;const aPattern:string);override;
  202. procedure DoRelocationFixup(objsec:TObjSection);override;
  203. public
  204. constructor createcoff(awin32:boolean);
  205. procedure MemPos_Header;override;
  206. procedure DataPos_Header;override;
  207. procedure DataPos_Symbols;override;
  208. end;
  209. TDJCoffexeoutput = class(TCoffexeoutput)
  210. constructor create;override;
  211. procedure MemPos_Header;override;
  212. end;
  213. TPECoffexeoutput = class(TCoffexeoutput)
  214. private
  215. FImports: TFPHashObjectList;
  216. textobjsection,
  217. idata2objsection,
  218. idata4objsection,
  219. idata5objsection,
  220. idata6objsection,
  221. idata7objsection : TObjSection;
  222. FRelocsGenerated : boolean;
  223. procedure GenerateRelocs;
  224. public
  225. constructor create;override;
  226. procedure MarkTargetSpecificSections(WorkList:TFPObjectList);override;
  227. procedure AfterUnusedSectionRemoval;override;
  228. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  229. procedure MemPos_Start;override;
  230. procedure MemPos_ExeSection(const aname:string);override;
  231. end;
  232. TObjSymbolArray = array[0..high(word)] of TObjSymbol;
  233. TObjSectionArray = array[0..high(smallint)] of TObjSection;
  234. TDJCoffAssembler = class(tinternalassembler)
  235. constructor create(info: pasminfo; smart:boolean);override;
  236. end;
  237. TPECoffassembler = class(tinternalassembler)
  238. constructor create(info: pasminfo; smart:boolean);override;
  239. end;
  240. type
  241. Treaddllproc = procedure(const dllname,funcname:string) of object;
  242. const
  243. {$ifdef i386}
  244. COFF_MAGIC = $14c;
  245. COFF_OPT_MAGIC = $10b;
  246. TLSDIR_SIZE = $18;
  247. {$endif i386}
  248. {$ifdef arm}
  249. COFF_OPT_MAGIC = $10b;
  250. TLSDIR_SIZE = $18;
  251. function COFF_MAGIC: word;
  252. const
  253. {$endif arm}
  254. {$ifdef x86_64}
  255. COFF_MAGIC = $8664;
  256. COFF_OPT_MAGIC = $20b;
  257. TLSDIR_SIZE = $28;
  258. {$endif x86_64}
  259. COFF_BIG_OBJ_MAGIC: array[0..15] of byte = ($C7, $A1, $BA, $D1, $EE, $BA, $A9, $4B, $AF, $20, $FA, $F6, $6A, $A4, $DC, $B8);
  260. COFF_BIG_OBJ_VERSION = 2;
  261. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  262. implementation
  263. uses
  264. {$ifdef win32}
  265. Windows,
  266. {$endif win32}
  267. SysUtils,
  268. cutils,verbose,globals,
  269. fmodule,aasmtai,aasmdata,
  270. ogmap,
  271. owar,
  272. version
  273. ;
  274. const
  275. COFF_FLAG_NORELOCS = $0001;
  276. COFF_FLAG_EXE = $0002;
  277. COFF_FLAG_NOLINES = $0004;
  278. COFF_FLAG_NOLSYMS = $0008;
  279. COFF_FLAG_AR16WR = $0080; { 16bit little endian }
  280. COFF_FLAG_AR32WR = $0100; { 32bit little endian }
  281. COFF_FLAG_AR32W = $0200; { 32bit big endian }
  282. COFF_FLAG_DLL = $2000;
  283. COFF_SYM_GLOBAL = 2;
  284. COFF_SYM_LOCAL = 3;
  285. COFF_SYM_LABEL = 6;
  286. COFF_SYM_FUNCTION = 101;
  287. COFF_SYM_FILE = 103;
  288. COFF_SYM_SECTION = 104;
  289. COFF_STYP_REG = $0000; { "regular": allocated, relocated, loaded }
  290. COFF_STYP_DSECT = $0001; { "dummy": relocated only }
  291. COFF_STYP_NOLOAD = $0002; { "noload": allocated, relocated, not loaded }
  292. COFF_STYP_GROUP = $0004; { "grouped": formed of input sections }
  293. COFF_STYP_PAD = $0008;
  294. COFF_STYP_COPY = $0010;
  295. COFF_STYP_TEXT = $0020;
  296. COFF_STYP_DATA = $0040;
  297. COFF_STYP_BSS = $0080;
  298. COFF_STYP_INFO = $0200;
  299. COFF_STYP_OVER = $0400;
  300. COFF_STYP_LIB = $0800;
  301. PE_SUBSYSTEM_NATIVE = 1;
  302. PE_SUBSYSTEM_WINDOWS_GUI = 2;
  303. PE_SUBSYSTEM_WINDOWS_CUI = 3;
  304. PE_SUBSYSTEM_WINDOWS_CE_GUI = 9;
  305. PE_FILE_RELOCS_STRIPPED = $0001;
  306. PE_FILE_EXECUTABLE_IMAGE = $0002;
  307. PE_FILE_LINE_NUMS_STRIPPED = $0004;
  308. PE_FILE_LOCAL_SYMS_STRIPPED = $0008;
  309. PE_FILE_AGGRESSIVE_WS_TRIM = $0010;
  310. PE_FILE_LARGE_ADDRESS_AWARE = $0020;
  311. PE_FILE_16BIT_MACHINE = $0040;
  312. PE_FILE_BYTES_REVERSED_LO = $0080;
  313. PE_FILE_32BIT_MACHINE = $0100;
  314. PE_FILE_DEBUG_STRIPPED = $0200;
  315. PE_FILE_REMOVABLE_RUN_FROM_SWAP = $0400;
  316. PE_FILE_NET_RUN_FROM_SWAP = $0800;
  317. PE_FILE_SYSTEM = $1000;
  318. PE_FILE_DLL = $2000;
  319. PE_FILE_UP_SYSTEM_ONLY = $4000;
  320. PE_FILE_BYTES_REVERSED_HI = $8000;
  321. PE_SCN_CNT_CODE = $00000020; { Section contains code. }
  322. PE_SCN_CNT_INITIALIZED_DATA = $00000040; { Section contains initialized data. }
  323. PE_SCN_CNT_UNINITIALIZED_DATA = $00000080; { Section contains uninitialized data. }
  324. PE_SCN_LNK_OTHER = $00000100; { Reserved. }
  325. PE_SCN_LNK_INFO = $00000200; { Section contains comments or some other type of information. }
  326. PE_SCN_LNK_REMOVE = $00000800; { Section contents will not become part of image. }
  327. PE_SCN_LNK_COMDAT = $00001000; { Section contents comdat. }
  328. PE_SCN_MEM_FARDATA = $00008000;
  329. PE_SCN_MEM_PURGEABLE = $00020000;
  330. PE_SCN_MEM_16BIT = $00020000;
  331. PE_SCN_MEM_LOCKED = $00040000;
  332. PE_SCN_MEM_PRELOAD = $00080000;
  333. PE_SCN_ALIGN_MASK = $00f00000;
  334. PE_SCN_ALIGN_1BYTES = $00100000;
  335. PE_SCN_ALIGN_2BYTES = $00200000;
  336. PE_SCN_ALIGN_4BYTES = $00300000;
  337. PE_SCN_ALIGN_8BYTES = $00400000;
  338. PE_SCN_ALIGN_16BYTES = $00500000; { Default alignment if no others are specified. }
  339. PE_SCN_ALIGN_32BYTES = $00600000;
  340. PE_SCN_ALIGN_64BYTES = $00700000;
  341. PE_SCN_LNK_NRELOC_OVFL = $01000000; { Section contains extended relocations. }
  342. PE_SCN_MEM_NOT_CACHED = $04000000; { Section is not cachable. }
  343. PE_SCN_MEM_NOT_PAGED = $08000000; { Section is not pageable. }
  344. PE_SCN_MEM_SHARED = $10000000; { Section is shareable. }
  345. PE_SCN_MEM_DISCARDABLE = $02000000;
  346. PE_SCN_MEM_EXECUTE = $20000000;
  347. PE_SCN_MEM_READ = $40000000;
  348. PE_SCN_MEM_WRITE = $80000000;
  349. PE_DATADIR_EDATA = 0;
  350. PE_DATADIR_IDATA = 1;
  351. PE_DATADIR_RSRC = 2;
  352. PE_DATADIR_PDATA = 3;
  353. PE_DATADIR_SECURITY = 4;
  354. PE_DATADIR_RELOC = 5;
  355. PE_DATADIR_DEBUG = 6;
  356. PE_DATADIR_DESCRIPTION = 7;
  357. PE_DATADIR_SPECIAL = 8;
  358. PE_DATADIR_TLS = 9;
  359. PE_DATADIR_LOADCFG = 10;
  360. PE_DATADIR_BOUNDIMPORT = 11;
  361. PE_DATADIR_IMPORTADDRESSTABLE = 12;
  362. PE_DATADIR_DELAYIMPORT = 13;
  363. {$ifdef x86_64}
  364. IMAGE_REL_AMD64_ABSOLUTE = $0000; { Reference is absolute, no relocation is necessary }
  365. IMAGE_REL_AMD64_ADDR64 = $0001; { 64-bit address (VA). }
  366. IMAGE_REL_AMD64_ADDR32 = $0002; { 32-bit address (VA). }
  367. IMAGE_REL_AMD64_ADDR32NB = $0003; { 32-bit address w/o image base (RVA). }
  368. IMAGE_REL_AMD64_REL32 = $0004; { 32-bit relative address from byte following reloc }
  369. IMAGE_REL_AMD64_REL32_1 = $0005; { 32-bit relative address from byte distance 1 from reloc }
  370. IMAGE_REL_AMD64_REL32_2 = $0006; { 32-bit relative address from byte distance 2 from reloc }
  371. IMAGE_REL_AMD64_REL32_3 = $0007; { 32-bit relative address from byte distance 3 from reloc }
  372. IMAGE_REL_AMD64_REL32_4 = $0008; { 32-bit relative address from byte distance 4 from reloc }
  373. IMAGE_REL_AMD64_REL32_5 = $0009; { 32-bit relative address from byte distance 5 from reloc }
  374. IMAGE_REL_AMD64_SECTION = $000A; { Section index }
  375. IMAGE_REL_AMD64_SECREL = $000B; { 32 bit offset from base of section containing target }
  376. IMAGE_REL_AMD64_SECREL7 = $000C; { 7 bit unsigned offset from base of section containing target }
  377. IMAGE_REL_AMD64_TOKEN = $000D; { 32 bit metadata token }
  378. IMAGE_REL_AMD64_SREL32 = $000E; { 32 bit signed span-dependent value emitted into object }
  379. IMAGE_REL_AMD64_PAIR = $000F;
  380. IMAGE_REL_AMD64_SSPAN32 = $0010; { 32 bit signed span-dependent value applied at link time }
  381. { Direct 32 bit sign extended,
  382. win64 mingw GNU compiler
  383. also generates this type
  384. inside coff objects
  385. We assume they are equivalent to
  386. IMAGE_REL_AMD64_ADDR32 PM 2010-11-27 }
  387. R_X86_64_32S = $11;
  388. {$endif x86_64}
  389. {$ifdef arm}
  390. IMAGE_REL_ARM_ABSOLUTE = $0000; { No relocation required }
  391. IMAGE_REL_ARM_ADDR32 = $0001; { 32 bit address }
  392. IMAGE_REL_ARM_ADDR32NB = $0002; { 32 bit address w/o image base }
  393. IMAGE_REL_ARM_BRANCH24 = $0003; { 24 bit offset << 2 & sign ext. }
  394. IMAGE_REL_ARM_BRANCH11 = $0004; { Thumb: 2 11 bit offsets }
  395. IMAGE_REL_ARM_TOKEN = $0005; { clr token }
  396. IMAGE_REL_ARM_GPREL12 = $0006; { GP-relative addressing (ARM) }
  397. IMAGE_REL_ARM_GPREL7 = $0007; { GP-relative addressing (Thumb) }
  398. IMAGE_REL_ARM_BLX24 = $0008;
  399. IMAGE_REL_ARM_BLX11 = $0009;
  400. IMAGE_REL_ARM_SECTION = $000E; { Section table index }
  401. IMAGE_REL_ARM_SECREL = $000F; { Offset within section }
  402. IMAGE_REL_ARM_MOV32A = $0010; { 32-bit VA applied to MOVW+MOVT pair, added to existing imm (ARM) }
  403. IMAGE_REL_ARM_MOV32T = $0011; { 32-bit VA applied to MOVW+MOVT pair, added to existing imm (THUMB) }
  404. IMAGE_REL_ARM_BRANCH20T = $0012; { Thumb: 20 most significant bits of 32 bit B cond instruction }
  405. IMAGE_REL_ARM_BRANCH24T = $0014; { Thumb: 24 most significant bits of 32 bit B uncond instruction }
  406. IMAGE_REL_ARM_BLX23T = $0015; { 23 most significant bits of 32 bit BL/BLX instruction. Transformed to BLX if target is Thumb }
  407. {$endif arm}
  408. {$ifdef i386}
  409. IMAGE_REL_I386_DIR32 = 6;
  410. IMAGE_REL_I386_IMAGEBASE = 7;
  411. IMAGE_REL_I386_SECREL32 = 11;
  412. IMAGE_REL_I386_PCRLONG = 20;
  413. {$endif i386}
  414. { .reloc section fixup types }
  415. IMAGE_REL_BASED_HIGHLOW = 3; { Applies the delta to the 32-bit field at Offset. }
  416. IMAGE_REL_BASED_DIR64 = 10; { Applies the delta to the 64-bit field at Offset. }
  417. { values for coffsectionrec.select }
  418. IMAGE_COMDAT_SELECT_NODUPLICATES = 1;
  419. IMAGE_COMDAT_SELECT_ANY = 2;
  420. IMAGE_COMDAT_SELECT_SAME_SIZE = 3;
  421. IMAGE_COMDAT_SELECT_EXACT_MATCH = 4;
  422. IMAGE_COMDAT_SELECT_ASSOCIATIVE = 5;
  423. IMAGE_COMDAT_SELECT_LARGEST = 6;
  424. type
  425. coffdjoptheader=packed record
  426. magic : word;
  427. vstamp : word;
  428. tsize : longint;
  429. dsize : longint;
  430. bsize : longint;
  431. entry : longint;
  432. text_start : longint;
  433. data_start : longint;
  434. end;
  435. coffsectionrec=packed record
  436. len : longword;
  437. nrelocs : word;
  438. nlines : word;
  439. checksum: longword;
  440. assoc : word;
  441. select : byte;
  442. empty : array[0..2] of char;
  443. end;
  444. coffreloc=packed record
  445. address : longword;
  446. sym : longword;
  447. reloctype : word;
  448. end;
  449. strtableoffset=packed record
  450. Zeroes : longword;
  451. Offset : longword;
  452. end;
  453. coffsymbol=packed record
  454. name : array[0..3] of char; { real is [0..7], which overlaps the strpos ! }
  455. strpos : longword;
  456. value : longword;
  457. section : smallint;
  458. empty : word; { actually type, $20: function, 0: not a function }
  459. typ : byte;
  460. aux : byte;
  461. end;
  462. coffbigobjsymbol=packed record
  463. Name : record
  464. case boolean of
  465. True: (ShortName : array[0..7] of char);
  466. False: (Offset : strtableoffset)
  467. end;
  468. Value : longword;
  469. SectionNumber : longword;
  470. _Type : word;
  471. StorageClass : byte;
  472. NumberOfAuxSymbols : byte;
  473. end;
  474. { This is defined in rtl/win/sysos.inc source }
  475. tlsdirectory=packed record
  476. data_start, data_end : PUInt;
  477. index_pointer, callbacks_pointer : PUInt;
  478. zero_fill_size : dword;
  479. flags : dword;
  480. end;
  481. const
  482. SymbolMaxGrow = 200*sizeof(coffsymbol);
  483. StrsMaxGrow = 8192;
  484. coffsecnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  485. '.text','.data','.rdata','.rdata','.bss','.tls',
  486. '.pdata',{pdata}
  487. '.text', {stub}
  488. '.data',
  489. '.data',
  490. '.data',
  491. '.data',
  492. '.stab','.stabstr',
  493. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  494. '.eh_frame',
  495. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  496. '.fpc',
  497. '',
  498. '.init',
  499. '.fini',
  500. '.objc_class',
  501. '.objc_meta_class',
  502. '.objc_cat_cls_meth',
  503. '.objc_cat_inst_meth',
  504. '.objc_protocol',
  505. '.objc_string_object',
  506. '.objc_cls_meth',
  507. '.objc_inst_meth',
  508. '.objc_cls_refs',
  509. '.objc_message_refs',
  510. '.objc_symbols',
  511. '.objc_category',
  512. '.objc_class_vars',
  513. '.objc_instance_vars',
  514. '.objc_module_info',
  515. '.objc_class_names',
  516. '.objc_meth_var_types',
  517. '.objc_meth_var_names',
  518. '.objc_selector_strs',
  519. '.objc_protocol_ext',
  520. '.objc_class_ext',
  521. '.objc_property',
  522. '.objc_image_info',
  523. '.objc_cstring_object',
  524. '.objc_sel_fixup',
  525. '__DATA,__objc_data',
  526. '__DATA,__objc_const',
  527. '.objc_superrefs',
  528. '__DATA, __datacoal_nt,coalesced',
  529. '.objc_classlist',
  530. '.objc_nlclasslist',
  531. '.objc_catlist',
  532. '.obcj_nlcatlist',
  533. '.objc_protolist',
  534. '.stack',
  535. '.heap'
  536. );
  537. const go32v2stub : array[0..2047] of byte=(
  538. $4D,$5A,$00,$00,$04,$00,$00,$00,$20,$00,$27,$00,$FF,$FF,$00,
  539. $00,$60,$07,$00,$00,$54,$00,$00,$00,$00,$00,$00,$00,$0D,$0A,
  540. $73,$74,$75,$62,$2E,$68,$20,$67,$65,$6E,$65,$72,$61,$74,$65,
  541. $64,$20,$66,$72,$6F,$6D,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,
  542. $20,$62,$79,$20,$64,$6A,$61,$73,$6D,$2C,$20,$6F,$6E,$20,$54,
  543. $68,$75,$20,$44,$65,$63,$20,$20,$39,$20,$31,$30,$3A,$35,$39,
  544. $3A,$33,$31,$20,$31,$39,$39,$39,$0D,$0A,$54,$68,$65,$20,$53,
  545. $54,$55,$42,$2E,$45,$58,$45,$20,$73,$74,$75,$62,$20,$6C,$6F,
  546. $61,$64,$65,$72,$20,$69,$73,$20,$43,$6F,$70,$79,$72,$69,$67,
  547. $68,$74,$20,$28,$43,$29,$20,$31,$39,$39,$33,$2D,$31,$39,$39,
  548. $35,$20,$44,$4A,$20,$44,$65,$6C,$6F,$72,$69,$65,$2E,$20,$0D,
  549. $0A,$50,$65,$72,$6D,$69,$73,$73,$69,$6F,$6E,$20,$67,$72,$61,
  550. $6E,$74,$65,$64,$20,$74,$6F,$20,$75,$73,$65,$20,$66,$6F,$72,
  551. $20,$61,$6E,$79,$20,$70,$75,$72,$70,$6F,$73,$65,$20,$70,$72,
  552. $6F,$76,$69,$64,$65,$64,$20,$74,$68,$69,$73,$20,$63,$6F,$70,
  553. $79,$72,$69,$67,$68,$74,$20,$0D,$0A,$72,$65,$6D,$61,$69,$6E,
  554. $73,$20,$70,$72,$65,$73,$65,$6E,$74,$20,$61,$6E,$64,$20,$75,
  555. $6E,$6D,$6F,$64,$69,$66,$69,$65,$64,$2E,$20,$0D,$0A,$54,$68,
  556. $69,$73,$20,$6F,$6E,$6C,$79,$20,$61,$70,$70,$6C,$69,$65,$73,
  557. $20,$74,$6F,$20,$74,$68,$65,$20,$73,$74,$75,$62,$2C,$20,$61,
  558. $6E,$64,$20,$6E,$6F,$74,$20,$6E,$65,$63,$65,$73,$73,$61,$72,
  559. $69,$6C,$79,$20,$74,$68,$65,$20,$77,$68,$6F,$6C,$65,$20,$70,
  560. $72,$6F,$67,$72,$61,$6D,$2E,$0A,$0D,$0A,$24,$49,$64,$3A,$20,
  561. $73,$74,$75,$62,$2E,$61,$73,$6D,$20,$62,$75,$69,$6C,$74,$20,
  562. $31,$32,$2F,$30,$39,$2F,$39,$39,$20,$31,$30,$3A,$35,$39,$3A,
  563. $33,$31,$20,$62,$79,$20,$64,$6A,$61,$73,$6D,$20,$24,$0A,$0D,
  564. $0A,$40,$28,$23,$29,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,$20,
  565. $62,$75,$69,$6C,$74,$20,$31,$32,$2F,$30,$39,$2F,$39,$39,$20,
  566. $31,$30,$3A,$35,$39,$3A,$33,$31,$20,$62,$79,$20,$64,$6A,$61,
  567. $73,$6D,$0A,$0D,$0A,$1A,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  568. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  569. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  570. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  571. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  572. $00,$00,$67,$6F,$33,$32,$73,$74,$75,$62,$2C,$20,$76,$20,$32,
  573. $2E,$30,$32,$54,$00,$00,$00,$00,$00,$08,$00,$00,$00,$00,$00,
  574. $00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  575. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  576. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$43,$57,$53,$44,$50,
  577. $4D,$49,$2E,$45,$58,$45,$00,$00,$00,$00,$00,$0E,$1F,$8C,$1E,
  578. $24,$00,$8C,$06,$60,$07,$FC,$B4,$30,$CD,$21,$3C,$03,$73,$08,
  579. $B0,$6D,$BA,$A7,$05,$E9,$D4,$03,$A2,$69,$08,$BE,$20,$00,$8B,
  580. $04,$09,$C0,$75,$02,$B4,$FE,$BB,$70,$08,$39,$C3,$73,$02,$89,
  581. $C3,$89,$1C,$FE,$C7,$B9,$04,$FF,$D3,$EB,$B4,$4A,$CD,$21,$73,
  582. $08,$D3,$E3,$FE,$CF,$89,$1C,$EB,$D8,$26,$8E,$06,$2C,$00,$31,
  583. $FF,$30,$C0,$A9,$F2,$AE,$26,$81,$3D,$50,$41,$75,$15,$AF,$26,
  584. $81,$3D,$54,$48,$75,$0D,$AF,$26,$80,$3D,$3D,$75,$06,$47,$89,
  585. $3E,$8C,$04,$4F,$AE,$75,$DF,$AF,$B4,$3E,$BB,$13,$00,$CD,$21,
  586. $B4,$3E,$BB,$12,$00,$CD,$21,$06,$57,$31,$C9,$74,$12,$B0,$6E,
  587. $BA,$7E,$05,$E9,$5E,$03,$09,$C9,$75,$F4,$41,$E8,$A1,$03,$72,
  588. $EE,$B8,$87,$16,$CD,$2F,$09,$C0,$75,$ED,$80,$E3,$01,$74,$E8,
  589. $89,$3E,$00,$06,$8C,$06,$02,$06,$89,$36,$04,$06,$5F,$07,$E8,
  590. $D3,$02,$89,$3E,$2A,$00,$89,$36,$62,$07,$80,$3E,$2C,$00,$00,
  591. $74,$23,$B9,$08,$00,$BF,$2C,$00,$8A,$05,$47,$08,$C0,$74,$05,
  592. $88,$07,$43,$E2,$F4,$66,$C7,$07,$2E,$45,$58,$45,$83,$C3,$04,
  593. $C6,$07,$00,$89,$1E,$62,$07,$B8,$00,$3D,$BA,$64,$07,$CD,$21,
  594. $0F,$82,$B3,$02,$A3,$06,$06,$89,$C3,$B9,$06,$00,$BA,$B5,$07,
  595. $B4,$3F,$CD,$21,$31,$D2,$31,$C9,$A1,$B5,$07,$3D,$4C,$01,$74,
  596. $1B,$3D,$4D,$5A,$0F,$85,$98,$02,$8B,$16,$B9,$07,$C1,$E2,$09,
  597. $8B,$1E,$B7,$07,$09,$DB,$74,$05,$80,$EE,$02,$01,$DA,$89,$16,
  598. $BB,$07,$89,$0E,$BD,$07,$B8,$00,$42,$8B,$1E,$06,$06,$CD,$21,
  599. $B9,$A8,$00,$BA,$BF,$07,$B4,$3F,$CD,$21,$3D,$A8,$00,$75,$06,
  600. $81,$3E,$BF,$07,$4C,$01,$0F,$85,$61,$02,$66,$A1,$E3,$07,$66,
  601. $A3,$10,$06,$66,$8B,$0E,$BB,$07,$66,$A1,$03,$08,$66,$01,$C8,
  602. $66,$A3,$08,$06,$66,$A1,$2B,$08,$66,$01,$C8,$66,$A3,$0C,$06,
  603. $66,$8B,$1E,$4B,$08,$66,$A1,$4F,$08,$66,$01,$C3,$66,$B8,$01,
  604. $00,$01,$00,$66,$39,$C3,$73,$03,$66,$89,$C3,$66,$81,$C3,$FF,
  605. $FF,$00,$00,$31,$DB,$66,$89,$1E,$1C,$00,$E8,$F5,$02,$8B,$1E,
  606. $04,$06,$09,$DB,$74,$0A,$B4,$48,$CD,$21,$0F,$82,$15,$02,$8E,
  607. $C0,$E8,$08,$03,$B8,$01,$00,$FF,$1E,$00,$06,$0F,$82,$0F,$02,
  608. $8C,$06,$26,$00,$8C,$0E,$28,$00,$8C,$D8,$A3,$22,$00,$8E,$C0,
  609. $31,$C0,$B9,$01,$00,$CD,$31,$72,$07,$A3,$14,$06,$31,$C0,$CD,
  610. $31,$0F,$82,$F3,$01,$A3,$16,$06,$66,$8B,$0E,$1C,$00,$B8,$01,
  611. $05,$8B,$1E,$1E,$00,$CD,$31,$0F,$82,$E5,$01,$89,$1E,$1A,$06,
  612. $89,$0E,$18,$06,$89,$36,$1A,$00,$89,$3E,$18,$00,$B8,$07,$00,
  613. $8B,$1E,$14,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,$CD,$31,$B8,
  614. $09,$00,$8C,$C9,$83,$E1,$03,$C1,$E1,$05,$51,$81,$C9,$9B,$C0,
  615. $CD,$31,$B8,$08,$00,$8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,
  616. $B8,$07,$00,$8B,$1E,$16,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,
  617. $CD,$31,$B8,$09,$00,$59,$81,$C9,$93,$C0,$CD,$31,$B8,$08,$00,
  618. $8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,$B8,$00,$01,$BB,$00,
  619. $0F,$CD,$31,$73,$10,$3D,$08,$00,$0F,$85,$73,$01,$B8,$00,$01,
  620. $CD,$31,$0F,$82,$6A,$01,$A3,$1C,$06,$89,$16,$1E,$06,$C1,$E3,
  621. $04,$89,$1E,$20,$06,$66,$8B,$36,$08,$06,$66,$8B,$3E,$FB,$07,
  622. $66,$8B,$0E,$FF,$07,$E8,$49,$00,$66,$8B,$36,$0C,$06,$66,$8B,
  623. $3E,$23,$08,$66,$8B,$0E,$27,$08,$E8,$37,$00,$8E,$06,$16,$06,
  624. $66,$8B,$3E,$4B,$08,$66,$8B,$0E,$4F,$08,$66,$31,$C0,$66,$C1,
  625. $E9,$02,$67,$F3,$66,$AB,$B4,$3E,$8B,$1E,$06,$06,$CD,$21,$B8,
  626. $01,$01,$8B,$16,$1E,$06,$CD,$31,$1E,$0F,$A1,$8E,$1E,$16,$06,
  627. $66,$64,$FF,$2E,$10,$06,$66,$89,$F0,$66,$25,$FF,$01,$00,$00,
  628. $66,$01,$C1,$29,$C6,$66,$29,$C7,$66,$89,$0E,$26,$06,$66,$89,
  629. $3E,$22,$06,$E8,$0F,$01,$89,$36,$3E,$06,$66,$C1,$EE,$10,$89,
  630. $36,$42,$06,$8B,$1E,$06,$06,$89,$1E,$3A,$06,$C7,$06,$46,$06,
  631. $00,$42,$E8,$03,$01,$A1,$1C,$06,$A3,$4E,$06,$C7,$06,$3E,$06,
  632. $00,$00,$C6,$06,$47,$06,$3F,$A1,$28,$06,$09,$C0,$75,$09,$A1,
  633. $26,$06,$3B,$06,$20,$06,$76,$03,$A1,$20,$06,$A3,$42,$06,$E8,
  634. $D9,$00,$66,$31,$C9,$8B,$0E,$46,$06,$66,$8B,$3E,$22,$06,$66,
  635. $01,$0E,$22,$06,$66,$29,$0E,$26,$06,$66,$31,$F6,$C1,$E9,$02,
  636. $1E,$06,$8E,$06,$16,$06,$8E,$1E,$1E,$06,$67,$F3,$66,$A5,$07,
  637. $1F,$66,$03,$0E,$26,$06,$75,$AF,$C3,$3C,$3A,$74,$06,$3C,$2F,
  638. $74,$02,$3C,$5C,$C3,$BE,$64,$07,$89,$F3,$26,$8A,$05,$47,$88,
  639. $04,$38,$E0,$74,$0E,$08,$C0,$74,$0A,$46,$E8,$DE,$FF,$75,$EC,
  640. $89,$F3,$74,$E8,$C3,$B0,$66,$BA,$48,$05,$EB,$0C,$B0,$67,$BA,
  641. $55,$05,$EB,$05,$B0,$68,$BA,$5F,$05,$52,$8B,$1E,$62,$07,$C6,
  642. $07,$24,$BB,$64,$07,$EB,$28,$E8,$F5,$00,$B0,$69,$BA,$99,$05,
  643. $EB,$1A,$B0,$6A,$BA,$B2,$05,$EB,$13,$B0,$6B,$BA,$C4,$05,$EB,
  644. $0C,$B0,$6C,$BA,$D6,$05,$EB,$05,$B0,$69,$BA,$99,$05,$52,$BB,
  645. $3B,$05,$E8,$15,$00,$5B,$E8,$11,$00,$BB,$67,$04,$E8,$0B,$00,
  646. $B4,$4C,$CD,$21,$43,$50,$B4,$02,$CD,$21,$58,$8A,$17,$80,$FA,
  647. $24,$75,$F2,$C3,$0D,$0A,$24,$50,$51,$57,$31,$C0,$BF,$2A,$06,
  648. $B9,$19,$00,$F3,$AB,$5F,$59,$58,$C3,$B8,$00,$03,$BB,$21,$00,
  649. $31,$C9,$66,$BF,$2A,$06,$00,$00,$CD,$31,$C3,$00,$00,$30,$E4,
  650. $E8,$4E,$FF,$89,$DE,$8B,$3E,$8C,$04,$EB,$17,$B4,$3B,$E8,$41,
  651. $FF,$81,$FE,$64,$07,$74,$12,$8A,$44,$FF,$E8,$2A,$FF,$74,$04,
  652. $C6,$04,$5C,$46,$E8,$03,$00,$72,$E4,$C3,$E8,$34,$00,$BB,$44,
  653. $00,$8A,$07,$88,$04,$43,$46,$08,$C0,$75,$F6,$06,$57,$1E,$07,
  654. $E8,$9B,$FF,$BB,$2A,$06,$8C,$5F,$04,$89,$5F,$02,$BA,$64,$07,
  655. $B8,$00,$4B,$CD,$21,$5F,$07,$72,$09,$B4,$4D,$CD,$21,$2D,$00,
  656. $03,$F7,$D8,$EB,$28,$80,$3E,$69,$08,$05,$72,$20,$B8,$00,$58,
  657. $CD,$21,$A2,$67,$08,$B8,$02,$58,$CD,$21,$A2,$68,$08,$B8,$01,
  658. $58,$BB,$80,$00,$CD,$21,$B8,$03,$58,$BB,$01,$00,$CD,$21,$C3,
  659. $9C,$80,$3E,$69,$08,$05,$72,$1A,$50,$53,$B8,$03,$58,$8A,$1E,
  660. $68,$08,$30,$FF,$CD,$21,$B8,$01,$58,$8A,$1E,$67,$08,$30,$FF,
  661. $CD,$21,$5B,$58,$9D,$C3,$4C,$6F,$61,$64,$20,$65,$72,$72,$6F,
  662. $72,$3A,$20,$24,$3A,$20,$63,$61,$6E,$27,$74,$20,$6F,$70,$65,
  663. $6E,$24,$3A,$20,$6E,$6F,$74,$20,$45,$58,$45,$24,$3A,$20,$6E,
  664. $6F,$74,$20,$43,$4F,$46,$46,$20,$28,$43,$68,$65,$63,$6B,$20,
  665. $66,$6F,$72,$20,$76,$69,$72,$75,$73,$65,$73,$29,$24,$6E,$6F,
  666. $20,$44,$50,$4D,$49,$20,$2D,$20,$47,$65,$74,$20,$63,$73,$64,
  667. $70,$6D,$69,$2A,$62,$2E,$7A,$69,$70,$24,$6E,$6F,$20,$44,$4F,
  668. $53,$20,$6D,$65,$6D,$6F,$72,$79,$24,$6E,$65,$65,$64,$20,$44,
  669. $4F,$53,$20,$33,$24,$63,$61,$6E,$27,$74,$20,$73,$77,$69,$74,
  670. $63,$68,$20,$6D,$6F,$64,$65,$24,$6E,$6F,$20,$44,$50,$4D,$49,
  671. $20,$73,$65,$6C,$65,$63,$74,$6F,$72,$73,$24,$6E,$6F,$20,$44,
  672. $50,$4D,$49,$20,$6D,$65,$6D,$6F,$72,$79,$24,$90,$90,$90,$90,
  673. $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,
  674. $90,$90,$90,$90,$90,$90,$90,$90);
  675. const win32stub : array[0..127] of byte=(
  676. $4D,$5A,$90,$00,$03,$00,$00,$00,$04,$00,$00,$00,$FF,$FF,$00,$00,
  677. $B8,$00,$00,$00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,
  678. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  679. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$80,$00,$00,$00,
  680. $0E,$1F,$BA,$0E,$00,$B4,$09,$CD,$21,$B8,$01,$4C,$CD,$21,$54,$68,
  681. $69,$73,$20,$70,$72,$6F,$67,$72,$61,$6D,$20,$63,$61,$6E,$6E,$6F,
  682. $74,$20,$62,$65,$20,$72,$75,$6E,$20,$69,$6E,$20,$44,$4F,$53,$20,
  683. $6D,$6F,$64,$65,$2E,$0D,$0D,$0A,$24,$00,$00,$00,$00,$00,$00,$00);
  684. const pemagic : array[0..3] of byte = (
  685. $50,$45,$00,$00);
  686. {****************************************************************************
  687. Helpers
  688. ****************************************************************************}
  689. function djencodesechdrflags(aoptions:TObjSectionOptions):longword;
  690. begin
  691. if (oso_load in aoptions) then
  692. begin
  693. if oso_executable in aoptions then
  694. result:=COFF_STYP_TEXT
  695. else if not(oso_data in aoptions) then
  696. result:=COFF_STYP_BSS
  697. else
  698. result:=COFF_STYP_DATA;
  699. end
  700. else if oso_debug in aoptions then
  701. result:=COFF_STYP_INFO
  702. else
  703. result:=COFF_STYP_REG;
  704. end;
  705. function djdecodesechdrflags(const aname:string;flags:longword):TObjSectionOptions;
  706. begin
  707. result:=[];
  708. if flags and COFF_STYP_TEXT<>0 then
  709. result:=[oso_data,oso_load,oso_executable]
  710. else if flags and COFF_STYP_BSS<>0 then
  711. result:=[oso_load]
  712. else if flags and COFF_STYP_DATA<>0 then
  713. result:=[oso_data,oso_load]
  714. else if flags and COFF_STYP_INFO<>0 then
  715. result:=[oso_data,oso_debug]
  716. else
  717. result:=[oso_data]
  718. end;
  719. function peencodesechdrflags(aoptions:TObjSectionOptions;aalign:shortint):longword;
  720. begin
  721. if oso_executable in aoptions then
  722. result:=PE_SCN_CNT_CODE or PE_SCN_MEM_EXECUTE
  723. else
  724. begin
  725. if (oso_data in aoptions) then
  726. result:=PE_SCN_CNT_INITIALIZED_DATA
  727. else
  728. result:=PE_SCN_CNT_UNINITIALIZED_DATA;
  729. end;
  730. if oso_write in aoptions then
  731. result:=result or PE_SCN_MEM_WRITE or PE_SCN_MEM_READ
  732. else
  733. result:=result or PE_SCN_MEM_READ;
  734. if not (oso_load in aoptions) then
  735. result:=result or PE_SCN_MEM_DISCARDABLE;
  736. case aalign of
  737. 1 : result:=result or PE_SCN_ALIGN_1BYTES;
  738. 2 : result:=result or PE_SCN_ALIGN_2BYTES;
  739. 4 : result:=result or PE_SCN_ALIGN_4BYTES;
  740. 8 : result:=result or PE_SCN_ALIGN_8BYTES;
  741. 16 : result:=result or PE_SCN_ALIGN_16BYTES;
  742. 32 : result:=result or PE_SCN_ALIGN_32BYTES;
  743. 64 : result:=result or PE_SCN_ALIGN_64BYTES;
  744. else result:=result or PE_SCN_ALIGN_16BYTES;
  745. end;
  746. end;
  747. procedure pedecodesechdrflags(const aname:string;flags:longword;out aoptions:TObjSectionOptions;out aalign:shortint);
  748. var
  749. alignflag : longword;
  750. begin
  751. aoptions:=[];
  752. if flags and PE_SCN_CNT_CODE<>0 then
  753. include(aoptions,oso_executable);
  754. if flags and PE_SCN_MEM_DISCARDABLE<>0 then
  755. include(aoptions,oso_debug);
  756. if flags and PE_SCN_CNT_UNINITIALIZED_DATA=0 then
  757. include(aoptions,oso_data);
  758. if (flags and (PE_SCN_LNK_REMOVE or PE_SCN_MEM_DISCARDABLE)=0) then
  759. include(aoptions,oso_load);
  760. { read/write }
  761. if flags and PE_SCN_MEM_WRITE<>0 then
  762. include(aoptions,oso_write);
  763. { alignment }
  764. alignflag:=flags and PE_SCN_ALIGN_MASK;
  765. if alignflag=PE_SCN_ALIGN_64BYTES then
  766. aalign:=64
  767. else if alignflag=PE_SCN_ALIGN_32BYTES then
  768. aalign:=32
  769. else if alignflag=PE_SCN_ALIGN_16BYTES then
  770. aalign:=16
  771. else if alignflag=PE_SCN_ALIGN_8BYTES then
  772. aalign:=8
  773. else if alignflag=PE_SCN_ALIGN_4BYTES then
  774. aalign:=4
  775. else if alignflag=PE_SCN_ALIGN_2BYTES then
  776. aalign:=2
  777. else if alignflag=PE_SCN_ALIGN_1BYTES then
  778. aalign:=1
  779. else if alignflag=0 then
  780. aalign:=0
  781. else
  782. Internalerror(2009050401);
  783. end;
  784. {****************************************************************************
  785. TCoffObjSection
  786. ****************************************************************************}
  787. constructor TCoffObjSection.create(AList:TFPHashObjectList;const aname:string;aalign:shortint;aoptions:TObjSectionOptions);
  788. begin
  789. inherited create(AList,aname,aalign,aoptions);
  790. end;
  791. procedure TCoffObjSection.writereloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  792. begin
  793. AddSectionReloc(size,aTarget,reltype);
  794. write(offset,len);
  795. end;
  796. { We don't want overflow nor range checks here,
  797. wrapping is accepted in the address computation below }
  798. {$r-}
  799. {$q-}
  800. procedure TCoffExeOutput.DoRelocationFixup(objsec:TObjSection);
  801. var
  802. i,zero,address_size : longint;
  803. objreloc : TObjRelocation;
  804. address,
  805. relocval : aint;
  806. {$ifdef arm}
  807. addend : aint;
  808. {$endif arm}
  809. relocsec : TObjSection;
  810. {$ifdef cpu64bitaddr}
  811. s : string;
  812. {$endif cpu64bitaddr}
  813. data : TDynamicArray;
  814. begin
  815. data:=objsec.data;
  816. for i:=0 to objsec.ObjRelocations.Count-1 do
  817. begin
  818. objreloc:=TObjRelocation(objsec.ObjRelocations[i]);
  819. address_size:=4;
  820. case objreloc.typ of
  821. RELOC_NONE:
  822. continue;
  823. RELOC_ZERO:
  824. begin
  825. data.Seek(objreloc.dataoffset);
  826. zero:=0;
  827. data.Write(zero,4);
  828. continue;
  829. end;
  830. {$ifdef cpu64bitaddr}
  831. RELOC_ABSOLUTE:
  832. address_size:=8;
  833. {$endif cpu64bitaddr}
  834. end;
  835. address:=0;
  836. data.Seek(objreloc.dataoffset);
  837. data.Read(address,address_size);
  838. if assigned(objreloc.symbol) then
  839. begin
  840. relocsec:=objreloc.symbol.objsection;
  841. relocval:=objreloc.symbol.address;
  842. end
  843. else
  844. if assigned(objreloc.objsection) then
  845. begin
  846. relocsec:=objreloc.objsection;
  847. relocval:=objreloc.objsection.mempos
  848. end
  849. else
  850. internalerror(200205183);
  851. { Only debug sections are allowed to have relocs pointing to unused sections }
  852. if not relocsec.used and not (oso_debug in objsec.secoptions) then
  853. internalerror(200603061);
  854. if relocsec.used then
  855. case objreloc.typ of
  856. RELOC_RELATIVE :
  857. begin
  858. address:=address-objsec.mempos+relocval;
  859. if win32 then
  860. dec(address,objreloc.dataoffset+4);
  861. end;
  862. RELOC_RVA:
  863. begin
  864. { fixup address when the symbol was known in defined object }
  865. if (relocsec.objdata=objsec.objdata) then
  866. dec(address,TCoffObjSection(relocsec).orgmempos);
  867. {$ifdef arm}
  868. if (relocsec.objdata=objsec.objdata) and not TCoffObjData(objsec.objdata).eVCobj then
  869. inc(address, relocsec.MemPos)
  870. else
  871. {$endif arm}
  872. inc(address,relocval);
  873. end;
  874. RELOC_SECREL32 :
  875. begin
  876. { fixup address when the symbol was known in defined object }
  877. if (relocsec.objdata=objsec.objdata) then
  878. dec(address,relocsec.ExeSection.MemPos);
  879. inc(address,relocval);
  880. end;
  881. {$ifdef arm}
  882. RELOC_RELATIVE_24,
  883. RELOC_RELATIVE_CALL:
  884. begin
  885. addend:=sarlongint(((address and $ffffff) shl 8),6); // Sign-extend while shifting left twice
  886. relocval:=longint(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  887. address:=(address and $ff000000) or (relocval and $ffffff);
  888. relocval:=relocval shr 24;
  889. if (relocval<>$3f) and (relocval<>0) then
  890. internalerror(200606085); { offset overflow }
  891. end;
  892. RELOC_RELATIVE_24_THUMB,
  893. RELOC_RELATIVE_CALL_THUMB:
  894. begin
  895. addend:=sarlongint(((address and $ffffff) shl 8),6); // Sign-extend while shifting left twice, the assembler never sets the H bit
  896. relocval:=longint(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 1;
  897. address:=(address and $ff000000) or ((relocval shr 1) and $ffffff) or ((relocval and 1) shl 24);
  898. relocval:=relocval shr 25;
  899. if (relocval<>$3f) and (relocval<>0) then
  900. internalerror(200606085); { offset overflow }
  901. end;
  902. {$endif arm}
  903. {$ifdef x86_64}
  904. { 64 bit coff only }
  905. RELOC_RELATIVE_1:
  906. begin
  907. address:=address-objsec.mempos+relocval;
  908. dec(address,objreloc.dataoffset+5);
  909. end;
  910. RELOC_RELATIVE_2:
  911. begin
  912. address:=address-objsec.mempos+relocval;
  913. dec(address,objreloc.dataoffset+6);
  914. end;
  915. RELOC_RELATIVE_3:
  916. begin
  917. address:=address-objsec.mempos+relocval;
  918. dec(address,objreloc.dataoffset+7);
  919. end;
  920. RELOC_RELATIVE_4:
  921. begin
  922. address:=address-objsec.mempos+relocval;
  923. dec(address,objreloc.dataoffset+8);
  924. end;
  925. RELOC_RELATIVE_5:
  926. begin
  927. address:=address-objsec.mempos+relocval;
  928. dec(address,objreloc.dataoffset+9);
  929. end;
  930. RELOC_ABSOLUTE32,
  931. {$endif x86_64}
  932. RELOC_ABSOLUTE :
  933. begin
  934. if (not win32) and assigned(objreloc.symbol) and
  935. (objreloc.symbol.bind=AB_COMMON) then
  936. begin
  937. dec(address,objreloc.symbol.size);
  938. end
  939. else
  940. begin
  941. { fixup address when the symbol was known in defined object }
  942. if (relocsec.objdata=objsec.objdata) then
  943. dec(address,TCoffObjSection(relocsec).orgmempos);
  944. end;
  945. {$ifdef arm}
  946. if (relocsec.objdata=objsec.objdata) and not TCoffObjData(objsec.objdata).eVCobj then
  947. inc(address, relocsec.MemPos)
  948. else
  949. {$endif arm}
  950. inc(address,relocval);
  951. inc(address,imagebase);
  952. end;
  953. else
  954. internalerror(200604014);
  955. end
  956. else
  957. address:=0; { Relocation in debug section points to unused section, which is eliminated by linker }
  958. data.Seek(objreloc.dataoffset);
  959. data.Write(address,address_size);
  960. {$ifdef cpu64bitaddr}
  961. if (objreloc.typ = RELOC_ABSOLUTE32) and (objsec.name <> '.stab') then
  962. begin
  963. if assigned(objreloc.symbol) then
  964. s:=objreloc.symbol.Name
  965. else
  966. s:=objreloc.objsection.Name;
  967. Message2(link_w_32bit_absolute_reloc, objsec.ObjData.Name, s);
  968. end;
  969. {$endif cpu64bitaddr}
  970. end;
  971. end;
  972. {****************************************************************************
  973. TCoffObjData
  974. ****************************************************************************}
  975. constructor TCoffObjData.createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  976. begin
  977. inherited create(n);
  978. CObjSection:=ACObjSection;
  979. win32:=awin32;
  980. end;
  981. function TCoffObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  982. var
  983. sep : string[3];
  984. secname : string;
  985. begin
  986. { section type user gives the user full controll on the section name }
  987. if atype=sec_user then
  988. result:=aname
  989. else
  990. begin
  991. { non-PECOFF targets lack rodata support }
  992. if (atype in [sec_rodata,sec_rodata_norel]) and
  993. not (target_info.system in systems_all_windows) then
  994. atype:=sec_data;
  995. secname:=coffsecnames[atype];
  996. if create_smartlink_sections and
  997. (aname<>'') then
  998. begin
  999. case aorder of
  1000. secorder_begin :
  1001. sep:='.b_';
  1002. secorder_end :
  1003. sep:='.z_';
  1004. else
  1005. sep:='.n_';
  1006. end;
  1007. result:=secname+sep+aname
  1008. end
  1009. else
  1010. result:=secname;
  1011. end;
  1012. end;
  1013. function TCoffObjData.sectiontype2options(aType:TAsmSectionType): TObjSectionOptions;
  1014. begin
  1015. if (aType in [sec_rodata,sec_rodata_norel]) then
  1016. begin
  1017. if (target_info.system in systems_all_windows) then
  1018. aType:=sec_rodata_norel
  1019. else
  1020. aType:=sec_data;
  1021. end;
  1022. result:=inherited sectiontype2options(aType);
  1023. end;
  1024. procedure TCoffObjData.CreateDebugSections;
  1025. begin
  1026. if target_dbg.id=dbg_stabs then
  1027. begin
  1028. stabssec:=createsection(sec_stab);
  1029. stabstrsec:=createsection(sec_stabstr);
  1030. end;
  1031. end;
  1032. procedure TCoffObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reloctype:TObjRelocationType);
  1033. var
  1034. curraddr,
  1035. symaddr : aword;
  1036. begin
  1037. if CurrObjSec=nil then
  1038. internalerror(200403072);
  1039. if assigned(p) then
  1040. begin
  1041. { current address }
  1042. curraddr:=CurrObjSec.mempos+CurrObjSec.Size;
  1043. { external/common symbols don't have a fixed memory position yet }
  1044. if (p.bind=AB_COMMON) then
  1045. begin
  1046. { For go32v2 we need to use the size as address }
  1047. if not win32 then
  1048. symaddr:=p.size
  1049. else
  1050. symaddr:=0;
  1051. end
  1052. else
  1053. symaddr:=p.address;
  1054. { no symbol relocation need inside a section }
  1055. if (p.objsection=CurrObjSec) and
  1056. (p.bind<>AB_COMMON) then
  1057. begin
  1058. case reloctype of
  1059. RELOC_ABSOLUTE :
  1060. begin
  1061. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE);
  1062. inc(data,symaddr);
  1063. end;
  1064. {$ifdef cpu64bitaddr}
  1065. RELOC_ABSOLUTE32 :
  1066. begin
  1067. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE32);
  1068. inc(data,symaddr);
  1069. end;
  1070. {$endif cpu64bitaddr}
  1071. RELOC_RELATIVE :
  1072. begin
  1073. //inc(data,symaddr-len-CurrObjSec.Size);
  1074. data:=data+symaddr-len-CurrObjSec.Size;
  1075. end;
  1076. {$ifdef ARM}
  1077. RELOC_RELATIVE_24,
  1078. RELOC_RELATIVE_CALL:
  1079. begin
  1080. data:=(data and $ff000000) or (((((data and $ffffff) shl 2)+(symaddr-CurrObjSec.Size)) shr 2) and $FFFFFF); // TODO: Check overflow
  1081. end;
  1082. {$endif ARM}
  1083. RELOC_RVA,
  1084. RELOC_SECREL32 :
  1085. begin
  1086. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,reloctype);
  1087. inc(data,symaddr);
  1088. end;
  1089. else
  1090. internalerror(200604013);
  1091. end;
  1092. end
  1093. else
  1094. begin
  1095. if (p.objsection<>nil) and
  1096. (p.bind<>AB_COMMON) then
  1097. CurrObjSec.addsectionreloc(curraddr,p.objsection,reloctype)
  1098. else
  1099. CurrObjSec.addsymreloc(curraddr,p,reloctype);
  1100. if (not win32) or
  1101. (p.objsection<>nil) then
  1102. inc(data,symaddr);
  1103. if reloctype=RELOC_RELATIVE then
  1104. begin
  1105. if win32 then
  1106. dec(data,len-4)
  1107. else
  1108. dec(data,len+CurrObjSec.Size);
  1109. end;
  1110. end;
  1111. end
  1112. else
  1113. begin
  1114. if reloctype=RELOC_RVA then
  1115. internalerror(200603033);
  1116. end;
  1117. CurrObjSec.write(data,len);
  1118. end;
  1119. {****************************************************************************
  1120. TDJCoffObjData
  1121. ****************************************************************************}
  1122. constructor TDJCoffObjData.create(const n:string);
  1123. begin
  1124. inherited createcoff(n,false,TCoffObjSection);
  1125. end;
  1126. {****************************************************************************
  1127. TPECoffObjData
  1128. ****************************************************************************}
  1129. constructor TPECoffObjData.create(const n:string);
  1130. begin
  1131. inherited createcoff(n,true,TCoffObjSection);
  1132. end;
  1133. {****************************************************************************
  1134. TCoffObjOutput
  1135. ****************************************************************************}
  1136. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1137. begin
  1138. inherited create(AWriter);
  1139. win32:=awin32;
  1140. end;
  1141. destructor TCoffObjOutput.destroy;
  1142. begin
  1143. FCoffSyms.free;
  1144. FCoffStrs.free;
  1145. inherited destroy;
  1146. end;
  1147. procedure TCoffObjOutput.write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  1148. var
  1149. sym : coffsymbol;
  1150. bosym : coffbigobjsymbol;
  1151. strpos : longword;
  1152. begin
  1153. { symbolname }
  1154. if length(name)>8 then
  1155. begin
  1156. strpos:=FCoffStrs.size+4;
  1157. FCoffStrs.writestr(name);
  1158. FCoffStrs.writestr(#0);
  1159. end
  1160. else
  1161. strpos:=0;
  1162. if bigobj then
  1163. begin
  1164. fillchar(bosym,sizeof(bosym),0);
  1165. if length(name)>8 then
  1166. bosym.name.offset.offset:=strpos
  1167. else
  1168. move(name[1],bosym.name.shortname,length(name));
  1169. bosym.value:=value;
  1170. bosym.SectionNumber:=longword(section);
  1171. bosym.StorageClass:=typ;
  1172. bosym.NumberOfAuxSymbols:=aux;
  1173. inc(symidx);
  1174. FCoffSyms.write(bosym,sizeof(bosym));
  1175. end
  1176. else
  1177. begin
  1178. if section>$7fff then
  1179. internalerror(2017020302);
  1180. FillChar(sym,sizeof(sym),0);
  1181. if length(name)>8 then
  1182. sym.strpos:=strpos
  1183. else
  1184. move(name[1],sym.name,length(name));
  1185. sym.value:=value;
  1186. sym.section:=section;
  1187. sym.typ:=typ;
  1188. sym.aux:=aux;
  1189. inc(symidx);
  1190. FCoffSyms.write(sym,sizeof(sym));
  1191. end;
  1192. end;
  1193. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1194. var
  1195. secrec : coffsectionrec;
  1196. padding : word;
  1197. begin
  1198. with TCoffObjSection(p) do
  1199. begin
  1200. Inc(plongword(arg)^);
  1201. index:=plongword(arg)^;
  1202. secsymidx:=symidx;
  1203. { Both GNU and Microsoft toolchains write section symbols using
  1204. storage class 3 (STATIC).
  1205. No reason to use COFF_SYM_SECTION, it is silently converted to 3 by
  1206. PE binutils and causes warnings with DJGPP binutils. }
  1207. write_symbol(name,mempos,index,COFF_SYM_LOCAL,1);
  1208. { AUX }
  1209. fillchar(secrec,sizeof(secrec),0);
  1210. secrec.len:=Size;
  1211. if ObjRelocations.count<65535 then
  1212. secrec.nrelocs:=ObjRelocations.count
  1213. else
  1214. secrec.nrelocs:=65535;
  1215. inc(symidx);
  1216. FCoffSyms.write(secrec,sizeof(secrec));
  1217. { aux recs have the same size as symbols, so we need to add two
  1218. Byte of padding in case of a Big Obj Coff }
  1219. padding:=0;
  1220. if bigobj then
  1221. FCoffSyms.write(padding,sizeof(padding));
  1222. end;
  1223. end;
  1224. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1225. var
  1226. i : longint;
  1227. rel : coffreloc;
  1228. objreloc : TObjRelocation;
  1229. begin
  1230. if (TObjSection(p).ObjRelocations.Count>65535) then
  1231. begin
  1232. rel.address:=TObjSection(p).ObjRelocations.Count+1;
  1233. rel.sym:=0;
  1234. rel.reloctype:=0;
  1235. FWriter.Write(rel,sizeof(rel));
  1236. end;
  1237. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1238. begin
  1239. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1240. rel.address:=objreloc.dataoffset;
  1241. if assigned(objreloc.symbol) then
  1242. begin
  1243. if (objreloc.symbol.bind=AB_LOCAL) then
  1244. rel.sym:=objreloc.symbol.objsection.secsymidx
  1245. else
  1246. begin
  1247. if objreloc.symbol.symidx=-1 then
  1248. internalerror(200602233);
  1249. rel.sym:=objreloc.symbol.symidx;
  1250. end;
  1251. end
  1252. else
  1253. begin
  1254. if objreloc.objsection<>nil then
  1255. rel.sym:=objreloc.objsection.secsymidx
  1256. else
  1257. rel.sym:=0;
  1258. end;
  1259. case objreloc.typ of
  1260. {$ifdef arm}
  1261. RELOC_ABSOLUTE :
  1262. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1263. { I've no idea if this is correct (FK):
  1264. RELOC_RELATIVE :
  1265. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1266. }
  1267. RELOC_RVA :
  1268. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1269. RELOC_SECREL32 :
  1270. rel.reloctype:=IMAGE_REL_ARM_SECREL;
  1271. RELOC_RELATIVE_24 :
  1272. rel.reloctype:=IMAGE_REL_ARM_BRANCH24;
  1273. RELOC_RELATIVE_CALL :
  1274. rel.reloctype:=IMAGE_REL_ARM_BLX24;
  1275. RELOC_RELATIVE_24_THUMB:
  1276. rel.reloctype:=IMAGE_REL_ARM_BLX23T;
  1277. {$endif arm}
  1278. {$ifdef i386}
  1279. RELOC_RELATIVE :
  1280. rel.reloctype:=IMAGE_REL_I386_PCRLONG;
  1281. RELOC_ABSOLUTE :
  1282. rel.reloctype:=IMAGE_REL_I386_DIR32;
  1283. RELOC_RVA :
  1284. rel.reloctype:=IMAGE_REL_I386_IMAGEBASE;
  1285. RELOC_SECREL32 :
  1286. rel.reloctype:=IMAGE_REL_I386_SECREL32;
  1287. {$endif i386}
  1288. {$ifdef x86_64}
  1289. RELOC_NONE :
  1290. rel.reloctype:=IMAGE_REL_AMD64_ABSOLUTE;
  1291. RELOC_RELATIVE :
  1292. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1293. RELOC_ABSOLUTE32 :
  1294. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1295. RELOC_ABSOLUTE :
  1296. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1297. RELOC_RVA :
  1298. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1299. RELOC_RELATIVE_1 :
  1300. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1301. RELOC_RELATIVE_2 :
  1302. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1303. RELOC_RELATIVE_3 :
  1304. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1305. RELOC_RELATIVE_4 :
  1306. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1307. RELOC_RELATIVE_5 :
  1308. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1309. RELOC_SECREL32 :
  1310. rel.reloctype:=IMAGE_REL_AMD64_SECREL;
  1311. {$endif x86_64}
  1312. else
  1313. internalerror(200905071);
  1314. end;
  1315. FWriter.write(rel,sizeof(rel));
  1316. end;
  1317. end;
  1318. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1319. var
  1320. filename : string[20];
  1321. filenamelen : longint;
  1322. sectionval : word;
  1323. globalval : byte;
  1324. i : longint;
  1325. value : aword;
  1326. objsym : TObjSymbol;
  1327. secidx : longword;
  1328. begin
  1329. with TCoffObjData(data) do
  1330. begin
  1331. symidx:=0;
  1332. { The `.file' record, and the file name auxiliary record }
  1333. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1334. fillchar(filename,sizeof(filename),0);
  1335. filename:=ExtractFileName(current_module.mainsource);
  1336. inc(symidx);
  1337. if bigobj then
  1338. filenamelen:=sizeof(coffbigobjsymbol)
  1339. else
  1340. filenamelen:=sizeof(coffsymbol);
  1341. FCoffSyms.write(filename[1],filenamelen);
  1342. { Sections }
  1343. secidx:=0;
  1344. ObjSectionList.ForEachCall(@section_write_symbol,@secidx);
  1345. { ObjSymbols }
  1346. for i:=0 to ObjSymbolList.Count-1 do
  1347. begin
  1348. objsym:=TObjSymbol(ObjSymbolList[i]);
  1349. if (objsym.bind=AB_LOCAL) then
  1350. continue;
  1351. case objsym.bind of
  1352. AB_GLOBAL :
  1353. begin
  1354. globalval:=COFF_SYM_GLOBAL;
  1355. sectionval:=objsym.objsection.index;
  1356. value:=objsym.address;
  1357. end;
  1358. AB_LOCAL :
  1359. begin
  1360. globalval:=COFF_SYM_LOCAL;
  1361. sectionval:=objsym.objsection.index;
  1362. value:=objsym.address;
  1363. end;
  1364. else
  1365. begin
  1366. globalval:=COFF_SYM_GLOBAL;
  1367. sectionval:=0;
  1368. value:=objsym.size;
  1369. end;
  1370. end;
  1371. { symbolname }
  1372. objsym.symidx:=symidx;
  1373. write_symbol(objsym.name,value,sectionval,globalval,0);
  1374. end;
  1375. end;
  1376. end;
  1377. procedure TCoffObjOutput.section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  1378. begin
  1379. p.coffrelocpos:=datapos;
  1380. inc(datapos,sizeof(coffreloc)*p.ObjRelocations.count);
  1381. if p.ObjRelocations.count>65535 then
  1382. begin
  1383. if win32 then
  1384. inc(datapos,sizeof(coffreloc))
  1385. else
  1386. Message1(asmw_f_too_many_relocations,p.fullname);
  1387. end;
  1388. end;
  1389. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1390. var
  1391. sechdr : tcoffsechdr;
  1392. s : string;
  1393. strpos : aword;
  1394. begin
  1395. with TCoffObjSection(p) do
  1396. begin
  1397. fillchar(sechdr,sizeof(sechdr),0);
  1398. s:=name;
  1399. if length(s)>8 then
  1400. begin
  1401. strpos:=FCoffStrs.size+4;
  1402. FCoffStrs.writestr(s);
  1403. FCoffStrs.writestr(#0);
  1404. s:='/'+ToStr(strpos);
  1405. end;
  1406. move(s[1],sechdr.name,length(s));
  1407. if not win32 then
  1408. begin
  1409. sechdr.rvaofs:=mempos;
  1410. sechdr.vsize:=mempos;
  1411. end
  1412. else
  1413. begin
  1414. if not(oso_data in secoptions) then
  1415. sechdr.vsize:=Size;
  1416. end;
  1417. sechdr.DataSize:=size;
  1418. if (Size>0) and
  1419. (oso_data in secoptions) then
  1420. sechdr.datapos:=datapos;
  1421. if ObjRelocations.count<65535 then
  1422. sechdr.nrelocs:=ObjRelocations.count
  1423. else
  1424. sechdr.nrelocs:=65535;
  1425. sechdr.relocpos:=coffrelocpos;
  1426. if win32 then
  1427. begin
  1428. sechdr.flags:=peencodesechdrflags(secoptions,secalign);
  1429. if ObjRelocations.count>65535 then
  1430. sechdr.flags:=sechdr.flags or PE_SCN_LNK_NRELOC_OVFL;
  1431. end
  1432. else
  1433. sechdr.flags:=djencodesechdrflags(secoptions);
  1434. FWriter.write(sechdr,sizeof(sechdr));
  1435. end;
  1436. end;
  1437. function TCoffObjOutput.writedata(data:TObjData):boolean;
  1438. var
  1439. datapos,
  1440. sympos : aword;
  1441. i : longint;
  1442. header : tcoffheader;
  1443. boheader : tcoffbigobjheader;
  1444. begin
  1445. result:=false;
  1446. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  1447. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  1448. with TCoffObjData(data) do
  1449. begin
  1450. bigobj:=(ObjSectionList.Count>$7fff) and win32;
  1451. { Create Symbol Table }
  1452. create_symbols(data);
  1453. { Calculate the filepositions }
  1454. if bigobj then
  1455. datapos:=sizeof(tcoffbigobjheader)
  1456. else
  1457. datapos:=sizeof(tcoffheader);
  1458. datapos:=datapos+sizeof(tcoffsechdr)*ObjSectionList.Count;
  1459. { Sections first }
  1460. layoutsections(datapos);
  1461. { relocs }
  1462. for i:=0 to ObjSectionList.Count-1 do
  1463. section_set_reloc_datapos(TCoffObjSection(ObjSectionList[i]),datapos);
  1464. { Symbols }
  1465. sympos:=datapos;
  1466. { Generate COFF header }
  1467. if bigobj then
  1468. begin
  1469. fillchar(boheader,sizeof(boheader),0);
  1470. boheader.Sig1:=0;
  1471. boheader.Sig2:=$ffff;
  1472. boheader.Machine:=COFF_MAGIC;
  1473. boheader.Version:=COFF_BIG_OBJ_VERSION;
  1474. boheader.NumberOfSections:=longword(ObjSectionList.Count);
  1475. boheader.NumberOfSymbols:=longword(symidx);
  1476. boheader.PointerToSymbolTable:=sympos;
  1477. Move(COFF_BIG_OBJ_MAGIC,boheader.UUID,length(boheader.UUID));
  1478. FWriter.write(boheader,sizeof(boheader));
  1479. end
  1480. else
  1481. begin
  1482. fillchar(header,sizeof(tcoffheader),0);
  1483. header.mach:=COFF_MAGIC;
  1484. header.nsects:=ObjSectionList.Count;
  1485. header.sympos:=sympos;
  1486. header.syms:=symidx;
  1487. if win32 then
  1488. begin
  1489. {$ifndef x86_64}
  1490. header.flag:=PE_FILE_32BIT_MACHINE or
  1491. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1492. {$else x86_64}
  1493. header.flag:=PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  1494. {$endif x86_64}
  1495. end
  1496. else
  1497. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  1498. FWriter.write(header,sizeof(header));
  1499. end;
  1500. { Section headers }
  1501. ObjSectionList.ForEachCall(@section_write_header,nil);
  1502. { ObjSections }
  1503. WriteSectionContent(data);
  1504. { Relocs }
  1505. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  1506. { ObjSymbols }
  1507. if Sympos<>FWriter.ObjSize then
  1508. internalerror(200603051);
  1509. FWriter.writearray(FCoffSyms);
  1510. { Strings }
  1511. i:=FCoffStrs.size+4;
  1512. FWriter.write(i,4);
  1513. FWriter.writearray(FCoffStrs);
  1514. end;
  1515. FCoffStrs.Free;
  1516. FCoffStrs:=nil;
  1517. FCoffSyms.Free;
  1518. FCoffSyms:=nil;
  1519. end;
  1520. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  1521. begin
  1522. inherited createcoff(AWriter,false);
  1523. cobjdata:=TDJCoffObjData;
  1524. end;
  1525. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  1526. begin
  1527. inherited createcoff(AWriter,true);
  1528. cobjdata:=TPECoffObjData;
  1529. end;
  1530. {****************************************************************************
  1531. TCoffObjInput
  1532. ****************************************************************************}
  1533. constructor TCoffObjInput.createcoff(awin32:boolean);
  1534. begin
  1535. inherited create;
  1536. win32:=awin32;
  1537. bigobj:=false;
  1538. FSymTbl:=nil;
  1539. end;
  1540. destructor TCoffObjInput.destroy;
  1541. begin
  1542. FCoffSyms.free;
  1543. if assigned(FCoffStrs) then
  1544. freemem(FCoffStrs);
  1545. if assigned(FSymTbl) then
  1546. freemem(FSymTbl);
  1547. if assigned(FSecTbl) then
  1548. freemem(FSecTbl);
  1549. inherited destroy;
  1550. end;
  1551. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  1552. begin
  1553. result:=nil;
  1554. if (secidx<1) or (secidx>FSecCount) then
  1555. begin
  1556. InputError('Failed reading coff file, invalid section index');
  1557. exit;
  1558. end;
  1559. result:=FSecTbl^[secidx];
  1560. end;
  1561. function TCoffObjInput.Read_str(strpos:longword):string;
  1562. begin
  1563. if (FCoffStrs=nil) or (strpos>=FCoffStrSize) or (FCoffStrs[strpos]=#0) then
  1564. Internalerror(200205172);
  1565. result:=string(PChar(@FCoffStrs[strpos]));
  1566. end;
  1567. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  1568. var
  1569. rel : coffreloc;
  1570. rel_type : TObjRelocationType;
  1571. i : longint;
  1572. p : TObjSymbol;
  1573. begin
  1574. if s.coffrelocs=high(aword) then
  1575. begin
  1576. { If number of relocations exceeds 65535, it is stored in address field
  1577. of the first record, and includes this first fake relocation. }
  1578. FReader.read(rel,sizeof(rel));
  1579. s.coffrelocs:=rel.address-1;
  1580. if s.coffrelocs<=65535 then
  1581. InternalError(2013012503);
  1582. end;
  1583. for i:=1 to s.coffrelocs do
  1584. begin
  1585. FReader.read(rel,sizeof(rel));
  1586. case rel.reloctype of
  1587. {$ifdef arm}
  1588. IMAGE_REL_ARM_ABSOLUTE:
  1589. rel_type:=RELOC_NONE;
  1590. IMAGE_REL_ARM_ADDR32:
  1591. rel_type:=RELOC_ABSOLUTE;
  1592. IMAGE_REL_ARM_ADDR32NB:
  1593. rel_type:=RELOC_RVA;
  1594. IMAGE_REL_ARM_BRANCH24:
  1595. rel_type:=RELOC_RELATIVE_24;
  1596. IMAGE_REL_ARM_BLX24:
  1597. rel_type:=RELOC_RELATIVE_CALL;
  1598. IMAGE_REL_ARM_SECREL:
  1599. rel_type:=RELOC_SECREL32;
  1600. IMAGE_REL_ARM_BLX23T:
  1601. rel_type:=RELOC_RELATIVE_24_THUMB;
  1602. {$endif arm}
  1603. {$ifdef i386}
  1604. IMAGE_REL_I386_PCRLONG :
  1605. rel_type:=RELOC_RELATIVE;
  1606. IMAGE_REL_I386_DIR32 :
  1607. rel_type:=RELOC_ABSOLUTE;
  1608. IMAGE_REL_I386_IMAGEBASE :
  1609. rel_type:=RELOC_RVA;
  1610. IMAGE_REL_I386_SECREL32 :
  1611. rel_type:=RELOC_SECREL32;
  1612. {$endif i386}
  1613. {$ifdef x86_64}
  1614. IMAGE_REL_AMD64_ABSOLUTE:
  1615. rel_type:=RELOC_NONE;
  1616. IMAGE_REL_AMD64_REL32:
  1617. rel_type:=RELOC_RELATIVE;
  1618. IMAGE_REL_AMD64_ADDR32,
  1619. R_X86_64_32S:
  1620. rel_type:=RELOC_ABSOLUTE32;
  1621. IMAGE_REL_AMD64_ADDR64:
  1622. rel_type:=RELOC_ABSOLUTE;
  1623. IMAGE_REL_AMD64_ADDR32NB:
  1624. rel_type:=RELOC_RVA;
  1625. IMAGE_REL_AMD64_REL32_1:
  1626. rel_type:=RELOC_RELATIVE_1;
  1627. IMAGE_REL_AMD64_REL32_2:
  1628. rel_type:=RELOC_RELATIVE_2;
  1629. IMAGE_REL_AMD64_REL32_3:
  1630. rel_type:=RELOC_RELATIVE_3;
  1631. IMAGE_REL_AMD64_REL32_4:
  1632. rel_type:=RELOC_RELATIVE_4;
  1633. IMAGE_REL_AMD64_REL32_5:
  1634. rel_type:=RELOC_RELATIVE_5;
  1635. IMAGE_REL_AMD64_SECREL:
  1636. rel_type:=RELOC_SECREL32;
  1637. {$endif x86_64}
  1638. else
  1639. begin
  1640. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  1641. exit;
  1642. end;
  1643. end;
  1644. p:=FSymTbl^[rel.sym];
  1645. if assigned(p) then
  1646. s.addsymreloc(rel.address-s.mempos,p,rel_type)
  1647. else
  1648. begin
  1649. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  1650. exit;
  1651. end;
  1652. end;
  1653. end;
  1654. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  1655. var
  1656. size,
  1657. address,
  1658. nsyms,
  1659. symidx : aint;
  1660. i : longint;
  1661. sym : coffsymbol;
  1662. bosym : coffbigobjsymbol;
  1663. objsym : TObjSymbol;
  1664. bind : Tasmsymbind;
  1665. strname : string;
  1666. auxrec : array[0..sizeof(coffsymbol)-1] of byte;
  1667. boauxrec : array[0..sizeof(coffbigobjsymbol)-1] of byte;
  1668. objsec : TObjSection;
  1669. secidx : longint;
  1670. symvalue : longword;
  1671. auxcount : byte;
  1672. symcls : byte;
  1673. { keeps string manipulations out of main routine }
  1674. procedure UnsupportedSymbolType;
  1675. begin
  1676. Comment(V_Fatal,'Unsupported COFF symbol type '+tostr(symcls)+' at index '+tostr(symidx)+' while reading '+InputFileName);
  1677. end;
  1678. begin
  1679. with TCoffObjData(objdata) do
  1680. begin
  1681. if bigobj then
  1682. nsyms:=FCoffSyms.Size div sizeof(coffbigobjsymbol)
  1683. else
  1684. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1685. { Allocate memory for symidx -> TObjSymbol table }
  1686. FSymTbl:=AllocMem(nsyms*sizeof(TObjSymbol));
  1687. { Load the Symbols }
  1688. FCoffSyms.Seek(0);
  1689. symidx:=0;
  1690. while (symidx<nsyms) do
  1691. begin
  1692. if bigobj then
  1693. begin
  1694. FCoffSyms.Read(bosym,sizeof(bosym));
  1695. if bosym.Name.Offset.Zeroes<>0 then
  1696. begin
  1697. move(bosym.Name.ShortName,strname[1],8);
  1698. strname[9]:=#0;
  1699. strname[0]:=chr(strlen(@strname[1]));
  1700. if strname='' then
  1701. internalerror(2017020301);
  1702. end
  1703. else
  1704. strname:=Read_str(bosym.Name.Offset.Offset);
  1705. secidx:=longint(bosym.SectionNumber);
  1706. symvalue:=bosym.Value;
  1707. auxcount:=bosym.NumberOfAuxSymbols;
  1708. symcls:=bosym.StorageClass;
  1709. end
  1710. else
  1711. begin
  1712. FCoffSyms.Read(sym,sizeof(sym));
  1713. if plongint(@sym.name)^<>0 then
  1714. begin
  1715. move(sym.name,strname[1],8);
  1716. strname[9]:=#0;
  1717. strname[0]:=chr(strlen(@strname[1]));
  1718. if strname='' then
  1719. Internalerror(200205171);
  1720. end
  1721. else
  1722. strname:=Read_str(sym.strpos);
  1723. secidx:=sym.section;
  1724. symvalue:=sym.value;
  1725. auxcount:=sym.aux;
  1726. symcls:=sym.typ;
  1727. end;
  1728. bind:=AB_EXTERNAL;
  1729. size:=0;
  1730. address:=0;
  1731. objsym:=nil;
  1732. objsec:=nil;
  1733. case symcls of
  1734. COFF_SYM_GLOBAL :
  1735. begin
  1736. if secidx=0 then
  1737. begin
  1738. if symvalue=0 then
  1739. bind:=AB_EXTERNAL
  1740. else
  1741. begin
  1742. bind:=AB_COMMON;
  1743. size:=symvalue;
  1744. end;
  1745. end
  1746. else
  1747. begin
  1748. bind:=AB_GLOBAL;
  1749. objsec:=GetSection(secidx);
  1750. if symvalue>=objsec.mempos then
  1751. address:=symvalue-objsec.mempos;
  1752. end;
  1753. objsym:=CreateSymbol(strname);
  1754. objsym.bind:=bind;
  1755. objsym.typ:=AT_FUNCTION;
  1756. objsym.objsection:=objsec;
  1757. objsym.offset:=address;
  1758. objsym.size:=size;
  1759. end;
  1760. COFF_SYM_LABEL,
  1761. COFF_SYM_LOCAL :
  1762. begin
  1763. { do not add constants (section=-1) }
  1764. if secidx<>-1 then
  1765. begin
  1766. objsec:=GetSection(secidx);
  1767. if symvalue>=objsec.mempos then
  1768. address:=symvalue-objsec.mempos;
  1769. objsym:=CreateSymbol(strname);
  1770. objsym.bind:=AB_LOCAL;
  1771. objsym.typ:=AT_FUNCTION;
  1772. objsym.objsection:=objsec;
  1773. objsym.offset:=address;
  1774. objsym.size:=size;
  1775. end;
  1776. end;
  1777. COFF_SYM_SECTION :
  1778. begin
  1779. { GetSection checks that index is in range }
  1780. objsec:=GetSection(secidx);
  1781. if assigned(objsec) then
  1782. begin
  1783. if symvalue>=objsec.mempos then
  1784. address:=symvalue-objsec.mempos;
  1785. objsym:=CreateSymbol(strname);
  1786. objsym.bind:=AB_LOCAL;
  1787. objsym.typ:=AT_FUNCTION;
  1788. objsym.objsection:=objsec;
  1789. objsym.offset:=address;
  1790. objsym.size:=size;
  1791. end;
  1792. end;
  1793. COFF_SYM_FUNCTION,
  1794. COFF_SYM_FILE :
  1795. ;
  1796. else
  1797. UnsupportedSymbolType;
  1798. end;
  1799. FSymTbl^[symidx]:=objsym;
  1800. { read aux records }
  1801. for i:=1 to auxcount do
  1802. begin
  1803. if bigobj then
  1804. FCoffSyms.Read(boauxrec,sizeof(boauxrec))
  1805. else
  1806. FCoffSyms.Read(auxrec,sizeof(auxrec));
  1807. inc(symidx);
  1808. end;
  1809. inc(symidx);
  1810. end;
  1811. end;
  1812. end;
  1813. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  1814. begin
  1815. with TCoffObjSection(p) do
  1816. begin
  1817. { Skip debug sections }
  1818. if (oso_debug in secoptions) and
  1819. (cs_link_strip in current_settings.globalswitches) and
  1820. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1821. exit;
  1822. if coffrelocs>0 then
  1823. begin
  1824. FReader.Seek(coffrelocpos);
  1825. read_relocs(TCoffObjSection(p));
  1826. end;
  1827. end;
  1828. end;
  1829. function TCoffObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  1830. var
  1831. secalign : shortint;
  1832. secofs,
  1833. strpos,
  1834. i : longint;
  1835. sympos,
  1836. symcount,
  1837. symsize,
  1838. code : longint;
  1839. objsec : TCoffObjSection;
  1840. secoptions : TObjSectionOptions;
  1841. header : tcoffheader;
  1842. boheader : tcoffbigobjheader;
  1843. sechdr : tcoffsechdr;
  1844. secname : string;
  1845. secnamebuf : array[0..15] of char;
  1846. begin
  1847. FReader:=AReader;
  1848. InputFileName:=AReader.FileName;
  1849. objdata:=CObjData.Create(InputFileName);
  1850. result:=false;
  1851. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  1852. with TCoffObjData(objdata) do
  1853. begin
  1854. { Read COFF header }
  1855. if not AReader.read(header,sizeof(tcoffheader)) then
  1856. begin
  1857. InputError('Can''t read COFF Header');
  1858. exit;
  1859. end;
  1860. if (header.mach=0) and (header.nsects=$ffff) then
  1861. begin
  1862. { either a library or big obj COFF }
  1863. AReader.seek(0);
  1864. if not AReader.read(boheader,sizeof(boheader)) then
  1865. begin
  1866. InputError('Can''t read Big Obj COFF Header');
  1867. exit;
  1868. end;
  1869. if CompareByte(boheader.UUID,COFF_BIG_OBJ_MAGIC,length(boheader.uuid))<>0 then
  1870. begin
  1871. { ToDo: this should be treated as a library }
  1872. InputError('Illegal Big Obj COFF Magic');
  1873. exit;
  1874. end;
  1875. if boheader.Version<>COFF_BIG_OBJ_VERSION then
  1876. begin
  1877. InputError('Illegal Big Obj COFF Version');
  1878. exit;
  1879. end;
  1880. if boheader.Machine<>COFF_MAGIC then
  1881. begin
  1882. InputError('Illegal COFF Machine type');
  1883. exit;
  1884. end;
  1885. bigobj:=true;
  1886. end
  1887. else if header.mach<>COFF_MAGIC then
  1888. begin
  1889. InputError('Illegal COFF Magic');
  1890. exit;
  1891. end;
  1892. {$ifdef arm}
  1893. eVCobj:=header.flag=$100;
  1894. {$endif arm}
  1895. { ObjSymbols }
  1896. if bigobj then
  1897. begin
  1898. sympos:=longint(boheader.PointerToSymbolTable);
  1899. symcount:=longint(boheader.NumberOfSymbols);
  1900. symsize:=sizeof(CoffBigObjSymbol);
  1901. end
  1902. else
  1903. begin
  1904. sympos:=longint(header.sympos);
  1905. symcount:=longint(header.syms);
  1906. symsize:=sizeof(CoffSymbol);
  1907. end;
  1908. AReader.Seek(sympos);
  1909. if not AReader.ReadArray(FCoffSyms,symcount*symsize) then
  1910. begin
  1911. InputError('Error reading coff symbol table');
  1912. exit;
  1913. end;
  1914. { Strings }
  1915. if not AReader.Read(FCoffStrSize,4) then
  1916. begin
  1917. InputError('Error reading COFF string table');
  1918. exit;
  1919. end;
  1920. if (FCoffStrSize>4) then
  1921. begin
  1922. { allocate an extra byte and null-terminate }
  1923. GetMem(FCoffStrs,FCoffStrSize+1);
  1924. FCoffStrs[FCoffStrSize]:=#0;
  1925. for i:=0 to 3 do
  1926. FCoffStrs[i]:=#0;
  1927. if not AReader.Read(FCoffStrs[4],FCoffStrSize-4) then
  1928. begin
  1929. InputError('Error reading COFF string table');
  1930. exit;
  1931. end;
  1932. end;
  1933. { Section headers }
  1934. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  1935. if bigobj then
  1936. FSecCount:=longint(boheader.NumberOfSections)
  1937. else
  1938. FSecCount:=header.nsects;
  1939. FSecTbl:=AllocMem((FSecCount+1)*sizeof(TObjSection));
  1940. if bigobj then
  1941. secofs:=sizeof(tcoffbigobjheader)
  1942. else
  1943. secofs:=sizeof(tcoffheader)+header.opthdr;
  1944. AReader.Seek(secofs);
  1945. for i:=1 to FSecCount do
  1946. begin
  1947. if not AReader.read(sechdr,sizeof(sechdr)) then
  1948. begin
  1949. InputError('Error reading COFF Section Headers');
  1950. exit;
  1951. end;
  1952. move(sechdr.name,secnamebuf,8);
  1953. secnamebuf[8]:=#0;
  1954. secname:=strpas(secnamebuf);
  1955. if secname[1]='/' then
  1956. begin
  1957. Val(Copy(secname,2,8),strpos,code);
  1958. if code=0 then
  1959. secname:=Read_str(strpos)
  1960. else
  1961. begin
  1962. InputError('Error reading COFF Section Headers');
  1963. secname:='error';
  1964. end;
  1965. end;
  1966. if win32 then
  1967. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  1968. else
  1969. begin
  1970. secoptions:=djdecodesechdrflags(secname,sechdr.flags);
  1971. secalign:=sizeof(pint);
  1972. end;
  1973. if (Length(secname)>3) and (secname[2] in ['e','f','i','p','r']) then
  1974. begin
  1975. if (Pos('.edata',secname)=1) or
  1976. (Pos('.rsrc',secname)=1) or
  1977. {$ifndef x86_64}
  1978. (Pos('.pdata',secname)=1) or
  1979. {$endif}
  1980. (Pos('.fpc',secname)=1) then
  1981. include(secoptions,oso_keep);
  1982. if (Pos('.idata',secname)=1) then
  1983. begin
  1984. { TODO: idata keep can maybe replaced with grouping of text and idata}
  1985. include(secoptions,oso_keep);
  1986. secname:=secname + '.' + ExtractFileName(InputFileName);
  1987. end;
  1988. end;
  1989. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions,false));
  1990. FSecTbl^[i]:=objsec;
  1991. if not win32 then
  1992. objsec.mempos:=sechdr.rvaofs;
  1993. objsec.orgmempos:=sechdr.rvaofs;
  1994. objsec.coffrelocs:=sechdr.nrelocs;
  1995. if win32 then
  1996. begin
  1997. if (sechdr.flags and PE_SCN_LNK_NRELOC_OVFL)<>0 then
  1998. objsec.coffrelocs:=high(aword);
  1999. end;
  2000. objsec.coffrelocpos:=sechdr.relocpos;
  2001. objsec.datapos:=sechdr.datapos;
  2002. objsec.Size:=sechdr.dataSize;
  2003. end;
  2004. { Insert all ObjSymbols }
  2005. read_symbols(objdata);
  2006. { Section Data }
  2007. ReadSectionContent(objdata);
  2008. { Relocs }
  2009. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  2010. end;
  2011. if assigned(FCoffStrs) then
  2012. freemem(FCoffStrs);
  2013. FCoffStrs:=nil;
  2014. FCoffSyms.Free;
  2015. FCoffSyms:=nil;
  2016. result:=true;
  2017. end;
  2018. constructor TDJCoffObjInput.create;
  2019. begin
  2020. inherited createcoff(false);
  2021. cobjdata:=TDJCoffObjData;
  2022. end;
  2023. constructor TPECoffObjInput.create;
  2024. begin
  2025. inherited createcoff(true);
  2026. cobjdata:=TPECoffObjData;
  2027. end;
  2028. {****************************************************************************
  2029. TCoffexeoutput
  2030. ****************************************************************************}
  2031. constructor TCoffexeoutput.createcoff(awin32:boolean);
  2032. begin
  2033. inherited create;
  2034. win32:=awin32;
  2035. if target_info.system in [system_x86_64_win64] then
  2036. MaxMemPos:=$FFFFFFFF
  2037. else
  2038. if target_info.system in systems_wince then
  2039. MaxMemPos:=$1FFFFFF
  2040. else
  2041. MaxMemPos:=$7FFFFFFF;
  2042. end;
  2043. procedure TCoffexeoutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  2044. var
  2045. sym : coffsymbol;
  2046. begin
  2047. FillChar(sym,sizeof(sym),0);
  2048. if length(name)>8 then
  2049. begin
  2050. sym.strpos:=FCoffStrs.size+4;
  2051. FCoffStrs.writestr(name);
  2052. FCoffStrs.writestr(#0);
  2053. end
  2054. else
  2055. move(name[1],sym.name,length(name));
  2056. sym.value:=value;
  2057. sym.section:=section;
  2058. sym.typ:=typ;
  2059. sym.aux:=aux;
  2060. FWriter.write(sym,sizeof(sym));
  2061. end;
  2062. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  2063. var
  2064. secval,
  2065. value : aint;
  2066. globalval : byte;
  2067. exesec : TExeSection;
  2068. begin
  2069. if not assigned(texesymbol(p).objsymbol) then
  2070. internalerror(200603053);
  2071. with texesymbol(p).objsymbol do
  2072. begin
  2073. if assigned(objsection) then
  2074. exesec:=TExeSection(objsection.exesection)
  2075. else
  2076. exesec:=nil;
  2077. { There is no exesection defined for special internal symbols
  2078. like __image_base__ }
  2079. if assigned(exesec) then
  2080. begin
  2081. secval:=exesec.secsymidx;
  2082. if win32 then
  2083. value:=address-exesec.mempos
  2084. else
  2085. value:=address;
  2086. end
  2087. else
  2088. begin
  2089. secval:=-1;
  2090. value:=address;
  2091. end;
  2092. if bind=AB_LOCAL then
  2093. globalval:=COFF_SYM_LOCAL
  2094. else
  2095. globalval:=COFF_SYM_GLOBAL;
  2096. { reloctype address to the section in the executable }
  2097. write_symbol(name,value,secval,globalval,0);
  2098. end;
  2099. end;
  2100. procedure TCoffexeoutput.ExeSectionList_write_header(p:TObject;arg:pointer);
  2101. var
  2102. sechdr : tcoffsechdr;
  2103. s : string;
  2104. strpos : aword;
  2105. begin
  2106. with tExeSection(p) do
  2107. begin
  2108. fillchar(sechdr,sizeof(sechdr),0);
  2109. s:=name;
  2110. if length(s)>8 then
  2111. begin
  2112. strpos:=FCoffStrs.size+4;
  2113. FCoffStrs.writestr(s);
  2114. FCoffStrs.writestr(#0);
  2115. s:='/'+ToStr(strpos);
  2116. end;
  2117. move(s[1],sechdr.name,length(s));
  2118. if win32 then
  2119. begin
  2120. sechdr.rvaofs:=mempos;
  2121. sechdr.vsize:=Size;
  2122. { sechdr.dataSize is size of initialized data, rounded up to FileAlignment
  2123. (so it can be greater than VirtualSize). Must be zero for sections that
  2124. do not contain initialized data. }
  2125. if (oso_data in SecOptions) then
  2126. sechdr.datasize:=Align(Size,SectionDataAlign);
  2127. end
  2128. else
  2129. begin
  2130. if not (oso_debug in SecOptions) then
  2131. begin
  2132. sechdr.rvaofs:=mempos;
  2133. sechdr.vsize:=mempos;
  2134. end;
  2135. sechdr.datasize:=Size;
  2136. end;
  2137. if (Size>0) then
  2138. sechdr.datapos:=datapos-datapos_offset;
  2139. sechdr.nrelocs:=0;
  2140. sechdr.relocpos:=0;
  2141. if win32 then
  2142. begin
  2143. if (target_info.system in systems_nativent) and
  2144. (apptype = app_native) then
  2145. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign) or PE_SCN_MEM_NOT_PAGED
  2146. else
  2147. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign);
  2148. { some flags are invalid in executables, reset them }
  2149. sechdr.flags:=sechdr.flags and
  2150. not(PE_SCN_LNK_INFO or PE_SCN_LNK_REMOVE or
  2151. PE_SCN_LNK_COMDAT or PE_SCN_ALIGN_MASK);
  2152. end
  2153. else
  2154. sechdr.flags:=djencodesechdrflags(SecOptions);
  2155. FWriter.write(sechdr,sizeof(sechdr));
  2156. end;
  2157. end;
  2158. procedure TCoffexeoutput.ExeSectionList_pass2_header(p:TObject;arg:pointer);
  2159. begin
  2160. with TExeSection(p) do
  2161. begin
  2162. { The debuginfo sections should already be stripped }
  2163. { if (ExeWriteMode=ewm_exeonly) and
  2164. (oso_debug in SecOptions) then
  2165. internalerror(200801161); }
  2166. inc(plongint(arg)^);
  2167. secsymidx:=plongint(arg)^;
  2168. end;
  2169. end;
  2170. function tcoffexeoutput.totalheadersize:longword;
  2171. var
  2172. stubsize,
  2173. optheadersize : longword;
  2174. begin
  2175. if win32 then
  2176. begin
  2177. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  2178. optheadersize:=sizeof(tcoffpeoptheader);
  2179. end
  2180. else
  2181. begin
  2182. stubsize:=sizeof(go32v2stub);
  2183. optheadersize:=sizeof(coffdjoptheader);
  2184. end;
  2185. result:=stubsize+sizeof(tcoffheader)+optheadersize;
  2186. end;
  2187. procedure tcoffexeoutput.MemPos_Header;
  2188. begin
  2189. { calculate start positions after the headers }
  2190. currmempos:=totalheadersize+sizeof(tcoffsechdr)*longword(ExeSectionList.Count-2);
  2191. end;
  2192. procedure tcoffexeoutput.DataPos_Header;
  2193. begin
  2194. { retrieve amount of sections }
  2195. nsects:=0;
  2196. ExeSectionList.ForEachCall(@ExeSectionList_pass2_header,@nsects);
  2197. { calculate start positions after the headers }
  2198. currdatapos:=totalheadersize+longword(nsects)*sizeof(tcoffsechdr);
  2199. end;
  2200. procedure tcoffexeoutput.DataPos_Symbols;
  2201. begin
  2202. inherited DataPos_Symbols;
  2203. { Calculating symbols position and size }
  2204. nsyms:=ExeSymbolList.Count;
  2205. sympos:=Align(CurrDataPos,SectionDataAlign);
  2206. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  2207. end;
  2208. function TCoffexeoutput.writedata:boolean;
  2209. var
  2210. i : longword;
  2211. header : tcoffheader;
  2212. djoptheader : coffdjoptheader;
  2213. peoptheader : tcoffpeoptheader;
  2214. textExeSec,
  2215. dataExeSec,
  2216. bssExeSec,
  2217. idataExeSec : TExeSection;
  2218. hassymbols,
  2219. writeDbgStrings : boolean;
  2220. procedure UpdateDataDir(const secname:string;idx:longint);
  2221. var
  2222. exesec : TExeSection;
  2223. begin
  2224. exesec:=FindExeSection(secname);
  2225. if assigned(exesec) then
  2226. begin
  2227. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  2228. peoptheader.DataDirectory[idx].size:=exesec.Size;
  2229. end;
  2230. end;
  2231. procedure UpdateImports;
  2232. var
  2233. exesec: TExeSection;
  2234. objsec, iat_start, iat_end, ilt_start: TObjSection;
  2235. i: longint;
  2236. begin
  2237. exesec:=FindExeSection('.idata');
  2238. if exesec=nil then
  2239. exit;
  2240. iat_start:=nil;
  2241. iat_end:=nil;
  2242. ilt_start:=nil;
  2243. for i:=0 to exesec.ObjSectionList.Count-1 do
  2244. begin
  2245. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2246. if (ilt_start=nil) and (Pos('.idata$4',objsec.Name)=1) then
  2247. ilt_start:=objsec;
  2248. if Pos('.idata$5',objsec.Name)=1 then
  2249. begin
  2250. if iat_start=nil then
  2251. iat_start:=objsec;
  2252. end
  2253. else
  2254. if Assigned(iat_start) then
  2255. begin
  2256. iat_end:=objsec;
  2257. Break;
  2258. end;
  2259. end;
  2260. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=exesec.mempos;
  2261. if Assigned(ilt_start) then
  2262. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=ilt_start.mempos-exesec.mempos
  2263. else { should not happen }
  2264. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=exesec.Size;
  2265. if Assigned(iat_start) and Assigned(iat_end) then
  2266. begin
  2267. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].vaddr:=iat_start.mempos;
  2268. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].size:=iat_end.mempos-iat_start.mempos;
  2269. end;
  2270. end;
  2271. procedure UpdateTlsDataDir;
  2272. var
  2273. {callbacksection : TExeSection;}
  2274. tlsexesymbol: TExeSymbol;
  2275. tlssymbol: TObjSymbol;
  2276. callbackexesymbol: TExeSymbol;
  2277. //callbacksymbol: TObjSymbol;
  2278. begin
  2279. { according to GNU ld,
  2280. the callback routines should be placed into .CRT$XL*
  2281. sections, and the thread local variables in .tls
  2282. __tls_start__ and __tls_end__ symbols
  2283. should be used for the initialized part,
  2284. which we do not support yet. }
  2285. { For now, we only pass the address of the __tls_used
  2286. asm symbol into PE_DATADIR_TLS with the correct
  2287. size of this table (different for win32/win64 }
  2288. tlsexesymbol:=texesymbol(ExeSymbolList.Find(
  2289. target_info.Cprefix+'_tls_used'));
  2290. if assigned(tlsexesymbol) then
  2291. begin
  2292. tlssymbol:=tlsexesymbol.ObjSymbol;
  2293. peoptheader.DataDirectory[PE_DATADIR_TLS].vaddr:=tlssymbol.address;
  2294. { sizeof(TlsDirectory) is different on host and target when cross-compiling }
  2295. peoptheader.DataDirectory[PE_DATADIR_TLS].size:=TLSDIR_SIZE;
  2296. if IsSharedLibrary then
  2297. begin
  2298. { Here we should reset __FPC_tls_callbacks value to nil }
  2299. callbackexesymbol:=texesymbol(ExeSymbolList.Find(
  2300. '__FPC_tls_callbacks'));
  2301. if assigned (callbackexesymbol) then
  2302. begin
  2303. //callbacksymbol:=callbackexesymbol.ObjSymbol;
  2304. end;
  2305. end;
  2306. end;
  2307. end;
  2308. begin
  2309. result:=false;
  2310. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  2311. textExeSec:=FindExeSection('.text');
  2312. dataExeSec:=FindExeSection('.data');
  2313. bssExeSec:=FindExeSection('.bss');
  2314. if not assigned(TextExeSec) or
  2315. not assigned(DataExeSec) then
  2316. internalerror(200602231);
  2317. { do we need to write symbols? }
  2318. hassymbols:=(ExeWriteMode=ewm_dbgonly) or
  2319. (
  2320. (ExeWriteMode=ewm_exefull) and
  2321. not(cs_link_strip in current_settings.globalswitches)
  2322. );
  2323. writeDbgStrings:=hassymbols or ((ExeWriteMode=ewm_exeonly) and (cs_link_separate_dbg_file in current_settings.globalswitches));
  2324. { Stub }
  2325. if win32 then
  2326. begin
  2327. FWriter.write(win32stub,sizeof(win32stub));
  2328. FWriter.write(pemagic,sizeof(pemagic));
  2329. end
  2330. else
  2331. FWriter.write(go32v2stub,sizeof(go32v2stub));
  2332. { Initial header, will be updated later }
  2333. fillchar(header,sizeof(header),0);
  2334. header.mach:=COFF_MAGIC;
  2335. header.nsects:=nsects;
  2336. if writeDbgStrings then
  2337. header.sympos:=sympos-datapos_offset;
  2338. if hassymbols then
  2339. header.syms:=nsyms;
  2340. if win32 then
  2341. header.opthdr:=sizeof(tcoffpeoptheader)
  2342. else
  2343. header.opthdr:=sizeof(coffdjoptheader);
  2344. if win32 then
  2345. begin
  2346. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  2347. if target_info.system in [system_x86_64_win64] then
  2348. header.flag:=header.flag or PE_FILE_LARGE_ADDRESS_AWARE
  2349. else
  2350. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  2351. if IsSharedLibrary then
  2352. header.flag:=header.flag or PE_FILE_DLL;
  2353. if FindExeSection('.reloc')=nil then
  2354. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  2355. if (FindExeSection('.stab')=nil) and
  2356. (FindExeSection('.debug_info')=nil) and
  2357. (FindExeSection('.gnu_debuglink')=nil) then
  2358. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  2359. if not hassymbols then
  2360. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  2361. if SetPEFlagsSetExplicity then
  2362. header.flag:=header.flag or peflags;
  2363. end
  2364. else
  2365. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  2366. FWriter.write(header,sizeof(header));
  2367. { Optional COFF Header }
  2368. if win32 then
  2369. begin
  2370. fillchar(peoptheader,sizeof(peoptheader),0);
  2371. peoptheader.magic:=COFF_OPT_MAGIC;
  2372. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  2373. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  2374. peoptheader.tsize:=TextExeSec.Size;
  2375. peoptheader.dsize:=DataExeSec.Size;
  2376. if assigned(BSSExeSec) then
  2377. peoptheader.bsize:=BSSExeSec.Size;
  2378. peoptheader.text_start:=TextExeSec.mempos;
  2379. {$ifndef cpu64bitaddr}
  2380. peoptheader.data_start:=DataExeSec.mempos;
  2381. {$endif cpu64bitaddr}
  2382. peoptheader.entry:=EntrySym.Address;
  2383. peoptheader.ImageBase:=ImageBase;
  2384. peoptheader.SectionAlignment:=SectionMemAlign;
  2385. peoptheader.FileAlignment:=SectionDataAlign;
  2386. peoptheader.MajorOperatingSystemVersion:=4;
  2387. peoptheader.MinorOperatingSystemVersion:=0;
  2388. peoptheader.MajorImageVersion:=dllmajor;
  2389. peoptheader.MinorImageVersion:=dllminor;
  2390. if target_info.system in systems_wince then
  2391. peoptheader.MajorSubsystemVersion:=3
  2392. else
  2393. peoptheader.MajorSubsystemVersion:=4;
  2394. peoptheader.MinorSubsystemVersion:=0;
  2395. peoptheader.Win32Version:=0;
  2396. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  2397. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  2398. peoptheader.CheckSum:=0;
  2399. if (target_info.system in systems_nativent) and (not IsSharedLibrary or (apptype = app_native)) then
  2400. { Although I did not really test this, it seems that Subsystem is
  2401. not checked in DLLs except for maybe drivers}
  2402. peoptheader.Subsystem:=PE_SUBSYSTEM_NATIVE
  2403. else
  2404. if target_info.system in systems_wince then
  2405. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  2406. else
  2407. if apptype=app_gui then
  2408. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  2409. else
  2410. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  2411. if SetPEOptFlagsSetExplicity then
  2412. peoptheader.DllCharacteristics:=peoptflags
  2413. else
  2414. peoptheader.DllCharacteristics:=0;
  2415. peoptheader.SizeOfStackReserve:=stacksize;
  2416. peoptheader.SizeOfStackCommit:=$1000;
  2417. if MinStackSizeSetExplicity then
  2418. peoptheader.SizeOfStackCommit:=minstacksize;
  2419. if MaxStackSizeSetExplicity then
  2420. peoptheader.SizeOfStackReserve:=maxstacksize;
  2421. peoptheader.SizeOfHeapReserve:=$100000;
  2422. peoptheader.SizeOfHeapCommit:=$1000;
  2423. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  2424. UpdateImports;
  2425. UpdateTlsDataDir;
  2426. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  2427. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  2428. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  2429. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  2430. FWriter.write(peoptheader,sizeof(peoptheader));
  2431. end
  2432. else
  2433. begin
  2434. fillchar(djoptheader,sizeof(djoptheader),0);
  2435. djoptheader.magic:=COFF_OPT_MAGIC;
  2436. djoptheader.tsize:=TextExeSec.Size;
  2437. djoptheader.dsize:=DataExeSec.Size;
  2438. if assigned(BSSExeSec) then
  2439. djoptheader.bsize:=BSSExeSec.Size;
  2440. djoptheader.text_start:=TextExeSec.mempos;
  2441. djoptheader.data_start:=DataExeSec.mempos;
  2442. djoptheader.entry:=EntrySym.Address;
  2443. FWriter.write(djoptheader,sizeof(djoptheader));
  2444. end;
  2445. { For some unknown reason WM 6.1 requires .idata section to be read only.
  2446. Otherwise it refuses to load DLLs greater than 64KB.
  2447. Earlier versions of WinCE load DLLs regardless of .idata flags. }
  2448. if target_info.system in systems_wince then
  2449. begin
  2450. idataExeSec:=FindExeSection('.idata');
  2451. if idataExeSec<>nil then
  2452. idataExeSec.SecOptions:=idataExeSec.SecOptions - [oso_write];
  2453. end;
  2454. { Section headers }
  2455. ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
  2456. { Section data }
  2457. WriteExeSectionContent;
  2458. { Align after the last section }
  2459. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  2460. { Optional Symbols }
  2461. if SymPos<>FWriter.Size then
  2462. internalerror(200602252);
  2463. if hassymbols then
  2464. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  2465. if writeDbgStrings then
  2466. begin
  2467. { Strings }
  2468. i:=FCoffStrs.size+4;
  2469. FWriter.write(i,4);
  2470. FWriter.writearray(FCoffStrs);
  2471. end;
  2472. { Release }
  2473. FCoffStrs.Free;
  2474. result:=true;
  2475. end;
  2476. function IdataObjSectionCompare(Item1, Item2: Pointer): Integer;
  2477. var
  2478. I1 : TObjSection absolute Item1;
  2479. I2 : TObjSection absolute Item2;
  2480. begin
  2481. Result:=CompareStr(I1.Name,I2.Name);
  2482. end;
  2483. procedure TCoffexeoutput.Order_ObjSectionList(ObjSectionList: TFPObjectList;const aPattern:string);
  2484. begin
  2485. { Sort sections having '$' in the name, that's how PECOFF documentation
  2486. tells to handle them. However, look for '$' in the pattern, not in section
  2487. names, because the latter often get superfluous '$' due to mangling. }
  2488. if Pos('$',aPattern)>0 then
  2489. ObjSectionList.Sort(@IdataObjSectionCompare);
  2490. end;
  2491. constructor TDJCoffexeoutput.create;
  2492. begin
  2493. inherited createcoff(false);
  2494. datapos_offset:=sizeof(go32v2stub);
  2495. CExeSection:=TExeSection;
  2496. CObjData:=TDJCoffObjData;
  2497. end;
  2498. procedure TDJCoffexeoutput.MemPos_Header;
  2499. begin
  2500. { Headers are not loaded, first 4K page is reserved }
  2501. CurrMemPos:=$1000;
  2502. end;
  2503. constructor TPECoffexeoutput.create;
  2504. begin
  2505. inherited createcoff(true);
  2506. CExeSection:=TExeSection;
  2507. CObjData:=TPECoffObjData;
  2508. end;
  2509. procedure TPECoffexeoutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  2510. var
  2511. exesec:TExeSection;
  2512. objsec,textsec:TObjSection;
  2513. objreloc:TObjRelocation;
  2514. i,j:longint;
  2515. begin
  2516. if target_info.system<>system_x86_64_win64 then
  2517. exit;
  2518. exesec:=FindExeSection('.pdata');
  2519. if exesec=nil then
  2520. exit;
  2521. for i:=0 to exesec.ObjSectionList.Count-1 do
  2522. begin
  2523. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2524. if objsec.Used then
  2525. continue;
  2526. j:=0;
  2527. while j<objsec.ObjRelocations.Count do
  2528. begin
  2529. objreloc:=TObjRelocation(objsec.ObjRelocations[j]);
  2530. if objreloc.symbol=nil then
  2531. InternalError(2013041201);
  2532. textsec:=objreloc.symbol.objsection;
  2533. if textsec.used then
  2534. begin
  2535. WorkList.Add(objsec);
  2536. objsec.used:=true;
  2537. { The exact algorithm for non-smartlinked .pdata sections
  2538. is subject for refinement. Extreme cases are:
  2539. - several disjoint .pdata entries for a function, if function
  2540. is complex or if compiler splits it into chunks,
  2541. - single .pdata section referencing several .text sections,
  2542. may need to remove irrelevant parts like BFD does for
  2543. .eh_frame sections. }
  2544. break;
  2545. end;
  2546. inc(j,3);
  2547. end;
  2548. end;
  2549. end;
  2550. procedure TPECoffexeoutput.AfterUnusedSectionRemoval;
  2551. var
  2552. basedllname : string;
  2553. procedure StartImport(const dllname:string);
  2554. begin
  2555. if assigned(exemap) then
  2556. begin
  2557. exemap.Add('');
  2558. exemap.Add('Importing from DLL '+dllname);
  2559. end;
  2560. basedllname:=ExtractFileName(dllname);
  2561. { idata2 }
  2562. idata2objsection.writereloc_internal(idata4objsection,idata4objsection.size,sizeof(longint),RELOC_RVA);
  2563. idata2objsection.writezeros(2*sizeof(longint));
  2564. idata2objsection.writereloc_internal(idata7objsection,idata7objsection.size,sizeof(longint),RELOC_RVA);
  2565. idata2objsection.writereloc_internal(idata5objsection,idata5objsection.size,sizeof(longint),RELOC_RVA);
  2566. { idata7 }
  2567. idata7objsection.writestr(basedllname);
  2568. end;
  2569. procedure EndImport;
  2570. begin
  2571. { idata4 }
  2572. idata4objsection.writezeros(sizeof(longint));
  2573. if target_info.system=system_x86_64_win64 then
  2574. idata4objsection.writezeros(sizeof(longint));
  2575. { idata5 }
  2576. idata5objsection.writezeros(sizeof(longint));
  2577. if target_info.system=system_x86_64_win64 then
  2578. idata5objsection.writezeros(sizeof(longint));
  2579. end;
  2580. function AddImport(const afuncname,amangledname:string; AOrdNr:longint;isvar:boolean):TObjSymbol;
  2581. const
  2582. {$ifdef arm}
  2583. jmpopcode : array[0..7] of byte = (
  2584. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  2585. $00,$f0,$9c,$e5 // ldr pc, [ip]
  2586. );
  2587. {$else arm}
  2588. jmpopcode : array[0..1] of byte = (
  2589. $ff,$25
  2590. );
  2591. {$endif arm}
  2592. nopopcodes : array[0..1] of byte = (
  2593. $90,$90
  2594. );
  2595. var
  2596. ordint: dword;
  2597. procedure WriteTableEntry(objsec:TObjSection);
  2598. begin
  2599. if AOrdNr <= 0 then
  2600. begin
  2601. objsec.writereloc_internal(idata6objsection,idata6objsection.size,sizeof(longint),RELOC_RVA);
  2602. if target_info.system=system_x86_64_win64 then
  2603. objsec.writezeros(sizeof(longint));
  2604. end
  2605. else
  2606. begin
  2607. { import by ordinal }
  2608. ordint:=AOrdNr;
  2609. if target_info.system=system_x86_64_win64 then
  2610. begin
  2611. objsec.write(ordint,sizeof(ordint));
  2612. ordint:=$80000000;
  2613. objsec.write(ordint,sizeof(ordint));
  2614. end
  2615. else
  2616. begin
  2617. ordint:=ordint or $80000000;
  2618. objsec.write(ordint,sizeof(ordint));
  2619. end;
  2620. end;
  2621. end;
  2622. begin
  2623. result:=nil;
  2624. if assigned(exemap) then
  2625. begin
  2626. if AOrdNr <= 0 then
  2627. exemap.Add(' Importing Function '+afuncname)
  2628. else
  2629. exemap.Add(' Importing Function '+afuncname+' (OrdNr='+tostr(AOrdNr)+')');
  2630. end;
  2631. { idata4, import lookup table }
  2632. WriteTableEntry(idata4objsection);
  2633. { idata5, import address table }
  2634. internalobjdata.SetSection(idata5objsection);
  2635. if isvar then
  2636. result:=internalobjdata.SymbolDefine(amangledname,AB_GLOBAL,AT_DATA)
  2637. else
  2638. begin
  2639. internalobjdata.SetSection(textobjsection);
  2640. textobjsection.writezeros(align_aword(textobjsection.size,16)-textobjsection.size);
  2641. result:=internalobjdata.SymbolDefine('_'+amangledname,AB_GLOBAL,AT_FUNCTION);
  2642. textobjsection.write(jmpopcode,sizeof(jmpopcode));
  2643. {$ifdef x86_64}
  2644. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_RELATIVE);
  2645. {$else}
  2646. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_ABSOLUTE32);
  2647. {$endif x86_64}
  2648. textobjsection.write(nopopcodes,align(textobjsection.size,sizeof(nopopcodes))-textobjsection.size);
  2649. end;
  2650. { idata5 section data }
  2651. WriteTableEntry(idata5objsection);
  2652. if (AOrdNr<=0) then
  2653. begin
  2654. { index hint, function name, null terminator and align }
  2655. ordint:=abs(AOrdNr);
  2656. idata6objsection.write(ordint,2);
  2657. idata6objsection.writestr(afuncname);
  2658. idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
  2659. end;
  2660. end;
  2661. var
  2662. i,j : longint;
  2663. ImportLibrary : TImportLibrary;
  2664. ImportSymbol : TImportSymbol;
  2665. exesym : TExeSymbol;
  2666. newdll : boolean;
  2667. begin
  2668. for i:=0 to FImports.Count-1 do
  2669. begin
  2670. ImportLibrary:=TImportLibrary(FImports[i]);
  2671. newdll:=False;
  2672. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  2673. begin
  2674. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  2675. exesym:=ImportSymbol.CachedExeSymbol;
  2676. if assigned(exesym) and
  2677. exesym.Used then
  2678. begin
  2679. if (not newdll) then
  2680. StartImport(ImportLibrary.Name);
  2681. newdll:=True;
  2682. exesym.objsymbol:=AddImport(ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  2683. end;
  2684. end;
  2685. if newdll then
  2686. EndImport;
  2687. end;
  2688. FixupSymbols;
  2689. end;
  2690. procedure TPECoffexeoutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2691. var
  2692. i,j: longint;
  2693. ImportLibrary: TImportLibrary;
  2694. ImportSymbol: TImportSymbol;
  2695. exesym: TExeSymbol;
  2696. begin
  2697. { Here map import symbols to exe symbols and create necessary sections.
  2698. Actual import generation is done after unused sections (and symbols) are removed. }
  2699. FImports:=ImportLibraryList;
  2700. textobjsection:=internalObjData.CreateSection(sec_code);
  2701. textobjsection.SecOptions:=[oso_keep];
  2702. idata2objsection:=internalObjData.CreateSection(sec_idata2);
  2703. idata2objsection.SecOptions:=[oso_keep];
  2704. idata4objsection:=internalObjData.CreateSection(sec_idata4);
  2705. idata4objsection.SecOptions:=[oso_keep];
  2706. idata5objsection:=internalObjData.CreateSection(sec_idata5);
  2707. idata5objsection.SecOptions:=[oso_keep];
  2708. idata6objsection:=internalObjData.CreateSection(sec_idata6);
  2709. idata6objsection.SecOptions:=[oso_keep];
  2710. idata7objsection:=internalObjData.CreateSection(sec_idata7);
  2711. idata7objsection.SecOptions:=[oso_keep];
  2712. for i:=0 to ImportLibraryList.Count-1 do
  2713. begin
  2714. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  2715. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  2716. begin
  2717. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  2718. exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
  2719. if assigned(exesym) and
  2720. (exesym.State<>symstate_defined) then
  2721. begin
  2722. ImportSymbol.CachedExeSymbol:=exesym;
  2723. exesym.State:=symstate_defined;
  2724. end;
  2725. end;
  2726. end;
  2727. PackUnresolvedExeSymbols('after DLL imports');
  2728. end;
  2729. procedure TPECoffexeoutput.GenerateRelocs;
  2730. var
  2731. pgaddr, hdrpos : longword;
  2732. procedure FinishBlock;
  2733. var
  2734. p,len : longint;
  2735. begin
  2736. if hdrpos = longword(-1) then
  2737. exit;
  2738. p:=0;
  2739. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  2740. p:=internalObjData.CurrObjSec.Data.Pos;
  2741. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  2742. len:=p-hdrpos;
  2743. internalObjData.CurrObjSec.Data.write(len,4);
  2744. internalObjData.CurrObjSec.Data.seek(p);
  2745. hdrpos:=longword(-1);
  2746. end;
  2747. var
  2748. exesec : TExeSection;
  2749. objsec : TObjSection;
  2750. objreloc : TObjRelocation;
  2751. i,j,k : longint;
  2752. offset : longword;
  2753. w: word;
  2754. begin
  2755. if not RelocSection or FRelocsGenerated then
  2756. exit;
  2757. exesec:=FindExeSection('.reloc');
  2758. if exesec=nil then
  2759. exit;
  2760. objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_keep]);
  2761. exesec.AddObjSection(objsec);
  2762. pgaddr:=longword(-1);
  2763. hdrpos:=longword(-1);
  2764. for i:=0 to ExeSectionList.Count-1 do
  2765. begin
  2766. exesec:=TExeSection(ExeSectionList[i]);
  2767. for j:=0 to exesec.ObjSectionList.count-1 do
  2768. begin
  2769. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2770. { create relocs only for sections which are loaded in memory }
  2771. if not (oso_load in objsec.SecOptions) then
  2772. continue;
  2773. for k:=0 to objsec.ObjRelocations.Count-1 do
  2774. begin
  2775. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  2776. if not (objreloc.typ in [{$ifdef cpu64bitaddr}RELOC_ABSOLUTE32,{$endif cpu64bitaddr}RELOC_ABSOLUTE]) then
  2777. continue;
  2778. offset:=objsec.MemPos+objreloc.dataoffset;
  2779. if (offset<pgaddr) and (pgaddr<>longword(-1)) then
  2780. Internalerror(2007062701);
  2781. if (offset-pgaddr>=4096) or (pgaddr=longword(-1)) then
  2782. begin
  2783. FinishBlock;
  2784. pgaddr:=(offset div 4096)*4096;
  2785. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  2786. internalObjData.writebytes(pgaddr,4);
  2787. { Reserving space for block size. The size will be written later in FinishBlock }
  2788. internalObjData.writebytes(k,4);
  2789. end;
  2790. {$ifdef cpu64bitaddr}
  2791. if objreloc.typ = RELOC_ABSOLUTE then
  2792. w:=IMAGE_REL_BASED_DIR64
  2793. else
  2794. {$endif cpu64bitaddr}
  2795. w:=IMAGE_REL_BASED_HIGHLOW;
  2796. w:=(w shl 12) or (offset-pgaddr);
  2797. internalObjData.writebytes(w,2);
  2798. end;
  2799. end;
  2800. end;
  2801. FinishBlock;
  2802. FRelocsGenerated:=true;
  2803. end;
  2804. procedure TPECoffexeoutput.MemPos_Start;
  2805. var
  2806. exesec : TExeSection;
  2807. begin
  2808. if RelocSection then
  2809. begin
  2810. exesec:=FindExeSection('.reloc');
  2811. if exesec=nil then
  2812. InternalError(2012072401);
  2813. exesec.Disabled:=false;
  2814. end;
  2815. inherited;
  2816. end;
  2817. procedure TPECoffexeoutput.MemPos_ExeSection(const aname:string);
  2818. begin
  2819. if aname='.reloc' then
  2820. GenerateRelocs;
  2821. inherited;
  2822. end;
  2823. {****************************************************************************
  2824. TDJCoffAssembler
  2825. ****************************************************************************}
  2826. constructor TDJCoffAssembler.Create(info: pasminfo; smart:boolean);
  2827. begin
  2828. inherited;
  2829. CObjOutput:=TDJCoffObjOutput;
  2830. CInternalAr:=tarobjectwriter;
  2831. end;
  2832. {****************************************************************************
  2833. TPECoffAssembler
  2834. ****************************************************************************}
  2835. constructor TPECoffAssembler.Create(info: pasminfo; smart:boolean);
  2836. begin
  2837. inherited;
  2838. CObjOutput:=TPECoffObjOutput;
  2839. CInternalAr:=tarobjectwriter;
  2840. end;
  2841. {*****************************************************************************
  2842. DLLReader
  2843. *****************************************************************************}
  2844. {$ifdef win32}
  2845. var
  2846. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  2847. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  2848. {$endif win32}
  2849. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  2850. type
  2851. TPECoffExpDir=packed record
  2852. flag,
  2853. stamp : cardinal;
  2854. Major,
  2855. Minor : word;
  2856. Name,
  2857. Base,
  2858. NumFuncs,
  2859. NumNames,
  2860. AddrFuncs,
  2861. AddrNames,
  2862. AddrOrds : cardinal;
  2863. end;
  2864. var
  2865. DLLReader : TObjectReader;
  2866. DosHeader : array[0..$7f] of byte;
  2867. PEMagic : array[0..3] of byte;
  2868. Header : TCoffHeader;
  2869. peheader : tcoffpeoptheader;
  2870. NameOfs,
  2871. newheaderofs : longword;
  2872. FuncName : string;
  2873. expdir : TPECoffExpDir;
  2874. i : longint;
  2875. found : boolean;
  2876. sechdr : tCoffSecHdr;
  2877. {$ifdef win32}
  2878. p : pointer;
  2879. {$endif win32}
  2880. begin
  2881. result:=false;
  2882. fillchar(sechdr,sizeof(sechdr),0);
  2883. {$ifdef win32}
  2884. if (target_info.system=system_x86_64_win64) and
  2885. assigned(Wow64DisableWow64FsRedirection) then
  2886. Wow64DisableWow64FsRedirection(p);
  2887. {$endif win32}
  2888. DLLReader:=TObjectReader.Create;
  2889. DLLReader.OpenFile(dllname);
  2890. {$ifdef win32}
  2891. if (target_info.system=system_x86_64_win64) and
  2892. assigned(Wow64RevertWow64FsRedirection) then
  2893. Wow64RevertWow64FsRedirection(p);
  2894. {$endif win32}
  2895. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  2896. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  2897. begin
  2898. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  2899. exit;
  2900. end;
  2901. newheaderofs:=cardinal(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  2902. DLLReader.Seek(newheaderofs);
  2903. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  2904. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  2905. begin
  2906. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  2907. exit;
  2908. end;
  2909. if not DLLReader.Read(Header,sizeof(TCoffHeader)) or
  2910. (Header.mach<>COFF_MAGIC) or
  2911. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  2912. begin
  2913. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  2914. exit;
  2915. end;
  2916. { Read optheader }
  2917. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  2918. { Section headers }
  2919. found:=false;
  2920. for i:=1 to header.nsects do
  2921. begin
  2922. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  2923. begin
  2924. Comment(V_Error,'Error reading coff file '+DLLName);
  2925. exit;
  2926. end;
  2927. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  2928. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  2929. begin
  2930. found:=true;
  2931. break;
  2932. end;
  2933. end;
  2934. if not found then
  2935. begin
  2936. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  2937. exit;
  2938. end;
  2939. { Process edata }
  2940. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  2941. DLLReader.Read(expdir,sizeof(expdir));
  2942. for i:=0 to expdir.NumNames-1 do
  2943. begin
  2944. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  2945. DLLReader.Read(NameOfs,4);
  2946. Dec(NameOfs,sechdr.rvaofs);
  2947. if {(NameOfs<0) or}
  2948. (NameOfs>sechdr.vsize) then
  2949. begin
  2950. Comment(V_Error,'DLL does contains invalid exports');
  2951. break;
  2952. end;
  2953. { Read Function name from DLL, prepend _ and terminate with #0 }
  2954. DLLReader.Seek(sechdr.datapos+NameOfs);
  2955. DLLReader.Read((@FuncName[1])^,sizeof(FuncName)-3);
  2956. FuncName[sizeof(FuncName)-1]:=#0;
  2957. FuncName[0]:=chr(Strlen(@FuncName[1]));
  2958. readdllproc(DLLName,FuncName);
  2959. end;
  2960. DLLReader.Free;
  2961. end;
  2962. {$ifdef arm}
  2963. function COFF_MAGIC: word;
  2964. begin
  2965. if GenerateThumb2Code and (current_settings.cputype>=cpu_armv7) then
  2966. COFF_MAGIC:=$1c4 // IMAGE_FILE_MACHINE_ARMNT
  2967. else
  2968. COFF_MAGIC:=$1c0; // IMAGE_FILE_MACHINE_ARM
  2969. end;
  2970. {$endif arm}
  2971. {*****************************************************************************
  2972. Initialize
  2973. *****************************************************************************}
  2974. {$ifdef i386}
  2975. const
  2976. as_i386_coff_info : tasminfo =
  2977. (
  2978. id : as_i386_coff;
  2979. idtxt : 'COFF';
  2980. asmbin : '';
  2981. asmcmd : '';
  2982. supported_targets : [system_i386_go32v2];
  2983. flags : [af_outputbinary,af_smartlink_sections];
  2984. labelprefix : '.L';
  2985. comment : '';
  2986. dollarsign: '$';
  2987. );
  2988. as_i386_pecoff_info : tasminfo =
  2989. (
  2990. id : as_i386_pecoff;
  2991. idtxt : 'PECOFF';
  2992. asmbin : '';
  2993. asmcmd : '';
  2994. supported_targets : [system_i386_win32,system_i386_nativent];
  2995. flags : [af_outputbinary,af_smartlink_sections];
  2996. labelprefix : '.L';
  2997. comment : '';
  2998. dollarsign: '$';
  2999. );
  3000. as_i386_pecoffwdosx_info : tasminfo =
  3001. (
  3002. id : as_i386_pecoffwdosx;
  3003. idtxt : 'PEWDOSX';
  3004. asmbin : '';
  3005. asmcmd : '';
  3006. supported_targets : [system_i386_wdosx];
  3007. flags : [af_outputbinary];
  3008. labelprefix : '.L';
  3009. comment : '';
  3010. dollarsign: '$';
  3011. );
  3012. as_i386_pecoffwince_info : tasminfo =
  3013. (
  3014. id : as_i386_pecoffwince;
  3015. idtxt : 'PECOFFWINCE';
  3016. asmbin : '';
  3017. asmcmd : '';
  3018. supported_targets : [system_i386_wince];
  3019. flags : [af_outputbinary,af_smartlink_sections];
  3020. labelprefix : '.L';
  3021. comment : '';
  3022. dollarsign: '$';
  3023. );
  3024. {$endif i386}
  3025. {$ifdef x86_64}
  3026. const
  3027. as_x86_64_pecoff_info : tasminfo =
  3028. (
  3029. id : as_x86_64_pecoff;
  3030. idtxt : 'PECOFF';
  3031. asmbin : '';
  3032. asmcmd : '';
  3033. supported_targets : [system_x86_64_win64];
  3034. flags : [af_outputbinary,af_smartlink_sections];
  3035. labelprefix : '.L';
  3036. comment : '';
  3037. dollarsign: '$';
  3038. );
  3039. {$endif x86_64}
  3040. {$ifdef arm}
  3041. const
  3042. as_arm_pecoffwince_info : tasminfo =
  3043. (
  3044. id : as_arm_pecoffwince;
  3045. idtxt : 'PECOFFWINCE';
  3046. asmbin : '';
  3047. asmcmd : '';
  3048. supported_targets : [system_arm_wince];
  3049. flags : [af_outputbinary,af_smartlink_sections];
  3050. labelprefix : '.L';
  3051. comment : '';
  3052. dollarsign: '$';
  3053. );
  3054. {$endif arm}
  3055. {$ifdef win32}
  3056. procedure SetupProcVars;
  3057. var
  3058. hinstLib : THandle;
  3059. begin
  3060. Wow64DisableWow64FsRedirection:=nil;
  3061. Wow64RevertWow64FsRedirection:=nil;
  3062. hinstLib:=LoadLibrary('kernel32.dll');
  3063. if hinstLib<>0 then
  3064. begin
  3065. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  3066. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  3067. FreeLibrary(hinstLib);
  3068. end;
  3069. end;
  3070. {$endif win32}
  3071. initialization
  3072. {$ifdef i386}
  3073. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  3074. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  3075. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  3076. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  3077. {$endif i386}
  3078. {$ifdef x86_64}
  3079. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  3080. {$endif x86_64}
  3081. {$ifdef arm}
  3082. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  3083. {$endif arm}
  3084. {$ifdef win32}
  3085. SetupProcVars;
  3086. {$endif win32}
  3087. end.