ogcoff.pas 149 KB

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