ogcoff.pas 123 KB

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