ogcoff.pas 125 KB

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