ogcoff.pas 125 KB

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