ogcoff.pas 120 KB

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