ogcoff.pas 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101
  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. '.note'
  934. );
  935. const go32v2stub : array[0..2047] of byte=(
  936. $4D,$5A,$00,$00,$04,$00,$00,$00,$20,$00,$27,$00,$FF,$FF,$00,
  937. $00,$60,$07,$00,$00,$54,$00,$00,$00,$00,$00,$00,$00,$0D,$0A,
  938. $73,$74,$75,$62,$2E,$68,$20,$67,$65,$6E,$65,$72,$61,$74,$65,
  939. $64,$20,$66,$72,$6F,$6D,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,
  940. $20,$62,$79,$20,$64,$6A,$61,$73,$6D,$2C,$20,$6F,$6E,$20,$54,
  941. $68,$75,$20,$44,$65,$63,$20,$20,$39,$20,$31,$30,$3A,$35,$39,
  942. $3A,$33,$31,$20,$31,$39,$39,$39,$0D,$0A,$54,$68,$65,$20,$53,
  943. $54,$55,$42,$2E,$45,$58,$45,$20,$73,$74,$75,$62,$20,$6C,$6F,
  944. $61,$64,$65,$72,$20,$69,$73,$20,$43,$6F,$70,$79,$72,$69,$67,
  945. $68,$74,$20,$28,$43,$29,$20,$31,$39,$39,$33,$2D,$31,$39,$39,
  946. $35,$20,$44,$4A,$20,$44,$65,$6C,$6F,$72,$69,$65,$2E,$20,$0D,
  947. $0A,$50,$65,$72,$6D,$69,$73,$73,$69,$6F,$6E,$20,$67,$72,$61,
  948. $6E,$74,$65,$64,$20,$74,$6F,$20,$75,$73,$65,$20,$66,$6F,$72,
  949. $20,$61,$6E,$79,$20,$70,$75,$72,$70,$6F,$73,$65,$20,$70,$72,
  950. $6F,$76,$69,$64,$65,$64,$20,$74,$68,$69,$73,$20,$63,$6F,$70,
  951. $79,$72,$69,$67,$68,$74,$20,$0D,$0A,$72,$65,$6D,$61,$69,$6E,
  952. $73,$20,$70,$72,$65,$73,$65,$6E,$74,$20,$61,$6E,$64,$20,$75,
  953. $6E,$6D,$6F,$64,$69,$66,$69,$65,$64,$2E,$20,$0D,$0A,$54,$68,
  954. $69,$73,$20,$6F,$6E,$6C,$79,$20,$61,$70,$70,$6C,$69,$65,$73,
  955. $20,$74,$6F,$20,$74,$68,$65,$20,$73,$74,$75,$62,$2C,$20,$61,
  956. $6E,$64,$20,$6E,$6F,$74,$20,$6E,$65,$63,$65,$73,$73,$61,$72,
  957. $69,$6C,$79,$20,$74,$68,$65,$20,$77,$68,$6F,$6C,$65,$20,$70,
  958. $72,$6F,$67,$72,$61,$6D,$2E,$0A,$0D,$0A,$24,$49,$64,$3A,$20,
  959. $73,$74,$75,$62,$2E,$61,$73,$6D,$20,$62,$75,$69,$6C,$74,$20,
  960. $31,$32,$2F,$30,$39,$2F,$39,$39,$20,$31,$30,$3A,$35,$39,$3A,
  961. $33,$31,$20,$62,$79,$20,$64,$6A,$61,$73,$6D,$20,$24,$0A,$0D,
  962. $0A,$40,$28,$23,$29,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,$20,
  963. $62,$75,$69,$6C,$74,$20,$31,$32,$2F,$30,$39,$2F,$39,$39,$20,
  964. $31,$30,$3A,$35,$39,$3A,$33,$31,$20,$62,$79,$20,$64,$6A,$61,
  965. $73,$6D,$0A,$0D,$0A,$1A,$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,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  970. $00,$00,$67,$6F,$33,$32,$73,$74,$75,$62,$2C,$20,$76,$20,$32,
  971. $2E,$30,$32,$54,$00,$00,$00,$00,$00,$08,$00,$00,$00,$00,$00,
  972. $00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  973. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  974. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$43,$57,$53,$44,$50,
  975. $4D,$49,$2E,$45,$58,$45,$00,$00,$00,$00,$00,$0E,$1F,$8C,$1E,
  976. $24,$00,$8C,$06,$60,$07,$FC,$B4,$30,$CD,$21,$3C,$03,$73,$08,
  977. $B0,$6D,$BA,$A7,$05,$E9,$D4,$03,$A2,$69,$08,$BE,$20,$00,$8B,
  978. $04,$09,$C0,$75,$02,$B4,$FE,$BB,$70,$08,$39,$C3,$73,$02,$89,
  979. $C3,$89,$1C,$FE,$C7,$B9,$04,$FF,$D3,$EB,$B4,$4A,$CD,$21,$73,
  980. $08,$D3,$E3,$FE,$CF,$89,$1C,$EB,$D8,$26,$8E,$06,$2C,$00,$31,
  981. $FF,$30,$C0,$A9,$F2,$AE,$26,$81,$3D,$50,$41,$75,$15,$AF,$26,
  982. $81,$3D,$54,$48,$75,$0D,$AF,$26,$80,$3D,$3D,$75,$06,$47,$89,
  983. $3E,$8C,$04,$4F,$AE,$75,$DF,$AF,$B4,$3E,$BB,$13,$00,$CD,$21,
  984. $B4,$3E,$BB,$12,$00,$CD,$21,$06,$57,$31,$C9,$74,$12,$B0,$6E,
  985. $BA,$7E,$05,$E9,$5E,$03,$09,$C9,$75,$F4,$41,$E8,$A1,$03,$72,
  986. $EE,$B8,$87,$16,$CD,$2F,$09,$C0,$75,$ED,$80,$E3,$01,$74,$E8,
  987. $89,$3E,$00,$06,$8C,$06,$02,$06,$89,$36,$04,$06,$5F,$07,$E8,
  988. $D3,$02,$89,$3E,$2A,$00,$89,$36,$62,$07,$80,$3E,$2C,$00,$00,
  989. $74,$23,$B9,$08,$00,$BF,$2C,$00,$8A,$05,$47,$08,$C0,$74,$05,
  990. $88,$07,$43,$E2,$F4,$66,$C7,$07,$2E,$45,$58,$45,$83,$C3,$04,
  991. $C6,$07,$00,$89,$1E,$62,$07,$B8,$00,$3D,$BA,$64,$07,$CD,$21,
  992. $0F,$82,$B3,$02,$A3,$06,$06,$89,$C3,$B9,$06,$00,$BA,$B5,$07,
  993. $B4,$3F,$CD,$21,$31,$D2,$31,$C9,$A1,$B5,$07,$3D,$4C,$01,$74,
  994. $1B,$3D,$4D,$5A,$0F,$85,$98,$02,$8B,$16,$B9,$07,$C1,$E2,$09,
  995. $8B,$1E,$B7,$07,$09,$DB,$74,$05,$80,$EE,$02,$01,$DA,$89,$16,
  996. $BB,$07,$89,$0E,$BD,$07,$B8,$00,$42,$8B,$1E,$06,$06,$CD,$21,
  997. $B9,$A8,$00,$BA,$BF,$07,$B4,$3F,$CD,$21,$3D,$A8,$00,$75,$06,
  998. $81,$3E,$BF,$07,$4C,$01,$0F,$85,$61,$02,$66,$A1,$E3,$07,$66,
  999. $A3,$10,$06,$66,$8B,$0E,$BB,$07,$66,$A1,$03,$08,$66,$01,$C8,
  1000. $66,$A3,$08,$06,$66,$A1,$2B,$08,$66,$01,$C8,$66,$A3,$0C,$06,
  1001. $66,$8B,$1E,$4B,$08,$66,$A1,$4F,$08,$66,$01,$C3,$66,$B8,$01,
  1002. $00,$01,$00,$66,$39,$C3,$73,$03,$66,$89,$C3,$66,$81,$C3,$FF,
  1003. $FF,$00,$00,$31,$DB,$66,$89,$1E,$1C,$00,$E8,$F5,$02,$8B,$1E,
  1004. $04,$06,$09,$DB,$74,$0A,$B4,$48,$CD,$21,$0F,$82,$15,$02,$8E,
  1005. $C0,$E8,$08,$03,$B8,$01,$00,$FF,$1E,$00,$06,$0F,$82,$0F,$02,
  1006. $8C,$06,$26,$00,$8C,$0E,$28,$00,$8C,$D8,$A3,$22,$00,$8E,$C0,
  1007. $31,$C0,$B9,$01,$00,$CD,$31,$72,$07,$A3,$14,$06,$31,$C0,$CD,
  1008. $31,$0F,$82,$F3,$01,$A3,$16,$06,$66,$8B,$0E,$1C,$00,$B8,$01,
  1009. $05,$8B,$1E,$1E,$00,$CD,$31,$0F,$82,$E5,$01,$89,$1E,$1A,$06,
  1010. $89,$0E,$18,$06,$89,$36,$1A,$00,$89,$3E,$18,$00,$B8,$07,$00,
  1011. $8B,$1E,$14,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,$CD,$31,$B8,
  1012. $09,$00,$8C,$C9,$83,$E1,$03,$C1,$E1,$05,$51,$81,$C9,$9B,$C0,
  1013. $CD,$31,$B8,$08,$00,$8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,
  1014. $B8,$07,$00,$8B,$1E,$16,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,
  1015. $CD,$31,$B8,$09,$00,$59,$81,$C9,$93,$C0,$CD,$31,$B8,$08,$00,
  1016. $8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,$B8,$00,$01,$BB,$00,
  1017. $0F,$CD,$31,$73,$10,$3D,$08,$00,$0F,$85,$73,$01,$B8,$00,$01,
  1018. $CD,$31,$0F,$82,$6A,$01,$A3,$1C,$06,$89,$16,$1E,$06,$C1,$E3,
  1019. $04,$89,$1E,$20,$06,$66,$8B,$36,$08,$06,$66,$8B,$3E,$FB,$07,
  1020. $66,$8B,$0E,$FF,$07,$E8,$49,$00,$66,$8B,$36,$0C,$06,$66,$8B,
  1021. $3E,$23,$08,$66,$8B,$0E,$27,$08,$E8,$37,$00,$8E,$06,$16,$06,
  1022. $66,$8B,$3E,$4B,$08,$66,$8B,$0E,$4F,$08,$66,$31,$C0,$66,$C1,
  1023. $E9,$02,$67,$F3,$66,$AB,$B4,$3E,$8B,$1E,$06,$06,$CD,$21,$B8,
  1024. $01,$01,$8B,$16,$1E,$06,$CD,$31,$1E,$0F,$A1,$8E,$1E,$16,$06,
  1025. $66,$64,$FF,$2E,$10,$06,$66,$89,$F0,$66,$25,$FF,$01,$00,$00,
  1026. $66,$01,$C1,$29,$C6,$66,$29,$C7,$66,$89,$0E,$26,$06,$66,$89,
  1027. $3E,$22,$06,$E8,$0F,$01,$89,$36,$3E,$06,$66,$C1,$EE,$10,$89,
  1028. $36,$42,$06,$8B,$1E,$06,$06,$89,$1E,$3A,$06,$C7,$06,$46,$06,
  1029. $00,$42,$E8,$03,$01,$A1,$1C,$06,$A3,$4E,$06,$C7,$06,$3E,$06,
  1030. $00,$00,$C6,$06,$47,$06,$3F,$A1,$28,$06,$09,$C0,$75,$09,$A1,
  1031. $26,$06,$3B,$06,$20,$06,$76,$03,$A1,$20,$06,$A3,$42,$06,$E8,
  1032. $D9,$00,$66,$31,$C9,$8B,$0E,$46,$06,$66,$8B,$3E,$22,$06,$66,
  1033. $01,$0E,$22,$06,$66,$29,$0E,$26,$06,$66,$31,$F6,$C1,$E9,$02,
  1034. $1E,$06,$8E,$06,$16,$06,$8E,$1E,$1E,$06,$67,$F3,$66,$A5,$07,
  1035. $1F,$66,$03,$0E,$26,$06,$75,$AF,$C3,$3C,$3A,$74,$06,$3C,$2F,
  1036. $74,$02,$3C,$5C,$C3,$BE,$64,$07,$89,$F3,$26,$8A,$05,$47,$88,
  1037. $04,$38,$E0,$74,$0E,$08,$C0,$74,$0A,$46,$E8,$DE,$FF,$75,$EC,
  1038. $89,$F3,$74,$E8,$C3,$B0,$66,$BA,$48,$05,$EB,$0C,$B0,$67,$BA,
  1039. $55,$05,$EB,$05,$B0,$68,$BA,$5F,$05,$52,$8B,$1E,$62,$07,$C6,
  1040. $07,$24,$BB,$64,$07,$EB,$28,$E8,$F5,$00,$B0,$69,$BA,$99,$05,
  1041. $EB,$1A,$B0,$6A,$BA,$B2,$05,$EB,$13,$B0,$6B,$BA,$C4,$05,$EB,
  1042. $0C,$B0,$6C,$BA,$D6,$05,$EB,$05,$B0,$69,$BA,$99,$05,$52,$BB,
  1043. $3B,$05,$E8,$15,$00,$5B,$E8,$11,$00,$BB,$67,$04,$E8,$0B,$00,
  1044. $B4,$4C,$CD,$21,$43,$50,$B4,$02,$CD,$21,$58,$8A,$17,$80,$FA,
  1045. $24,$75,$F2,$C3,$0D,$0A,$24,$50,$51,$57,$31,$C0,$BF,$2A,$06,
  1046. $B9,$19,$00,$F3,$AB,$5F,$59,$58,$C3,$B8,$00,$03,$BB,$21,$00,
  1047. $31,$C9,$66,$BF,$2A,$06,$00,$00,$CD,$31,$C3,$00,$00,$30,$E4,
  1048. $E8,$4E,$FF,$89,$DE,$8B,$3E,$8C,$04,$EB,$17,$B4,$3B,$E8,$41,
  1049. $FF,$81,$FE,$64,$07,$74,$12,$8A,$44,$FF,$E8,$2A,$FF,$74,$04,
  1050. $C6,$04,$5C,$46,$E8,$03,$00,$72,$E4,$C3,$E8,$34,$00,$BB,$44,
  1051. $00,$8A,$07,$88,$04,$43,$46,$08,$C0,$75,$F6,$06,$57,$1E,$07,
  1052. $E8,$9B,$FF,$BB,$2A,$06,$8C,$5F,$04,$89,$5F,$02,$BA,$64,$07,
  1053. $B8,$00,$4B,$CD,$21,$5F,$07,$72,$09,$B4,$4D,$CD,$21,$2D,$00,
  1054. $03,$F7,$D8,$EB,$28,$80,$3E,$69,$08,$05,$72,$20,$B8,$00,$58,
  1055. $CD,$21,$A2,$67,$08,$B8,$02,$58,$CD,$21,$A2,$68,$08,$B8,$01,
  1056. $58,$BB,$80,$00,$CD,$21,$B8,$03,$58,$BB,$01,$00,$CD,$21,$C3,
  1057. $9C,$80,$3E,$69,$08,$05,$72,$1A,$50,$53,$B8,$03,$58,$8A,$1E,
  1058. $68,$08,$30,$FF,$CD,$21,$B8,$01,$58,$8A,$1E,$67,$08,$30,$FF,
  1059. $CD,$21,$5B,$58,$9D,$C3,$4C,$6F,$61,$64,$20,$65,$72,$72,$6F,
  1060. $72,$3A,$20,$24,$3A,$20,$63,$61,$6E,$27,$74,$20,$6F,$70,$65,
  1061. $6E,$24,$3A,$20,$6E,$6F,$74,$20,$45,$58,$45,$24,$3A,$20,$6E,
  1062. $6F,$74,$20,$43,$4F,$46,$46,$20,$28,$43,$68,$65,$63,$6B,$20,
  1063. $66,$6F,$72,$20,$76,$69,$72,$75,$73,$65,$73,$29,$24,$6E,$6F,
  1064. $20,$44,$50,$4D,$49,$20,$2D,$20,$47,$65,$74,$20,$63,$73,$64,
  1065. $70,$6D,$69,$2A,$62,$2E,$7A,$69,$70,$24,$6E,$6F,$20,$44,$4F,
  1066. $53,$20,$6D,$65,$6D,$6F,$72,$79,$24,$6E,$65,$65,$64,$20,$44,
  1067. $4F,$53,$20,$33,$24,$63,$61,$6E,$27,$74,$20,$73,$77,$69,$74,
  1068. $63,$68,$20,$6D,$6F,$64,$65,$24,$6E,$6F,$20,$44,$50,$4D,$49,
  1069. $20,$73,$65,$6C,$65,$63,$74,$6F,$72,$73,$24,$6E,$6F,$20,$44,
  1070. $50,$4D,$49,$20,$6D,$65,$6D,$6F,$72,$79,$24,$90,$90,$90,$90,
  1071. $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,
  1072. $90,$90,$90,$90,$90,$90,$90,$90);
  1073. const win32stub : array[0..127] of byte=(
  1074. $4D,$5A,$90,$00,$03,$00,$00,$00,$04,$00,$00,$00,$FF,$FF,$00,$00,
  1075. $B8,$00,$00,$00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,
  1076. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  1077. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$80,$00,$00,$00,
  1078. $0E,$1F,$BA,$0E,$00,$B4,$09,$CD,$21,$B8,$01,$4C,$CD,$21,$54,$68,
  1079. $69,$73,$20,$70,$72,$6F,$67,$72,$61,$6D,$20,$63,$61,$6E,$6E,$6F,
  1080. $74,$20,$62,$65,$20,$72,$75,$6E,$20,$69,$6E,$20,$44,$4F,$53,$20,
  1081. $6D,$6F,$64,$65,$2E,$0D,$0D,$0A,$24,$00,$00,$00,$00,$00,$00,$00);
  1082. const pemagic : array[0..3] of byte = (
  1083. $50,$45,$00,$00);
  1084. {****************************************************************************
  1085. Helpers
  1086. ****************************************************************************}
  1087. function djencodesechdrflags(aoptions:TObjSectionOptions):longword;
  1088. begin
  1089. if (oso_load in aoptions) then
  1090. begin
  1091. if oso_executable in aoptions then
  1092. result:=COFF_STYP_TEXT
  1093. else if not(oso_data in aoptions) then
  1094. result:=COFF_STYP_BSS
  1095. else
  1096. result:=COFF_STYP_DATA;
  1097. end
  1098. else if oso_debug in aoptions then
  1099. result:=COFF_STYP_INFO
  1100. else
  1101. result:=COFF_STYP_REG;
  1102. end;
  1103. function djdecodesechdrflags(const aname:string;flags:longword):TObjSectionOptions;
  1104. begin
  1105. result:=[];
  1106. if flags and COFF_STYP_TEXT<>0 then
  1107. result:=[oso_data,oso_load,oso_executable]
  1108. else if flags and COFF_STYP_BSS<>0 then
  1109. result:=[oso_load]
  1110. else if flags and COFF_STYP_DATA<>0 then
  1111. result:=[oso_data,oso_load]
  1112. else if flags and COFF_STYP_INFO<>0 then
  1113. result:=[oso_data,oso_debug]
  1114. else
  1115. result:=[oso_data]
  1116. end;
  1117. function peencodesechdrflags(aoptions:TObjSectionOptions;aalign:longint):longword;
  1118. begin
  1119. if oso_executable in aoptions then
  1120. result:=PE_SCN_CNT_CODE or PE_SCN_MEM_EXECUTE
  1121. else
  1122. begin
  1123. if (oso_data in aoptions) then
  1124. result:=PE_SCN_CNT_INITIALIZED_DATA
  1125. else
  1126. result:=PE_SCN_CNT_UNINITIALIZED_DATA;
  1127. end;
  1128. if oso_write in aoptions then
  1129. result:=result or PE_SCN_MEM_WRITE or PE_SCN_MEM_READ
  1130. else
  1131. result:=result or PE_SCN_MEM_READ;
  1132. if not (oso_load in aoptions) then
  1133. result:=result or PE_SCN_MEM_DISCARDABLE;
  1134. case aalign of
  1135. 1 : result:=result or PE_SCN_ALIGN_1BYTES;
  1136. 2 : result:=result or PE_SCN_ALIGN_2BYTES;
  1137. 4 : result:=result or PE_SCN_ALIGN_4BYTES;
  1138. 8 : result:=result or PE_SCN_ALIGN_8BYTES;
  1139. 16 : result:=result or PE_SCN_ALIGN_16BYTES;
  1140. 32 : result:=result or PE_SCN_ALIGN_32BYTES;
  1141. 64 : result:=result or PE_SCN_ALIGN_64BYTES;
  1142. 128 : result:=result or PE_SCN_ALIGN_128BYTES;
  1143. 256 : result:=result or PE_SCN_ALIGN_256BYTES;
  1144. 512 : result:=result or PE_SCN_ALIGN_512BYTES;
  1145. 1024 : result:=result or PE_SCN_ALIGN_1024BYTES;
  1146. 2048 : result:=result or PE_SCN_ALIGN_2048BYTES;
  1147. 4096 : result:=result or PE_SCN_ALIGN_4096BYTES;
  1148. 8192 : result:=result or PE_SCN_ALIGN_8192BYTES;
  1149. else result:=result or PE_SCN_ALIGN_16BYTES;
  1150. end;
  1151. end;
  1152. procedure pedecodesechdrflags(const aname:string;flags:longword;out aoptions:TObjSectionOptions;out aalign:longint);
  1153. var
  1154. alignflag : longword;
  1155. begin
  1156. aoptions:=[];
  1157. if flags and PE_SCN_CNT_CODE<>0 then
  1158. include(aoptions,oso_executable);
  1159. if flags and PE_SCN_MEM_DISCARDABLE<>0 then
  1160. include(aoptions,oso_debug);
  1161. if flags and PE_SCN_CNT_UNINITIALIZED_DATA=0 then
  1162. include(aoptions,oso_data);
  1163. if (flags and (PE_SCN_LNK_REMOVE or PE_SCN_MEM_DISCARDABLE)=0) then
  1164. include(aoptions,oso_load);
  1165. if flags and PE_SCN_LNK_COMDAT<>0 then
  1166. include(aoptions,oso_comdat);
  1167. { read/write }
  1168. if flags and PE_SCN_MEM_WRITE<>0 then
  1169. include(aoptions,oso_write);
  1170. { alignment }
  1171. alignflag:=flags and PE_SCN_ALIGN_MASK;
  1172. if alignflag=PE_SCN_ALIGN_8192BYTES then
  1173. aalign:=8192
  1174. else if alignflag=PE_SCN_ALIGN_4096BYTES then
  1175. aalign:=4096
  1176. else if alignflag=PE_SCN_ALIGN_2048BYTES then
  1177. aalign:=2048
  1178. else if alignflag=PE_SCN_ALIGN_1024BYTES then
  1179. aalign:=1024
  1180. else if alignflag=PE_SCN_ALIGN_512BYTES then
  1181. aalign:=512
  1182. else if alignflag=PE_SCN_ALIGN_256BYTES then
  1183. aalign:=256
  1184. else if alignflag=PE_SCN_ALIGN_128BYTES then
  1185. aalign:=128
  1186. else if alignflag=PE_SCN_ALIGN_64BYTES then
  1187. aalign:=64
  1188. else if alignflag=PE_SCN_ALIGN_32BYTES then
  1189. aalign:=32
  1190. else if alignflag=PE_SCN_ALIGN_16BYTES then
  1191. aalign:=16
  1192. else if alignflag=PE_SCN_ALIGN_8BYTES then
  1193. aalign:=8
  1194. else if alignflag=PE_SCN_ALIGN_4BYTES then
  1195. aalign:=4
  1196. else if alignflag=PE_SCN_ALIGN_2BYTES then
  1197. aalign:=2
  1198. else if alignflag=PE_SCN_ALIGN_1BYTES then
  1199. aalign:=1
  1200. else if alignflag=0 then
  1201. aalign:=0
  1202. else
  1203. Internalerror(2009050401);
  1204. end;
  1205. function encodeBase64(p:aword):string;
  1206. const
  1207. alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
  1208. 'abcdefghijklmnopqrstuvwxyz' +
  1209. '0123456789+/';
  1210. var
  1211. i,
  1212. idx,
  1213. rem : longint;
  1214. begin
  1215. setlength(result,6);
  1216. idx := 6;
  1217. for i:=0 to 5 do
  1218. begin
  1219. rem:=p mod 64;
  1220. p:=p div 64;
  1221. result[idx]:=alphabet[rem+1];
  1222. dec(idx);
  1223. end;
  1224. if p<>0 then
  1225. internalerror(2020091601);
  1226. end;
  1227. function decodeBase64(const s:string;out p:longint):boolean;
  1228. var
  1229. i : longint;
  1230. v : aword;
  1231. begin
  1232. if length(s)>6 then
  1233. exit(false);
  1234. p:=0;
  1235. for i:=1 to length(s) do
  1236. begin
  1237. v:=0;
  1238. if (s[i]>='A') and (s[i]<='Z') then // 0..25
  1239. v:=Ord(s[i])-Ord('A')
  1240. else if (s[i]>='a') and (s[i]<='z') then // 26..51
  1241. v:=Ord(s[i])-Ord('a')+26
  1242. else if (s[i]>='0') and (s[i]<='9') then // 52..61
  1243. v:=Ord(s[i])-Ord('0')+52
  1244. else if s[i]='+' then // 62
  1245. v:=62
  1246. else if s[i]='/' then // 63
  1247. v:=63
  1248. else
  1249. exit(false);
  1250. p:=(p*64)+v;
  1251. end;
  1252. result:=true;
  1253. end;
  1254. {****************************************************************************
  1255. TCoffObjSection
  1256. ****************************************************************************}
  1257. constructor TCoffObjSection.create(AList:TFPHashObjectList;const aname:string;aalign:longint;aoptions:TObjSectionOptions);
  1258. begin
  1259. inherited create(AList,aname,aalign,aoptions);
  1260. end;
  1261. procedure TCoffObjSection.writereloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  1262. begin
  1263. AddSectionReloc(size,aTarget,reltype);
  1264. write(offset,len);
  1265. end;
  1266. { We don't want overflow nor range checks here,
  1267. wrapping is accepted in the address computation below }
  1268. {$r-}
  1269. {$q-}
  1270. procedure TCoffExeOutput.DoRelocationFixup(objsec:TObjSection);
  1271. var
  1272. i,zero,address_size : longint;
  1273. objreloc : TObjRelocation;
  1274. address,
  1275. relocval : aint;
  1276. {$if defined(arm) or defined(aarch64)}
  1277. addend : aint;
  1278. {$endif arm or aarch64}
  1279. relocsec : TObjSection;
  1280. {$ifdef cpu64bitaddr}
  1281. s : string;
  1282. {$endif cpu64bitaddr}
  1283. data : TDynamicArray;
  1284. begin
  1285. data:=objsec.data;
  1286. for i:=0 to objsec.ObjRelocations.Count-1 do
  1287. begin
  1288. objreloc:=TObjRelocation(objsec.ObjRelocations[i]);
  1289. address_size:=4;
  1290. case objreloc.typ of
  1291. RELOC_NONE:
  1292. continue;
  1293. RELOC_ZERO:
  1294. begin
  1295. data.Seek(objreloc.dataoffset);
  1296. zero:=0;
  1297. data.Write(zero,4);
  1298. continue;
  1299. end;
  1300. {$ifdef cpu64bitaddr}
  1301. RELOC_ABSOLUTE:
  1302. address_size:=8;
  1303. {$endif cpu64bitaddr}
  1304. else
  1305. ;
  1306. end;
  1307. address:=0;
  1308. data.Seek(objreloc.dataoffset);
  1309. data.Read(address,address_size);
  1310. if assigned(objreloc.symbol) then
  1311. begin
  1312. relocsec:=objreloc.symbol.objsection;
  1313. relocval:=objreloc.symbol.address;
  1314. end
  1315. else
  1316. if assigned(objreloc.objsection) then
  1317. begin
  1318. relocsec:=objreloc.objsection;
  1319. relocval:=objreloc.objsection.mempos
  1320. end
  1321. else
  1322. internalerror(200205183);
  1323. { Only debug sections are allowed to have relocs pointing to unused sections }
  1324. if not relocsec.used and not (oso_debug in objsec.secoptions) then
  1325. internalerror(200603061);
  1326. if relocsec.used then
  1327. case objreloc.typ of
  1328. RELOC_RELATIVE :
  1329. begin
  1330. address:=address-objsec.mempos+relocval;
  1331. if win32 then
  1332. dec(address,objreloc.dataoffset+4);
  1333. end;
  1334. RELOC_RVA:
  1335. begin
  1336. { fixup address when the symbol was known in defined object }
  1337. if (relocsec.objdata=objsec.objdata) then
  1338. dec(address,TCoffObjSection(relocsec).orgmempos);
  1339. {$ifdef arm}
  1340. if (relocsec.objdata=objsec.objdata) and not TCoffObjData(objsec.objdata).eVCobj then
  1341. inc(address, relocsec.MemPos)
  1342. else
  1343. {$endif arm}
  1344. inc(address,relocval);
  1345. end;
  1346. RELOC_SECREL32 :
  1347. begin
  1348. { fixup address when the symbol was known in defined object }
  1349. if (relocsec.objdata=objsec.objdata) then
  1350. dec(address,relocsec.ExeSection.MemPos);
  1351. inc(address,relocval);
  1352. end;
  1353. {$ifdef arm}
  1354. RELOC_RELATIVE_24,
  1355. RELOC_RELATIVE_CALL:
  1356. begin
  1357. addend:=sarlongint(((address and $ffffff) shl 8),6); // Sign-extend while shifting left twice
  1358. relocval:=longint(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  1359. address:=(address and $ff000000) or (relocval and $ffffff);
  1360. relocval:=relocval shr 24;
  1361. if (relocval<>$3f) and (relocval<>0) then
  1362. internalerror(200606085); { offset overflow }
  1363. end;
  1364. RELOC_RELATIVE_24_THUMB,
  1365. RELOC_RELATIVE_CALL_THUMB:
  1366. begin
  1367. addend:=sarlongint(((address and $ffffff) shl 8),6); // Sign-extend while shifting left twice, the assembler never sets the H bit
  1368. relocval:=longint(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 1;
  1369. address:=(address and $ff000000) or ((relocval shr 1) and $ffffff) or ((relocval and 1) shl 24);
  1370. relocval:=relocval shr 25;
  1371. if (relocval<>$3f) and (relocval<>0) then
  1372. internalerror(2006060801); { offset overflow }
  1373. end;
  1374. {$endif arm}
  1375. {$ifdef aarch64}
  1376. RELOC_RELATIVE_26:
  1377. begin
  1378. addend:=sarint64(((address and $3ffffff) shl 38),36); // Sign-extend while shifting left twice
  1379. relocval:=int64(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  1380. address:=(address and $fc000000) or (relocval and $3ffffff);
  1381. relocval:=relocval shr 58;
  1382. if (relocval<>$f) and (relocval<>0) then
  1383. internalerror(2020032202); { offset overflow }
  1384. end;
  1385. RELOC_RELATIVE_19:
  1386. begin
  1387. addend:=sarint64(((address and $7ffe0) shl 45),43); // Sign-extend while shifting left twice
  1388. relocval:=int64(relocval - objsec.mempos - objreloc.dataoffset + addend) shr 2;
  1389. address:=(address and $fff80000) or (relocval and $7ffff);
  1390. relocval:=relocval shr 51;
  1391. if (relocval<>$3f) and (relocval<>0) then
  1392. internalerror(2020032203); { offset overflow }
  1393. end;
  1394. RELOC_ADR_PREL_LO21:
  1395. begin
  1396. addend:=((address shr 29) and $3) or (((address shr 5) and $7ffff) shl 2);
  1397. { sign extend the value if necessary }
  1398. if (addend and (1 shl 21)) <> 0 then
  1399. addend:=addend or sarint64(1 shl 63,12);
  1400. relocval:=relocval and $1fffff;
  1401. relocval:=int64(relocval-objsec.mempos-objreloc.dataoffset+addend);
  1402. address:=address and ($3 shl 29) and ($7ffff shl 5);
  1403. address:=address or ((relocval and $3) shl 29) or (((relocval shr 2) and $7ffff) shl 5);
  1404. end;
  1405. RELOC_ADR_PREL_PG_HI21:
  1406. begin
  1407. addend:=((address shr 29) and $3) or (((address shr 5) and $7ffff) shl 2);
  1408. { sign extend the value if necessary }
  1409. if (addend and (1 shl 21)) <> 0 then
  1410. addend:=addend or sarint64(1 shl 63, 12);
  1411. relocval:=relocval shr 12;
  1412. relocval:=int64((relocval-(objsec.mempos+objreloc.dataoffset) shr 12)+addend);
  1413. address:=address and not (($3 shl 29) or ($7ffff shl 5));
  1414. address:=address or ((relocval and $3) shl 29) or (((relocval shr 2) and $7ffff) shl 5);
  1415. end;
  1416. RELOC_LDST8_ABS_LO12,
  1417. RELOC_ADD_ABS_LO12:
  1418. begin
  1419. addend:=(address shr 10) and $fff;
  1420. relocval:=(relocval + addend) and $fff;
  1421. address:=address and not ($fff shl 10);
  1422. address:=address or (relocval shl 10);
  1423. end;
  1424. {$endif aarch64}
  1425. {$ifdef x86_64}
  1426. { 64 bit coff only }
  1427. RELOC_RELATIVE_1:
  1428. begin
  1429. address:=address-objsec.mempos+relocval;
  1430. dec(address,objreloc.dataoffset+5);
  1431. end;
  1432. RELOC_RELATIVE_2:
  1433. begin
  1434. address:=address-objsec.mempos+relocval;
  1435. dec(address,objreloc.dataoffset+6);
  1436. end;
  1437. RELOC_RELATIVE_3:
  1438. begin
  1439. address:=address-objsec.mempos+relocval;
  1440. dec(address,objreloc.dataoffset+7);
  1441. end;
  1442. RELOC_RELATIVE_4:
  1443. begin
  1444. address:=address-objsec.mempos+relocval;
  1445. dec(address,objreloc.dataoffset+8);
  1446. end;
  1447. RELOC_RELATIVE_5:
  1448. begin
  1449. address:=address-objsec.mempos+relocval;
  1450. dec(address,objreloc.dataoffset+9);
  1451. end;
  1452. {$endif x86_64}
  1453. {$ifdef cpu64bitaddr}
  1454. RELOC_ABSOLUTE32,
  1455. {$endif cpu64bitaddr}
  1456. RELOC_ABSOLUTE :
  1457. begin
  1458. if (not win32) and assigned(objreloc.symbol) and
  1459. (objreloc.symbol.bind=AB_COMMON) then
  1460. begin
  1461. dec(address,objreloc.symbol.size);
  1462. end
  1463. else
  1464. begin
  1465. { fixup address when the symbol was known in defined object }
  1466. if (relocsec.objdata=objsec.objdata) then
  1467. dec(address,TCoffObjSection(relocsec).orgmempos);
  1468. end;
  1469. {$ifdef arm}
  1470. if (relocsec.objdata=objsec.objdata) and not TCoffObjData(objsec.objdata).eVCobj then
  1471. inc(address, relocsec.MemPos)
  1472. else
  1473. {$endif arm}
  1474. inc(address,relocval);
  1475. inc(address,imagebase);
  1476. end;
  1477. else
  1478. internalerror(200604014);
  1479. end
  1480. else
  1481. address:=0; { Relocation in debug section points to unused section, which is eliminated by linker }
  1482. data.Seek(objreloc.dataoffset);
  1483. data.Write(address,address_size);
  1484. {$ifdef cpu64bitaddr}
  1485. if (objreloc.typ = RELOC_ABSOLUTE32) and (objsec.name <> '.stab') then
  1486. begin
  1487. if assigned(objreloc.symbol) then
  1488. s:=objreloc.symbol.Name
  1489. else
  1490. s:=objreloc.objsection.Name;
  1491. Message2(link_w_32bit_absolute_reloc, objsec.ObjData.Name, s);
  1492. end;
  1493. {$endif cpu64bitaddr}
  1494. end;
  1495. end;
  1496. {****************************************************************************
  1497. TCoffObjData
  1498. ****************************************************************************}
  1499. constructor TCoffObjData.createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
  1500. begin
  1501. inherited create(n);
  1502. CObjSection:=ACObjSection;
  1503. win32:=awin32;
  1504. end;
  1505. function TCoffObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1506. var
  1507. sep : string[3];
  1508. secname : string;
  1509. begin
  1510. { section type user gives the user full controll on the section name }
  1511. if atype=sec_user then
  1512. result:=aname
  1513. else
  1514. begin
  1515. { non-PECOFF targets lack rodata support }
  1516. if (atype in [sec_rodata,sec_rodata_norel]) and
  1517. not (target_info.system in systems_all_windows) then
  1518. atype:=sec_data;
  1519. secname:=coffsecnames[atype];
  1520. if create_smartlink_sections and
  1521. (aname<>'') then
  1522. begin
  1523. case aorder of
  1524. secorder_begin :
  1525. sep:='.b_';
  1526. secorder_end :
  1527. sep:='.z_';
  1528. else
  1529. sep:='.n_';
  1530. end;
  1531. result:=secname+sep+aname
  1532. end
  1533. else
  1534. result:=secname;
  1535. end;
  1536. end;
  1537. procedure TCoffObjData.CreateDebugSections;
  1538. begin
  1539. if target_dbg.id=dbg_stabs then
  1540. begin
  1541. stabssec:=createsection(sec_stab);
  1542. stabstrsec:=createsection(sec_stabstr);
  1543. end;
  1544. end;
  1545. procedure TCoffObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reloctype:TObjRelocationType);
  1546. type
  1547. multi = record
  1548. case integer of
  1549. 0 : (ba : array[0..sizeof(aint)-1] of byte);
  1550. 1 : (b : byte);
  1551. 2 : (w : word);
  1552. 4 : (d : dword);
  1553. 8 : (q : qword);
  1554. end;
  1555. var
  1556. ba : multi;
  1557. curraddr,
  1558. symaddr : aword;
  1559. begin
  1560. if CurrObjSec=nil then
  1561. internalerror(2004030701);
  1562. if assigned(p) then
  1563. begin
  1564. { current address }
  1565. curraddr:=CurrObjSec.mempos+CurrObjSec.Size;
  1566. { external/common symbols don't have a fixed memory position yet }
  1567. if (p.bind=AB_COMMON) then
  1568. begin
  1569. { For go32v2 we need to use the size as address }
  1570. if not win32 then
  1571. symaddr:=p.size
  1572. else
  1573. symaddr:=0;
  1574. end
  1575. else
  1576. symaddr:=p.address;
  1577. { no symbol relocation need inside a section }
  1578. if (p.objsection=CurrObjSec) and
  1579. (p.bind<>AB_COMMON) then
  1580. begin
  1581. case reloctype of
  1582. RELOC_ABSOLUTE :
  1583. begin
  1584. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE);
  1585. inc(data,symaddr);
  1586. end;
  1587. {$ifdef cpu64bitaddr}
  1588. RELOC_ABSOLUTE32 :
  1589. begin
  1590. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,RELOC_ABSOLUTE32);
  1591. inc(data,symaddr);
  1592. end;
  1593. {$endif cpu64bitaddr}
  1594. RELOC_RELATIVE :
  1595. begin
  1596. //inc(data,symaddr-len-CurrObjSec.Size);
  1597. data:=data+symaddr-len-CurrObjSec.Size;
  1598. end;
  1599. {$ifdef ARM}
  1600. RELOC_RELATIVE_24,
  1601. RELOC_RELATIVE_CALL:
  1602. begin
  1603. data:=(data and $ff000000) or (((((data and $ffffff) shl 2)+(symaddr-CurrObjSec.Size)) shr 2) and $FFFFFF); // TODO: Check overflow
  1604. end;
  1605. {$endif ARM}
  1606. RELOC_RVA,
  1607. RELOC_SECREL32 :
  1608. begin
  1609. CurrObjSec.addsectionreloc(curraddr,CurrObjSec,reloctype);
  1610. inc(data,symaddr);
  1611. end;
  1612. else
  1613. internalerror(200604013);
  1614. end;
  1615. end
  1616. else
  1617. begin
  1618. if (p.objsection<>nil) and
  1619. (p.bind<>AB_COMMON) then
  1620. CurrObjSec.addsectionreloc(curraddr,p.objsection,reloctype)
  1621. else
  1622. CurrObjSec.addsymreloc(curraddr,p,reloctype);
  1623. if (not win32) or
  1624. (p.objsection<>nil) then
  1625. inc(data,symaddr);
  1626. if reloctype=RELOC_RELATIVE then
  1627. begin
  1628. if win32 then
  1629. dec(data,len-4)
  1630. else
  1631. dec(data,len+CurrObjSec.Size);
  1632. end;
  1633. end;
  1634. end
  1635. else
  1636. begin
  1637. if reloctype=RELOC_RVA then
  1638. internalerror(200603033);
  1639. end;
  1640. if target_info.endian<>source_info.endian then
  1641. begin
  1642. ba.q:=0;
  1643. if (len<=sizeof(data)) then
  1644. case len of
  1645. 1 : ba.b:=byte(data);
  1646. 2 : begin
  1647. ba.w:=word(data);
  1648. ba.w:=swapendian(ba.w);
  1649. end;
  1650. 4 : begin
  1651. ba.d:=dword(data);
  1652. ba.d:=swapendian(ba.d);
  1653. end;
  1654. 8 : begin
  1655. ba.q:=qword(data);
  1656. ba.q:=swapendian(ba.q);
  1657. end;
  1658. else
  1659. internalerror(2024012501);
  1660. end;
  1661. CurrObjSec.write(ba,len);
  1662. end
  1663. else
  1664. CurrObjSec.write(data,len);
  1665. end;
  1666. {****************************************************************************
  1667. TDJCoffObjData
  1668. ****************************************************************************}
  1669. constructor TDJCoffObjData.create(const n:string);
  1670. begin
  1671. inherited createcoff(n,false,TCoffObjSection);
  1672. end;
  1673. {****************************************************************************
  1674. TPECoffObjData
  1675. ****************************************************************************}
  1676. constructor TPECoffObjData.create(const n:string);
  1677. begin
  1678. inherited createcoff(n,true,TCoffObjSection);
  1679. end;
  1680. {****************************************************************************
  1681. TCoffObjOutput
  1682. ****************************************************************************}
  1683. constructor TCoffObjOutput.createcoff(AWriter:TObjectWriter;awin32:boolean);
  1684. begin
  1685. inherited create(AWriter);
  1686. win32:=awin32;
  1687. end;
  1688. destructor TCoffObjOutput.destroy;
  1689. begin
  1690. FCoffSyms.free;
  1691. FCoffSyms := nil;
  1692. FCoffStrs.free;
  1693. FCoffStrs := nil;
  1694. inherited destroy;
  1695. end;
  1696. procedure TCoffObjOutput.write_symbol(const name:string;value:aword;section:longint;typ,aux:byte);
  1697. var
  1698. sym : coffsymbol;
  1699. bosym : coffbigobjsymbol;
  1700. strpos : longword;
  1701. begin
  1702. { symbolname }
  1703. if length(name)>8 then
  1704. begin
  1705. strpos:=FCoffStrs.size+4;
  1706. FCoffStrs.writestr(name);
  1707. FCoffStrs.writestr(#0);
  1708. end
  1709. else
  1710. strpos:=0;
  1711. if bigobj then
  1712. begin
  1713. fillchar(bosym,sizeof(bosym),0);
  1714. if length(name)>8 then
  1715. bosym.name.offset.offset:=strpos
  1716. else
  1717. move(name[1],bosym.name.shortname,length(name));
  1718. bosym.value:=value;
  1719. bosym.SectionNumber:=longword(section);
  1720. bosym.StorageClass:=typ;
  1721. bosym.NumberOfAuxSymbols:=aux;
  1722. inc(symidx);
  1723. MaybeSwap(bosym);
  1724. FCoffSyms.write(bosym,sizeof(bosym));
  1725. end
  1726. else
  1727. begin
  1728. if section>$7fff then
  1729. internalerror(2017020302);
  1730. FillChar(sym,sizeof(sym),0);
  1731. if length(name)>8 then
  1732. sym.strpos:=strpos
  1733. else
  1734. move(name[1],sym.name,length(name));
  1735. sym.value:=value;
  1736. sym.section:=section;
  1737. sym.typ:=typ;
  1738. sym.aux:=aux;
  1739. inc(symidx);
  1740. MaybeSwap(sym);
  1741. FCoffSyms.write(sym,sizeof(sym));
  1742. end;
  1743. end;
  1744. procedure TCoffObjOutput.section_write_symbol(p:TObject;arg:pointer);
  1745. var
  1746. secrec : coffsectionrec;
  1747. padding : word;
  1748. begin
  1749. with TCoffObjSection(p) do
  1750. begin
  1751. Inc(plongword(arg)^);
  1752. index:=plongword(arg)^;
  1753. secsymidx:=symidx;
  1754. { Both GNU and Microsoft toolchains write section symbols using
  1755. storage class 3 (STATIC).
  1756. No reason to use COFF_SYM_SECTION, it is silently converted to 3 by
  1757. PE binutils and causes warnings with DJGPP binutils. }
  1758. write_symbol(name,mempos,index,COFF_SYM_LOCAL,1);
  1759. { AUX }
  1760. fillchar(secrec,sizeof(secrec),0);
  1761. secrec.len:=Size;
  1762. if ObjRelocations.count<65535 then
  1763. secrec.nrelocs:=ObjRelocations.count
  1764. else
  1765. secrec.nrelocs:=65535;
  1766. inc(symidx);
  1767. MaybeSwap(secrec);
  1768. FCoffSyms.write(secrec,sizeof(secrec));
  1769. { aux recs have the same size as symbols, so we need to add two
  1770. Byte of padding in case of a Big Obj Coff }
  1771. padding:=0;
  1772. if bigobj then
  1773. FCoffSyms.write(padding,sizeof(padding));
  1774. end;
  1775. end;
  1776. procedure TCoffObjOutput.section_write_relocs(p:TObject;arg:pointer);
  1777. var
  1778. i : longint;
  1779. rel : coffreloc;
  1780. objreloc : TObjRelocation;
  1781. begin
  1782. if (TObjSection(p).ObjRelocations.Count>65535) then
  1783. begin
  1784. rel.address:=TObjSection(p).ObjRelocations.Count+1;
  1785. rel.sym:=0;
  1786. rel.reloctype:=0;
  1787. MaybeSwap(rel);
  1788. FWriter.Write(rel,sizeof(rel));
  1789. end;
  1790. for i:=0 to TObjSection(p).ObjRelocations.Count-1 do
  1791. begin
  1792. objreloc:=TObjRelocation(TObjSection(p).ObjRelocations[i]);
  1793. rel.address:=objreloc.dataoffset;
  1794. if assigned(objreloc.symbol) then
  1795. begin
  1796. if (objreloc.symbol.bind=AB_LOCAL) then
  1797. rel.sym:=objreloc.symbol.objsection.secsymidx
  1798. else
  1799. begin
  1800. if objreloc.symbol.symidx=-1 then
  1801. internalerror(200602233);
  1802. rel.sym:=objreloc.symbol.symidx;
  1803. end;
  1804. end
  1805. else
  1806. begin
  1807. if objreloc.objsection<>nil then
  1808. rel.sym:=objreloc.objsection.secsymidx
  1809. else
  1810. rel.sym:=0;
  1811. end;
  1812. case objreloc.typ of
  1813. {$ifdef arm}
  1814. RELOC_ABSOLUTE :
  1815. rel.reloctype:=IMAGE_REL_ARM_ADDR32;
  1816. { I've no idea if this is correct (FK):
  1817. RELOC_RELATIVE :
  1818. rel.reloctype:=IMAGE_REL_ARM_GPREL12;
  1819. }
  1820. RELOC_RVA :
  1821. rel.reloctype:=IMAGE_REL_ARM_ADDR32NB;
  1822. RELOC_SECREL32 :
  1823. rel.reloctype:=IMAGE_REL_ARM_SECREL;
  1824. RELOC_RELATIVE_24 :
  1825. rel.reloctype:=IMAGE_REL_ARM_BRANCH24;
  1826. RELOC_RELATIVE_CALL :
  1827. rel.reloctype:=IMAGE_REL_ARM_BLX24;
  1828. RELOC_RELATIVE_24_THUMB:
  1829. rel.reloctype:=IMAGE_REL_ARM_BLX23T;
  1830. {$endif arm}
  1831. {$ifdef i386}
  1832. RELOC_RELATIVE :
  1833. rel.reloctype:=IMAGE_REL_I386_PCRLONG;
  1834. RELOC_ABSOLUTE :
  1835. rel.reloctype:=IMAGE_REL_I386_DIR32;
  1836. RELOC_RVA :
  1837. rel.reloctype:=IMAGE_REL_I386_IMAGEBASE;
  1838. RELOC_SECREL32 :
  1839. rel.reloctype:=IMAGE_REL_I386_SECREL32;
  1840. {$endif i386}
  1841. {$ifdef x86_64}
  1842. RELOC_NONE :
  1843. rel.reloctype:=IMAGE_REL_AMD64_ABSOLUTE;
  1844. RELOC_RELATIVE :
  1845. rel.reloctype:=IMAGE_REL_AMD64_REL32;
  1846. RELOC_ABSOLUTE32 :
  1847. rel.reloctype:=IMAGE_REL_AMD64_ADDR32;
  1848. RELOC_ABSOLUTE :
  1849. rel.reloctype:=IMAGE_REL_AMD64_ADDR64;
  1850. RELOC_RVA :
  1851. rel.reloctype:=IMAGE_REL_AMD64_ADDR32NB;
  1852. RELOC_RELATIVE_1 :
  1853. rel.reloctype:=IMAGE_REL_AMD64_REL32_1;
  1854. RELOC_RELATIVE_2 :
  1855. rel.reloctype:=IMAGE_REL_AMD64_REL32_2;
  1856. RELOC_RELATIVE_3 :
  1857. rel.reloctype:=IMAGE_REL_AMD64_REL32_3;
  1858. RELOC_RELATIVE_4 :
  1859. rel.reloctype:=IMAGE_REL_AMD64_REL32_4;
  1860. RELOC_RELATIVE_5 :
  1861. rel.reloctype:=IMAGE_REL_AMD64_REL32_5;
  1862. RELOC_SECREL32 :
  1863. rel.reloctype:=IMAGE_REL_AMD64_SECREL;
  1864. {$endif x86_64}
  1865. {$ifdef aarch64}
  1866. RELOC_NONE :
  1867. rel.reloctype:=IMAGE_REL_ARM64_ABSOLUTE;
  1868. RELOC_ABSOLUTE32 :
  1869. rel.reloctype:=IMAGE_REL_ARM64_ADDR32;
  1870. RELOC_RVA :
  1871. rel.reloctype:=IMAGE_REL_ARM64_ADDR32NB;
  1872. RELOC_RELATIVE_26 :
  1873. rel.reloctype:=IMAGE_REL_ARM64_BRANCH26;
  1874. RELOC_ADR_PREL_PG_HI21 :
  1875. rel.reloctype:=IMAGE_REL_ARM64_PAGEBASE_REL21;
  1876. RELOC_ADR_PREL_LO21 :
  1877. rel.reloctype:=IMAGE_REL_ARM64_REL21;
  1878. RELOC_ADD_ABS_LO12:
  1879. rel.reloctype:=IMAGE_REL_ARM64_PAGEOFFSET_12A;
  1880. RELOC_LDST8_ABS_LO12:
  1881. rel.reloctype:=IMAGE_REL_ARM64_PAGEOFFSET_12L;
  1882. RELOC_SECREL32:
  1883. rel.reloctype:=IMAGE_REL_ARM64_SECREL;
  1884. {IMAGE_REL_ARM64_SECREL_LOW12A
  1885. IMAGE_REL_ARM64_SECREL_HIGH12A
  1886. IMAGE_REL_ARM64_SECREL_LOW12L
  1887. IMAGE_REL_ARM64_TOKEN
  1888. IMAGE_REL_ARM64_SECTION}
  1889. RELOC_ABSOLUTE:
  1890. rel.reloctype:=IMAGE_REL_ARM64_ADDR64;
  1891. RELOC_RELATIVE_19:
  1892. rel.reloctype:=IMAGE_REL_ARM64_BRANCH19;
  1893. {$endif aarch64}
  1894. else
  1895. internalerror(200905071);
  1896. end;
  1897. MaybeSwap(rel);
  1898. FWriter.write(rel,sizeof(rel));
  1899. end;
  1900. end;
  1901. procedure TCoffObjOutput.create_symbols(data:TObjData);
  1902. var
  1903. filename : string[20];
  1904. filenamelen : longint;
  1905. sectionval : word;
  1906. globalval : byte;
  1907. i : longint;
  1908. value : aword;
  1909. objsym : TObjSymbol;
  1910. secidx : longword;
  1911. begin
  1912. with TCoffObjData(data) do
  1913. begin
  1914. symidx:=0;
  1915. { The `.file' record, and the file name auxiliary record }
  1916. write_symbol('.file', 0, -2, COFF_SYM_FILE, 1);
  1917. fillchar(filename,sizeof(filename),0);
  1918. filename:=ExtractFileName(current_module.mainsource);
  1919. inc(symidx);
  1920. if bigobj then
  1921. filenamelen:=sizeof(coffbigobjsymbol)
  1922. else
  1923. filenamelen:=sizeof(coffsymbol);
  1924. FCoffSyms.write(filename[1],filenamelen);
  1925. { Sections }
  1926. secidx:=0;
  1927. ObjSectionList.ForEachCall(@section_write_symbol,@secidx);
  1928. { ObjSymbols }
  1929. for i:=0 to ObjSymbolList.Count-1 do
  1930. begin
  1931. objsym:=TObjSymbol(ObjSymbolList[i]);
  1932. if (objsym.bind=AB_LOCAL) then
  1933. continue;
  1934. case objsym.bind of
  1935. AB_GLOBAL,
  1936. AB_PRIVATE_EXTERN:
  1937. begin
  1938. globalval:=COFF_SYM_GLOBAL;
  1939. sectionval:=objsym.objsection.index;
  1940. value:=objsym.address;
  1941. end;
  1942. AB_LOCAL :
  1943. begin
  1944. globalval:=COFF_SYM_LOCAL;
  1945. sectionval:=objsym.objsection.index;
  1946. value:=objsym.address;
  1947. end;
  1948. else
  1949. begin
  1950. globalval:=COFF_SYM_GLOBAL;
  1951. sectionval:=0;
  1952. value:=objsym.size;
  1953. end;
  1954. end;
  1955. { symbolname }
  1956. objsym.symidx:=symidx;
  1957. write_symbol(objsym.name,value,sectionval,globalval,0);
  1958. end;
  1959. end;
  1960. end;
  1961. procedure TCoffObjOutput.section_set_reloc_datapos(p:TCoffObjSection;var datapos:aword);
  1962. begin
  1963. p.coffrelocpos:=datapos;
  1964. inc(datapos,sizeof(coffreloc)*p.ObjRelocations.count);
  1965. if p.ObjRelocations.count>65535 then
  1966. begin
  1967. if win32 then
  1968. inc(datapos,sizeof(coffreloc))
  1969. else
  1970. Message1(asmw_f_too_many_relocations,p.fullname);
  1971. end;
  1972. end;
  1973. procedure TCoffObjOutput.section_write_header(p:TObject;arg:pointer);
  1974. var
  1975. sechdr : tcoffsechdr;
  1976. s : string;
  1977. strpos : aword;
  1978. begin
  1979. with TCoffObjSection(p) do
  1980. begin
  1981. fillchar(sechdr,sizeof(sechdr),0);
  1982. s:=name;
  1983. if length(s)>8 then
  1984. begin
  1985. strpos:=FCoffStrs.size+4;
  1986. FCoffStrs.writestr(s);
  1987. FCoffStrs.writestr(#0);
  1988. if strpos>=10000000 then
  1989. s:='//'+encodeBase64(strpos)
  1990. else
  1991. s:='/'+ToStr(strpos);
  1992. if length(s)>8 then
  1993. internalerror(2020091501);
  1994. end;
  1995. move(s[1],sechdr.name,length(s));
  1996. if not win32 then
  1997. begin
  1998. sechdr.rvaofs:=mempos;
  1999. sechdr.vsize:=mempos;
  2000. end
  2001. else
  2002. begin
  2003. if not(oso_data in secoptions) then
  2004. sechdr.vsize:=Size;
  2005. end;
  2006. sechdr.DataSize:=size;
  2007. if (Size>0) and
  2008. (oso_data in secoptions) then
  2009. sechdr.datapos:=datapos;
  2010. if ObjRelocations.count<65535 then
  2011. sechdr.nrelocs:=ObjRelocations.count
  2012. else
  2013. sechdr.nrelocs:=65535;
  2014. sechdr.relocpos:=coffrelocpos;
  2015. if win32 then
  2016. begin
  2017. sechdr.flags:=peencodesechdrflags(secoptions,secalign);
  2018. if ObjRelocations.count>65535 then
  2019. sechdr.flags:=sechdr.flags or PE_SCN_LNK_NRELOC_OVFL;
  2020. end
  2021. else
  2022. sechdr.flags:=djencodesechdrflags(secoptions);
  2023. MaybeSwap(sechdr);
  2024. FWriter.write(sechdr,sizeof(sechdr));
  2025. end;
  2026. end;
  2027. function TCoffObjOutput.writedata(data:TObjData):boolean;
  2028. var
  2029. datapos,
  2030. sympos : aword;
  2031. i : longint;
  2032. header : tcoffheader;
  2033. boheader : tcoffbigobjheader;
  2034. begin
  2035. result:=false;
  2036. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  2037. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  2038. with TCoffObjData(data) do
  2039. begin
  2040. bigobj:=(ObjSectionList.Count>$7fff) and win32;
  2041. { Create Symbol Table }
  2042. create_symbols(data);
  2043. { Calculate the filepositions }
  2044. if bigobj then
  2045. datapos:=sizeof(tcoffbigobjheader)
  2046. else
  2047. datapos:=sizeof(tcoffheader);
  2048. datapos:=datapos+sizeof(tcoffsechdr)*ObjSectionList.Count;
  2049. { Sections first }
  2050. layoutsections(datapos);
  2051. { relocs }
  2052. for i:=0 to ObjSectionList.Count-1 do
  2053. section_set_reloc_datapos(TCoffObjSection(ObjSectionList[i]),datapos);
  2054. { Symbols }
  2055. sympos:=datapos;
  2056. { Generate COFF header }
  2057. if bigobj then
  2058. begin
  2059. fillchar(boheader,sizeof(boheader),0);
  2060. boheader.Sig1:=0;
  2061. boheader.Sig2:=$ffff;
  2062. boheader.Machine:=COFF_MAGIC;
  2063. boheader.Version:=COFF_BIG_OBJ_VERSION;
  2064. boheader.NumberOfSections:=longword(ObjSectionList.Count);
  2065. boheader.NumberOfSymbols:=longword(symidx);
  2066. boheader.PointerToSymbolTable:=sympos;
  2067. Move(COFF_BIG_OBJ_MAGIC,boheader.UUID,length(boheader.UUID));
  2068. MaybeSwap(boheader);
  2069. FWriter.write(boheader,sizeof(boheader));
  2070. end
  2071. else
  2072. begin
  2073. fillchar(header,sizeof(tcoffheader),0);
  2074. header.mach:=COFF_MAGIC;
  2075. header.nsects:=ObjSectionList.Count;
  2076. header.sympos:=sympos;
  2077. header.syms:=symidx;
  2078. if win32 then
  2079. begin
  2080. {$ifndef cpu64bitaddr}
  2081. header.flag:=PE_FILE_32BIT_MACHINE or
  2082. PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  2083. {$else cpu64bitaddr}
  2084. header.flag:=PE_FILE_LINE_NUMS_STRIPPED or PE_FILE_LOCAL_SYMS_STRIPPED;
  2085. {$endif cpu64bitaddr}
  2086. end
  2087. else
  2088. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES or COFF_FLAG_NOLSYMS;
  2089. MaybeSwap(header);
  2090. FWriter.write(header,sizeof(header));
  2091. end;
  2092. { Section headers }
  2093. ObjSectionList.ForEachCall(@section_write_header,nil);
  2094. { ObjSections }
  2095. WriteSectionContent(data);
  2096. { Relocs }
  2097. ObjSectionList.ForEachCall(@section_write_relocs,nil);
  2098. { ObjSymbols }
  2099. if Sympos<>FWriter.ObjSize then
  2100. internalerror(200603051);
  2101. FWriter.writearray(FCoffSyms);
  2102. { Strings }
  2103. i:=FCoffStrs.size+4;
  2104. if source_info.endian<>target_info.endian then
  2105. i:=SwapEndian(i);
  2106. FWriter.write(i,4);
  2107. FWriter.writearray(FCoffStrs);
  2108. end;
  2109. FCoffStrs.Free;
  2110. FCoffStrs:=nil;
  2111. FCoffSyms.Free;
  2112. FCoffSyms:=nil;
  2113. end;
  2114. constructor TDJCoffObjOutput.create(AWriter:TObjectWriter);
  2115. begin
  2116. inherited createcoff(AWriter,false);
  2117. cobjdata:=TDJCoffObjData;
  2118. end;
  2119. constructor TPECoffObjOutput.create(AWriter:TObjectWriter);
  2120. begin
  2121. inherited createcoff(AWriter,true);
  2122. cobjdata:=TPECoffObjData;
  2123. end;
  2124. {****************************************************************************
  2125. TCoffObjInput
  2126. ****************************************************************************}
  2127. constructor TCoffObjInput.createcoff(awin32:boolean);
  2128. begin
  2129. inherited create;
  2130. win32:=awin32;
  2131. bigobj:=false;
  2132. FSymTbl:=nil;
  2133. end;
  2134. destructor TCoffObjInput.destroy;
  2135. begin
  2136. FCoffSyms.free;
  2137. FCoffSyms := nil;
  2138. FCoffStrs:=nil;
  2139. FSymTbl:=nil;
  2140. FSecTbl:=nil;
  2141. inherited destroy;
  2142. end;
  2143. function TCoffObjInput.GetSection(secidx:longint):TObjSection;
  2144. begin
  2145. result:=nil;
  2146. if (secidx<1) or (secidx>FSecCount) then
  2147. begin
  2148. InputError('Failed reading coff file, invalid section index');
  2149. exit;
  2150. end;
  2151. result:=FSecTbl[secidx];
  2152. end;
  2153. function TCoffObjInput.Read_str(strpos:longword):string;
  2154. begin
  2155. if (FCoffStrs=nil) or (strpos>=FCoffStrSize) or (FCoffStrs[strpos]=#0) then
  2156. Internalerror(200205172);
  2157. result:=string(PChar(@FCoffStrs[strpos]));
  2158. end;
  2159. procedure TCoffObjInput.read_relocs(s:TCoffObjSection);
  2160. var
  2161. rel : coffreloc;
  2162. rel_type : TObjRelocationType;
  2163. i : longint;
  2164. p : TObjSymbol;
  2165. begin
  2166. if s.coffrelocs=high(aword) then
  2167. begin
  2168. { If number of relocations exceeds 65535, it is stored in address field
  2169. of the first record, and includes this first fake relocation. }
  2170. FReader.read(rel,sizeof(rel));
  2171. MaybeSwap(rel);
  2172. s.coffrelocs:=rel.address-1;
  2173. if s.coffrelocs<=65535 then
  2174. InternalError(2013012503);
  2175. end;
  2176. for i:=1 to s.coffrelocs do
  2177. begin
  2178. FReader.read(rel,sizeof(rel));
  2179. MaybeSwap(rel);
  2180. case rel.reloctype of
  2181. {$ifdef arm}
  2182. IMAGE_REL_ARM_ABSOLUTE:
  2183. rel_type:=RELOC_NONE;
  2184. IMAGE_REL_ARM_ADDR32:
  2185. rel_type:=RELOC_ABSOLUTE;
  2186. IMAGE_REL_ARM_ADDR32NB:
  2187. rel_type:=RELOC_RVA;
  2188. IMAGE_REL_ARM_BRANCH24:
  2189. rel_type:=RELOC_RELATIVE_24;
  2190. IMAGE_REL_ARM_BLX24:
  2191. rel_type:=RELOC_RELATIVE_CALL;
  2192. IMAGE_REL_ARM_SECREL:
  2193. rel_type:=RELOC_SECREL32;
  2194. IMAGE_REL_ARM_BLX23T:
  2195. rel_type:=RELOC_RELATIVE_24_THUMB;
  2196. {$endif arm}
  2197. {$ifdef i386}
  2198. IMAGE_REL_I386_PCRLONG :
  2199. rel_type:=RELOC_RELATIVE;
  2200. IMAGE_REL_I386_DIR32 :
  2201. rel_type:=RELOC_ABSOLUTE;
  2202. IMAGE_REL_I386_IMAGEBASE :
  2203. rel_type:=RELOC_RVA;
  2204. IMAGE_REL_I386_SECREL32 :
  2205. rel_type:=RELOC_SECREL32;
  2206. {$endif i386}
  2207. {$ifdef x86_64}
  2208. IMAGE_REL_AMD64_ABSOLUTE:
  2209. rel_type:=RELOC_NONE;
  2210. IMAGE_REL_AMD64_REL32:
  2211. rel_type:=RELOC_RELATIVE;
  2212. IMAGE_REL_AMD64_ADDR32,
  2213. R_X86_64_32S:
  2214. rel_type:=RELOC_ABSOLUTE32;
  2215. IMAGE_REL_AMD64_ADDR64:
  2216. rel_type:=RELOC_ABSOLUTE;
  2217. IMAGE_REL_AMD64_ADDR32NB:
  2218. rel_type:=RELOC_RVA;
  2219. IMAGE_REL_AMD64_REL32_1:
  2220. rel_type:=RELOC_RELATIVE_1;
  2221. IMAGE_REL_AMD64_REL32_2:
  2222. rel_type:=RELOC_RELATIVE_2;
  2223. IMAGE_REL_AMD64_REL32_3:
  2224. rel_type:=RELOC_RELATIVE_3;
  2225. IMAGE_REL_AMD64_REL32_4:
  2226. rel_type:=RELOC_RELATIVE_4;
  2227. IMAGE_REL_AMD64_REL32_5:
  2228. rel_type:=RELOC_RELATIVE_5;
  2229. IMAGE_REL_AMD64_SECREL:
  2230. rel_type:=RELOC_SECREL32;
  2231. {$endif x86_64}
  2232. {$ifdef aarch64}
  2233. IMAGE_REL_ARM64_ABSOLUTE:
  2234. rel_type:=RELOC_NONE;
  2235. IMAGE_REL_ARM64_ADDR32:
  2236. rel_type:=RELOC_ABSOLUTE32;
  2237. IMAGE_REL_ARM64_ADDR32NB:
  2238. rel_type:=RELOC_RVA;
  2239. IMAGE_REL_ARM64_BRANCH26:
  2240. rel_type:=RELOC_RELATIVE_26;
  2241. IMAGE_REL_ARM64_PAGEBASE_REL21:
  2242. rel_type:=RELOC_ADR_PREL_PG_HI21;
  2243. IMAGE_REL_ARM64_REL21:
  2244. rel_type:=RELOC_ADR_PREL_LO21;
  2245. IMAGE_REL_ARM64_PAGEOFFSET_12A:
  2246. rel_type:=RELOC_ADD_ABS_LO12;
  2247. IMAGE_REL_ARM64_PAGEOFFSET_12L:
  2248. rel_type:=RELOC_LDST8_ABS_LO12;
  2249. IMAGE_REL_ARM64_SECREL:
  2250. rel_type:=RELOC_SECREL32;
  2251. //IMAGE_REL_ARM64_SECREL_LOW12A
  2252. //IMAGE_REL_ARM64_SECREL_HIGH12A
  2253. //IMAGE_REL_ARM64_SECREL_LOW12L
  2254. //IMAGE_REL_ARM64_TOKEN
  2255. //IMAGE_REL_ARM64_SECTION
  2256. IMAGE_REL_ARM64_ADDR64:
  2257. rel_type:=RELOC_ABSOLUTE;
  2258. //IMAGE_REL_ARM64_BRANCH19
  2259. {$endif aarch64}
  2260. else
  2261. begin
  2262. InputError('Failed reading coff file, illegal reloctype $'+system.hexstr(rel.reloctype,4));
  2263. exit;
  2264. end;
  2265. end;
  2266. p:=FSymTbl[rel.sym];
  2267. if assigned(p) then
  2268. s.addsymreloc(rel.address-s.mempos,p,rel_type)
  2269. else
  2270. begin
  2271. InputError('Failed reading coff file, can''t resolve symbol of relocation');
  2272. exit;
  2273. end;
  2274. end;
  2275. end;
  2276. procedure TCoffObjInput.read_symbols(objdata:TObjData);
  2277. var
  2278. size,
  2279. address,
  2280. nsyms,
  2281. symidx : aint;
  2282. i : longint;
  2283. sym : coffsymbol;
  2284. bosym : coffbigobjsymbol;
  2285. objsym : TObjSymbol;
  2286. bind : Tasmsymbind;
  2287. strname : string;
  2288. auxrec : array[0..sizeof(coffsymbol)-1] of byte;
  2289. boauxrec : array[0..sizeof(coffbigobjsymbol)-1] of byte;
  2290. psecrec : pcoffsectionrec;
  2291. secrec : coffsectionrec;
  2292. objsec : TObjSection;
  2293. secidx : longint;
  2294. symvalue : longword;
  2295. auxcount : byte;
  2296. symcls : byte;
  2297. comdatsel : TObjSectionComdatSelection;
  2298. { keeps string manipulations out of main routine }
  2299. procedure UnsupportedSymbolType;
  2300. begin
  2301. Comment(V_Fatal,'Unsupported COFF symbol type '+tostr(symcls)+' at index '+tostr(symidx)+' while reading '+InputFileName);
  2302. end;
  2303. begin
  2304. with TCoffObjData(objdata) do
  2305. begin
  2306. if bigobj then
  2307. nsyms:=FCoffSyms.Size div sizeof(coffbigobjsymbol)
  2308. else
  2309. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  2310. { Allocate memory for symidx -> TObjSymbol table }
  2311. SetLength(FSymTbl,nsyms);
  2312. { Load the Symbols }
  2313. FCoffSyms.Seek(0);
  2314. symidx:=0;
  2315. while (symidx<nsyms) do
  2316. begin
  2317. if bigobj then
  2318. begin
  2319. FCoffSyms.Read(bosym,sizeof(bosym));
  2320. MaybeSwap(bosym);
  2321. if bosym.Name.Offset.Zeroes<>0 then
  2322. begin
  2323. { Added for sake of global data analysis }
  2324. strname[0]:=#0;
  2325. move(bosym.Name.ShortName,strname[1],8);
  2326. strname[9]:=#0;
  2327. strname[0]:=chr(strlen(@strname[1]));
  2328. if strname='' then
  2329. internalerror(2017020301);
  2330. end
  2331. else
  2332. strname:=Read_str(bosym.Name.Offset.Offset);
  2333. secidx:=longint(bosym.SectionNumber);
  2334. symvalue:=bosym.Value;
  2335. auxcount:=bosym.NumberOfAuxSymbols;
  2336. symcls:=bosym.StorageClass;
  2337. end
  2338. else
  2339. begin
  2340. FCoffSyms.Read(sym,sizeof(sym));
  2341. MaybeSwap(sym);
  2342. if plongint(@sym.name)^<>0 then
  2343. begin
  2344. { Added for sake of global data analysis }
  2345. strname[0]:=#0;
  2346. move(sym.name,strname[1],8);
  2347. strname[9]:=#0;
  2348. strname[0]:=chr(strlen(@strname[1]));
  2349. if strname='' then
  2350. Internalerror(200205171);
  2351. end
  2352. else
  2353. strname:=Read_str(sym.strpos);
  2354. secidx:=sym.section;
  2355. symvalue:=sym.value;
  2356. auxcount:=sym.aux;
  2357. symcls:=sym.typ;
  2358. end;
  2359. bind:=AB_EXTERNAL;
  2360. size:=0;
  2361. address:=0;
  2362. objsym:=nil;
  2363. objsec:=nil;
  2364. case symcls of
  2365. COFF_SYM_GLOBAL :
  2366. begin
  2367. if secidx=0 then
  2368. begin
  2369. if symvalue=0 then
  2370. bind:=AB_EXTERNAL
  2371. else
  2372. begin
  2373. bind:=AB_COMMON;
  2374. size:=symvalue;
  2375. end;
  2376. end
  2377. else
  2378. begin
  2379. bind:=AB_GLOBAL;
  2380. objsec:=GetSection(secidx);
  2381. if symvalue>=objsec.mempos then
  2382. address:=symvalue-objsec.mempos;
  2383. end;
  2384. objsym:=CreateSymbol(strname);
  2385. objsym.bind:=bind;
  2386. objsym.typ:=AT_FUNCTION;
  2387. objsym.objsection:=objsec;
  2388. objsym.offset:=address;
  2389. objsym.size:=size;
  2390. end;
  2391. COFF_SYM_LABEL,
  2392. COFF_SYM_LOCAL :
  2393. begin
  2394. { do not add constants (section=-1) }
  2395. if secidx<>-1 then
  2396. begin
  2397. objsec:=GetSection(secidx);
  2398. if symvalue>=objsec.mempos then
  2399. address:=symvalue-objsec.mempos;
  2400. objsym:=CreateSymbol(strname);
  2401. objsym.bind:=AB_LOCAL;
  2402. objsym.typ:=AT_FUNCTION;
  2403. objsym.objsection:=objsec;
  2404. objsym.offset:=address;
  2405. objsym.size:=size;
  2406. end;
  2407. end;
  2408. COFF_SYM_SECTION :
  2409. begin
  2410. { GetSection checks that index is in range }
  2411. objsec:=GetSection(secidx);
  2412. if assigned(objsec) then
  2413. begin
  2414. if symvalue>=objsec.mempos then
  2415. address:=symvalue-objsec.mempos;
  2416. objsym:=CreateSymbol(strname);
  2417. objsym.bind:=AB_LOCAL;
  2418. objsym.typ:=AT_FUNCTION;
  2419. objsym.objsection:=objsec;
  2420. objsym.offset:=address;
  2421. objsym.size:=size;
  2422. end;
  2423. end;
  2424. COFF_SYM_FUNCTION,
  2425. COFF_SYM_FILE :
  2426. ;
  2427. else
  2428. UnsupportedSymbolType;
  2429. end;
  2430. FSymTbl[symidx]:=objsym;
  2431. { read aux records }
  2432. { handle COMDAT symbols }
  2433. if (symcls=COFF_SYM_LOCAL) and (auxcount=1) and (symvalue=0) and (oso_comdat in objsym.objsection.SecOptions) then
  2434. begin
  2435. if bigobj then
  2436. begin
  2437. FCoffSyms.Read(boauxrec,sizeof(boauxrec));
  2438. psecrec:=pcoffsectionrec(@boauxrec[0]);
  2439. end
  2440. else
  2441. begin
  2442. FCoffSyms.Read(auxrec,sizeof(auxrec));
  2443. psecrec:=pcoffsectionrec(@auxrec);
  2444. end;
  2445. secrec:=psecrec^;
  2446. MaybeSwap(secrec);
  2447. case secrec.select of
  2448. IMAGE_COMDAT_SELECT_NODUPLICATES:
  2449. comdatsel:=oscs_none;
  2450. IMAGE_COMDAT_SELECT_ANY:
  2451. comdatsel:=oscs_any;
  2452. IMAGE_COMDAT_SELECT_SAME_SIZE:
  2453. comdatsel:=oscs_same_size;
  2454. IMAGE_COMDAT_SELECT_EXACT_MATCH:
  2455. comdatsel:=oscs_exact_match;
  2456. IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  2457. comdatsel:=oscs_associative;
  2458. IMAGE_COMDAT_SELECT_LARGEST:
  2459. comdatsel:=oscs_largest;
  2460. else begin
  2461. comdatsel:=oscs_none;
  2462. { there are object files out here that have comdat symbols
  2463. including an associative section, but with a comdat selection
  2464. of 0; it seems that other linkers just ignore those... }
  2465. if secrec.select<>0 then
  2466. Message2(link_e_comdat_select_unsupported,inttostr(secrec.select),objsym.objsection.name);
  2467. end;
  2468. end;
  2469. if comdatsel in [oscs_associative] then
  2470. { only temporary }
  2471. Comment(V_Error,'Associative COMDAT sections are not yet supported (symbol: '+objsym.objsection.Name+')')
  2472. else if (comdatsel=oscs_associative) and (secrec.assoc=0) then
  2473. Message1(link_e_comdat_associative_section_expected,objsym.objsection.name)
  2474. else if (objsym.objsection.ComdatSelection<>oscs_none) and (comdatsel<>oscs_none) and (objsym.objsection.ComdatSelection<>comdatsel) then
  2475. Message2(link_e_comdat_not_matching,objsym.objsection.Name,objsym.Name)
  2476. else
  2477. begin
  2478. objsym.objsection.ComdatSelection:=comdatsel;
  2479. if (secrec.assoc<>0) and not assigned(objsym.objsection.AssociativeSection) then
  2480. begin
  2481. objsym.objsection.AssociativeSection:=GetSection(secrec.assoc-1);
  2482. if not assigned(objsym.objsection.AssociativeSection) then
  2483. Message1(link_e_comdat_associative_section_not_found,objsym.objsection.Name);
  2484. end;
  2485. end;
  2486. dec(auxcount);
  2487. inc(symidx);
  2488. end;
  2489. for i:=1 to auxcount do
  2490. begin
  2491. if bigobj then
  2492. FCoffSyms.Read(boauxrec,sizeof(boauxrec))
  2493. else
  2494. FCoffSyms.Read(auxrec,sizeof(auxrec));
  2495. inc(symidx);
  2496. end;
  2497. inc(symidx);
  2498. end;
  2499. end;
  2500. end;
  2501. procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
  2502. begin
  2503. with TCoffObjSection(p) do
  2504. begin
  2505. { Skip debug sections }
  2506. if (oso_debug in secoptions) and
  2507. (cs_link_strip in current_settings.globalswitches) and
  2508. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  2509. exit;
  2510. if coffrelocs>0 then
  2511. begin
  2512. FReader.Seek(coffrelocpos);
  2513. read_relocs(TCoffObjSection(p));
  2514. end;
  2515. end;
  2516. end;
  2517. function TCoffObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  2518. var
  2519. secalign : longint;
  2520. secofs,
  2521. strpos,
  2522. i : longint;
  2523. sympos,
  2524. symcount,
  2525. symsize,
  2526. code : longint;
  2527. objsec : TCoffObjSection;
  2528. secoptions : TObjSectionOptions;
  2529. header : tcoffheader;
  2530. boheader : tcoffbigobjheader;
  2531. sechdr : tcoffsechdr;
  2532. secname : string;
  2533. secnamebuf : array[0..15] of char;
  2534. begin
  2535. FReader:=AReader;
  2536. InputFileName:=AReader.FileName;
  2537. objdata:=CObjData.Create(InputFileName);
  2538. result:=false;
  2539. boheader:=default(tcoffbigobjheader);
  2540. FCoffSyms:=TDynamicArray.Create(SymbolMaxGrow);
  2541. with TCoffObjData(objdata) do
  2542. begin
  2543. { Read COFF header }
  2544. if not AReader.read(header,sizeof(tcoffheader)) then
  2545. begin
  2546. InputError('Can''t read COFF Header');
  2547. exit;
  2548. end;
  2549. MaybeSwap(header);
  2550. if (header.mach=0) and (header.nsects=$ffff) then
  2551. begin
  2552. { either a library or big obj COFF }
  2553. AReader.seek(0);
  2554. if not AReader.read(boheader,sizeof(boheader)) then
  2555. begin
  2556. InputError('Can''t read Big Obj COFF Header');
  2557. exit;
  2558. end;
  2559. MaybeSwap(boheader);
  2560. if CompareByte(boheader.UUID,COFF_BIG_OBJ_MAGIC,length(boheader.uuid))<>0 then
  2561. begin
  2562. { ToDo: this should be treated as a library }
  2563. InputError('Illegal Big Obj COFF Magic');
  2564. exit;
  2565. end;
  2566. if boheader.Version<>COFF_BIG_OBJ_VERSION then
  2567. begin
  2568. InputError('Illegal Big Obj COFF Version');
  2569. exit;
  2570. end;
  2571. if boheader.Machine<>COFF_MAGIC then
  2572. begin
  2573. InputError('Illegal COFF Machine type');
  2574. exit;
  2575. end;
  2576. bigobj:=true;
  2577. end
  2578. else if header.mach<>COFF_MAGIC then
  2579. begin
  2580. InputError('Illegal COFF Magic');
  2581. exit;
  2582. end;
  2583. {$ifdef arm}
  2584. eVCobj:=header.flag=$100;
  2585. {$endif arm}
  2586. { ObjSymbols }
  2587. if bigobj then
  2588. begin
  2589. sympos:=longint(boheader.PointerToSymbolTable);
  2590. symcount:=longint(boheader.NumberOfSymbols);
  2591. symsize:=sizeof(CoffBigObjSymbol);
  2592. end
  2593. else
  2594. begin
  2595. sympos:=longint(header.sympos);
  2596. symcount:=longint(header.syms);
  2597. symsize:=sizeof(CoffSymbol);
  2598. end;
  2599. AReader.Seek(sympos);
  2600. if not AReader.ReadArray(FCoffSyms,symcount*symsize) then
  2601. begin
  2602. InputError('Error reading coff symbol table');
  2603. exit;
  2604. end;
  2605. { Strings }
  2606. if not AReader.Read(FCoffStrSize,4) then
  2607. begin
  2608. InputError('Error reading COFF string table');
  2609. exit;
  2610. end;
  2611. if (FCoffStrSize>4) then
  2612. begin
  2613. { allocate an extra byte and null-terminate }
  2614. SetLength(FCoffStrs,FCoffStrSize+1);
  2615. FCoffStrs[FCoffStrSize]:=#0;
  2616. for i:=0 to 3 do
  2617. FCoffStrs[i]:=#0;
  2618. if not AReader.Read(FCoffStrs[4],FCoffStrSize-4) then
  2619. begin
  2620. InputError('Error reading COFF string table');
  2621. exit;
  2622. end;
  2623. end;
  2624. { Section headers }
  2625. { Allocate SecIdx -> TObjSection table, secidx is 1-based }
  2626. if bigobj then
  2627. FSecCount:=longint(boheader.NumberOfSections)
  2628. else
  2629. FSecCount:=header.nsects;
  2630. SetLength(FSecTbl,(FSecCount+1));
  2631. if bigobj then
  2632. secofs:=sizeof(tcoffbigobjheader)
  2633. else
  2634. secofs:=sizeof(tcoffheader)+header.opthdr;
  2635. AReader.Seek(secofs);
  2636. for i:=1 to FSecCount do
  2637. begin
  2638. if not AReader.read(sechdr,sizeof(sechdr)) then
  2639. begin
  2640. InputError('Error reading COFF Section Headers');
  2641. exit;
  2642. end;
  2643. MaybeSwap(sechdr);
  2644. move(sechdr.name,secnamebuf,8);
  2645. secnamebuf[8]:=#0;
  2646. secname:=strpas(secnamebuf);
  2647. if secname[1]='/' then
  2648. begin
  2649. if secname[2]='/' then
  2650. begin
  2651. if not decodeBase64(copy(secname,3,8),strpos) then
  2652. begin
  2653. InputError('Error reading COFF Section Headers');
  2654. secname:='error';
  2655. end
  2656. else
  2657. secname:=Read_str(strpos);
  2658. end
  2659. else
  2660. begin
  2661. Val(Copy(secname,2,8),strpos,code);
  2662. if code=0 then
  2663. secname:=Read_str(strpos)
  2664. else
  2665. begin
  2666. InputError('Error reading COFF Section Headers');
  2667. secname:='error';
  2668. end;
  2669. end;
  2670. end;
  2671. if win32 then
  2672. pedecodesechdrflags(secname,sechdr.flags,secoptions,secalign)
  2673. else
  2674. begin
  2675. secoptions:=djdecodesechdrflags(secname,sechdr.flags);
  2676. secalign:=sizeof(pint);
  2677. end;
  2678. if (Length(secname)>3) and (secname[2] in ['e','f','i','p','r']) then
  2679. begin
  2680. if (Pos('.edata',secname)=1) or
  2681. (Pos('.rsrc',secname)=1) or
  2682. ((target_info.system=system_arm_wince) and (Pos('.pdata',secname)=1)) or
  2683. (Pos('.fpc',secname)=1) then
  2684. include(secoptions,oso_keep);
  2685. if (Pos('.idata',secname)=1) then
  2686. begin
  2687. { TODO: idata keep can maybe replaced with grouping of text and idata}
  2688. include(secoptions,oso_keep);
  2689. secname:=secname + '.' + ExtractFileName(InputFileName);
  2690. end;
  2691. end;
  2692. objsec:=TCoffObjSection(createsection(secname,secalign,secoptions,false));
  2693. FSecTbl[i]:=objsec;
  2694. if not win32 then
  2695. objsec.mempos:=sechdr.rvaofs;
  2696. objsec.orgmempos:=sechdr.rvaofs;
  2697. objsec.coffrelocs:=sechdr.nrelocs;
  2698. if win32 then
  2699. begin
  2700. if (sechdr.flags and PE_SCN_LNK_NRELOC_OVFL)<>0 then
  2701. objsec.coffrelocs:=high(aword);
  2702. end;
  2703. objsec.coffrelocpos:=sechdr.relocpos;
  2704. objsec.datapos:=sechdr.datapos;
  2705. objsec.Size:=sechdr.dataSize;
  2706. end;
  2707. { Insert all ObjSymbols }
  2708. read_symbols(objdata);
  2709. { Section Data }
  2710. ReadSectionContent(objdata);
  2711. { Relocs }
  2712. ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
  2713. end;
  2714. FCoffStrs:=nil;
  2715. FCoffSyms.Free;
  2716. FCoffSyms:=nil;
  2717. result:=true;
  2718. end;
  2719. constructor TDJCoffObjInput.create;
  2720. begin
  2721. inherited createcoff(false);
  2722. cobjdata:=TDJCoffObjData;
  2723. end;
  2724. constructor TPECoffObjInput.create;
  2725. begin
  2726. inherited createcoff(true);
  2727. cobjdata:=TPECoffObjData;
  2728. end;
  2729. {****************************************************************************
  2730. TCoffexeoutput
  2731. ****************************************************************************}
  2732. constructor TCoffexeoutput.createcoff(awin32:boolean);
  2733. begin
  2734. inherited create;
  2735. win32:=awin32;
  2736. if target_info.system in [system_x86_64_win64,system_aarch64_win64] then
  2737. MaxMemPos:=$FFFFFFFF
  2738. else
  2739. if target_info.system in systems_wince then
  2740. MaxMemPos:=$1FFFFFF
  2741. else
  2742. MaxMemPos:=$7FFFFFFF;
  2743. end;
  2744. procedure TCoffexeoutput.write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
  2745. var
  2746. sym : coffsymbol;
  2747. begin
  2748. FillChar(sym,sizeof(sym),0);
  2749. if length(name)>8 then
  2750. begin
  2751. sym.strpos:=FCoffStrs.size+4;
  2752. FCoffStrs.writestr(name);
  2753. FCoffStrs.writestr(#0);
  2754. end
  2755. else
  2756. move(name[1],sym.name,length(name));
  2757. sym.value:=value;
  2758. sym.section:=section;
  2759. sym.typ:=typ;
  2760. sym.aux:=aux;
  2761. MaybeSwap(sym);
  2762. FWriter.write(sym,sizeof(sym));
  2763. end;
  2764. procedure TCoffexeoutput.globalsyms_write_symbol(p:TObject;arg:pointer);
  2765. var
  2766. secval,
  2767. value : aint;
  2768. globalval : byte;
  2769. exesec : TExeSection;
  2770. begin
  2771. if not assigned(texesymbol(p).objsymbol) then
  2772. internalerror(2006030505);
  2773. with texesymbol(p).objsymbol do
  2774. begin
  2775. if assigned(objsection) then
  2776. exesec:=TExeSection(objsection.exesection)
  2777. else
  2778. exesec:=nil;
  2779. { There is no exesection defined for special internal symbols
  2780. like __image_base__ }
  2781. if assigned(exesec) then
  2782. begin
  2783. secval:=exesec.secsymidx;
  2784. if win32 then
  2785. value:=address-exesec.mempos
  2786. else
  2787. value:=address;
  2788. end
  2789. else
  2790. begin
  2791. secval:=-1;
  2792. value:=address;
  2793. end;
  2794. if bind=AB_LOCAL then
  2795. globalval:=COFF_SYM_LOCAL
  2796. else
  2797. globalval:=COFF_SYM_GLOBAL;
  2798. { reloctype address to the section in the executable }
  2799. write_symbol(name,value,secval,globalval,0);
  2800. end;
  2801. end;
  2802. procedure TCoffexeoutput.ExeSectionList_write_header(p:TObject;arg:pointer);
  2803. var
  2804. sechdr : tcoffsechdr;
  2805. s : string;
  2806. strpos : aword;
  2807. begin
  2808. with tExeSection(p) do
  2809. begin
  2810. fillchar(sechdr,sizeof(sechdr),0);
  2811. s:=name;
  2812. if length(s)>8 then
  2813. begin
  2814. strpos:=FCoffStrs.size+4;
  2815. FCoffStrs.writestr(s);
  2816. FCoffStrs.writestr(#0);
  2817. s:='/'+ToStr(strpos);
  2818. end;
  2819. move(s[1],sechdr.name,length(s));
  2820. if win32 then
  2821. begin
  2822. sechdr.rvaofs:=mempos;
  2823. sechdr.vsize:=Size;
  2824. { sechdr.dataSize is size of initialized data, rounded up to FileAlignment
  2825. (so it can be greater than VirtualSize). Must be zero for sections that
  2826. do not contain initialized data. }
  2827. if (oso_data in SecOptions) then
  2828. sechdr.datasize:=Align(Size,SectionDataAlign);
  2829. end
  2830. else
  2831. begin
  2832. if not (oso_debug in SecOptions) then
  2833. begin
  2834. sechdr.rvaofs:=mempos;
  2835. sechdr.vsize:=mempos;
  2836. end;
  2837. sechdr.datasize:=Size;
  2838. end;
  2839. if (Size>0) then
  2840. sechdr.datapos:=datapos-datapos_offset;
  2841. sechdr.nrelocs:=0;
  2842. sechdr.relocpos:=0;
  2843. if win32 then
  2844. begin
  2845. if (target_info.system in systems_nativent) and
  2846. (apptype = app_native) then
  2847. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign) or PE_SCN_MEM_NOT_PAGED
  2848. else
  2849. sechdr.flags:=peencodesechdrflags(SecOptions,SecAlign);
  2850. { some flags are invalid in executables, reset them }
  2851. sechdr.flags:=sechdr.flags and
  2852. not(PE_SCN_LNK_INFO or PE_SCN_LNK_REMOVE or
  2853. PE_SCN_LNK_COMDAT or PE_SCN_ALIGN_MASK);
  2854. end
  2855. else
  2856. sechdr.flags:=djencodesechdrflags(SecOptions);
  2857. MaybeSwap(sechdr);
  2858. FWriter.write(sechdr,sizeof(sechdr));
  2859. end;
  2860. end;
  2861. procedure TCoffexeoutput.ExeSectionList_pass2_header(p:TObject;arg:pointer);
  2862. begin
  2863. with TExeSection(p) do
  2864. begin
  2865. { The debuginfo sections should already be stripped }
  2866. { if (ExeWriteMode=ewm_exeonly) and
  2867. (oso_debug in SecOptions) then
  2868. internalerror(200801161); }
  2869. inc(plongint(arg)^);
  2870. secsymidx:=plongint(arg)^;
  2871. end;
  2872. end;
  2873. function tcoffexeoutput.totalheadersize:longword;
  2874. var
  2875. stubsize,
  2876. optheadersize : longword;
  2877. begin
  2878. if win32 then
  2879. begin
  2880. stubsize:=sizeof(win32stub)+sizeof(pemagic);
  2881. optheadersize:=sizeof(tcoffpeoptheader);
  2882. end
  2883. else
  2884. begin
  2885. stubsize:=sizeof(go32v2stub);
  2886. optheadersize:=sizeof(coffdjoptheader);
  2887. end;
  2888. result:=stubsize+sizeof(tcoffheader)+optheadersize;
  2889. end;
  2890. procedure tcoffexeoutput.MemPos_Header;
  2891. begin
  2892. { calculate start positions after the headers }
  2893. currmempos:=totalheadersize+sizeof(tcoffsechdr)*longword(ExeSectionList.Count-2);
  2894. end;
  2895. procedure tcoffexeoutput.DataPos_Header;
  2896. begin
  2897. { retrieve amount of sections }
  2898. nsects:=0;
  2899. ExeSectionList.ForEachCall(@ExeSectionList_pass2_header,@nsects);
  2900. { calculate start positions after the headers }
  2901. currdatapos:=totalheadersize+longword(nsects)*sizeof(tcoffsechdr);
  2902. end;
  2903. procedure tcoffexeoutput.DataPos_Symbols;
  2904. begin
  2905. inherited DataPos_Symbols;
  2906. { Calculating symbols position and size }
  2907. nsyms:=ExeSymbolList.Count;
  2908. sympos:=Align(CurrDataPos,SectionDataAlign);
  2909. inc(CurrDataPos,sizeof(coffsymbol)*nsyms);
  2910. end;
  2911. function TCoffexeoutput.writedata:boolean;
  2912. var
  2913. i : longword;
  2914. header : tcoffheader;
  2915. djoptheader : coffdjoptheader;
  2916. peoptheader : tcoffpeoptheader;
  2917. textExeSec,
  2918. dataExeSec,
  2919. bssExeSec,
  2920. idataExeSec : TExeSection;
  2921. hassymbols,
  2922. writeDbgStrings : boolean;
  2923. procedure UpdateDataDir(const secname:string;idx:longint);
  2924. var
  2925. exesec : TExeSection;
  2926. begin
  2927. exesec:=FindExeSection(secname);
  2928. if assigned(exesec) then
  2929. begin
  2930. peoptheader.DataDirectory[idx].vaddr:=exesec.mempos;
  2931. peoptheader.DataDirectory[idx].size:=exesec.Size;
  2932. end;
  2933. end;
  2934. procedure UpdateImports;
  2935. var
  2936. exesec: TExeSection;
  2937. objsec, iat_start, iat_end, ilt_start: TObjSection;
  2938. i: longint;
  2939. begin
  2940. exesec:=FindExeSection('.idata');
  2941. if exesec=nil then
  2942. exit;
  2943. iat_start:=nil;
  2944. iat_end:=nil;
  2945. ilt_start:=nil;
  2946. for i:=0 to exesec.ObjSectionList.Count-1 do
  2947. begin
  2948. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2949. if (ilt_start=nil) and (Pos('.idata$4',objsec.Name)=1) then
  2950. ilt_start:=objsec;
  2951. if Pos('.idata$5',objsec.Name)=1 then
  2952. begin
  2953. if iat_start=nil then
  2954. iat_start:=objsec;
  2955. end
  2956. else
  2957. if Assigned(iat_start) then
  2958. begin
  2959. iat_end:=objsec;
  2960. Break;
  2961. end;
  2962. end;
  2963. peoptheader.DataDirectory[PE_DATADIR_IDATA].vaddr:=exesec.mempos;
  2964. if Assigned(ilt_start) then
  2965. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=ilt_start.mempos-exesec.mempos
  2966. else { should not happen }
  2967. peoptheader.DataDirectory[PE_DATADIR_IDATA].size:=exesec.Size;
  2968. if Assigned(iat_start) and Assigned(iat_end) then
  2969. begin
  2970. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].vaddr:=iat_start.mempos;
  2971. peoptheader.DataDirectory[PE_DATADIR_IMPORTADDRESSTABLE].size:=iat_end.mempos-iat_start.mempos;
  2972. end;
  2973. end;
  2974. procedure UpdateTlsDataDir;
  2975. var
  2976. {callbacksection : TExeSection;}
  2977. tlsexesymbol: TExeSymbol;
  2978. tlssymbol: TObjSymbol;
  2979. callbackexesymbol: TExeSymbol;
  2980. //callbacksymbol: TObjSymbol;
  2981. begin
  2982. { according to GNU ld,
  2983. the callback routines should be placed into .CRT$XL*
  2984. sections, and the thread local variables in .tls
  2985. __tls_start__ and __tls_end__ symbols
  2986. should be used for the initialized part,
  2987. which we do not support yet. }
  2988. { For now, we only pass the address of the __tls_used
  2989. asm symbol into PE_DATADIR_TLS with the correct
  2990. size of this table (different for win32/win64 }
  2991. tlsexesymbol:=texesymbol(ExeSymbolList.Find(
  2992. target_info.Cprefix+'_tls_used'));
  2993. if assigned(tlsexesymbol) then
  2994. begin
  2995. tlssymbol:=tlsexesymbol.ObjSymbol;
  2996. peoptheader.DataDirectory[PE_DATADIR_TLS].vaddr:=tlssymbol.address;
  2997. { sizeof(TlsDirectory) is different on host and target when cross-compiling }
  2998. peoptheader.DataDirectory[PE_DATADIR_TLS].size:=TLSDIR_SIZE;
  2999. if IsSharedLibrary then
  3000. begin
  3001. { Here we should reset __FPC_tls_callbacks value to nil }
  3002. callbackexesymbol:=texesymbol(ExeSymbolList.Find(
  3003. '__FPC_tls_callbacks'));
  3004. if assigned (callbackexesymbol) then
  3005. begin
  3006. //callbacksymbol:=callbackexesymbol.ObjSymbol;
  3007. end;
  3008. end;
  3009. end;
  3010. end;
  3011. begin
  3012. result:=false;
  3013. FCoffStrs:=TDynamicArray.Create(StrsMaxGrow);
  3014. textExeSec:=FindExeSection('.text');
  3015. dataExeSec:=FindExeSection('.data');
  3016. bssExeSec:=FindExeSection('.bss');
  3017. if not assigned(TextExeSec) or
  3018. not assigned(DataExeSec) then
  3019. internalerror(2006022302);
  3020. { do we need to write symbols? }
  3021. hassymbols:=(ExeWriteMode=ewm_dbgonly) or
  3022. (
  3023. (ExeWriteMode=ewm_exefull) and
  3024. not(cs_link_strip in current_settings.globalswitches)
  3025. );
  3026. writeDbgStrings:=hassymbols or ((ExeWriteMode=ewm_exeonly) and (cs_link_separate_dbg_file in current_settings.globalswitches));
  3027. { Stub }
  3028. if win32 then
  3029. begin
  3030. FWriter.write(win32stub,sizeof(win32stub));
  3031. FWriter.write(pemagic,sizeof(pemagic));
  3032. end
  3033. else
  3034. FWriter.write(go32v2stub,sizeof(go32v2stub));
  3035. { Initial header, will be updated later }
  3036. fillchar(header,sizeof(header),0);
  3037. header.mach:=COFF_MAGIC;
  3038. header.nsects:=nsects;
  3039. if writeDbgStrings then
  3040. header.sympos:=sympos-datapos_offset;
  3041. if hassymbols then
  3042. header.syms:=nsyms;
  3043. if win32 then
  3044. header.opthdr:=sizeof(tcoffpeoptheader)
  3045. else
  3046. header.opthdr:=sizeof(coffdjoptheader);
  3047. if win32 then
  3048. begin
  3049. header.flag:=PE_FILE_EXECUTABLE_IMAGE or PE_FILE_LINE_NUMS_STRIPPED;
  3050. if target_info.system in [system_x86_64_win64,system_aarch64_win64] then
  3051. header.flag:=header.flag or PE_FILE_LARGE_ADDRESS_AWARE
  3052. else
  3053. header.flag:=header.flag or PE_FILE_32BIT_MACHINE;
  3054. if IsSharedLibrary then
  3055. header.flag:=header.flag or PE_FILE_DLL;
  3056. if FindExeSection('.reloc')=nil then
  3057. header.flag:=header.flag or PE_FILE_RELOCS_STRIPPED;
  3058. if (FindExeSection('.stab')=nil) and
  3059. (FindExeSection('.debug_info')=nil) and
  3060. (FindExeSection('.gnu_debuglink')=nil) then
  3061. header.flag:=header.flag or PE_FILE_DEBUG_STRIPPED;
  3062. if not hassymbols then
  3063. header.flag:=header.flag or PE_FILE_LOCAL_SYMS_STRIPPED;
  3064. if SetPEFlagsSetExplicity then
  3065. header.flag:=header.flag or peflags;
  3066. end
  3067. else
  3068. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  3069. MaybeSwap(header);
  3070. FWriter.write(header,sizeof(header));
  3071. { Optional COFF Header }
  3072. if win32 then
  3073. begin
  3074. fillchar(peoptheader,sizeof(peoptheader),0);
  3075. peoptheader.magic:=COFF_OPT_MAGIC;
  3076. peoptheader.MajorLinkerVersion:=ord(version_nr)-ord('0');
  3077. peoptheader.MinorLinkerVersion:=(ord(release_nr)-ord('0'))*10 + (ord(patch_nr)-ord('0'));
  3078. peoptheader.tsize:=TextExeSec.Size;
  3079. peoptheader.dsize:=DataExeSec.Size;
  3080. if assigned(BSSExeSec) then
  3081. peoptheader.bsize:=BSSExeSec.Size;
  3082. peoptheader.text_start:=TextExeSec.mempos;
  3083. {$ifndef cpu64bitaddr}
  3084. peoptheader.data_start:=DataExeSec.mempos;
  3085. {$endif cpu64bitaddr}
  3086. peoptheader.entry:=EntrySym.Address;
  3087. peoptheader.ImageBase:=ImageBase;
  3088. peoptheader.SectionAlignment:=SectionMemAlign;
  3089. peoptheader.FileAlignment:=SectionDataAlign;
  3090. if SetPEOSVersionSetExplicitely then
  3091. begin
  3092. peoptheader.MajorOperatingSystemVersion:=peosversionmajor;
  3093. peoptheader.MinorOperatingSystemVersion:=peosversionminor;
  3094. end
  3095. else
  3096. begin
  3097. peoptheader.MajorOperatingSystemVersion:=4;
  3098. peoptheader.MinorOperatingSystemVersion:=0;
  3099. end;
  3100. if SetPEUserVersionSetExplicitely then
  3101. begin
  3102. peoptheader.MajorImageVersion:=peuserversionmajor;
  3103. peoptheader.MinorImageVersion:=peuserversionminor;
  3104. end
  3105. else
  3106. begin
  3107. peoptheader.MajorImageVersion:=dllmajor;
  3108. peoptheader.MinorImageVersion:=dllminor;
  3109. end;
  3110. if SetPESubSysVersionSetExplicitely then
  3111. begin
  3112. peoptheader.MajorSubsystemVersion:=pesubsysversionmajor;
  3113. peoptheader.MinorSubsystemVersion:=pesubsysversionminor;
  3114. end
  3115. else
  3116. begin
  3117. if target_info.system in systems_wince then
  3118. peoptheader.MajorSubsystemVersion:=3
  3119. else
  3120. peoptheader.MajorSubsystemVersion:=4;
  3121. peoptheader.MinorSubsystemVersion:=0;
  3122. end;
  3123. peoptheader.Win32Version:=0;
  3124. peoptheader.SizeOfImage:=Align(CurrMemPos,SectionMemAlign);
  3125. peoptheader.SizeOfHeaders:=textExeSec.DataPos;
  3126. peoptheader.CheckSum:=0;
  3127. if (target_info.system in systems_nativent) and (not IsSharedLibrary or (apptype = app_native)) then
  3128. { Although I did not really test this, it seems that Subsystem is
  3129. not checked in DLLs except for maybe drivers}
  3130. peoptheader.Subsystem:=PE_SUBSYSTEM_NATIVE
  3131. else
  3132. if target_info.system in systems_wince then
  3133. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CE_GUI
  3134. else
  3135. if apptype=app_gui then
  3136. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_GUI
  3137. else
  3138. peoptheader.Subsystem:=PE_SUBSYSTEM_WINDOWS_CUI;
  3139. if target_info.system in [system_aarch64_win64] then
  3140. peoptheader.DllCharacteristics:=PE_DLLCHARACTERISTICS_DYNAMIC_BASE or
  3141. PE_DLLCHARACTERISTICS_NX_COMPAT or
  3142. PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA
  3143. else
  3144. peoptheader.DllCharacteristics:=0;
  3145. if SetPEOptFlagsSetExplicity then
  3146. peoptheader.DllCharacteristics:=peoptheader.DllCharacteristics or peoptflags;
  3147. peoptheader.SizeOfStackReserve:=stacksize;
  3148. peoptheader.SizeOfStackCommit:=$1000;
  3149. if MinStackSizeSetExplicity then
  3150. peoptheader.SizeOfStackCommit:=minstacksize;
  3151. if MaxStackSizeSetExplicity then
  3152. peoptheader.SizeOfStackReserve:=maxstacksize;
  3153. peoptheader.SizeOfHeapReserve:=$100000;
  3154. peoptheader.SizeOfHeapCommit:=$1000;
  3155. peoptheader.NumberOfRvaAndSizes:=PE_DATADIR_ENTRIES;
  3156. UpdateImports;
  3157. UpdateTlsDataDir;
  3158. UpdateDataDir('.edata',PE_DATADIR_EDATA);
  3159. UpdateDataDir('.rsrc',PE_DATADIR_RSRC);
  3160. UpdateDataDir('.pdata',PE_DATADIR_PDATA);
  3161. UpdateDataDir('.reloc',PE_DATADIR_RELOC);
  3162. MaybeSwap(peoptheader);
  3163. FWriter.write(peoptheader,sizeof(peoptheader));
  3164. end
  3165. else
  3166. begin
  3167. fillchar(djoptheader,sizeof(djoptheader),0);
  3168. djoptheader.magic:=COFF_OPT_MAGIC;
  3169. djoptheader.tsize:=TextExeSec.Size;
  3170. djoptheader.dsize:=DataExeSec.Size;
  3171. if assigned(BSSExeSec) then
  3172. djoptheader.bsize:=BSSExeSec.Size;
  3173. djoptheader.text_start:=TextExeSec.mempos;
  3174. djoptheader.data_start:=DataExeSec.mempos;
  3175. djoptheader.entry:=EntrySym.Address;
  3176. MaybeSwap(djoptheader);
  3177. FWriter.write(djoptheader,sizeof(djoptheader));
  3178. end;
  3179. { For some unknown reason WM 6.1 requires .idata section to be read only.
  3180. Otherwise it refuses to load DLLs greater than 64KB.
  3181. Earlier versions of WinCE load DLLs regardless of .idata flags. }
  3182. if target_info.system in systems_wince then
  3183. begin
  3184. idataExeSec:=FindExeSection('.idata');
  3185. if idataExeSec<>nil then
  3186. idataExeSec.SecOptions:=idataExeSec.SecOptions - [oso_write];
  3187. end;
  3188. { Section headers }
  3189. ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
  3190. { Section data }
  3191. WriteExeSectionContent;
  3192. { Align after the last section }
  3193. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  3194. { Optional Symbols }
  3195. if SymPos<>FWriter.Size then
  3196. internalerror(200602252);
  3197. if hassymbols then
  3198. ExeSymbolList.ForEachCall(@globalsyms_write_symbol,nil);
  3199. if writeDbgStrings then
  3200. begin
  3201. { Strings }
  3202. i:=FCoffStrs.size+4;
  3203. if source_info.endian<>target_info.endian then
  3204. i:=SwapEndian(i);
  3205. FWriter.write(i,4);
  3206. FWriter.writearray(FCoffStrs);
  3207. end;
  3208. { Release }
  3209. FCoffStrs.Free;
  3210. FCoffStrs := nil;
  3211. result:=true;
  3212. end;
  3213. function IdataObjSectionCompare(Item1, Item2: Pointer): Integer;
  3214. var
  3215. I1 : TObjSection absolute Item1;
  3216. I2 : TObjSection absolute Item2;
  3217. begin
  3218. Result:=CompareStr(I1.Name,I2.Name);
  3219. end;
  3220. procedure TCoffexeoutput.Order_ObjSectionList(ObjSectionList: TFPObjectList;const aPattern:string);
  3221. begin
  3222. { Sort sections having '$' in the name, that's how PECOFF documentation
  3223. tells to handle them. However, look for '$' in the pattern, not in section
  3224. names, because the latter often get superfluous '$' due to mangling. }
  3225. if Pos('$',aPattern)>0 then
  3226. ObjSectionList.Sort(@IdataObjSectionCompare);
  3227. end;
  3228. constructor TDJCoffexeoutput.create;
  3229. begin
  3230. inherited createcoff(false);
  3231. datapos_offset:=sizeof(go32v2stub);
  3232. CExeSection:=TExeSection;
  3233. CObjData:=TDJCoffObjData;
  3234. end;
  3235. procedure TDJCoffexeoutput.MemPos_Header;
  3236. begin
  3237. { Headers are not loaded, first 4K page is reserved }
  3238. CurrMemPos:=$1000;
  3239. end;
  3240. constructor TPECoffexeoutput.create;
  3241. begin
  3242. inherited createcoff(true);
  3243. CExeSection:=TExeSection;
  3244. CObjData:=TPECoffObjData;
  3245. end;
  3246. procedure TPECoffexeoutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  3247. var
  3248. exesec:TExeSection;
  3249. objsec,textsec:TObjSection;
  3250. objreloc:TObjRelocation;
  3251. i,j:longint;
  3252. begin
  3253. if not (target_info.system in [system_x86_64_win64,system_aarch64_win64]) then
  3254. exit;
  3255. exesec:=FindExeSection('.pdata');
  3256. if exesec=nil then
  3257. exit;
  3258. for i:=0 to exesec.ObjSectionList.Count-1 do
  3259. begin
  3260. objsec:=TObjSection(exesec.ObjSectionList[i]);
  3261. if objsec.Used then
  3262. continue;
  3263. j:=0;
  3264. while j<objsec.ObjRelocations.Count do
  3265. begin
  3266. objreloc:=TObjRelocation(objsec.ObjRelocations[j]);
  3267. if objreloc.symbol=nil then
  3268. InternalError(2013041201);
  3269. textsec:=objreloc.symbol.objsection;
  3270. if textsec.used then
  3271. begin
  3272. WorkList.Add(objsec);
  3273. objsec.used:=true;
  3274. { The exact algorithm for non-smartlinked .pdata sections
  3275. is subject for refinement. Extreme cases are:
  3276. - several disjoint .pdata entries for a function, if function
  3277. is complex or if compiler splits it into chunks,
  3278. - single .pdata section referencing several .text sections,
  3279. may need to remove irrelevant parts like BFD does for
  3280. .eh_frame sections. }
  3281. break;
  3282. end;
  3283. if target_info.system=system_aarch64_win64 then
  3284. inc(j,2)
  3285. else
  3286. inc(j,3);
  3287. end;
  3288. end;
  3289. end;
  3290. procedure TPECoffexeoutput.AfterUnusedSectionRemoval;
  3291. var
  3292. basedllname : string;
  3293. procedure StartImport(const dllname:string);
  3294. begin
  3295. if assigned(exemap) then
  3296. begin
  3297. exemap.Add('');
  3298. exemap.Add('Importing from DLL '+dllname);
  3299. end;
  3300. basedllname:=ExtractFileName(dllname);
  3301. { idata2 }
  3302. idata2objsection.writereloc_internal(idata4objsection,idata4objsection.size,sizeof(longint),RELOC_RVA);
  3303. idata2objsection.writezeros(2*sizeof(longint));
  3304. idata2objsection.writereloc_internal(idata7objsection,idata7objsection.size,sizeof(longint),RELOC_RVA);
  3305. idata2objsection.writereloc_internal(idata5objsection,idata5objsection.size,sizeof(longint),RELOC_RVA);
  3306. { idata7 }
  3307. idata7objsection.writestr(basedllname);
  3308. end;
  3309. procedure EndImport;
  3310. begin
  3311. { idata4 }
  3312. idata4objsection.writezeros(sizeof(longint));
  3313. if target_info.system in systems_peoptplus then
  3314. idata4objsection.writezeros(sizeof(longint));
  3315. { idata5 }
  3316. idata5objsection.writezeros(sizeof(longint));
  3317. if target_info.system in systems_peoptplus then
  3318. idata5objsection.writezeros(sizeof(longint));
  3319. end;
  3320. function AddImport(const afuncname,amangledname:string; AOrdNr:longint;isvar:boolean):TObjSymbol;
  3321. const
  3322. {$if defined(arm)}
  3323. jmpopcode : array[0..7] of byte = (
  3324. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  3325. $00,$f0,$9c,$e5 // ldr pc, [ip]
  3326. );
  3327. {$elseif defined(aarch64)}
  3328. jmpopcode : array[0..11] of byte = (
  3329. $70,$00,$00,$58, // ldr ip0, .+12
  3330. $10,$02,$40,$F9, // ldr ip0, [ip0]
  3331. $00,$02,$1F,$D6 // br ip0
  3332. );
  3333. {$else}
  3334. jmpopcode : array[0..1] of byte = (
  3335. $ff,$25
  3336. );
  3337. {$endif}
  3338. nopopcodes : array[0..1] of byte = (
  3339. $90,$90
  3340. );
  3341. var
  3342. ordint: dword;
  3343. word_ordint: word;
  3344. procedure WriteTableEntry(objsec:TObjSection);
  3345. begin
  3346. if AOrdNr <= 0 then
  3347. begin
  3348. objsec.writereloc_internal(idata6objsection,idata6objsection.size,sizeof(longint),RELOC_RVA);
  3349. if target_info.system in systems_peoptplus then
  3350. objsec.writezeros(sizeof(longint));
  3351. end
  3352. else
  3353. begin
  3354. { import by ordinal }
  3355. ordint:=AOrdNr;
  3356. if target_info.system in systems_peoptplus then
  3357. begin
  3358. if source_info.endian<>target_info.endian then
  3359. ordint:=SwapEndian(ordint);
  3360. objsec.write(ordint,4);
  3361. ordint:=$80000000;
  3362. if source_info.endian<>target_info.endian then
  3363. ordint:=SwapEndian(ordint);
  3364. objsec.write(ordint,4);
  3365. end
  3366. else
  3367. begin
  3368. ordint:=ordint or $80000000;
  3369. if source_info.endian<>target_info.endian then
  3370. ordint:=SwapEndian(ordint);
  3371. objsec.write(ordint,4);
  3372. end;
  3373. end;
  3374. end;
  3375. begin
  3376. result:=nil;
  3377. if assigned(exemap) then
  3378. begin
  3379. if AOrdNr <= 0 then
  3380. exemap.Add(' Importing Function '+afuncname)
  3381. else
  3382. exemap.Add(' Importing Function '+afuncname+' (OrdNr='+tostr(AOrdNr)+')');
  3383. end;
  3384. { idata4, import lookup table }
  3385. WriteTableEntry(idata4objsection);
  3386. { idata5, import address table }
  3387. internalobjdata.SetSection(idata5objsection);
  3388. if isvar then
  3389. result:=internalobjdata.SymbolDefine(amangledname,AB_GLOBAL,AT_DATA)
  3390. else
  3391. begin
  3392. internalobjdata.SetSection(textobjsection);
  3393. textobjsection.writezeros(align_aword(textobjsection.size,16)-textobjsection.size);
  3394. result:=internalobjdata.SymbolDefine('_'+amangledname,AB_GLOBAL,AT_FUNCTION);
  3395. textobjsection.write(jmpopcode,sizeof(jmpopcode));
  3396. {$if defined(x86_64)}
  3397. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_RELATIVE);
  3398. {$elseif defined(aarch64)}
  3399. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,8,RELOC_ABSOLUTE);
  3400. {$else}
  3401. textobjsection.writereloc_internal(idata5objsection,idata5objsection.size,4,RELOC_ABSOLUTE32);
  3402. {$endif x86_64 or aarch64}
  3403. textobjsection.write(nopopcodes,align(textobjsection.size,qword(sizeof(nopopcodes)))-textobjsection.size);
  3404. end;
  3405. { idata5 section data }
  3406. WriteTableEntry(idata5objsection);
  3407. if (AOrdNr<=0) then
  3408. begin
  3409. { index hint, function name, null terminator and align }
  3410. word_ordint:=abs(AOrdNr);
  3411. if source_info.endian<>target_info.endian then
  3412. word_ordint:=SwapEndian(word_ordint);
  3413. idata6objsection.write(word_ordint,2);
  3414. idata6objsection.writestr(afuncname);
  3415. idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
  3416. end;
  3417. end;
  3418. var
  3419. i,j : longint;
  3420. ImportLibrary : TImportLibrary;
  3421. ImportSymbol : TImportSymbol;
  3422. exesym : TExeSymbol;
  3423. newdll : boolean;
  3424. begin
  3425. for i:=0 to FImports.Count-1 do
  3426. begin
  3427. ImportLibrary:=TImportLibrary(FImports[i]);
  3428. newdll:=False;
  3429. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  3430. begin
  3431. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  3432. exesym:=ImportSymbol.CachedExeSymbol;
  3433. if assigned(exesym) and
  3434. exesym.Used then
  3435. begin
  3436. if (not newdll) then
  3437. StartImport(ImportLibrary.Name);
  3438. newdll:=True;
  3439. exesym.objsymbol:=AddImport(ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  3440. end;
  3441. end;
  3442. if newdll then
  3443. EndImport;
  3444. end;
  3445. FixupSymbols;
  3446. end;
  3447. procedure TPECoffexeoutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  3448. var
  3449. i,j: longint;
  3450. ImportLibrary: TImportLibrary;
  3451. ImportSymbol: TImportSymbol;
  3452. exesym: TExeSymbol;
  3453. begin
  3454. { Here map import symbols to exe symbols and create necessary sections.
  3455. Actual import generation is done after unused sections (and symbols) are removed. }
  3456. FImports:=ImportLibraryList;
  3457. textobjsection:=internalObjData.CreateSection(sec_code);
  3458. textobjsection.SecOptions:=[oso_keep];
  3459. idata2objsection:=internalObjData.CreateSection(sec_idata2);
  3460. idata2objsection.SecOptions:=[oso_keep];
  3461. idata4objsection:=internalObjData.CreateSection(sec_idata4);
  3462. idata4objsection.SecOptions:=[oso_keep];
  3463. idata5objsection:=internalObjData.CreateSection(sec_idata5);
  3464. idata5objsection.SecOptions:=[oso_keep];
  3465. idata6objsection:=internalObjData.CreateSection(sec_idata6);
  3466. idata6objsection.SecOptions:=[oso_keep];
  3467. idata7objsection:=internalObjData.CreateSection(sec_idata7);
  3468. idata7objsection.SecOptions:=[oso_keep];
  3469. for i:=0 to ImportLibraryList.Count-1 do
  3470. begin
  3471. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  3472. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  3473. begin
  3474. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  3475. exesym:=TExeSymbol(ExeSymbolList.Find(ImportSymbol.MangledName));
  3476. if assigned(exesym) and
  3477. (exesym.State<>symstate_defined) then
  3478. begin
  3479. ImportSymbol.CachedExeSymbol:=exesym;
  3480. exesym.State:=symstate_defined;
  3481. end;
  3482. end;
  3483. end;
  3484. PackUnresolvedExeSymbols('after DLL imports');
  3485. end;
  3486. procedure TPECoffexeoutput.GenerateRelocs;
  3487. var
  3488. pgaddr, hdrpos : longword;
  3489. procedure FinishBlock;
  3490. var
  3491. p,len : longint;
  3492. begin
  3493. if hdrpos = longword(-1) then
  3494. exit;
  3495. p:=0;
  3496. internalobjdata.writebytes(p,align(internalobjdata.CurrObjSec.size,4)-internalobjdata.CurrObjSec.size);
  3497. p:=internalObjData.CurrObjSec.Data.Pos;
  3498. internalObjData.CurrObjSec.Data.seek(hdrpos+4);
  3499. len:=p-hdrpos;
  3500. if source_info.endian<>target_info.endian then
  3501. len:=SwapEndian(len);
  3502. internalObjData.CurrObjSec.Data.write(len,4);
  3503. internalObjData.CurrObjSec.Data.seek(p);
  3504. hdrpos:=longword(-1);
  3505. end;
  3506. var
  3507. exesec : TExeSection;
  3508. objsec : TObjSection;
  3509. objreloc : TObjRelocation;
  3510. i,j,k : longint;
  3511. offset : longword;
  3512. w: word;
  3513. begin
  3514. if not RelocSection or FRelocsGenerated then
  3515. exit;
  3516. exesec:=FindExeSection('.reloc');
  3517. if exesec=nil then
  3518. exit;
  3519. objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_keep]);
  3520. exesec.AddObjSection(objsec);
  3521. pgaddr:=longword(-1);
  3522. hdrpos:=longword(-1);
  3523. for i:=0 to ExeSectionList.Count-1 do
  3524. begin
  3525. exesec:=TExeSection(ExeSectionList[i]);
  3526. for j:=0 to exesec.ObjSectionList.count-1 do
  3527. begin
  3528. objsec:=TObjSection(exesec.ObjSectionList[j]);
  3529. { create relocs only for sections which are loaded in memory }
  3530. if not (oso_load in objsec.SecOptions) then
  3531. continue;
  3532. for k:=0 to objsec.ObjRelocations.Count-1 do
  3533. begin
  3534. objreloc:=TObjRelocation(objsec.ObjRelocations[k]);
  3535. if not (objreloc.typ in [{$ifdef cpu64bitaddr}RELOC_ABSOLUTE32,{$endif cpu64bitaddr}RELOC_ABSOLUTE]) then
  3536. continue;
  3537. offset:=objsec.MemPos+objreloc.dataoffset;
  3538. if (offset<pgaddr) and (pgaddr<>longword(-1)) then
  3539. Internalerror(2007062701);
  3540. if (offset-pgaddr>=4096) or (pgaddr=longword(-1)) then
  3541. begin
  3542. FinishBlock;
  3543. pgaddr:=(offset div 4096)*4096;
  3544. hdrpos:=internalObjData.CurrObjSec.Data.Pos;
  3545. if source_info.endian<>target_info.endian then
  3546. pgaddr:=SwapEndian(pgaddr);
  3547. internalObjData.writebytes(pgaddr,4);
  3548. { Reserving space for block size. The size will be written later in FinishBlock }
  3549. internalObjData.writebytes(pgaddr,4);
  3550. end;
  3551. {$ifdef cpu64bitaddr}
  3552. if objreloc.typ = RELOC_ABSOLUTE then
  3553. w:=IMAGE_REL_BASED_DIR64
  3554. else
  3555. {$endif cpu64bitaddr}
  3556. w:=IMAGE_REL_BASED_HIGHLOW;
  3557. w:=(w shl 12) or (offset-pgaddr);
  3558. if source_info.endian<>target_info.endian then
  3559. w:=SwapEndian(w);
  3560. internalObjData.writebytes(w,2);
  3561. end;
  3562. end;
  3563. end;
  3564. FinishBlock;
  3565. FRelocsGenerated:=true;
  3566. end;
  3567. procedure TPECoffexeoutput.MemPos_Start;
  3568. var
  3569. exesec : TExeSection;
  3570. begin
  3571. if RelocSection then
  3572. begin
  3573. exesec:=FindExeSection('.reloc');
  3574. if exesec=nil then
  3575. InternalError(2012072403);
  3576. exesec.Disabled:=false;
  3577. end;
  3578. inherited;
  3579. end;
  3580. procedure TPECoffexeoutput.MemPos_ExeSection(const aname:string);
  3581. begin
  3582. if aname='.reloc' then
  3583. GenerateRelocs;
  3584. inherited;
  3585. end;
  3586. {****************************************************************************
  3587. TDJCoffAssembler
  3588. ****************************************************************************}
  3589. constructor TDJCoffAssembler.Create(info: pasminfo; smart:boolean);
  3590. begin
  3591. inherited;
  3592. CObjOutput:=TDJCoffObjOutput;
  3593. CInternalAr:=tarobjectwriter;
  3594. end;
  3595. {****************************************************************************
  3596. TPECoffAssembler
  3597. ****************************************************************************}
  3598. constructor TPECoffAssembler.Create(info: pasminfo; smart:boolean);
  3599. begin
  3600. inherited;
  3601. CObjOutput:=TPECoffObjOutput;
  3602. CInternalAr:=tarobjectwriter;
  3603. end;
  3604. {*****************************************************************************
  3605. DLLReader
  3606. *****************************************************************************}
  3607. {$ifdef win32}
  3608. var
  3609. Wow64DisableWow64FsRedirection : function (var OldValue : pointer) : boolean;stdcall;
  3610. Wow64RevertWow64FsRedirection : function (OldValue : pointer) : boolean;stdcall;
  3611. {$endif win32}
  3612. function ReadDLLImports(const dllname:string;readdllproc:Treaddllproc):boolean;
  3613. var
  3614. DLLReader : TObjectReader;
  3615. DosHeader : array[0..$7f] of byte;
  3616. PEMagic : array[0..3] of byte;
  3617. Header : TCoffHeader;
  3618. peheader : tcoffpeoptheader;
  3619. NameOfs,
  3620. newheaderofs : longword;
  3621. FuncName : string;
  3622. expdir : TPECoffExpDir;
  3623. i : longint;
  3624. found : boolean;
  3625. header_ok : boolean;
  3626. sechdr : tCoffSecHdr;
  3627. {$ifdef win32}
  3628. p : pointer;
  3629. {$endif win32}
  3630. begin
  3631. result:=false;
  3632. fillchar(sechdr,sizeof(sechdr),0);
  3633. {$ifdef win32}
  3634. if (target_info.system=system_x86_64_win64) and
  3635. assigned(Wow64DisableWow64FsRedirection) then
  3636. Wow64DisableWow64FsRedirection(p);
  3637. {$endif win32}
  3638. DLLReader:=TObjectReader.Create;
  3639. DLLReader.OpenFile(dllname);
  3640. {$ifdef win32}
  3641. if (target_info.system=system_x86_64_win64) and
  3642. assigned(Wow64RevertWow64FsRedirection) then
  3643. Wow64RevertWow64FsRedirection(p);
  3644. {$endif win32}
  3645. if not DLLReader.Read(DosHeader,sizeof(DosHeader)) or
  3646. (DosHeader[0]<>$4d) or (DosHeader[1]<>$5a) then
  3647. begin
  3648. Comment(V_Error,'Invalid DLL '+dllname+', Dos Header invalid');
  3649. exit;
  3650. end;
  3651. newheaderofs:=cardinal(DosHeader[$3c]) or (DosHeader[$3d] shl 8) or (DosHeader[$3e] shl 16) or (DosHeader[$3f] shl 24);
  3652. DLLReader.Seek(newheaderofs);
  3653. if not DLLReader.Read(PEMagic,sizeof(PEMagic)) or
  3654. (PEMagic[0]<>$50) or (PEMagic[1]<>$45) or (PEMagic[2]<>$00) or (PEMagic[3]<>$00) then
  3655. begin
  3656. Comment(V_Error,'Invalid DLL '+dllname+': invalid magic code');
  3657. exit;
  3658. end;
  3659. header_ok:=DLLReader.Read(Header,sizeof(TCoffHeader));
  3660. MaybeSwap(Header);
  3661. if not header_ok or
  3662. (Header.mach<>COFF_MAGIC) or
  3663. (Header.opthdr<>sizeof(tcoffpeoptheader)) then
  3664. begin
  3665. Comment(V_Error,'Invalid DLL '+dllname+', invalid header size');
  3666. exit;
  3667. end;
  3668. { Read optheader }
  3669. DLLreader.Read(peheader,sizeof(tcoffpeoptheader));
  3670. MaybeSwap(peheader);
  3671. { Section headers }
  3672. found:=false;
  3673. for i:=1 to header.nsects do
  3674. begin
  3675. if not DLLreader.read(sechdr,sizeof(sechdr)) then
  3676. begin
  3677. Comment(V_Error,'Error reading coff file '+DLLName);
  3678. exit;
  3679. end;
  3680. MaybeSwap(sechdr);
  3681. if (sechdr.rvaofs<=peheader.DataDirectory[PE_DATADIR_EDATA].vaddr) and
  3682. (peheader.DataDirectory[PE_DATADIR_EDATA].vaddr<sechdr.rvaofs+sechdr.vsize) then
  3683. begin
  3684. found:=true;
  3685. break;
  3686. end;
  3687. end;
  3688. if not found then
  3689. begin
  3690. Comment(V_Warning,'DLL '+DLLName+' does not contain any exports');
  3691. exit;
  3692. end;
  3693. { Process edata }
  3694. DLLReader.Seek(sechdr.datapos+peheader.DataDirectory[PE_DATADIR_EDATA].vaddr-sechdr.rvaofs);
  3695. DLLReader.Read(expdir,sizeof(expdir));
  3696. MaybeSwap(expdir);
  3697. for i:=0 to expdir.NumNames-1 do
  3698. begin
  3699. DLLReader.Seek(sechdr.datapos+expdir.AddrNames-sechdr.rvaofs+i*4);
  3700. DLLReader.Read(NameOfs,4);
  3701. Dec(NameOfs,sechdr.rvaofs);
  3702. if {(NameOfs<0) or}
  3703. (NameOfs>sechdr.vsize) then
  3704. begin
  3705. Comment(V_Error,'DLL does contains invalid exports');
  3706. break;
  3707. end;
  3708. { Read Function name from DLL, prepend _ and terminate with #0 }
  3709. DLLReader.Seek(sechdr.datapos+NameOfs);
  3710. DLLReader.Read((@FuncName[1])^,sizeof(FuncName)-3);
  3711. FuncName[sizeof(FuncName)-1]:=#0;
  3712. FuncName[0]:=chr(Strlen(@FuncName[1]));
  3713. readdllproc(DLLName,FuncName);
  3714. end;
  3715. DLLReader.Free;
  3716. DLLReader := nil;
  3717. end;
  3718. {$ifdef arm}
  3719. function COFF_MAGIC: word;
  3720. begin
  3721. if GenerateThumb2Code and (current_settings.cputype>=cpu_armv7) then
  3722. COFF_MAGIC:=$1c4 // IMAGE_FILE_MACHINE_ARMNT
  3723. else
  3724. COFF_MAGIC:=$1c0; // IMAGE_FILE_MACHINE_ARM
  3725. end;
  3726. {$endif arm}
  3727. {*****************************************************************************
  3728. Initialize
  3729. *****************************************************************************}
  3730. {$ifdef i386}
  3731. const
  3732. as_i386_coff_info : tasminfo =
  3733. (
  3734. id : as_i386_coff;
  3735. idtxt : 'COFF';
  3736. asmbin : '';
  3737. asmcmd : '';
  3738. supported_targets : [system_i386_go32v2];
  3739. flags : [af_outputbinary,af_smartlink_sections];
  3740. labelprefix : '.L';
  3741. labelmaxlen : -1;
  3742. comment : '';
  3743. dollarsign: '$';
  3744. );
  3745. as_i386_pecoff_info : tasminfo =
  3746. (
  3747. id : as_i386_pecoff;
  3748. idtxt : 'PECOFF';
  3749. asmbin : '';
  3750. asmcmd : '';
  3751. supported_targets : [system_i386_win32,system_i386_nativent];
  3752. flags : [af_outputbinary,af_smartlink_sections];
  3753. labelprefix : '.L';
  3754. labelmaxlen : -1;
  3755. comment : '';
  3756. dollarsign: '$';
  3757. );
  3758. as_i386_pecoffwdosx_info : tasminfo =
  3759. (
  3760. id : as_i386_pecoffwdosx;
  3761. idtxt : 'PEWDOSX';
  3762. asmbin : '';
  3763. asmcmd : '';
  3764. supported_targets : [system_i386_wdosx];
  3765. flags : [af_outputbinary];
  3766. labelprefix : '.L';
  3767. labelmaxlen : -1;
  3768. comment : '';
  3769. dollarsign: '$';
  3770. );
  3771. as_i386_pecoffwince_info : tasminfo =
  3772. (
  3773. id : as_i386_pecoffwince;
  3774. idtxt : 'PECOFFWINCE';
  3775. asmbin : '';
  3776. asmcmd : '';
  3777. supported_targets : [system_i386_wince];
  3778. flags : [af_outputbinary,af_smartlink_sections];
  3779. labelprefix : '.L';
  3780. labelmaxlen : -1;
  3781. comment : '';
  3782. dollarsign: '$';
  3783. );
  3784. {$endif i386}
  3785. {$ifdef x86_64}
  3786. const
  3787. as_x86_64_pecoff_info : tasminfo =
  3788. (
  3789. id : as_x86_64_pecoff;
  3790. idtxt : 'PECOFF';
  3791. asmbin : '';
  3792. asmcmd : '';
  3793. supported_targets : [system_x86_64_win64];
  3794. flags : [af_outputbinary,af_smartlink_sections];
  3795. labelprefix : '.L';
  3796. labelmaxlen : -1;
  3797. comment : '';
  3798. dollarsign: '$';
  3799. );
  3800. {$endif x86_64}
  3801. {$ifdef arm}
  3802. const
  3803. as_arm_pecoffwince_info : tasminfo =
  3804. (
  3805. id : as_arm_pecoffwince;
  3806. idtxt : 'PECOFFWINCE';
  3807. asmbin : '';
  3808. asmcmd : '';
  3809. supported_targets : [system_arm_wince];
  3810. flags : [af_outputbinary,af_smartlink_sections];
  3811. labelprefix : '.L';
  3812. labelmaxlen : -1;
  3813. comment : '';
  3814. dollarsign: '$';
  3815. );
  3816. {$endif arm}
  3817. {$ifdef win32}
  3818. procedure SetupProcVars;
  3819. var
  3820. hinstLib : THandle;
  3821. begin
  3822. Wow64DisableWow64FsRedirection:=nil;
  3823. Wow64RevertWow64FsRedirection:=nil;
  3824. hinstLib:=LoadLibrary('kernel32.dll');
  3825. if hinstLib<>0 then
  3826. begin
  3827. pointer(Wow64DisableWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64DisableWow64FsRedirection');
  3828. pointer(Wow64RevertWow64FsRedirection):=GetProcAddress(hinstLib,'Wow64RevertWow64FsRedirection');
  3829. FreeLibrary(hinstLib);
  3830. end;
  3831. end;
  3832. {$endif win32}
  3833. initialization
  3834. {$ifdef i386}
  3835. RegisterAssembler(as_i386_coff_info,TDJCoffAssembler);
  3836. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  3837. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  3838. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  3839. {$endif i386}
  3840. {$ifdef x86_64}
  3841. RegisterAssembler(as_x86_64_pecoff_info,TPECoffAssembler);
  3842. {$endif x86_64}
  3843. {$ifdef arm}
  3844. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  3845. {$endif arm}
  3846. {$ifdef win32}
  3847. SetupProcVars;
  3848. {$endif win32}
  3849. end.