ogcoff.pas 127 KB

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