ogcoff.pas 125 KB

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