ogcoff.pas 150 KB

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