ogcoff.pas 133 KB

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