ogcoff.pas 150 KB

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