ogcoff.pas 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095
  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. TObjSymbolArray = array of TObjSymbol;
  34. TObjSectionArray = array of TObjSection;
  35. tcoffpedatadir = packed record
  36. vaddr : longword;
  37. size : longword;
  38. end;
  39. tcoffheader = packed record
  40. mach : word;
  41. nsects : word;
  42. time : longword;
  43. sympos : longword;
  44. syms : longword;
  45. opthdr : word;
  46. flag : word;
  47. end;
  48. tcoffbigobjheader = packed record
  49. Sig1 : word;
  50. Sig2 : word;
  51. Version : word;
  52. Machine : word;
  53. TimeDateStame : longword;
  54. UUID : array[0..15] of byte;
  55. unused : array[0..3] of longword;
  56. NumberOfSections : longword;
  57. PointerToSymbolTable : longword;
  58. NumberOfSymbols : longword;
  59. end;
  60. tcoffpeoptheader = packed record
  61. Magic : word;
  62. MajorLinkerVersion : byte;
  63. MinorLinkerVersion : byte;
  64. tsize : longword;
  65. dsize : longword;
  66. bsize : longword;
  67. entry : longword;
  68. text_start : longword;
  69. {$ifndef cpu64bitaddr}
  70. data_start : longword;
  71. {$endif cpu64bitaddr}
  72. ImageBase : aword;
  73. SectionAlignment : longword;
  74. FileAlignment : longword;
  75. MajorOperatingSystemVersion : word;
  76. MinorOperatingSystemVersion : word;
  77. MajorImageVersion : word;
  78. MinorImageVersion : word;
  79. MajorSubsystemVersion : word;
  80. MinorSubsystemVersion : word;
  81. Win32Version : longword;
  82. SizeOfImage : longword;
  83. SizeOfHeaders : longword;
  84. CheckSum : longword;
  85. Subsystem : word;
  86. DllCharacteristics : word;
  87. SizeOfStackReserve : aword;
  88. SizeOfStackCommit : aword;
  89. SizeOfHeapReserve : aword;
  90. SizeOfHeapCommit : aword;
  91. LoaderFlags : longword; { This field is obsolete }
  92. NumberOfRvaAndSizes : longword;
  93. DataDirectory : array[0..PE_DATADIR_ENTRIES-1] of tcoffpedatadir;
  94. end;
  95. tcoffsechdr = packed record
  96. name : array[0..7] of char;
  97. vsize : longword;
  98. rvaofs : longword;
  99. datasize : longword;
  100. datapos : longword;
  101. relocpos : longword;
  102. lineno1 : longword;
  103. nrelocs : word;
  104. lineno2 : word;
  105. flags : longword;
  106. end;
  107. TCoffObjSection = class(TObjSection)
  108. private
  109. orgmempos,
  110. coffrelocs,
  111. coffrelocpos : aword;
  112. public
  113. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);override;
  114. procedure writereloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);override;
  115. end;
  116. TCoffObjData = class(TObjData)
  117. private
  118. win32 : boolean;
  119. {$ifdef arm}
  120. eVCobj : boolean;
  121. {$endif arm}
  122. public
  123. constructor createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  124. procedure CreateDebugSections;override;
  125. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  126. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reloctype:TObjRelocationType);override;
  127. end;
  128. TDJCoffObjData = class(TCoffObjData)
  129. constructor create(const n:string);override;
  130. end;
  131. TPECoffObjData = class(TCoffObjData)
  132. constructor create(const n:string);override;
  133. end;
  134. TCoffObjOutput = class(tObjOutput)
  135. private
  136. win32 : boolean;
  137. bigobj : boolean;
  138. symidx : longint;
  139. FCoffSyms,
  140. FCoffStrs : tdynamicarray;
  141. procedure write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  142. procedure section_write_symbol(p:TObject;arg:pointer);
  143. procedure section_write_relocs(p:TObject;arg:pointer);
  144. procedure create_symbols(data:TObjData);
  145. procedure section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  146. procedure section_write_header(p:TObject;arg:pointer);
  147. protected
  148. function writedata(data:TObjData):boolean;override;
  149. public
  150. constructor createcoff(AWriter:TObjectWriter;awin32:boolean);
  151. destructor destroy;override;
  152. end;
  153. TDJCoffObjOutput = class(TCoffObjOutput)
  154. constructor create(AWriter:TObjectWriter);override;
  155. end;
  156. TPECoffObjOutput = class(TCoffObjOutput)
  157. constructor create(AWriter:TObjectWriter);override;
  158. end;
  159. TCoffObjInput = class(tObjInput)
  160. private
  161. FCoffsyms : tdynamicarray;
  162. FCoffStrs : TAnsiCharDynArray;
  163. FCoffStrSize: longword;
  164. { Convert symidx -> TObjSymbol }
  165. FSymTbl : TObjSymbolArray;
  166. { Convert secidx -> TObjSection }
  167. FSecCount : Longint;
  168. FSecTbl : TObjSectionArray;
  169. win32 : boolean;
  170. bigobj : boolean;
  171. function GetSection(secidx:longint):TObjSection;
  172. function Read_str(strpos:longword):string;
  173. procedure read_relocs(s:TCoffObjSection);
  174. procedure read_symbols(objdata:TObjData);
  175. procedure ObjSections_read_relocs(p:TObject;arg:pointer);
  176. public
  177. constructor createcoff(awin32:boolean);
  178. destructor destroy;override;
  179. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  180. end;
  181. TDJCoffObjInput = class(TCoffObjInput)
  182. constructor create;override;
  183. end;
  184. TPECoffObjInput = class(TCoffObjInput)
  185. constructor create;override;
  186. end;
  187. TCoffexeoutput = class(texeoutput)
  188. private
  189. FCoffStrs : tdynamicarray;
  190. win32 : boolean;
  191. nsects : word;
  192. nsyms,
  193. sympos : aword;
  194. datapos_offset: longword;
  195. function totalheadersize:longword;
  196. procedure ExeSectionList_pass2_header(p:TObject;arg:pointer);
  197. procedure write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  198. procedure globalsyms_write_symbol(p:TObject;arg:pointer);
  199. procedure ExeSectionList_write_header(p:TObject;arg:pointer);
  200. protected
  201. function writedata:boolean;override;
  202. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList;const aPattern:string);override;
  203. procedure DoRelocationFixup(objsec:TObjSection);override;
  204. public
  205. constructor createcoff(awin32:boolean);
  206. procedure MemPos_Header;override;
  207. procedure DataPos_Header;override;
  208. procedure DataPos_Symbols;override;
  209. end;
  210. TDJCoffexeoutput = class(TCoffexeoutput)
  211. constructor create;override;
  212. procedure MemPos_Header;override;
  213. end;
  214. TPECoffexeoutput = class(TCoffexeoutput)
  215. private
  216. FImports: TFPHashObjectList;
  217. textobjsection,
  218. idata2objsection,
  219. idata4objsection,
  220. idata5objsection,
  221. idata6objsection,
  222. idata7objsection : TObjSection;
  223. FRelocsGenerated : boolean;
  224. procedure GenerateRelocs;
  225. public
  226. constructor create;override;
  227. procedure MarkTargetSpecificSections(WorkList:TFPObjectList);override;
  228. procedure AfterUnusedSectionRemoval;override;
  229. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  230. procedure MemPos_Start;override;
  231. procedure MemPos_ExeSection(const aname:string);override;
  232. end;
  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. type
  1546. multi = record
  1547. case integer of
  1548. 0 : (ba : array[0..sizeof(aint)-1] of byte);
  1549. 1 : (b : byte);
  1550. 2 : (w : word);
  1551. 4 : (d : dword);
  1552. 8 : (q : qword);
  1553. end;
  1554. var
  1555. ba : multi;
  1556. curraddr,
  1557. symaddr : aword;
  1558. begin
  1559. if CurrObjSec=nil then
  1560. internalerror(2004030701);
  1561. if assigned(p) then
  1562. begin
  1563. { current address }
  1564. curraddr:=CurrObjSec.mempos+CurrObjSec.Size;
  1565. { external/common symbols don't have a fixed memory position yet }
  1566. if (p.bind=AB_COMMON) then
  1567. begin
  1568. { For go32v2 we need to use the size as address }
  1569. if not win32 then
  1570. symaddr:=p.size
  1571. else
  1572. symaddr:=0;
  1573. end
  1574. else
  1575. symaddr:=p.address;
  1576. { no symbol relocation need inside a section }
  1577. if (p.objsection=CurrObjSec) and
  1578. (p.bind<>AB_COMMON) then
  1579. begin
  1580. case reloctype of
  1581. RELOC_ABSOLUTE :
  1582. begin
  1583. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE);
  1584. inc(data,symaddr);
  1585. end;
  1586. {$ifdef cpu64bitaddr}
  1587. RELOC_ABSOLUTE32 :
  1588. begin
  1589. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE32);
  1590. inc(data,symaddr);
  1591. end;
  1592. {$endif cpu64bitaddr}
  1593. RELOC_RELATIVE :
  1594. begin
  1595. //inc(data,symaddr-len-CurrObjSec.Size);
  1596. data:=data+symaddr-len-CurrObjSec.Size;
  1597. end;
  1598. {$ifdef ARM}
  1599. RELOC_RELATIVE_24,
  1600. RELOC_RELATIVE_CALL:
  1601. begin
  1602. data:=(data and $ff000000) or (((((data and $ffffff) shl 2)+(symaddr-CurrObjSec.Size)) shr 2) and $FFFFFF); // TODO: Check overflow
  1603. end;
  1604. {$endif ARM}
  1605. RELOC_RVA,
  1606. RELOC_SECREL32 :
  1607. begin
  1608. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,reloctype);
  1609. inc(data,symaddr);
  1610. end;
  1611. else
  1612. internalerror(200604013);
  1613. end;
  1614. end
  1615. else
  1616. begin
  1617. if (p.objsection<>nil) and
  1618. (p.bind<>AB_COMMON) then
  1619. CurrObjSec.addsectionreloc(curraddr,p.objsection,reloctype)
  1620. else
  1621. CurrObjSec.addsymreloc(curraddr,p,reloctype);
  1622. if (not win32) or
  1623. (p.objsection<>nil) then
  1624. inc(data,symaddr);
  1625. if reloctype=RELOC_RELATIVE then
  1626. begin
  1627. if win32 then
  1628. dec(data,len-4)
  1629. else
  1630. dec(data,len+CurrObjSec.Size);
  1631. end;
  1632. end;
  1633. end
  1634. else
  1635. begin
  1636. if reloctype=RELOC_RVA then
  1637. internalerror(200603033);
  1638. end;
  1639. if target_info.endian<>source_info.endian then
  1640. begin
  1641. ba.q:=0;
  1642. if (len<=sizeof(data)) then
  1643. case len of
  1644. 1 : ba.b:=byte(data);
  1645. 2 : begin
  1646. ba.w:=word(data);
  1647. ba.w:=swapendian(ba.w);
  1648. end;
  1649. 4 : begin
  1650. ba.d:=dword(data);
  1651. ba.d:=swapendian(ba.d);
  1652. end;
  1653. 8 : begin
  1654. ba.q:=qword(data);
  1655. ba.q:=swapendian(ba.q);
  1656. end;
  1657. else
  1658. internalerror(2024012501);
  1659. end;
  1660. CurrObjSec.write(ba,len);
  1661. end
  1662. else
  1663. CurrObjSec.write(data,len);
  1664. end;
  1665. {****************************************************************************
  1666. TDJCoffObjData
  1667. ****************************************************************************}
  1668. constructor TDJCoffObjData.create(const n:string);
  1669. begin
  1670. inherited createcoff(n,false,TCoffObjSection);
  1671. end;
  1672. {****************************************************************************
  1673. TPECoffObjData
  1674. ****************************************************************************}
  1675. constructor TPECoffObjData.create(const n:string);
  1676. begin
  1677. inherited createcoff(n,true,TCoffObjSection);
  1678. end;
  1679. {****************************************************************************
  1680. TCoffObjOutput
  1681. ****************************************************************************}
  1682. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1683. begin
  1684. inherited create(AWriter);
  1685. win32:=awin32;
  1686. end;
  1687. destructor TCoffObjOutput.destroy;
  1688. begin
  1689. FCoffSyms.free;
  1690. FCoffStrs.free;
  1691. inherited destroy;
  1692. end;
  1693. procedure TCoffObjOutput.write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  1694. var
  1695. sym : coffsymbol;
  1696. bosym : coffbigobjsymbol;
  1697. strpos : longword;
  1698. begin
  1699. { symbolname }
  1700. if length(name)>8 then
  1701. begin
  1702. strpos:=FCoffStrs.size+4;
  1703. FCoffStrs.writestr(name);
  1704. FCoffStrs.writestr(#0);
  1705. end
  1706. else
  1707. strpos:=0;
  1708. if bigobj then
  1709. begin
  1710. fillchar(bosym,sizeof(bosym),0);
  1711. if length(name)>8 then
  1712. bosym.name.offset.offset:=strpos
  1713. else
  1714. move(name[1],bosym.name.shortname,length(name));
  1715. bosym.value:=value;
  1716. bosym.SectionNumber:=longword(section);
  1717. bosym.StorageClass:=typ;
  1718. bosym.NumberOfAuxSymbols:=aux;
  1719. inc(symidx);
  1720. MaybeSwap(bosym);
  1721. FCoffSyms.write(bosym,sizeof(bosym));
  1722. end
  1723. else
  1724. begin
  1725. if section>$7fff then
  1726. internalerror(2017020302);
  1727. FillChar(sym,sizeof(sym),0);
  1728. if length(name)>8 then
  1729. sym.strpos:=strpos
  1730. else
  1731. move(name[1],sym.name,length(name));
  1732. sym.value:=value;
  1733. sym.section:=section;
  1734. sym.typ:=typ;
  1735. sym.aux:=aux;
  1736. inc(symidx);
  1737. MaybeSwap(sym);
  1738. FCoffSyms.write(sym,sizeof(sym));
  1739. end;
  1740. end;
  1741. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1742. var
  1743. secrec : coffsectionrec;
  1744. padding : word;
  1745. begin
  1746. with TCoffObjSection(p) do
  1747. begin
  1748. Inc(plongword(arg)^);
  1749. index:=plongword(arg)^;
  1750. secsymidx:=symidx;
  1751. { Both GNU and Microsoft toolchains write section symbols using
  1752. storage class 3 (STATIC).
  1753. No reason to use COFF_SYM_SECTION, it is silently converted to 3 by
  1754. PE binutils and causes warnings with DJGPP binutils. }
  1755. write_symbol(name,mempos,index,COFF_SYM_LOCAL,1);
  1756. { AUX }
  1757. fillchar(secrec,sizeof(secrec),0);
  1758. secrec.len:=Size;
  1759. if ObjRelocations.count<65535 then
  1760. secrec.nrelocs:=ObjRelocations.count
  1761. else
  1762. secrec.nrelocs:=65535;
  1763. inc(symidx);
  1764. MaybeSwap(secrec);
  1765. FCoffSyms.write(secrec,sizeof(secrec));
  1766. { aux recs have the same size as symbols, so we need to add two
  1767. Byte of padding in case of a Big Obj Coff }
  1768. padding:=0;
  1769. if bigobj then
  1770. FCoffSyms.write(padding,sizeof(padding));
  1771. end;
  1772. end;
  1773. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1774. var
  1775. i : longint;
  1776. rel : coffreloc;
  1777. objreloc : TObjRelocation;
  1778. begin
  1779. if (TObjSection(p).ObjRelocations.Count>65535) then
  1780. begin
  1781. rel.address:=TObjSection(p).ObjRelocations.Count+1;
  1782. rel.sym:=0;
  1783. rel.reloctype:=0;
  1784. MaybeSwap(rel);
  1785. FWriter.Write(rel,sizeof(rel));
  1786. end;
  1787. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1788. begin
  1789. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1790. rel.address:=objreloc.dataoffset;
  1791. if assigned(objreloc.symbol) then
  1792. begin
  1793. if (objreloc.symbol.bind=AB_LOCAL) then
  1794. rel.sym:=objreloc.symbol.objsection.secsymidx
  1795. else
  1796. begin
  1797. if objreloc.symbol.symidx=-1 then
  1798. internalerror(200602233);
  1799. rel.sym:=objreloc.symbol.symidx;
  1800. end;
  1801. end
  1802. else
  1803. begin
  1804. if objreloc.objsection<>nil then
  1805. rel.sym:=objreloc.objsection.secsymidx
  1806. else
  1807. rel.sym:=0;
  1808. end;
  1809. case objreloc.typ of
  1810. {$ifdef arm}
  1811. RELOC_ABSOLUTE :
  1812. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1813. { I've no idea if this is correct (FK):
  1814. RELOC_RELATIVE :
  1815. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1816. }
  1817. RELOC_RVA :
  1818. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1819. RELOC_SECREL32 :
  1820. rel.reloctype:=IMAGE_REL_ARM_SECREL;
  1821. RELOC_RELATIVE_24 :
  1822. rel.reloctype:=IMAGE_REL_ARM_BRANCH24;
  1823. RELOC_RELATIVE_CALL :
  1824. rel.reloctype:=IMAGE_REL_ARM_BLX24;
  1825. RELOC_RELATIVE_24_THUMB:
  1826. rel.reloctype:=IMAGE_REL_ARM_BLX23T;
  1827. {$endif arm}
  1828. {$ifdef i386}
  1829. RELOC_RELATIVE :
  1830. rel.reloctype:=IMAGE_REL_I386_PCRLONG;
  1831. RELOC_ABSOLUTE :
  1832. rel.reloctype:=IMAGE_REL_I386_DIR32;
  1833. RELOC_RVA :
  1834. rel.reloctype:=IMAGE_REL_I386_IMAGEBASE;
  1835. RELOC_SECREL32 :
  1836. rel.reloctype:=IMAGE_REL_I386_SECREL32;
  1837. {$endif i386}
  1838. {$ifdef x86_64}
  1839. RELOC_NONE :
  1840. rel.reloctype:=IMAGE_REL_AMD64_ABSOLUTE;
  1841. RELOC_RELATIVE :
  1842. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1843. RELOC_ABSOLUTE32 :
  1844. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1845. RELOC_ABSOLUTE :
  1846. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1847. RELOC_RVA :
  1848. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1849. RELOC_RELATIVE_1 :
  1850. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1851. RELOC_RELATIVE_2 :
  1852. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1853. RELOC_RELATIVE_3 :
  1854. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1855. RELOC_RELATIVE_4 :
  1856. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1857. RELOC_RELATIVE_5 :
  1858. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1859. RELOC_SECREL32 :
  1860. rel.reloctype:=IMAGE_REL_AMD64_SECREL;
  1861. {$endif x86_64}
  1862. {$ifdef aarch64}
  1863. RELOC_NONE :
  1864. rel.reloctype:=IMAGE_REL_ARM64_ABSOLUTE;
  1865. RELOC_ABSOLUTE32 :
  1866. rel.reloctype:=IMAGE_REL_ARM64_ADDR32;
  1867. RELOC_RVA :
  1868. rel.reloctype:=IMAGE_REL_ARM64_ADDR32NB;
  1869. RELOC_RELATIVE_26 :
  1870. rel.reloctype:=IMAGE_REL_ARM64_BRANCH26;
  1871. RELOC_ADR_PREL_PG_HI21 :
  1872. rel.reloctype:=IMAGE_REL_ARM64_PAGEBASE_REL21;
  1873. RELOC_ADR_PREL_LO21 :
  1874. rel.reloctype:=IMAGE_REL_ARM64_REL21;
  1875. RELOC_ADD_ABS_LO12:
  1876. rel.reloctype:=IMAGE_REL_ARM64_PAGEOFFSET_12A;
  1877. RELOC_LDST8_ABS_LO12:
  1878. rel.reloctype:=IMAGE_REL_ARM64_PAGEOFFSET_12L;
  1879. RELOC_SECREL32:
  1880. rel.reloctype:=IMAGE_REL_ARM64_SECREL;
  1881. {IMAGE_REL_ARM64_SECREL_LOW12A
  1882. IMAGE_REL_ARM64_SECREL_HIGH12A
  1883. IMAGE_REL_ARM64_SECREL_LOW12L
  1884. IMAGE_REL_ARM64_TOKEN
  1885. IMAGE_REL_ARM64_SECTION}
  1886. RELOC_ABSOLUTE:
  1887. rel.reloctype:=IMAGE_REL_ARM64_ADDR64;
  1888. RELOC_RELATIVE_19:
  1889. rel.reloctype:=IMAGE_REL_ARM64_BRANCH19;
  1890. {$endif aarch64}
  1891. else
  1892. internalerror(200905071);
  1893. end;
  1894. MaybeSwap(rel);
  1895. FWriter.write(rel,sizeof(rel));
  1896. end;
  1897. end;
  1898. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1899. var
  1900. filename : string[20];
  1901. filenamelen : longint;
  1902. sectionval : word;
  1903. globalval : byte;
  1904. i : longint;
  1905. value : aword;
  1906. objsym : TObjSymbol;
  1907. secidx : longword;
  1908. begin
  1909. with TCoffObjData(data) do
  1910. begin
  1911. symidx:=0;
  1912. { The `.file' record, and the file name auxiliary record }
  1913. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1914. fillchar(filename,sizeof(filename),0);
  1915. filename:=ExtractFileName(current_module.mainsource);
  1916. inc(symidx);
  1917. if bigobj then
  1918. filenamelen:=sizeof(coffbigobjsymbol)
  1919. else
  1920. filenamelen:=sizeof(coffsymbol);
  1921. FCoffSyms.write(filename[1],filenamelen);
  1922. { Sections }
  1923. secidx:=0;
  1924. ObjSectionList.ForEachCall(@section_write_symbol,@secidx);
  1925. { ObjSymbols }
  1926. for i:=0 to ObjSymbolList.Count-1 do
  1927. begin
  1928. objsym:=TObjSymbol(ObjSymbolList[i]);
  1929. if (objsym.bind=AB_LOCAL) then
  1930. continue;
  1931. case objsym.bind of
  1932. AB_GLOBAL,
  1933. AB_PRIVATE_EXTERN:
  1934. begin
  1935. globalval:=COFF_SYM_GLOBAL;
  1936. sectionval:=objsym.objsection.index;
  1937. value:=objsym.address;
  1938. end;
  1939. AB_LOCAL :
  1940. begin
  1941. globalval:=COFF_SYM_LOCAL;
  1942. sectionval:=objsym.objsection.index;
  1943. value:=objsym.address;
  1944. end;
  1945. else
  1946. begin
  1947. globalval:=COFF_SYM_GLOBAL;
  1948. sectionval:=0;
  1949. value:=objsym.size;
  1950. end;
  1951. end;
  1952. { symbolname }
  1953. objsym.symidx:=symidx;
  1954. write_symbol(objsym.name,value,sectionval,globalval,0);
  1955. end;
  1956. end;
  1957. end;
  1958. procedure TCoffObjOutput.section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  1959. begin
  1960. p.coffrelocpos:=datapos;
  1961. inc(datapos,sizeof(coffreloc)*p.ObjRelocations.count);
  1962. if p.ObjRelocations.count>65535 then
  1963. begin
  1964. if win32 then
  1965. inc(datapos,sizeof(coffreloc))
  1966. else
  1967. Message1(asmw_f_too_many_relocations,p.fullname);
  1968. end;
  1969. end;
  1970. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1971. var
  1972. sechdr : tcoffsechdr;
  1973. s : string;
  1974. strpos : aword;
  1975. begin
  1976. with TCoffObjSection(p) do
  1977. begin
  1978. fillchar(sechdr,sizeof(sechdr),0);
  1979. s:=name;
  1980. if length(s)>8 then
  1981. begin
  1982. strpos:=FCoffStrs.size+4;
  1983. FCoffStrs.writestr(s);
  1984. FCoffStrs.writestr(#0);
  1985. if strpos>=10000000 then
  1986. s:='//'+encodeBase64(strpos)
  1987. else
  1988. s:='/'+ToStr(strpos);
  1989. if length(s)>8 then
  1990. internalerror(2020091501);
  1991. end;
  1992. move(s[1],sechdr.name,length(s));
  1993. if not win32 then
  1994. begin
  1995. sechdr.rvaofs:=mempos;
  1996. sechdr.vsize:=mempos;
  1997. end
  1998. else
  1999. begin
  2000. if not(oso_data in secoptions) then
  2001. sechdr.vsize:=Size;
  2002. end;
  2003. sechdr.DataSize:=size;
  2004. if (Size>0) and
  2005. (oso_data in secoptions) then
  2006. sechdr.datapos:=datapos;
  2007. if ObjRelocations.count<65535 then
  2008. sechdr.nrelocs:=ObjRelocations.count
  2009. else
  2010. sechdr.nrelocs:=65535;
  2011. sechdr.relocpos:=coffrelocpos;
  2012. if win32 then
  2013. begin
  2014. sechdr.flags:=peencodesechdrflags(secoptions,secalign);
  2015. if ObjRelocations.count>65535 then
  2016. sechdr.flags:=sechdr.flags or PE_SCN_LNK_NRELOC_OVFL;
  2017. end
  2018. else
  2019. sechdr.flags:=djencodesechdrflags(secoptions);
  2020. MaybeSwap(sechdr);
  2021. FWriter.write(sechdr,sizeof(sechdr));
  2022. end;
  2023. end;
  2024. function TCoffObjOutput.writedata(data:TObjData):boolean;
  2025. var
  2026. datapos,
  2027. sympos : aword;
  2028. i : longint;
  2029. header : tcoffheader;
  2030. boheader : tcoffbigobjheader;
  2031. begin
  2032. result:=false;
  2033. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  2034. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  2035. with TCoffObjData(data) do
  2036. begin
  2037. bigobj:=(ObjSectionList.Count>$7fff) and win32;
  2038. { Create Symbol Table }
  2039. create_symbols(data);
  2040. { Calculate the filepositions }
  2041. if bigobj then
  2042. datapos:=sizeof(tcoffbigobjheader)
  2043. else
  2044. datapos:=sizeof(tcoffheader);
  2045. datapos:=datapos+sizeof(tcoffsechdr)*ObjSectionList.Count;
  2046. { Sections first }
  2047. layoutsections(datapos);
  2048. { relocs }
  2049. for i:=0 to ObjSectionList.Count-1 do
  2050. section_set_reloc_datapos(TCoffObjSection(ObjSectionList[i]),datapos);
  2051. { Symbols }
  2052. sympos:=datapos;
  2053. { Generate COFF header }
  2054. if bigobj then
  2055. begin
  2056. fillchar(boheader,sizeof(boheader),0);
  2057. boheader.Sig1:=0;
  2058. boheader.Sig2:=$ffff;
  2059. boheader.Machine:=COFF_MAGIC;
  2060. boheader.Version:=COFF_BIG_OBJ_VERSION;
  2061. boheader.NumberOfSections:=longword(ObjSectionList.Count);
  2062. boheader.NumberOfSymbols:=longword(symidx);
  2063. boheader.PointerToSymbolTable:=sympos;
  2064. Move(COFF_BIG_OBJ_MAGIC,boheader.UUID,length(boheader.UUID));
  2065. MaybeSwap(boheader);
  2066. FWriter.write(boheader,sizeof(boheader));
  2067. end
  2068. else
  2069. begin
  2070. fillchar(header,sizeof(tcoffheader),0);
  2071. header.mach:=COFF_MAGIC;
  2072. header.nsects:=ObjSectionList.Count;
  2073. header.sympos:=sympos;
  2074. header.syms:=symidx;
  2075. if win32 then
  2076. begin
  2077. {$ifndef cpu64bitaddr}
  2078. header.flag:=PE_FILE_32BIT_MACHINE or
  2079. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  2080. {$else cpu64bitaddr}
  2081. header.flag:=PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  2082. {$endif cpu64bitaddr}
  2083. end
  2084. else
  2085. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  2086. MaybeSwap(header);
  2087. FWriter.write(header,sizeof(header));
  2088. end;
  2089. { Section headers }
  2090. ObjSectionList.ForEachCall(@section_write_header,nil);
  2091. { ObjSections }
  2092. WriteSectionContent(data);
  2093. { Relocs }
  2094. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  2095. { ObjSymbols }
  2096. if Sympos<>FWriter.ObjSize then
  2097. internalerror(200603051);
  2098. FWriter.writearray(FCoffSyms);
  2099. { Strings }
  2100. i:=FCoffStrs.size+4;
  2101. if source_info.endian<>target_info.endian then
  2102. i:=SwapEndian(i);
  2103. FWriter.write(i,4);
  2104. FWriter.writearray(FCoffStrs);
  2105. end;
  2106. FCoffStrs.Free;
  2107. FCoffStrs:=nil;
  2108. FCoffSyms.Free;
  2109. FCoffSyms:=nil;
  2110. end;
  2111. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  2112. begin
  2113. inherited createcoff(AWriter,false);
  2114. cobjdata:=TDJCoffObjData;
  2115. end;
  2116. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  2117. begin
  2118. inherited createcoff(AWriter,true);
  2119. cobjdata:=TPECoffObjData;
  2120. end;
  2121. {****************************************************************************
  2122. TCoffObjInput
  2123. ****************************************************************************}
  2124. constructor TCoffObjInput.createcoff(awin32:boolean);
  2125. begin
  2126. inherited create;
  2127. win32:=awin32;
  2128. bigobj:=false;
  2129. FSymTbl:=nil;
  2130. end;
  2131. destructor TCoffObjInput.destroy;
  2132. begin
  2133. FCoffSyms.free;
  2134. FCoffStrs:=nil;
  2135. FSymTbl:=nil;
  2136. FSecTbl:=nil;
  2137. inherited destroy;
  2138. end;
  2139. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  2140. begin
  2141. result:=nil;
  2142. if (secidx<1) or (secidx>FSecCount) then
  2143. begin
  2144. InputError('Failed reading coff file, invalid section index');
  2145. exit;
  2146. end;
  2147. result:=FSecTbl[secidx];
  2148. end;
  2149. function TCoffObjInput.Read_str(strpos:longword):string;
  2150. begin
  2151. if (FCoffStrs=nil) or (strpos>=FCoffStrSize) or (FCoffStrs[strpos]=#0) then
  2152. Internalerror(200205172);
  2153. result:=string(PChar(@FCoffStrs[strpos]));
  2154. end;
  2155. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  2156. var
  2157. rel : coffreloc;
  2158. rel_type : TObjRelocationType;
  2159. i : longint;
  2160. p : TObjSymbol;
  2161. begin
  2162. if s.coffrelocs=high(aword) then
  2163. begin
  2164. { If number of relocations exceeds 65535, it is stored in address field
  2165. of the first record, and includes this first fake relocation. }
  2166. FReader.read(rel,sizeof(rel));
  2167. MaybeSwap(rel);
  2168. s.coffrelocs:=rel.address-1;
  2169. if s.coffrelocs<=65535 then
  2170. InternalError(2013012503);
  2171. end;
  2172. for i:=1 to s.coffrelocs do
  2173. begin
  2174. FReader.read(rel,sizeof(rel));
  2175. MaybeSwap(rel);
  2176. case rel.reloctype of
  2177. {$ifdef arm}
  2178. IMAGE_REL_ARM_ABSOLUTE:
  2179. rel_type:=RELOC_NONE;
  2180. IMAGE_REL_ARM_ADDR32:
  2181. rel_type:=RELOC_ABSOLUTE;
  2182. IMAGE_REL_ARM_ADDR32NB:
  2183. rel_type:=RELOC_RVA;
  2184. IMAGE_REL_ARM_BRANCH24:
  2185. rel_type:=RELOC_RELATIVE_24;
  2186. IMAGE_REL_ARM_BLX24:
  2187. rel_type:=RELOC_RELATIVE_CALL;
  2188. IMAGE_REL_ARM_SECREL:
  2189. rel_type:=RELOC_SECREL32;
  2190. IMAGE_REL_ARM_BLX23T:
  2191. rel_type:=RELOC_RELATIVE_24_THUMB;
  2192. {$endif arm}
  2193. {$ifdef i386}
  2194. IMAGE_REL_I386_PCRLONG :
  2195. rel_type:=RELOC_RELATIVE;
  2196. IMAGE_REL_I386_DIR32 :
  2197. rel_type:=RELOC_ABSOLUTE;
  2198. IMAGE_REL_I386_IMAGEBASE :
  2199. rel_type:=RELOC_RVA;
  2200. IMAGE_REL_I386_SECREL32 :
  2201. rel_type:=RELOC_SECREL32;
  2202. {$endif i386}
  2203. {$ifdef x86_64}
  2204. IMAGE_REL_AMD64_ABSOLUTE:
  2205. rel_type:=RELOC_NONE;
  2206. IMAGE_REL_AMD64_REL32:
  2207. rel_type:=RELOC_RELATIVE;
  2208. IMAGE_REL_AMD64_ADDR32,
  2209. R_X86_64_32S:
  2210. rel_type:=RELOC_ABSOLUTE32;
  2211. IMAGE_REL_AMD64_ADDR64:
  2212. rel_type:=RELOC_ABSOLUTE;
  2213. IMAGE_REL_AMD64_ADDR32NB:
  2214. rel_type:=RELOC_RVA;
  2215. IMAGE_REL_AMD64_REL32_1:
  2216. rel_type:=RELOC_RELATIVE_1;
  2217. IMAGE_REL_AMD64_REL32_2:
  2218. rel_type:=RELOC_RELATIVE_2;
  2219. IMAGE_REL_AMD64_REL32_3:
  2220. rel_type:=RELOC_RELATIVE_3;
  2221. IMAGE_REL_AMD64_REL32_4:
  2222. rel_type:=RELOC_RELATIVE_4;
  2223. IMAGE_REL_AMD64_REL32_5:
  2224. rel_type:=RELOC_RELATIVE_5;
  2225. IMAGE_REL_AMD64_SECREL:
  2226. rel_type:=RELOC_SECREL32;
  2227. {$endif x86_64}
  2228. {$ifdef aarch64}
  2229. IMAGE_REL_ARM64_ABSOLUTE:
  2230. rel_type:=RELOC_NONE;
  2231. IMAGE_REL_ARM64_ADDR32:
  2232. rel_type:=RELOC_ABSOLUTE32;
  2233. IMAGE_REL_ARM64_ADDR32NB:
  2234. rel_type:=RELOC_RVA;
  2235. IMAGE_REL_ARM64_BRANCH26:
  2236. rel_type:=RELOC_RELATIVE_26;
  2237. IMAGE_REL_ARM64_PAGEBASE_REL21:
  2238. rel_type:=RELOC_ADR_PREL_PG_HI21;
  2239. IMAGE_REL_ARM64_REL21:
  2240. rel_type:=RELOC_ADR_PREL_LO21;
  2241. IMAGE_REL_ARM64_PAGEOFFSET_12A:
  2242. rel_type:=RELOC_ADD_ABS_LO12;
  2243. IMAGE_REL_ARM64_PAGEOFFSET_12L:
  2244. rel_type:=RELOC_LDST8_ABS_LO12;
  2245. IMAGE_REL_ARM64_SECREL:
  2246. rel_type:=RELOC_SECREL32;
  2247. //IMAGE_REL_ARM64_SECREL_LOW12A
  2248. //IMAGE_REL_ARM64_SECREL_HIGH12A
  2249. //IMAGE_REL_ARM64_SECREL_LOW12L
  2250. //IMAGE_REL_ARM64_TOKEN
  2251. //IMAGE_REL_ARM64_SECTION
  2252. IMAGE_REL_ARM64_ADDR64:
  2253. rel_type:=RELOC_ABSOLUTE;
  2254. //IMAGE_REL_ARM64_BRANCH19
  2255. {$endif aarch64}
  2256. else
  2257. begin
  2258. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  2259. exit;
  2260. end;
  2261. end;
  2262. p:=FSymTbl[rel.sym];
  2263. if assigned(p) then
  2264. s.addsymreloc(rel.address-s.mempos,p,rel_type)
  2265. else
  2266. begin
  2267. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  2268. exit;
  2269. end;
  2270. end;
  2271. end;
  2272. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  2273. var
  2274. size,
  2275. address,
  2276. nsyms,
  2277. symidx : aint;
  2278. i : longint;
  2279. sym : coffsymbol;
  2280. bosym : coffbigobjsymbol;
  2281. objsym : TObjSymbol;
  2282. bind : Tasmsymbind;
  2283. strname : string;
  2284. auxrec : array[0..sizeof(coffsymbol)-1] of byte;
  2285. boauxrec : array[0..sizeof(coffbigobjsymbol)-1] of byte;
  2286. psecrec : pcoffsectionrec;
  2287. secrec : coffsectionrec;
  2288. objsec : TObjSection;
  2289. secidx : longint;
  2290. symvalue : longword;
  2291. auxcount : byte;
  2292. symcls : byte;
  2293. comdatsel : TObjSectionComdatSelection;
  2294. { keeps string manipulations out of main routine }
  2295. procedure UnsupportedSymbolType;
  2296. begin
  2297. Comment(V_Fatal,'Unsupported COFF symbol type '+tostr(symcls)+' at index '+tostr(symidx)+' while reading '+InputFileName);
  2298. end;
  2299. begin
  2300. with TCoffObjData(objdata) do
  2301. begin
  2302. if bigobj then
  2303. nsyms:=FCoffSyms.Size div sizeof(coffbigobjsymbol)
  2304. else
  2305. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  2306. { Allocate memory for symidx -> TObjSymbol table }
  2307. SetLength(FSymTbl,nsyms);
  2308. { Load the Symbols }
  2309. FCoffSyms.Seek(0);
  2310. symidx:=0;
  2311. while (symidx<nsyms) do
  2312. begin
  2313. if bigobj then
  2314. begin
  2315. FCoffSyms.Read(bosym,sizeof(bosym));
  2316. MaybeSwap(bosym);
  2317. if bosym.Name.Offset.Zeroes<>0 then
  2318. begin
  2319. { Added for sake of global data analysis }
  2320. strname[0]:=#0;
  2321. move(bosym.Name.ShortName,strname[1],8);
  2322. strname[9]:=#0;
  2323. strname[0]:=chr(strlen(@strname[1]));
  2324. if strname='' then
  2325. internalerror(2017020301);
  2326. end
  2327. else
  2328. strname:=Read_str(bosym.Name.Offset.Offset);
  2329. secidx:=longint(bosym.SectionNumber);
  2330. symvalue:=bosym.Value;
  2331. auxcount:=bosym.NumberOfAuxSymbols;
  2332. symcls:=bosym.StorageClass;
  2333. end
  2334. else
  2335. begin
  2336. FCoffSyms.Read(sym,sizeof(sym));
  2337. MaybeSwap(sym);
  2338. if plongint(@sym.name)^<>0 then
  2339. begin
  2340. { Added for sake of global data analysis }
  2341. strname[0]:=#0;
  2342. move(sym.name,strname[1],8);
  2343. strname[9]:=#0;
  2344. strname[0]:=chr(strlen(@strname[1]));
  2345. if strname='' then
  2346. Internalerror(200205171);
  2347. end
  2348. else
  2349. strname:=Read_str(sym.strpos);
  2350. secidx:=sym.section;
  2351. symvalue:=sym.value;
  2352. auxcount:=sym.aux;
  2353. symcls:=sym.typ;
  2354. end;
  2355. bind:=AB_EXTERNAL;
  2356. size:=0;
  2357. address:=0;
  2358. objsym:=nil;
  2359. objsec:=nil;
  2360. case symcls of
  2361. COFF_SYM_GLOBAL :
  2362. begin
  2363. if secidx=0 then
  2364. begin
  2365. if symvalue=0 then
  2366. bind:=AB_EXTERNAL
  2367. else
  2368. begin
  2369. bind:=AB_COMMON;
  2370. size:=symvalue;
  2371. end;
  2372. end
  2373. else
  2374. begin
  2375. bind:=AB_GLOBAL;
  2376. objsec:=GetSection(secidx);
  2377. if symvalue>=objsec.mempos then
  2378. address:=symvalue-objsec.mempos;
  2379. end;
  2380. objsym:=CreateSymbol(strname);
  2381. objsym.bind:=bind;
  2382. objsym.typ:=AT_FUNCTION;
  2383. objsym.objsection:=objsec;
  2384. objsym.offset:=address;
  2385. objsym.size:=size;
  2386. end;
  2387. COFF_SYM_LABEL,
  2388. COFF_SYM_LOCAL :
  2389. begin
  2390. { do not add constants (section=-1) }
  2391. if secidx<>-1 then
  2392. begin
  2393. objsec:=GetSection(secidx);
  2394. if symvalue>=objsec.mempos then
  2395. address:=symvalue-objsec.mempos;
  2396. objsym:=CreateSymbol(strname);
  2397. objsym.bind:=AB_LOCAL;
  2398. objsym.typ:=AT_FUNCTION;
  2399. objsym.objsection:=objsec;
  2400. objsym.offset:=address;
  2401. objsym.size:=size;
  2402. end;
  2403. end;
  2404. COFF_SYM_SECTION :
  2405. begin
  2406. { GetSection checks that index is in range }
  2407. objsec:=GetSection(secidx);
  2408. if assigned(objsec) then
  2409. begin
  2410. if symvalue>=objsec.mempos then
  2411. address:=symvalue-objsec.mempos;
  2412. objsym:=CreateSymbol(strname);
  2413. objsym.bind:=AB_LOCAL;
  2414. objsym.typ:=AT_FUNCTION;
  2415. objsym.objsection:=objsec;
  2416. objsym.offset:=address;
  2417. objsym.size:=size;
  2418. end;
  2419. end;
  2420. COFF_SYM_FUNCTION,
  2421. COFF_SYM_FILE :
  2422. ;
  2423. else
  2424. UnsupportedSymbolType;
  2425. end;
  2426. FSymTbl[symidx]:=objsym;
  2427. { read aux records }
  2428. { handle COMDAT symbols }
  2429. if (symcls=COFF_SYM_LOCAL) and (auxcount=1) and (symvalue=0) and (oso_comdat in objsym.objsection.SecOptions) then
  2430. begin
  2431. if bigobj then
  2432. begin
  2433. FCoffSyms.Read(boauxrec,sizeof(boauxrec));
  2434. psecrec:=pcoffsectionrec(@boauxrec[0]);
  2435. end
  2436. else
  2437. begin
  2438. FCoffSyms.Read(auxrec,sizeof(auxrec));
  2439. psecrec:=pcoffsectionrec(@auxrec);
  2440. end;
  2441. secrec:=psecrec^;
  2442. MaybeSwap(secrec);
  2443. case secrec.select of
  2444. IMAGE_COMDAT_SELECT_NODUPLICATES:
  2445. comdatsel:=oscs_none;
  2446. IMAGE_COMDAT_SELECT_ANY:
  2447. comdatsel:=oscs_any;
  2448. IMAGE_COMDAT_SELECT_SAME_SIZE:
  2449. comdatsel:=oscs_same_size;
  2450. IMAGE_COMDAT_SELECT_EXACT_MATCH:
  2451. comdatsel:=oscs_exact_match;
  2452. IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  2453. comdatsel:=oscs_associative;
  2454. IMAGE_COMDAT_SELECT_LARGEST:
  2455. comdatsel:=oscs_largest;
  2456. else begin
  2457. comdatsel:=oscs_none;
  2458. { there are object files out here that have comdat symbols
  2459. including an associative section, but with a comdat selection
  2460. of 0; it seems that other linkers just ignore those... }
  2461. if secrec.select<>0 then
  2462. Message2(link_e_comdat_select_unsupported,inttostr(secrec.select),objsym.objsection.name);
  2463. end;
  2464. end;
  2465. if comdatsel in [oscs_associative] then
  2466. { only temporary }
  2467. Comment(V_Error,'Associative COMDAT sections are not yet supported (symbol: '+objsym.objsection.Name+')')
  2468. else if (comdatsel=oscs_associative) and (secrec.assoc=0) then
  2469. Message1(link_e_comdat_associative_section_expected,objsym.objsection.name)
  2470. else if (objsym.objsection.ComdatSelection<>oscs_none) and (comdatsel<>oscs_none) and (objsym.objsection.ComdatSelection<>comdatsel) then
  2471. Message2(link_e_comdat_not_matching,objsym.objsection.Name,objsym.Name)
  2472. else
  2473. begin
  2474. objsym.objsection.ComdatSelection:=comdatsel;
  2475. if (secrec.assoc<>0) and not assigned(objsym.objsection.AssociativeSection) then
  2476. begin
  2477. objsym.objsection.AssociativeSection:=GetSection(secrec.assoc-1);
  2478. if not assigned(objsym.objsection.AssociativeSection) then
  2479. Message1(link_e_comdat_associative_section_not_found,objsym.objsection.Name);
  2480. end;
  2481. end;
  2482. dec(auxcount);
  2483. inc(symidx);
  2484. end;
  2485. for i:=1 to auxcount do
  2486. begin
  2487. if bigobj then
  2488. FCoffSyms.Read(boauxrec,sizeof(boauxrec))
  2489. else
  2490. FCoffSyms.Read(auxrec,sizeof(auxrec));
  2491. inc(symidx);
  2492. end;
  2493. inc(symidx);
  2494. end;
  2495. end;
  2496. end;
  2497. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  2498. begin
  2499. with TCoffObjSection(p) do
  2500. begin
  2501. { Skip debug sections }
  2502. if (oso_debug in secoptions) and
  2503. (cs_link_strip in current_settings.globalswitches) and
  2504. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  2505. exit;
  2506. if coffrelocs>0 then
  2507. begin
  2508. FReader.Seek(coffrelocpos);
  2509. read_relocs(TCoffObjSection(p));
  2510. end;
  2511. end;
  2512. end;
  2513. function TCoffObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  2514. var
  2515. secalign : longint;
  2516. secofs,
  2517. strpos,
  2518. i : longint;
  2519. sympos,
  2520. symcount,
  2521. symsize,
  2522. code : longint;
  2523. objsec : TCoffObjSection;
  2524. secoptions : TObjSectionOptions;
  2525. header : tcoffheader;
  2526. boheader : tcoffbigobjheader;
  2527. sechdr : tcoffsechdr;
  2528. secname : string;
  2529. secnamebuf : array[0..15] of char;
  2530. begin
  2531. FReader:=AReader;
  2532. InputFileName:=AReader.FileName;
  2533. objdata:=CObjData.Create(InputFileName);
  2534. result:=false;
  2535. boheader:=default(tcoffbigobjheader);
  2536. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  2537. with TCoffObjData(objdata) do
  2538. begin
  2539. { Read COFF header }
  2540. if not AReader.read(header,sizeof(tcoffheader)) then
  2541. begin
  2542. InputError('Can''t read COFF Header');
  2543. exit;
  2544. end;
  2545. MaybeSwap(header);
  2546. if (header.mach=0) and (header.nsects=$ffff) then
  2547. begin
  2548. { either a library or big obj COFF }
  2549. AReader.seek(0);
  2550. if not AReader.read(boheader,sizeof(boheader)) then
  2551. begin
  2552. InputError('Can''t read Big Obj COFF Header');
  2553. exit;
  2554. end;
  2555. MaybeSwap(boheader);
  2556. if CompareByte(boheader.UUID,COFF_BIG_OBJ_MAGIC,length(boheader.uuid))<>0 then
  2557. begin
  2558. { ToDo: this should be treated as a library }
  2559. InputError('Illegal Big Obj COFF Magic');
  2560. exit;
  2561. end;
  2562. if boheader.Version<>COFF_BIG_OBJ_VERSION then
  2563. begin
  2564. InputError('Illegal Big Obj COFF Version');
  2565. exit;
  2566. end;
  2567. if boheader.Machine<>COFF_MAGIC then
  2568. begin
  2569. InputError('Illegal COFF Machine type');
  2570. exit;
  2571. end;
  2572. bigobj:=true;
  2573. end
  2574. else if header.mach<>COFF_MAGIC then
  2575. begin
  2576. InputError('Illegal COFF Magic');
  2577. exit;
  2578. end;
  2579. {$ifdef arm}
  2580. eVCobj:=header.flag=$100;
  2581. {$endif arm}
  2582. { ObjSymbols }
  2583. if bigobj then
  2584. begin
  2585. sympos:=longint(boheader.PointerToSymbolTable);
  2586. symcount:=longint(boheader.NumberOfSymbols);
  2587. symsize:=sizeof(CoffBigObjSymbol);
  2588. end
  2589. else
  2590. begin
  2591. sympos:=longint(header.sympos);
  2592. symcount:=longint(header.syms);
  2593. symsize:=sizeof(CoffSymbol);
  2594. end;
  2595. AReader.Seek(sympos);
  2596. if not AReader.ReadArray(FCoffSyms,symcount*symsize) then
  2597. begin
  2598. InputError('Error reading coff symbol table');
  2599. exit;
  2600. end;
  2601. { Strings }
  2602. if not AReader.Read(FCoffStrSize,4) then
  2603. begin
  2604. InputError('Error reading COFF string table');
  2605. exit;
  2606. end;
  2607. if (FCoffStrSize>4) then
  2608. begin
  2609. { allocate an extra byte and null-terminate }
  2610. SetLength(FCoffStrs,FCoffStrSize+1);
  2611. FCoffStrs[FCoffStrSize]:=#0;
  2612. for i:=0 to 3 do
  2613. FCoffStrs[i]:=#0;
  2614. if not AReader.Read(FCoffStrs[4],FCoffStrSize-4) then
  2615. begin
  2616. InputError('Error reading COFF string table');
  2617. exit;
  2618. end;
  2619. end;
  2620. { Section headers }
  2621. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  2622. if bigobj then
  2623. FSecCount:=longint(boheader.NumberOfSections)
  2624. else
  2625. FSecCount:=header.nsects;
  2626. SetLength(FSecTbl,(FSecCount+1));
  2627. if bigobj then
  2628. secofs:=sizeof(tcoffbigobjheader)
  2629. else
  2630. secofs:=sizeof(tcoffheader)+header.opthdr;
  2631. AReader.Seek(secofs);
  2632. for i:=1 to FSecCount do
  2633. begin
  2634. if not AReader.read(sechdr,sizeof(sechdr)) then
  2635. begin
  2636. InputError('Error reading COFF Section Headers');
  2637. exit;
  2638. end;
  2639. MaybeSwap(sechdr);
  2640. move(sechdr.name,secnamebuf,8);
  2641. secnamebuf[8]:=#0;
  2642. secname:=strpas(secnamebuf);
  2643. if secname[1]='/' then
  2644. begin
  2645. if secname[2]='/' then
  2646. begin
  2647. if not decodeBase64(copy(secname,3,8),strpos) then
  2648. begin
  2649. InputError('Error reading COFF Section Headers');
  2650. secname:='error';
  2651. end
  2652. else
  2653. secname:=Read_str(strpos);
  2654. end
  2655. else
  2656. begin
  2657. Val(Copy(secname,2,8),strpos,code);
  2658. if code=0 then
  2659. secname:=Read_str(strpos)
  2660. else
  2661. begin
  2662. InputError('Error reading COFF Section Headers');
  2663. secname:='error';
  2664. end;
  2665. end;
  2666. end;
  2667. if win32 then
  2668. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  2669. else
  2670. begin
  2671. secoptions:=djdecodesechdrflags(secname,sechdr.flags);
  2672. secalign:=sizeof(pint);
  2673. end;
  2674. if (Length(secname)>3) and (secname[2] in ['e','f','i','p','r']) then
  2675. begin
  2676. if (Pos('.edata',secname)=1) or
  2677. (Pos('.rsrc',secname)=1) or
  2678. ((target_info.system=system_arm_wince) and (Pos('.pdata',secname)=1)) or
  2679. (Pos('.fpc',secname)=1) then
  2680. include(secoptions,oso_keep);
  2681. if (Pos('.idata',secname)=1) then
  2682. begin
  2683. { TODO: idata keep can maybe replaced with grouping of text and idata}
  2684. include(secoptions,oso_keep);
  2685. secname:=secname + '.' + ExtractFileName(InputFileName);
  2686. end;
  2687. end;
  2688. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions,false));
  2689. FSecTbl[i]:=objsec;
  2690. if not win32 then
  2691. objsec.mempos:=sechdr.rvaofs;
  2692. objsec.orgmempos:=sechdr.rvaofs;
  2693. objsec.coffrelocs:=sechdr.nrelocs;
  2694. if win32 then
  2695. begin
  2696. if (sechdr.flags and PE_SCN_LNK_NRELOC_OVFL)<>0 then
  2697. objsec.coffrelocs:=high(aword);
  2698. end;
  2699. objsec.coffrelocpos:=sechdr.relocpos;
  2700. objsec.datapos:=sechdr.datapos;
  2701. objsec.Size:=sechdr.dataSize;
  2702. end;
  2703. { Insert all ObjSymbols }
  2704. read_symbols(objdata);
  2705. { Section Data }
  2706. ReadSectionContent(objdata);
  2707. { Relocs }
  2708. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  2709. end;
  2710. FCoffStrs:=nil;
  2711. FCoffSyms.Free;
  2712. FCoffSyms:=nil;
  2713. result:=true;
  2714. end;
  2715. constructor TDJCoffObjInput.create;
  2716. begin
  2717. inherited createcoff(false);
  2718. cobjdata:=TDJCoffObjData;
  2719. end;
  2720. constructor TPECoffObjInput.create;
  2721. begin
  2722. inherited createcoff(true);
  2723. cobjdata:=TPECoffObjData;
  2724. end;
  2725. {****************************************************************************
  2726. TCoffexeoutput
  2727. ****************************************************************************}
  2728. constructor TCoffexeoutput.createcoff(awin32:boolean);
  2729. begin
  2730. inherited create;
  2731. win32:=awin32;
  2732. if target_info.system in [system_x86_64_win64,system_aarch64_win64] then
  2733. MaxMemPos:=$FFFFFFFF
  2734. else
  2735. if target_info.system in systems_wince then
  2736. MaxMemPos:=$1FFFFFF
  2737. else
  2738. MaxMemPos:=$7FFFFFFF;
  2739. end;
  2740. procedure TCoffexeoutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  2741. var
  2742. sym : coffsymbol;
  2743. begin
  2744. FillChar(sym,sizeof(sym),0);
  2745. if length(name)>8 then
  2746. begin
  2747. sym.strpos:=FCoffStrs.size+4;
  2748. FCoffStrs.writestr(name);
  2749. FCoffStrs.writestr(#0);
  2750. end
  2751. else
  2752. move(name[1],sym.name,length(name));
  2753. sym.value:=value;
  2754. sym.section:=section;
  2755. sym.typ:=typ;
  2756. sym.aux:=aux;
  2757. MaybeSwap(sym);
  2758. FWriter.write(sym,sizeof(sym));
  2759. end;
  2760. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  2761. var
  2762. secval,
  2763. value : aint;
  2764. globalval : byte;
  2765. exesec : TExeSection;
  2766. begin
  2767. if not assigned(texesymbol(p).objsymbol) then
  2768. internalerror(2006030505);
  2769. with texesymbol(p).objsymbol do
  2770. begin
  2771. if assigned(objsection) then
  2772. exesec:=TExeSection(objsection.exesection)
  2773. else
  2774. exesec:=nil;
  2775. { There is no exesection defined for special internal symbols
  2776. like __image_base__ }
  2777. if assigned(exesec) then
  2778. begin
  2779. secval:=exesec.secsymidx;
  2780. if win32 then
  2781. value:=address-exesec.mempos
  2782. else
  2783. value:=address;
  2784. end
  2785. else
  2786. begin
  2787. secval:=-1;
  2788. value:=address;
  2789. end;
  2790. if bind=AB_LOCAL then
  2791. globalval:=COFF_SYM_LOCAL
  2792. else
  2793. globalval:=COFF_SYM_GLOBAL;
  2794. { reloctype address to the section in the executable }
  2795. write_symbol(name,value,secval,globalval,0);
  2796. end;
  2797. end;
  2798. procedure TCoffexeoutput.ExeSectionList_write_header(p:TObject;arg:pointer);
  2799. var
  2800. sechdr : tcoffsechdr;
  2801. s : string;
  2802. strpos : aword;
  2803. begin
  2804. with tExeSection(p) do
  2805. begin
  2806. fillchar(sechdr,sizeof(sechdr),0);
  2807. s:=name;
  2808. if length(s)>8 then
  2809. begin
  2810. strpos:=FCoffStrs.size+4;
  2811. FCoffStrs.writestr(s);
  2812. FCoffStrs.writestr(#0);
  2813. s:='/'+ToStr(strpos);
  2814. end;
  2815. move(s[1],sechdr.name,length(s));
  2816. if win32 then
  2817. begin
  2818. sechdr.rvaofs:=mempos;
  2819. sechdr.vsize:=Size;
  2820. { sechdr.dataSize is size of initialized data, rounded up to FileAlignment
  2821. (so it can be greater than VirtualSize). Must be zero for sections that
  2822. do not contain initialized data. }
  2823. if (oso_data in SecOptions) then
  2824. sechdr.datasize:=Align(Size,SectionDataAlign);
  2825. end
  2826. else
  2827. begin
  2828. if not (oso_debug in SecOptions) then
  2829. begin
  2830. sechdr.rvaofs:=mempos;
  2831. sechdr.vsize:=mempos;
  2832. end;
  2833. sechdr.datasize:=Size;
  2834. end;
  2835. if (Size>0) then
  2836. sechdr.datapos:=datapos-datapos_offset;
  2837. sechdr.nrelocs:=0;
  2838. sechdr.relocpos:=0;
  2839. if win32 then
  2840. begin
  2841. if (target_info.system in systems_nativent) and
  2842. (apptype = app_native) then
  2843. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign) or PE_SCN_MEM_NOT_PAGED
  2844. else
  2845. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign);
  2846. { some flags are invalid in executables, reset them }
  2847. sechdr.flags:=sechdr.flags and
  2848. not(PE_SCN_LNK_INFO or PE_SCN_LNK_REMOVE or
  2849. PE_SCN_LNK_COMDAT or PE_SCN_ALIGN_MASK);
  2850. end
  2851. else
  2852. sechdr.flags:=djencodesechdrflags(SecOptions);
  2853. MaybeSwap(sechdr);
  2854. FWriter.write(sechdr,sizeof(sechdr));
  2855. end;
  2856. end;
  2857. procedure TCoffexeoutput.ExeSectionList_pass2_header(p:TObject;arg:pointer);
  2858. begin
  2859. with TExeSection(p) do
  2860. begin
  2861. { The debuginfo sections should already be stripped }
  2862. { if (ExeWriteMode=ewm_exeonly) and
  2863. (oso_debug in SecOptions) then
  2864. internalerror(200801161); }
  2865. inc(plongint(arg)^);
  2866. secsymidx:=plongint(arg)^;
  2867. end;
  2868. end;
  2869. function tcoffexeoutput.totalheadersize:longword;
  2870. var
  2871. stubsize,
  2872. optheadersize : longword;
  2873. begin
  2874. if win32 then
  2875. begin
  2876. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  2877. optheadersize:=sizeof(tcoffpeoptheader);
  2878. end
  2879. else
  2880. begin
  2881. stubsize:=sizeof(go32v2stub);
  2882. optheadersize:=sizeof(coffdjoptheader);
  2883. end;
  2884. result:=stubsize+sizeof(tcoffheader)+optheadersize;
  2885. end;
  2886. procedure tcoffexeoutput.MemPos_Header;
  2887. begin
  2888. { calculate start positions after the headers }
  2889. currmempos:=totalheadersize+sizeof(tcoffsechdr)*longword(ExeSectionList.Count-2);
  2890. end;
  2891. procedure tcoffexeoutput.DataPos_Header;
  2892. begin
  2893. { retrieve amount of sections }
  2894. nsects:=0;
  2895. ExeSectionList.ForEachCall(@ExeSectionList_pass2_header,@nsects);
  2896. { calculate start positions after the headers }
  2897. currdatapos:=totalheadersize+longword(nsects)*sizeof(tcoffsechdr);
  2898. end;
  2899. procedure tcoffexeoutput.DataPos_Symbols;
  2900. begin
  2901. inherited DataPos_Symbols;
  2902. { Calculating symbols position and size }
  2903. nsyms:=ExeSymbolList.Count;
  2904. sympos:=Align(CurrDataPos,SectionDataAlign);
  2905. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  2906. end;
  2907. function TCoffexeoutput.writedata:boolean;
  2908. var
  2909. i : longword;
  2910. header : tcoffheader;
  2911. djoptheader : coffdjoptheader;
  2912. peoptheader : tcoffpeoptheader;
  2913. textExeSec,
  2914. dataExeSec,
  2915. bssExeSec,
  2916. idataExeSec : TExeSection;
  2917. hassymbols,
  2918. writeDbgStrings : boolean;
  2919. procedure UpdateDataDir(const secname:string;idx:longint);
  2920. var
  2921. exesec : TExeSection;
  2922. begin
  2923. exesec:=FindExeSection(secname);
  2924. if assigned(exesec) then
  2925. begin
  2926. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  2927. peoptheader.DataDirectory[idx].size:=exesec.Size;
  2928. end;
  2929. end;
  2930. procedure UpdateImports;
  2931. var
  2932. exesec: TExeSection;
  2933. objsec, iat_start, iat_end, ilt_start: TObjSection;
  2934. i: longint;
  2935. begin
  2936. exesec:=FindExeSection('.idata');
  2937. if exesec=nil then
  2938. exit;
  2939. iat_start:=nil;
  2940. iat_end:=nil;
  2941. ilt_start:=nil;
  2942. for i:=0 to exesec.ObjSectionList.Count-1 do
  2943. begin
  2944. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2945. if (ilt_start=nil) and (Pos('.idata$4',objsec.Name)=1) then
  2946. ilt_start:=objsec;
  2947. if Pos('.idata$5',objsec.Name)=1 then
  2948. begin
  2949. if iat_start=nil then
  2950. iat_start:=objsec;
  2951. end
  2952. else
  2953. if Assigned(iat_start) then
  2954. begin
  2955. iat_end:=objsec;
  2956. Break;
  2957. end;
  2958. end;
  2959. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=exesec.mempos;
  2960. if Assigned(ilt_start) then
  2961. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=ilt_start.mempos-exesec.mempos
  2962. else { should not happen }
  2963. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=exesec.Size;
  2964. if Assigned(iat_start) and Assigned(iat_end) then
  2965. begin
  2966. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].vaddr:=iat_start.mempos;
  2967. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].size:=iat_end.mempos-iat_start.mempos;
  2968. end;
  2969. end;
  2970. procedure UpdateTlsDataDir;
  2971. var
  2972. {callbacksection : TExeSection;}
  2973. tlsexesymbol: TExeSymbol;
  2974. tlssymbol: TObjSymbol;
  2975. callbackexesymbol: TExeSymbol;
  2976. //callbacksymbol: TObjSymbol;
  2977. begin
  2978. { according to GNU ld,
  2979. the callback routines should be placed into .CRT$XL*
  2980. sections, and the thread local variables in .tls
  2981. __tls_start__ and __tls_end__ symbols
  2982. should be used for the initialized part,
  2983. which we do not support yet. }
  2984. { For now, we only pass the address of the __tls_used
  2985. asm symbol into PE_DATADIR_TLS with the correct
  2986. size of this table (different for win32/win64 }
  2987. tlsexesymbol:=texesymbol(ExeSymbolList.Find(
  2988. target_info.Cprefix+'_tls_used'));
  2989. if assigned(tlsexesymbol) then
  2990. begin
  2991. tlssymbol:=tlsexesymbol.ObjSymbol;
  2992. peoptheader.DataDirectory[PE_DATADIR_TLS].vaddr:=tlssymbol.address;
  2993. { sizeof(TlsDirectory) is different on host and target when cross-compiling }
  2994. peoptheader.DataDirectory[PE_DATADIR_TLS].size:=TLSDIR_SIZE;
  2995. if IsSharedLibrary then
  2996. begin
  2997. { Here we should reset __FPC_tls_callbacks value to nil }
  2998. callbackexesymbol:=texesymbol(ExeSymbolList.Find(
  2999. '__FPC_tls_callbacks'));
  3000. if assigned (callbackexesymbol) then
  3001. begin
  3002. //callbacksymbol:=callbackexesymbol.ObjSymbol;
  3003. end;
  3004. end;
  3005. end;
  3006. end;
  3007. begin
  3008. result:=false;
  3009. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  3010. textExeSec:=FindExeSection('.text');
  3011. dataExeSec:=FindExeSection('.data');
  3012. bssExeSec:=FindExeSection('.bss');
  3013. if not assigned(TextExeSec) or
  3014. not assigned(DataExeSec) then
  3015. internalerror(2006022302);
  3016. { do we need to write symbols? }
  3017. hassymbols:=(ExeWriteMode=ewm_dbgonly) or
  3018. (
  3019. (ExeWriteMode=ewm_exefull) and
  3020. not(cs_link_strip in current_settings.globalswitches)
  3021. );
  3022. writeDbgStrings:=hassymbols or ((ExeWriteMode=ewm_exeonly) and (cs_link_separate_dbg_file in current_settings.globalswitches));
  3023. { Stub }
  3024. if win32 then
  3025. begin
  3026. FWriter.write(win32stub,sizeof(win32stub));
  3027. FWriter.write(pemagic,sizeof(pemagic));
  3028. end
  3029. else
  3030. FWriter.write(go32v2stub,sizeof(go32v2stub));
  3031. { Initial header, will be updated later }
  3032. fillchar(header,sizeof(header),0);
  3033. header.mach:=COFF_MAGIC;
  3034. header.nsects:=nsects;
  3035. if writeDbgStrings then
  3036. header.sympos:=sympos-datapos_offset;
  3037. if hassymbols then
  3038. header.syms:=nsyms;
  3039. if win32 then
  3040. header.opthdr:=sizeof(tcoffpeoptheader)
  3041. else
  3042. header.opthdr:=sizeof(coffdjoptheader);
  3043. if win32 then
  3044. begin
  3045. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  3046. if target_info.system in [system_x86_64_win64,system_aarch64_win64] then
  3047. header.flag:=header.flag or PE_FILE_LARGE_ADDRESS_AWARE
  3048. else
  3049. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  3050. if IsSharedLibrary then
  3051. header.flag:=header.flag or PE_FILE_DLL;
  3052. if FindExeSection('.reloc')=nil then
  3053. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  3054. if (FindExeSection('.stab')=nil) and
  3055. (FindExeSection('.debug_info')=nil) and
  3056. (FindExeSection('.gnu_debuglink')=nil) then
  3057. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  3058. if not hassymbols then
  3059. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  3060. if SetPEFlagsSetExplicity then
  3061. header.flag:=header.flag or peflags;
  3062. end
  3063. else
  3064. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  3065. MaybeSwap(header);
  3066. FWriter.write(header,sizeof(header));
  3067. { Optional COFF Header }
  3068. if win32 then
  3069. begin
  3070. fillchar(peoptheader,sizeof(peoptheader),0);
  3071. peoptheader.magic:=COFF_OPT_MAGIC;
  3072. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  3073. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  3074. peoptheader.tsize:=TextExeSec.Size;
  3075. peoptheader.dsize:=DataExeSec.Size;
  3076. if assigned(BSSExeSec) then
  3077. peoptheader.bsize:=BSSExeSec.Size;
  3078. peoptheader.text_start:=TextExeSec.mempos;
  3079. {$ifndef cpu64bitaddr}
  3080. peoptheader.data_start:=DataExeSec.mempos;
  3081. {$endif cpu64bitaddr}
  3082. peoptheader.entry:=EntrySym.Address;
  3083. peoptheader.ImageBase:=ImageBase;
  3084. peoptheader.SectionAlignment:=SectionMemAlign;
  3085. peoptheader.FileAlignment:=SectionDataAlign;
  3086. if SetPEOSVersionSetExplicitely then
  3087. begin
  3088. peoptheader.MajorOperatingSystemVersion:=peosversionmajor;
  3089. peoptheader.MinorOperatingSystemVersion:=peosversionminor;
  3090. end
  3091. else
  3092. begin
  3093. peoptheader.MajorOperatingSystemVersion:=4;
  3094. peoptheader.MinorOperatingSystemVersion:=0;
  3095. end;
  3096. if SetPEUserVersionSetExplicitely then
  3097. begin
  3098. peoptheader.MajorImageVersion:=peuserversionmajor;
  3099. peoptheader.MinorImageVersion:=peuserversionminor;
  3100. end
  3101. else
  3102. begin
  3103. peoptheader.MajorImageVersion:=dllmajor;
  3104. peoptheader.MinorImageVersion:=dllminor;
  3105. end;
  3106. if SetPESubSysVersionSetExplicitely then
  3107. begin
  3108. peoptheader.MajorSubsystemVersion:=pesubsysversionmajor;
  3109. peoptheader.MinorSubsystemVersion:=pesubsysversionminor;
  3110. end
  3111. else
  3112. begin
  3113. if target_info.system in systems_wince then
  3114. peoptheader.MajorSubsystemVersion:=3
  3115. else
  3116. peoptheader.MajorSubsystemVersion:=4;
  3117. peoptheader.MinorSubsystemVersion:=0;
  3118. end;
  3119. peoptheader.Win32Version:=0;
  3120. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  3121. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  3122. peoptheader.CheckSum:=0;
  3123. if (target_info.system in systems_nativent) and (not IsSharedLibrary or (apptype = app_native)) then
  3124. { Although I did not really test this, it seems that Subsystem is
  3125. not checked in DLLs except for maybe drivers}
  3126. peoptheader.Subsystem:=PE_SUBSYSTEM_NATIVE
  3127. else
  3128. if target_info.system in systems_wince then
  3129. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  3130. else
  3131. if apptype=app_gui then
  3132. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  3133. else
  3134. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  3135. if target_info.system in [system_aarch64_win64] then
  3136. peoptheader.DllCharacteristics:=PE_DLLCHARACTERISTICS_DYNAMIC_BASE or
  3137. PE_DLLCHARACTERISTICS_NX_COMPAT or
  3138. PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA
  3139. else
  3140. peoptheader.DllCharacteristics:=0;
  3141. if SetPEOptFlagsSetExplicity then
  3142. peoptheader.DllCharacteristics:=peoptheader.DllCharacteristics or peoptflags;
  3143. peoptheader.SizeOfStackReserve:=stacksize;
  3144. peoptheader.SizeOfStackCommit:=$1000;
  3145. if MinStackSizeSetExplicity then
  3146. peoptheader.SizeOfStackCommit:=minstacksize;
  3147. if MaxStackSizeSetExplicity then
  3148. peoptheader.SizeOfStackReserve:=maxstacksize;
  3149. peoptheader.SizeOfHeapReserve:=$100000;
  3150. peoptheader.SizeOfHeapCommit:=$1000;
  3151. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  3152. UpdateImports;
  3153. UpdateTlsDataDir;
  3154. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  3155. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  3156. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  3157. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  3158. MaybeSwap(peoptheader);
  3159. FWriter.write(peoptheader,sizeof(peoptheader));
  3160. end
  3161. else
  3162. begin
  3163. fillchar(djoptheader,sizeof(djoptheader),0);
  3164. djoptheader.magic:=COFF_OPT_MAGIC;
  3165. djoptheader.tsize:=TextExeSec.Size;
  3166. djoptheader.dsize:=DataExeSec.Size;
  3167. if assigned(BSSExeSec) then
  3168. djoptheader.bsize:=BSSExeSec.Size;
  3169. djoptheader.text_start:=TextExeSec.mempos;
  3170. djoptheader.data_start:=DataExeSec.mempos;
  3171. djoptheader.entry:=EntrySym.Address;
  3172. MaybeSwap(djoptheader);
  3173. FWriter.write(djoptheader,sizeof(djoptheader));
  3174. end;
  3175. { For some unknown reason WM 6.1 requires .idata section to be read only.
  3176. Otherwise it refuses to load DLLs greater than 64KB.
  3177. Earlier versions of WinCE load DLLs regardless of .idata flags. }
  3178. if target_info.system in systems_wince then
  3179. begin
  3180. idataExeSec:=FindExeSection('.idata');
  3181. if idataExeSec<>nil then
  3182. idataExeSec.SecOptions:=idataExeSec.SecOptions - [oso_write];
  3183. end;
  3184. { Section headers }
  3185. ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
  3186. { Section data }
  3187. WriteExeSectionContent;
  3188. { Align after the last section }
  3189. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  3190. { Optional Symbols }
  3191. if SymPos<>FWriter.Size then
  3192. internalerror(200602252);
  3193. if hassymbols then
  3194. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  3195. if writeDbgStrings then
  3196. begin
  3197. { Strings }
  3198. i:=FCoffStrs.size+4;
  3199. if source_info.endian<>target_info.endian then
  3200. i:=SwapEndian(i);
  3201. FWriter.write(i,4);
  3202. FWriter.writearray(FCoffStrs);
  3203. end;
  3204. { Release }
  3205. FCoffStrs.Free;
  3206. result:=true;
  3207. end;
  3208. function IdataObjSectionCompare(Item1, Item2: Pointer): Integer;
  3209. var
  3210. I1 : TObjSection absolute Item1;
  3211. I2 : TObjSection absolute Item2;
  3212. begin
  3213. Result:=CompareStr(I1.Name,I2.Name);
  3214. end;
  3215. procedure TCoffexeoutput.Order_ObjSectionList(ObjSectionList: TFPObjectList;const aPattern:string);
  3216. begin
  3217. { Sort sections having '$' in the name, that's how PECOFF documentation
  3218. tells to handle them. However, look for '$' in the pattern, not in section
  3219. names, because the latter often get superfluous '$' due to mangling. }
  3220. if Pos('$',aPattern)>0 then
  3221. ObjSectionList.Sort(@IdataObjSectionCompare);
  3222. end;
  3223. constructor TDJCoffexeoutput.create;
  3224. begin
  3225. inherited createcoff(false);
  3226. datapos_offset:=sizeof(go32v2stub);
  3227. CExeSection:=TExeSection;
  3228. CObjData:=TDJCoffObjData;
  3229. end;
  3230. procedure TDJCoffexeoutput.MemPos_Header;
  3231. begin
  3232. { Headers are not loaded, first 4K page is reserved }
  3233. CurrMemPos:=$1000;
  3234. end;
  3235. constructor TPECoffexeoutput.create;
  3236. begin
  3237. inherited createcoff(true);
  3238. CExeSection:=TExeSection;
  3239. CObjData:=TPECoffObjData;
  3240. end;
  3241. procedure TPECoffexeoutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  3242. var
  3243. exesec:TExeSection;
  3244. objsec,textsec:TObjSection;
  3245. objreloc:TObjRelocation;
  3246. i,j:longint;
  3247. begin
  3248. if not (target_info.system in [system_x86_64_win64,system_aarch64_win64]) then
  3249. exit;
  3250. exesec:=FindExeSection('.pdata');
  3251. if exesec=nil then
  3252. exit;
  3253. for i:=0 to exesec.ObjSectionList.Count-1 do
  3254. begin
  3255. objsec:=TObjSection(exesec.ObjSectionList[i]);
  3256. if objsec.Used then
  3257. continue;
  3258. j:=0;
  3259. while j<objsec.ObjRelocations.Count do
  3260. begin
  3261. objreloc:=TObjRelocation(objsec.ObjRelocations[j]);
  3262. if objreloc.symbol=nil then
  3263. InternalError(2013041201);
  3264. textsec:=objreloc.symbol.objsection;
  3265. if textsec.used then
  3266. begin
  3267. WorkList.Add(objsec);
  3268. objsec.used:=true;
  3269. { The exact algorithm for non-smartlinked .pdata sections
  3270. is subject for refinement. Extreme cases are:
  3271. - several disjoint .pdata entries for a function, if function
  3272. is complex or if compiler splits it into chunks,
  3273. - single .pdata section referencing several .text sections,
  3274. may need to remove irrelevant parts like BFD does for
  3275. .eh_frame sections. }
  3276. break;
  3277. end;
  3278. if target_info.system=system_aarch64_win64 then
  3279. inc(j,2)
  3280. else
  3281. inc(j,3);
  3282. end;
  3283. end;
  3284. end;
  3285. procedure TPECoffexeoutput.AfterUnusedSectionRemoval;
  3286. var
  3287. basedllname : string;
  3288. procedure StartImport(const dllname:string);
  3289. begin
  3290. if assigned(exemap) then
  3291. begin
  3292. exemap.Add('');
  3293. exemap.Add('Importing from DLL '+dllname);
  3294. end;
  3295. basedllname:=ExtractFileName(dllname);
  3296. { idata2 }
  3297. idata2objsection.writereloc_internal(idata4objsection,idata4objsection.size,sizeof(longint),RELOC_RVA);
  3298. idata2objsection.writezeros(2*sizeof(longint));
  3299. idata2objsection.writereloc_internal(idata7objsection,idata7objsection.size,sizeof(longint),RELOC_RVA);
  3300. idata2objsection.writereloc_internal(idata5objsection,idata5objsection.size,sizeof(longint),RELOC_RVA);
  3301. { idata7 }
  3302. idata7objsection.writestr(basedllname);
  3303. end;
  3304. procedure EndImport;
  3305. begin
  3306. { idata4 }
  3307. idata4objsection.writezeros(sizeof(longint));
  3308. if target_info.system in systems_peoptplus then
  3309. idata4objsection.writezeros(sizeof(longint));
  3310. { idata5 }
  3311. idata5objsection.writezeros(sizeof(longint));
  3312. if target_info.system in systems_peoptplus then
  3313. idata5objsection.writezeros(sizeof(longint));
  3314. end;
  3315. function AddImport(const afuncname,amangledname:string; AOrdNr:longint;isvar:boolean):TObjSymbol;
  3316. const
  3317. {$if defined(arm)}
  3318. jmpopcode : array[0..7] of byte = (
  3319. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  3320. $00,$f0,$9c,$e5 // ldr pc, [ip]
  3321. );
  3322. {$elseif defined(aarch64)}
  3323. jmpopcode : array[0..11] of byte = (
  3324. $70,$00,$00,$58, // ldr ip0, .+12
  3325. $10,$02,$40,$F9, // ldr ip0, [ip0]
  3326. $00,$02,$1F,$D6 // br ip0
  3327. );
  3328. {$else}
  3329. jmpopcode : array[0..1] of byte = (
  3330. $ff,$25
  3331. );
  3332. {$endif}
  3333. nopopcodes : array[0..1] of byte = (
  3334. $90,$90
  3335. );
  3336. var
  3337. ordint: dword;
  3338. word_ordint: word;
  3339. procedure WriteTableEntry(objsec:TObjSection);
  3340. begin
  3341. if AOrdNr <= 0 then
  3342. begin
  3343. objsec.writereloc_internal(idata6objsection,idata6objsection.size,sizeof(longint),RELOC_RVA);
  3344. if target_info.system in systems_peoptplus then
  3345. objsec.writezeros(sizeof(longint));
  3346. end
  3347. else
  3348. begin
  3349. { import by ordinal }
  3350. ordint:=AOrdNr;
  3351. if target_info.system in systems_peoptplus then
  3352. begin
  3353. if source_info.endian<>target_info.endian then
  3354. ordint:=SwapEndian(ordint);
  3355. objsec.write(ordint,4);
  3356. ordint:=$80000000;
  3357. if source_info.endian<>target_info.endian then
  3358. ordint:=SwapEndian(ordint);
  3359. objsec.write(ordint,4);
  3360. end
  3361. else
  3362. begin
  3363. ordint:=ordint or $80000000;
  3364. if source_info.endian<>target_info.endian then
  3365. ordint:=SwapEndian(ordint);
  3366. objsec.write(ordint,4);
  3367. end;
  3368. end;
  3369. end;
  3370. begin
  3371. result:=nil;
  3372. if assigned(exemap) then
  3373. begin
  3374. if AOrdNr <= 0 then
  3375. exemap.Add(' Importing Function '+afuncname)
  3376. else
  3377. exemap.Add(' Importing Function '+afuncname+' (OrdNr='+tostr(AOrdNr)+')');
  3378. end;
  3379. { idata4, import lookup table }
  3380. WriteTableEntry(idata4objsection);
  3381. { idata5, import address table }
  3382. internalobjdata.SetSection(idata5objsection);
  3383. if isvar then
  3384. result:=internalobjdata.SymbolDefine(amangledname,AB_GLOBAL,AT_DATA)
  3385. else
  3386. begin
  3387. internalobjdata.SetSection(textobjsection);
  3388. textobjsection.writezeros(align_aword(textobjsection.size,16)-textobjsection.size);
  3389. result:=internalobjdata.SymbolDefine('_'+amangledname,AB_GLOBAL,AT_FUNCTION);
  3390. textobjsection.write(jmpopcode,sizeof(jmpopcode));
  3391. {$if defined(x86_64)}
  3392. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_RELATIVE);
  3393. {$elseif defined(aarch64)}
  3394. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,8,RELOC_ABSOLUTE);
  3395. {$else}
  3396. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_ABSOLUTE32);
  3397. {$endif x86_64 or aarch64}
  3398. textobjsection.write(nopopcodes,align(textobjsection.size,qword(sizeof(nopopcodes)))-textobjsection.size);
  3399. end;
  3400. { idata5 section data }
  3401. WriteTableEntry(idata5objsection);
  3402. if (AOrdNr<=0) then
  3403. begin
  3404. { index hint, function name, null terminator and align }
  3405. word_ordint:=abs(AOrdNr);
  3406. if source_info.endian<>target_info.endian then
  3407. word_ordint:=SwapEndian(word_ordint);
  3408. idata6objsection.write(word_ordint,2);
  3409. idata6objsection.writestr(afuncname);
  3410. idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
  3411. end;
  3412. end;
  3413. var
  3414. i,j : longint;
  3415. ImportLibrary : TImportLibrary;
  3416. ImportSymbol : TImportSymbol;
  3417. exesym : TExeSymbol;
  3418. newdll : boolean;
  3419. begin
  3420. for i:=0 to FImports.Count-1 do
  3421. begin
  3422. ImportLibrary:=TImportLibrary(FImports[i]);
  3423. newdll:=False;
  3424. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  3425. begin
  3426. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  3427. exesym:=ImportSymbol.CachedExeSymbol;
  3428. if assigned(exesym) and
  3429. exesym.Used then
  3430. begin
  3431. if (not newdll) then
  3432. StartImport(ImportLibrary.Name);
  3433. newdll:=True;
  3434. exesym.objsymbol:=AddImport(ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  3435. end;
  3436. end;
  3437. if newdll then
  3438. EndImport;
  3439. end;
  3440. FixupSymbols;
  3441. end;
  3442. procedure TPECoffexeoutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  3443. var
  3444. i,j: longint;
  3445. ImportLibrary: TImportLibrary;
  3446. ImportSymbol: TImportSymbol;
  3447. exesym: TExeSymbol;
  3448. begin
  3449. { Here map import symbols to exe symbols and create necessary sections.
  3450. Actual import generation is done after unused sections (and symbols) are removed. }
  3451. FImports:=ImportLibraryList;
  3452. textobjsection:=internalObjData.CreateSection(sec_code);
  3453. textobjsection.SecOptions:=[oso_keep];
  3454. idata2objsection:=internalObjData.CreateSection(sec_idata2);
  3455. idata2objsection.SecOptions:=[oso_keep];
  3456. idata4objsection:=internalObjData.CreateSection(sec_idata4);
  3457. idata4objsection.SecOptions:=[oso_keep];
  3458. idata5objsection:=internalObjData.CreateSection(sec_idata5);
  3459. idata5objsection.SecOptions:=[oso_keep];
  3460. idata6objsection:=internalObjData.CreateSection(sec_idata6);
  3461. idata6objsection.SecOptions:=[oso_keep];
  3462. idata7objsection:=internalObjData.CreateSection(sec_idata7);
  3463. idata7objsection.SecOptions:=[oso_keep];
  3464. for i:=0 to ImportLibraryList.Count-1 do
  3465. begin
  3466. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  3467. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  3468. begin
  3469. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  3470. exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
  3471. if assigned(exesym) and
  3472. (exesym.State<>symstate_defined) then
  3473. begin
  3474. ImportSymbol.CachedExeSymbol:=exesym;
  3475. exesym.State:=symstate_defined;
  3476. end;
  3477. end;
  3478. end;
  3479. PackUnresolvedExeSymbols('after DLL imports');
  3480. end;
  3481. procedure TPECoffexeoutput.GenerateRelocs;
  3482. var
  3483. pgaddr, hdrpos : longword;
  3484. procedure FinishBlock;
  3485. var
  3486. p,len : longint;
  3487. begin
  3488. if hdrpos = longword(-1) then
  3489. exit;
  3490. p:=0;
  3491. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  3492. p:=internalObjData.CurrObjSec.Data.Pos;
  3493. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  3494. len:=p-hdrpos;
  3495. if source_info.endian<>target_info.endian then
  3496. len:=SwapEndian(len);
  3497. internalObjData.CurrObjSec.Data.write(len,4);
  3498. internalObjData.CurrObjSec.Data.seek(p);
  3499. hdrpos:=longword(-1);
  3500. end;
  3501. var
  3502. exesec : TExeSection;
  3503. objsec : TObjSection;
  3504. objreloc : TObjRelocation;
  3505. i,j,k : longint;
  3506. offset : longword;
  3507. w: word;
  3508. begin
  3509. if not RelocSection or FRelocsGenerated then
  3510. exit;
  3511. exesec:=FindExeSection('.reloc');
  3512. if exesec=nil then
  3513. exit;
  3514. objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_keep]);
  3515. exesec.AddObjSection(objsec);
  3516. pgaddr:=longword(-1);
  3517. hdrpos:=longword(-1);
  3518. for i:=0 to ExeSectionList.Count-1 do
  3519. begin
  3520. exesec:=TExeSection(ExeSectionList[i]);
  3521. for j:=0 to exesec.ObjSectionList.count-1 do
  3522. begin
  3523. objsec:=TObjSection(exesec.ObjSectionList[j]);
  3524. { create relocs only for sections which are loaded in memory }
  3525. if not (oso_load in objsec.SecOptions) then
  3526. continue;
  3527. for k:=0 to objsec.ObjRelocations.Count-1 do
  3528. begin
  3529. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  3530. if not (objreloc.typ in [{$ifdef cpu64bitaddr}RELOC_ABSOLUTE32,{$endif cpu64bitaddr}RELOC_ABSOLUTE]) then
  3531. continue;
  3532. offset:=objsec.MemPos+objreloc.dataoffset;
  3533. if (offset<pgaddr) and (pgaddr<>longword(-1)) then
  3534. Internalerror(2007062701);
  3535. if (offset-pgaddr>=4096) or (pgaddr=longword(-1)) then
  3536. begin
  3537. FinishBlock;
  3538. pgaddr:=(offset div 4096)*4096;
  3539. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  3540. if source_info.endian<>target_info.endian then
  3541. pgaddr:=SwapEndian(pgaddr);
  3542. internalObjData.writebytes(pgaddr,4);
  3543. { Reserving space for block size. The size will be written later in FinishBlock }
  3544. internalObjData.writebytes(pgaddr,4);
  3545. end;
  3546. {$ifdef cpu64bitaddr}
  3547. if objreloc.typ = RELOC_ABSOLUTE then
  3548. w:=IMAGE_REL_BASED_DIR64
  3549. else
  3550. {$endif cpu64bitaddr}
  3551. w:=IMAGE_REL_BASED_HIGHLOW;
  3552. w:=(w shl 12) or (offset-pgaddr);
  3553. if source_info.endian<>target_info.endian then
  3554. w:=SwapEndian(w);
  3555. internalObjData.writebytes(w,2);
  3556. end;
  3557. end;
  3558. end;
  3559. FinishBlock;
  3560. FRelocsGenerated:=true;
  3561. end;
  3562. procedure TPECoffexeoutput.MemPos_Start;
  3563. var
  3564. exesec : TExeSection;
  3565. begin
  3566. if RelocSection then
  3567. begin
  3568. exesec:=FindExeSection('.reloc');
  3569. if exesec=nil then
  3570. InternalError(2012072403);
  3571. exesec.Disabled:=false;
  3572. end;
  3573. inherited;
  3574. end;
  3575. procedure TPECoffexeoutput.MemPos_ExeSection(const aname:string);
  3576. begin
  3577. if aname='.reloc' then
  3578. GenerateRelocs;
  3579. inherited;
  3580. end;
  3581. {****************************************************************************
  3582. TDJCoffAssembler
  3583. ****************************************************************************}
  3584. constructor TDJCoffAssembler.Create(info: pasminfo; smart:boolean);
  3585. begin
  3586. inherited;
  3587. CObjOutput:=TDJCoffObjOutput;
  3588. CInternalAr:=tarobjectwriter;
  3589. end;
  3590. {****************************************************************************
  3591. TPECoffAssembler
  3592. ****************************************************************************}
  3593. constructor TPECoffAssembler.Create(info: pasminfo; smart:boolean);
  3594. begin
  3595. inherited;
  3596. CObjOutput:=TPECoffObjOutput;
  3597. CInternalAr:=tarobjectwriter;
  3598. end;
  3599. {*****************************************************************************
  3600. DLLReader
  3601. *****************************************************************************}
  3602. {$ifdef win32}
  3603. var
  3604. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  3605. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  3606. {$endif win32}
  3607. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  3608. var
  3609. DLLReader : TObjectReader;
  3610. DosHeader : array[0..$7f] of byte;
  3611. PEMagic : array[0..3] of byte;
  3612. Header : TCoffHeader;
  3613. peheader : tcoffpeoptheader;
  3614. NameOfs,
  3615. newheaderofs : longword;
  3616. FuncName : string;
  3617. expdir : TPECoffExpDir;
  3618. i : longint;
  3619. found : boolean;
  3620. header_ok : boolean;
  3621. sechdr : tCoffSecHdr;
  3622. {$ifdef win32}
  3623. p : pointer;
  3624. {$endif win32}
  3625. begin
  3626. result:=false;
  3627. fillchar(sechdr,sizeof(sechdr),0);
  3628. {$ifdef win32}
  3629. if (target_info.system=system_x86_64_win64) and
  3630. assigned(Wow64DisableWow64FsRedirection) then
  3631. Wow64DisableWow64FsRedirection(p);
  3632. {$endif win32}
  3633. DLLReader:=TObjectReader.Create;
  3634. DLLReader.OpenFile(dllname);
  3635. {$ifdef win32}
  3636. if (target_info.system=system_x86_64_win64) and
  3637. assigned(Wow64RevertWow64FsRedirection) then
  3638. Wow64RevertWow64FsRedirection(p);
  3639. {$endif win32}
  3640. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  3641. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  3642. begin
  3643. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  3644. exit;
  3645. end;
  3646. newheaderofs:=cardinal(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  3647. DLLReader.Seek(newheaderofs);
  3648. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  3649. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  3650. begin
  3651. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  3652. exit;
  3653. end;
  3654. header_ok:=DLLReader.Read(Header,sizeof(TCoffHeader));
  3655. MaybeSwap(Header);
  3656. if not header_ok or
  3657. (Header.mach<>COFF_MAGIC) or
  3658. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  3659. begin
  3660. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  3661. exit;
  3662. end;
  3663. { Read optheader }
  3664. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  3665. MaybeSwap(peheader);
  3666. { Section headers }
  3667. found:=false;
  3668. for i:=1 to header.nsects do
  3669. begin
  3670. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  3671. begin
  3672. Comment(V_Error,'Error reading coff file '+DLLName);
  3673. exit;
  3674. end;
  3675. MaybeSwap(sechdr);
  3676. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  3677. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  3678. begin
  3679. found:=true;
  3680. break;
  3681. end;
  3682. end;
  3683. if not found then
  3684. begin
  3685. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  3686. exit;
  3687. end;
  3688. { Process edata }
  3689. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  3690. DLLReader.Read(expdir,sizeof(expdir));
  3691. MaybeSwap(expdir);
  3692. for i:=0 to expdir.NumNames-1 do
  3693. begin
  3694. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  3695. DLLReader.Read(NameOfs,4);
  3696. Dec(NameOfs,sechdr.rvaofs);
  3697. if {(NameOfs<0) or}
  3698. (NameOfs>sechdr.vsize) then
  3699. begin
  3700. Comment(V_Error,'DLL does contains invalid exports');
  3701. break;
  3702. end;
  3703. { Read Function name from DLL, prepend _ and terminate with #0 }
  3704. DLLReader.Seek(sechdr.datapos+NameOfs);
  3705. DLLReader.Read((@FuncName[1])^,sizeof(FuncName)-3);
  3706. FuncName[sizeof(FuncName)-1]:=#0;
  3707. FuncName[0]:=chr(Strlen(@FuncName[1]));
  3708. readdllproc(DLLName,FuncName);
  3709. end;
  3710. DLLReader.Free;
  3711. end;
  3712. {$ifdef arm}
  3713. function COFF_MAGIC: word;
  3714. begin
  3715. if GenerateThumb2Code and (current_settings.cputype>=cpu_armv7) then
  3716. COFF_MAGIC:=$1c4 // IMAGE_FILE_MACHINE_ARMNT
  3717. else
  3718. COFF_MAGIC:=$1c0; // IMAGE_FILE_MACHINE_ARM
  3719. end;
  3720. {$endif arm}
  3721. {*****************************************************************************
  3722. Initialize
  3723. *****************************************************************************}
  3724. {$ifdef i386}
  3725. const
  3726. as_i386_coff_info : tasminfo =
  3727. (
  3728. id : as_i386_coff;
  3729. idtxt : 'COFF';
  3730. asmbin : '';
  3731. asmcmd : '';
  3732. supported_targets : [system_i386_go32v2];
  3733. flags : [af_outputbinary,af_smartlink_sections];
  3734. labelprefix : '.L';
  3735. labelmaxlen : -1;
  3736. comment : '';
  3737. dollarsign: '$';
  3738. );
  3739. as_i386_pecoff_info : tasminfo =
  3740. (
  3741. id : as_i386_pecoff;
  3742. idtxt : 'PECOFF';
  3743. asmbin : '';
  3744. asmcmd : '';
  3745. supported_targets : [system_i386_win32,system_i386_nativent];
  3746. flags : [af_outputbinary,af_smartlink_sections];
  3747. labelprefix : '.L';
  3748. labelmaxlen : -1;
  3749. comment : '';
  3750. dollarsign: '$';
  3751. );
  3752. as_i386_pecoffwdosx_info : tasminfo =
  3753. (
  3754. id : as_i386_pecoffwdosx;
  3755. idtxt : 'PEWDOSX';
  3756. asmbin : '';
  3757. asmcmd : '';
  3758. supported_targets : [system_i386_wdosx];
  3759. flags : [af_outputbinary];
  3760. labelprefix : '.L';
  3761. labelmaxlen : -1;
  3762. comment : '';
  3763. dollarsign: '$';
  3764. );
  3765. as_i386_pecoffwince_info : tasminfo =
  3766. (
  3767. id : as_i386_pecoffwince;
  3768. idtxt : 'PECOFFWINCE';
  3769. asmbin : '';
  3770. asmcmd : '';
  3771. supported_targets : [system_i386_wince];
  3772. flags : [af_outputbinary,af_smartlink_sections];
  3773. labelprefix : '.L';
  3774. labelmaxlen : -1;
  3775. comment : '';
  3776. dollarsign: '$';
  3777. );
  3778. {$endif i386}
  3779. {$ifdef x86_64}
  3780. const
  3781. as_x86_64_pecoff_info : tasminfo =
  3782. (
  3783. id : as_x86_64_pecoff;
  3784. idtxt : 'PECOFF';
  3785. asmbin : '';
  3786. asmcmd : '';
  3787. supported_targets : [system_x86_64_win64];
  3788. flags : [af_outputbinary,af_smartlink_sections];
  3789. labelprefix : '.L';
  3790. labelmaxlen : -1;
  3791. comment : '';
  3792. dollarsign: '$';
  3793. );
  3794. {$endif x86_64}
  3795. {$ifdef arm}
  3796. const
  3797. as_arm_pecoffwince_info : tasminfo =
  3798. (
  3799. id : as_arm_pecoffwince;
  3800. idtxt : 'PECOFFWINCE';
  3801. asmbin : '';
  3802. asmcmd : '';
  3803. supported_targets : [system_arm_wince];
  3804. flags : [af_outputbinary,af_smartlink_sections];
  3805. labelprefix : '.L';
  3806. labelmaxlen : -1;
  3807. comment : '';
  3808. dollarsign: '$';
  3809. );
  3810. {$endif arm}
  3811. {$ifdef win32}
  3812. procedure SetupProcVars;
  3813. var
  3814. hinstLib : THandle;
  3815. begin
  3816. Wow64DisableWow64FsRedirection:=nil;
  3817. Wow64RevertWow64FsRedirection:=nil;
  3818. hinstLib:=LoadLibrary('kernel32.dll');
  3819. if hinstLib<>0 then
  3820. begin
  3821. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  3822. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  3823. FreeLibrary(hinstLib);
  3824. end;
  3825. end;
  3826. {$endif win32}
  3827. initialization
  3828. {$ifdef i386}
  3829. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  3830. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  3831. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  3832. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  3833. {$endif i386}
  3834. {$ifdef x86_64}
  3835. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  3836. {$endif x86_64}
  3837. {$ifdef arm}
  3838. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  3839. {$endif arm}
  3840. {$ifdef win32}
  3841. SetupProcVars;
  3842. {$endif win32}
  3843. end.