ogcoff.pas 120 KB

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