2
0

ogcoff.pas 149 KB

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