ogcoff.pas 121 KB

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