ogcoff.pas 147 KB

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