ogcoff.pas 150 KB

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