ogcoff.pas 125 KB

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