ogcoff.pas 125 KB

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