ogcoff.pas 135 KB

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