ogcoff.pas 125 KB

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