ogcoff.pas 133 KB

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