ogbase.pas 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the base stuff for binary object file writers
  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 ogbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. cclasses,
  24. { targets }
  25. systems,globtype,
  26. { outputwriters }
  27. owbase,
  28. { assembler }
  29. aasmbase;
  30. type
  31. TObjSection = class;
  32. TObjData = class;
  33. TExeSection = class;
  34. TExeSymbol = class;
  35. TExeOutput = class;
  36. TObjRelocationType = (
  37. { Relocation to absolute address }
  38. RELOC_ABSOLUTE,
  39. {$ifdef x86_64}
  40. { 32bit Relocation to absolute address }
  41. RELOC_ABSOLUTE32,
  42. { 64 bit coff only }
  43. RELOC_RELATIVE_1,
  44. RELOC_RELATIVE_2,
  45. RELOC_RELATIVE_3,
  46. RELOC_RELATIVE_4,
  47. RELOC_RELATIVE_5,
  48. { PIC }
  49. RELOC_GOTPCREL,
  50. RELOC_PLT32,
  51. {$endif x86_64}
  52. {$ifdef i386}
  53. { PIC }
  54. RELOC_GOTPC,
  55. RELOC_GOT32,
  56. RELOC_PLT32,
  57. {$endif i386}
  58. {$ifdef arm}
  59. RELOC_RELATIVE_24,
  60. {$endif arm}
  61. { Relative relocation }
  62. RELOC_RELATIVE,
  63. { PECoff (Windows) RVA relocation }
  64. RELOC_RVA,
  65. { PECoff (Windows) section relocation, required by DWARF2 debug info }
  66. RELOC_SECREL32,
  67. { Generate a 0 value at the place of the relocation,
  68. this is used to remove unused vtable entries }
  69. RELOC_ZERO,
  70. { No relocation is needed. It is used in ARM object files.
  71. Also internal linker use this reloc to make virtual (not real)
  72. links to some sections }
  73. RELOC_NONE,
  74. { Darwin relocation, using PAIR }
  75. RELOC_PIC_PAIR,
  76. { Untranslated target-specific value }
  77. RELOC_RAW
  78. );
  79. {$ifndef x86_64}
  80. const
  81. RELOC_ABSOLUTE32 = RELOC_ABSOLUTE;
  82. {$endif x86_64}
  83. const
  84. { stab types }
  85. N_GSYM = $20;
  86. N_STSYM = 38; { initialized const }
  87. N_LCSYM = 40; { non initialized variable}
  88. N_Function = $24; { function or const }
  89. N_TextLine = $44;
  90. N_DataLine = $46;
  91. N_BssLine = $48;
  92. N_RSYM = $40; { register variable }
  93. N_LSYM = $80;
  94. N_tsym = 160;
  95. N_SourceFile = $64;
  96. N_IncludeFile = $84;
  97. N_BINCL = $82;
  98. N_EINCL = $A2;
  99. N_LBRAC = $C0;
  100. N_EXCL = $C2;
  101. N_RBRAC = $E0;
  102. { GNU extensions }
  103. debuglinkname='.gnu_debuglink';
  104. { TObjRelocation.flags }
  105. { 'ftype' field contains platform-specific value }
  106. rf_raw = 1;
  107. { relocation must be added to dynamic list }
  108. rf_dynamic = 2;
  109. type
  110. TObjSectionOption = (
  111. { Has Data available in the file }
  112. oso_Data,
  113. { Is loaded into memory }
  114. oso_load,
  115. { Writable }
  116. oso_write,
  117. { Contains executable instructions }
  118. oso_executable,
  119. { Never discard section }
  120. oso_keep,
  121. { Procedure Linkage Table specific treatment }
  122. oso_plt,
  123. { Contains debug info and can be stripped }
  124. oso_debug,
  125. { Contains only strings }
  126. oso_strings,
  127. { Ignore this section }
  128. oso_disabled,
  129. { Must be cloned when writing separate debug file }
  130. oso_debug_copy
  131. );
  132. TObjSectionOptions = set of TObjSectionOption;
  133. TObjSymbol = class(TFPHashObject)
  134. public
  135. bind : TAsmsymbind;
  136. typ : TAsmsymtype;
  137. { Current assemble pass, used to detect duplicate labels }
  138. pass : byte;
  139. objsection : TObjSection;
  140. symidx : longint;
  141. offset,
  142. size : aword;
  143. { Used for external and common solving during linking }
  144. exesymbol : TExeSymbol;
  145. { Darwin asm is using indirect symbols resolving }
  146. indsymbol : TObjSymbol;
  147. constructor create(AList:TFPHashObjectList;const AName:string);
  148. function address:aword;
  149. procedure SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  150. end;
  151. { Stabs is common for all targets }
  152. TObjStabEntry=packed record
  153. strpos : longint;
  154. ntype : byte;
  155. nother : byte;
  156. ndesc : word;
  157. nvalue : longint;
  158. end;
  159. PObjStabEntry=^TObjStabEntry;
  160. TObjRelocation = class
  161. private
  162. function GetType:TObjRelocationType;
  163. procedure SetType(v:TObjRelocationType);
  164. public
  165. DataOffset,
  166. orgsize : aword; { COFF: original size of the symbol to relocate }
  167. { ELF: explicit addend }
  168. symbol : TObjSymbol;
  169. objsection : TObjSection; { only used if symbol=nil }
  170. ftype : byte;
  171. size : byte;
  172. flags : byte;
  173. constructor CreateSymbol(ADataOffset:aword;s:TObjSymbol;Atyp:TObjRelocationType);
  174. constructor CreateSection(ADataOffset:aword;aobjsec:TObjSection;Atyp:TObjRelocationType);
  175. constructor CreateRaw(ADataOffset:aword;s:TObjSymbol;ARawType:byte);
  176. function TargetName:TSymStr;
  177. property typ: TObjRelocationType read GetType write SetType;
  178. end;
  179. TObjSection = class(TFPHashObject)
  180. private
  181. FData : TDynamicArray;
  182. FSecOptions : TObjSectionOptions;
  183. FCachedFullName : pshortstring;
  184. procedure SetSecOptions(Aoptions:TObjSectionOptions);
  185. public
  186. ObjData : TObjData;
  187. index : longword; { index of section in section headers }
  188. SecSymIdx : longint; { index for the section in symtab }
  189. SecAlign : shortint; { alignment of the section }
  190. { section Data }
  191. Size,
  192. DataPos,
  193. MemPos : aword;
  194. DataAlignBytes : shortint;
  195. { Relocations (=references) to other sections }
  196. ObjRelocations : TFPObjectList;
  197. { executable linking }
  198. ExeSection : TExeSection;
  199. USed : Boolean;
  200. VTRefList : TFPObjectList;
  201. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);virtual;
  202. destructor destroy;override;
  203. function write(const d;l:aword):aword;
  204. { writes string plus zero byte }
  205. function writestr(const s:string):aword;
  206. function WriteZeros(l:longword):aword;
  207. { writes content of s without null termination }
  208. function WriteBytes(const s:string):aword;
  209. procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);virtual;
  210. function setmempos(mpos:qword):qword;
  211. procedure setDatapos(var dpos:aword);
  212. procedure alloc(l:aword);
  213. procedure addsymReloc(ofs:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  214. procedure addsectionReloc(ofs:aword;aobjsec:TObjSection;Reloctype:TObjRelocationType);
  215. procedure addrawReloc(ofs:aword;p:TObjSymbol;RawReloctype:byte);
  216. procedure ReleaseData;
  217. function FullName:string;
  218. property Data:TDynamicArray read FData;
  219. property SecOptions:TObjSectionOptions read FSecOptions write SetSecOptions;
  220. end;
  221. TObjSectionClass = class of TObjSection;
  222. TString80 = string[80];
  223. TObjData = class(TLinkedListItem)
  224. private
  225. FCurrObjSec : TObjSection;
  226. FObjSectionList : TFPHashObjectList;
  227. FCObjSection : TObjSectionClass;
  228. { Symbols that will be defined in this object file }
  229. FObjSymbolList : TFPHashObjectList;
  230. FCachedAsmSymbolList : TFPObjectList;
  231. { Special info sections that are written to during object generation }
  232. FStabsObjSec,
  233. FStabStrObjSec : TObjSection;
  234. procedure section_reset(p:TObject;arg:pointer);
  235. procedure section_afteralloc(p:TObject;arg:pointer);
  236. procedure section_afterwrite(p:TObject;arg:pointer);
  237. protected
  238. FName : TString80;
  239. property CObjSection:TObjSectionClass read FCObjSection write FCObjSection;
  240. public
  241. CurrPass : byte;
  242. ExecStack : boolean;
  243. constructor create(const n:string);virtual;
  244. destructor destroy;override;
  245. { Sections }
  246. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
  247. function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;virtual;
  248. function sectiontype2align(atype:TAsmSectiontype):shortint;virtual;
  249. function createsection(atype:TAsmSectionType;const aname:string='';aorder:TAsmSectionOrder=secorder_default):TObjSection;
  250. function createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean=true):TObjSection;virtual;
  251. procedure CreateDebugSections;virtual;
  252. function findsection(const aname:string):TObjSection;
  253. procedure setsection(asec:TObjSection);
  254. { Symbols }
  255. function createsymbol(const aname:string):TObjSymbol;
  256. function symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  257. function symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  258. function symbolref(asmsym:TAsmSymbol):TObjSymbol;
  259. function symbolref(const aname:string):TObjSymbol;
  260. procedure ResetCachedAsmSymbols;
  261. { Allocation }
  262. procedure alloc(len:aword);
  263. procedure allocalign(len:shortint);
  264. procedure writebytes(const Data;len:aword);
  265. procedure writeReloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);virtual;abstract;
  266. procedure beforealloc;virtual;
  267. procedure beforewrite;virtual;
  268. procedure afteralloc;virtual;
  269. procedure afterwrite;virtual;
  270. procedure resetsections;
  271. procedure layoutsections(var datapos:aword);
  272. property Name:TString80 read FName;
  273. property CurrObjSec:TObjSection read FCurrObjSec;
  274. property ObjSymbolList:TFPHashObjectList read FObjSymbolList;
  275. property ObjSectionList:TFPHashObjectList read FObjSectionList;
  276. property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
  277. property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
  278. end;
  279. TObjDataClass = class of TObjData;
  280. TObjOutput = class
  281. private
  282. FCObjData : TObjDataClass;
  283. protected
  284. { writer }
  285. FWriter : TObjectwriter;
  286. function writeData(Data:TObjData):boolean;virtual;abstract;
  287. property CObjData : TObjDataClass read FCObjData write FCObjData;
  288. procedure WriteSectionContent(Data:TObjData);
  289. public
  290. constructor create(AWriter:TObjectWriter);virtual;
  291. destructor destroy;override;
  292. function newObjData(const n:string):TObjData;
  293. function startObjectfile(const fn:string):boolean;
  294. function writeobjectfile(Data:TObjData):boolean;
  295. procedure exportsymbol(p:TObjSymbol);
  296. property Writer:TObjectWriter read FWriter;
  297. end;
  298. TObjOutputClass=class of TObjOutput;
  299. TObjInput = class
  300. private
  301. FCObjData : TObjDataClass;
  302. protected
  303. { reader }
  304. FReader : TObjectReader;
  305. InputFileName : string;
  306. property CObjData : TObjDataClass read FCObjData write FCObjData;
  307. procedure ReadSectionContent(Data:TObjData);
  308. public
  309. constructor create;virtual;
  310. destructor destroy;override;
  311. function newObjData(const n:string):TObjData;
  312. function ReadObjData(AReader:TObjectreader;Data:TObjData):boolean;virtual;abstract;
  313. class function CanReadObjData(AReader:TObjectreader):boolean;virtual;
  314. procedure inputerror(const s : string);
  315. end;
  316. TObjInputClass=class of TObjInput;
  317. TVTableEntry=record
  318. ObjRelocation : TObjRelocation;
  319. orgreloctype,
  320. orgrelocflags : byte;
  321. Enabled,
  322. Used : Boolean;
  323. end;
  324. PVTableEntry=^TVTableEntry;
  325. TExeVTable = class
  326. private
  327. procedure CheckIdx(VTableIdx:longint);
  328. public
  329. ExeSymbol : TExeSymbol;
  330. EntryCnt : Longint;
  331. EntryArray : PVTableEntry;
  332. Consolidated : Boolean;
  333. ChildList : TFPObjectList;
  334. constructor Create(AExeSymbol:TExeSymbol);
  335. destructor Destroy;override;
  336. procedure AddChild(vt:TExeVTable);
  337. procedure AddEntry(VTableIdx:Longint);
  338. procedure SetVTableSize(ASize:longint);
  339. function VTableRef(VTableIdx:Longint):TObjRelocation;
  340. end;
  341. TSymbolState = (symstate_undefined,symstate_defined,symstate_common,symstate_defweak,symstate_dynamic);
  342. TExeSymbol = class(TFPHashObject)
  343. ObjSymbol : TObjSymbol;
  344. State : TSymbolState;
  345. { Used for vmt references optimization }
  346. VTable : TExeVTable;
  347. end;
  348. TExeSection = class(TFPHashObject)
  349. private
  350. FSecSymIdx : longint;
  351. FObjSectionList : TFPObjectList;
  352. public
  353. Size,
  354. DataPos,
  355. MemPos : aword;
  356. SecAlign : shortint;
  357. SecOptions : TObjSectionOptions;
  358. constructor create(AList:TFPHashObjectList;const AName:string);virtual;
  359. destructor destroy;override;
  360. procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);virtual;
  361. property ObjSectionList:TFPObjectList read FObjSectionList;
  362. property SecSymIdx:longint read FSecSymIdx write FSecSymIdx;
  363. end;
  364. TExeSectionClass=class of TExeSection;
  365. TlibKind = (lkArchive,lkObject,lkGroup);
  366. TStaticLibrary = class(TObject)
  367. private
  368. FName : TCmdStr;
  369. FPayload : TObject; { lkArchive: TObjectReader }
  370. { lkObject: TObjData }
  371. { lkGroup: TFPObjectList }
  372. FObjInputClass : TObjInputClass;
  373. FKind: TlibKind;
  374. FAsNeeded : Boolean;
  375. function GetArReader:TObjectReader;
  376. function GetGroupMembers:TFPObjectList;
  377. function GetObjData:TObjData;
  378. public
  379. constructor create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  380. constructor create_object(AObjData:TObjData);
  381. constructor create_group;
  382. destructor destroy;override;
  383. property ArReader:TObjectReader read GetArReader;
  384. property ObjInputClass:TObjInputClass read FObjInputClass;
  385. property GroupMembers:TFPObjectList read GetGroupMembers;
  386. property ObjData:TObjData read GetObjData;
  387. property AsNeeded:Boolean read FAsNeeded write FAsNeeded;
  388. property Kind:TLibKind read FKind;
  389. end;
  390. TImportLibrary = class(TFPHashObject)
  391. private
  392. FImportSymbolList : TFPHashObjectList;
  393. public
  394. constructor create(AList:TFPHashObjectList;const AName:string);
  395. destructor destroy;override;
  396. property ImportSymbolList:TFPHashObjectList read FImportSymbolList;
  397. end;
  398. TImportSymbol = class(TFPHashObject)
  399. private
  400. FOrdNr : longint;
  401. FIsVar : boolean;
  402. FMangledName : string;
  403. public
  404. constructor create(AList:TFPHashObjectList;const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
  405. property OrdNr: longint read FOrdNr;
  406. property MangledName: string read FMangledName;
  407. property IsVar: boolean read FIsVar;
  408. end;
  409. TExeWriteMode = (ewm_exefull,ewm_dbgonly,ewm_exeonly);
  410. TExeOutput = class
  411. private
  412. { ExeSectionList }
  413. FCObjData : TObjDataClass;
  414. FCExeSection : TExeSectionClass;
  415. FCurrExeSec : TExeSection;
  416. FExeSectionList : TFPHashObjectList;
  417. Fzeronr : longint;
  418. Fvaluesnr : longint;
  419. { Symbols }
  420. FExeSymbolList : TFPHashObjectList;
  421. FUnresolvedExeSymbols : TFPObjectList;
  422. FExternalObjSymbols,
  423. FCommonObjSymbols : TFPObjectList;
  424. FProvidedObjSymbols : TFPObjectList;
  425. FIndirectObjSymbols : TFPObjectList;
  426. FEntryName : string;
  427. FExeVTableList : TFPObjectList;
  428. { Objects }
  429. FObjDataList : TFPObjectList;
  430. { Position calculation }
  431. FImageBase : aword;
  432. FCurrMemPos : qword;
  433. procedure SetCurrMemPos(const AValue: qword);
  434. protected
  435. { writer }
  436. FExeWriteMode : TExeWriteMode;
  437. FWriter : TObjectwriter;
  438. commonObjSection : TObjSection;
  439. internalObjData : TObjData;
  440. EntrySym : TObjSymbol;
  441. SectionDataAlign,
  442. SectionMemAlign : aword;
  443. function writeData:boolean;virtual;abstract;
  444. property CExeSection:TExeSectionClass read FCExeSection write FCExeSection;
  445. property CObjData:TObjDataClass read FCObjData write FCObjData;
  446. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);virtual;
  447. procedure WriteExeSectionContent;
  448. procedure DoRelocationFixup(objsec:TObjSection);virtual;abstract;
  449. public
  450. CurrDataPos : aword;
  451. MaxMemPos : qword;
  452. IsSharedLibrary : boolean;
  453. ExecStack : boolean;
  454. constructor create;virtual;
  455. destructor destroy;override;
  456. function FindExeSection(const aname:string):TExeSection;
  457. procedure AddObjData(ObjData:TObjData);
  458. procedure Load_Start;virtual;
  459. procedure Load_EntryName(const aname:string);virtual;
  460. procedure Load_Symbol(const aname:string);virtual;
  461. procedure Load_ProvideSymbol(const aname:string);virtual;
  462. procedure Load_IsSharedLibrary;
  463. procedure Load_ImageBase(const avalue:string);
  464. procedure Load_DynamicObject(ObjData:TObjData);virtual;
  465. procedure Order_Start;virtual;
  466. procedure Order_End;virtual;
  467. procedure Order_ExeSection(const aname:string);virtual;
  468. procedure Order_Align(const avalue:string);virtual;
  469. procedure Order_Zeros(const avalue:string);virtual;
  470. procedure Order_Values(bytesize : aword; const avalue:string);virtual;
  471. procedure Order_Symbol(const aname:string);virtual;
  472. procedure Order_ProvideSymbol(const aname:string);virtual;
  473. procedure Order_EndExeSection;virtual;
  474. procedure Order_ObjSection(const aname:string);virtual;
  475. procedure MemPos_Start;virtual;
  476. procedure MemPos_Header;virtual;
  477. procedure MemPos_ExeSection(exesec:TExeSection);
  478. procedure MemPos_ExeSection(const aname:string);virtual;
  479. procedure MemPos_EndExeSection;virtual;
  480. procedure DataPos_Start;virtual;
  481. procedure DataPos_Header;virtual;
  482. procedure DataPos_ExeSection(exesec:TExeSection);
  483. procedure DataPos_ExeSection(const aname:string);virtual;
  484. procedure DataPos_EndExeSection;virtual;
  485. procedure DataPos_Symbols;virtual;
  486. procedure BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  487. procedure PackUnresolvedExeSymbols(const s:string);
  488. procedure ResolveSymbols(StaticLibraryList:TFPObjectList);
  489. procedure PrintMemoryMap;
  490. procedure FixupSymbols;
  491. procedure FixupRelocations;
  492. procedure RemoveUnusedExeSymbols;
  493. procedure MergeStabs;
  494. procedure MarkEmptySections;
  495. procedure RemoveUnreferencedSections;
  496. procedure RemoveDisabledSections;
  497. procedure RemoveDebugInfo;
  498. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);virtual;
  499. procedure GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  500. function WriteExeFile(const fn:string):boolean;
  501. procedure ParseScript (linkscript:TCmdStrList); virtual;
  502. property Writer:TObjectWriter read FWriter;
  503. property ExeSectionList:TFPHashObjectList read FExeSectionList;
  504. property ObjDataList:TFPObjectList read FObjDataList;
  505. property ExeSymbolList:TFPHashObjectList read FExeSymbolList;
  506. property UnresolvedExeSymbols:TFPObjectList read FUnresolvedExeSymbols;
  507. property ExternalObjSymbols:TFPObjectList read FExternalObjSymbols;
  508. property CommonObjSymbols:TFPObjectList read FCommonObjSymbols;
  509. property IndirectObjSymbols:TFPObjectList read FIndirectObjSymbols;
  510. property ExeVTableList:TFPObjectList read FExeVTableList;
  511. property EntryName:string read FEntryName write FEntryName;
  512. property ImageBase:aword read FImageBase write FImageBase;
  513. property CurrExeSec:TExeSection read FCurrExeSec;
  514. property ExeWriteMode:TExeWriteMode read FExeWriteMode write FExeWriteMode;
  515. property CurrMemPos:qword read FCurrMemPos write SetCurrMemPos;
  516. end;
  517. TExeOutputClass=class of TExeOutput;
  518. var
  519. exeoutput : TExeOutput;
  520. implementation
  521. uses
  522. SysUtils,
  523. globals,verbose,fmodule,ogmap;
  524. const
  525. SectionDataMaxGrow = 4096;
  526. {$ifdef MEMDEBUG}
  527. var
  528. memobjsymbols,
  529. memobjsections : TMemDebug;
  530. {$endif MEMDEBUG}
  531. {*****************************************************************************
  532. TObjSymbol
  533. *****************************************************************************}
  534. constructor TObjSymbol.create(AList:TFPHashObjectList;const AName:string);
  535. begin;
  536. inherited create(AList,AName);
  537. bind:=AB_EXTERNAL;
  538. typ:=AT_NONE;
  539. symidx:=-1;
  540. size:=0;
  541. offset:=0;
  542. objsection:=nil;
  543. end;
  544. function TObjSymbol.address:aword;
  545. begin
  546. if assigned(objsection) then
  547. result:=offset+objsection.mempos
  548. else
  549. result:=0;
  550. end;
  551. procedure TObjSymbol.SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  552. begin
  553. if not(abind in [AB_GLOBAL,AB_LOCAL,AB_COMMON,AB_IMPORT]) then
  554. internalerror(200603016);
  555. if not assigned(aobjsec) then
  556. internalerror(200603017);
  557. if (bind in [AB_EXTERNAL,AB_LAZY]) or
  558. { Put all COMMON to GLOBAL in step 3 of
  559. TExeOutput.ResolveSymbols }
  560. ((abind=AB_GLOBAL) and (bind=AB_COMMON)) then
  561. begin
  562. { Do not change the AB_TYPE of common symbols yet }
  563. { This will be done in FixupSymbols }
  564. if (pass<>0) or (bind<>AB_COMMON) then
  565. bind:=abind;
  566. typ:=atyp;
  567. end
  568. else
  569. begin
  570. if pass=apass then
  571. begin
  572. Message1(asmw_e_duplicate_label,name);
  573. exit;
  574. end;
  575. end;
  576. pass:=apass;
  577. { Code can never grow after a pass }
  578. if assigned(objsection) and
  579. (objsection=aobjsec) and
  580. (aobjsec.size>offset) then
  581. internalerror(200603014);
  582. objsection:=aobjsec;
  583. offset:=aobjsec.size;
  584. end;
  585. {****************************************************************************
  586. TObjRelocation
  587. ****************************************************************************}
  588. constructor TObjRelocation.CreateSymbol(ADataOffset:aword;s:TObjSymbol;Atyp:TObjRelocationType);
  589. begin
  590. if not assigned(s) then
  591. internalerror(200603034);
  592. DataOffset:=ADataOffset;
  593. Symbol:=s;
  594. OrgSize:=0;
  595. ObjSection:=nil;
  596. ftype:=ord(Atyp);
  597. end;
  598. constructor TObjRelocation.CreateSection(ADataOffset:aword;aobjsec:TObjSection;Atyp:TObjRelocationType);
  599. begin
  600. if not assigned(aobjsec) then
  601. internalerror(200603036);
  602. DataOffset:=ADataOffset;
  603. Symbol:=nil;
  604. OrgSize:=0;
  605. ObjSection:=aobjsec;
  606. ftype:=ord(Atyp);
  607. end;
  608. constructor TObjRelocation.CreateRaw(ADataOffset:aword;s:TObjSymbol;ARawType:byte);
  609. begin
  610. if not assigned(s) then
  611. internalerror(2012091701);
  612. DataOffset:=ADataOffset;
  613. Symbol:=s;
  614. ObjSection:=nil;
  615. orgsize:=0;
  616. ftype:=ARawType;
  617. flags:=rf_raw;
  618. end;
  619. function TObjRelocation.GetType:TObjRelocationType;
  620. begin
  621. if (flags and rf_raw)=0 then
  622. result:=TObjRelocationType(ftype)
  623. else
  624. result:=RELOC_RAW;
  625. end;
  626. procedure TObjRelocation.SetType(v:TObjRelocationType);
  627. begin
  628. ftype:=ord(v);
  629. flags:=flags and (not rf_raw);
  630. end;
  631. function TObjRelocation.TargetName:TSymStr;
  632. begin
  633. if assigned(symbol) then
  634. if symbol.typ=AT_SECTION then
  635. result:=symbol.objsection.name
  636. else
  637. result:=symbol.Name
  638. else
  639. result:=objsection.Name;
  640. end;
  641. {****************************************************************************
  642. TObjSection
  643. ****************************************************************************}
  644. constructor TObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  645. begin
  646. inherited Create(AList,Aname);
  647. { Data }
  648. Size:=0;
  649. Datapos:=0;
  650. mempos:=0;
  651. FData:=Nil;
  652. { Setting the secoptions allocates Data if needed }
  653. secoptions:=Aoptions;
  654. secalign:=Aalign;
  655. secsymidx:=0;
  656. { relocation }
  657. ObjRelocations:=TFPObjectList.Create(true);
  658. VTRefList:=TFPObjectList.Create(false);
  659. end;
  660. destructor TObjSection.destroy;
  661. begin
  662. if assigned(Data) then
  663. Data.Free;
  664. stringdispose(FCachedFullName);
  665. ObjRelocations.Free;
  666. VTRefList.Free;
  667. inherited destroy;
  668. end;
  669. procedure TObjSection.SetSecOptions(Aoptions:TObjSectionOptions);
  670. begin
  671. FSecOptions:=FSecOptions+AOptions;
  672. if (oso_Data in secoptions) and
  673. not assigned(FData) then
  674. FData:=TDynamicArray.Create(SectionDataMaxGrow);
  675. end;
  676. function TObjSection.write(const d;l:aword):aword;
  677. begin
  678. result:=size;
  679. if assigned(Data) then
  680. begin
  681. if Size<>Data.size then
  682. internalerror(200602281);
  683. Data.write(d,l);
  684. inc(Size,l);
  685. end
  686. else
  687. internalerror(200602289);
  688. end;
  689. function TObjSection.writestr(const s:string):aword;
  690. var
  691. b: byte;
  692. begin
  693. result:=Write(s[1],length(s));
  694. b:=0;
  695. Write(b,1);
  696. end;
  697. function TObjSection.WriteBytes(const s:string):aword;
  698. begin
  699. result:=Write(s[1],length(s));
  700. end;
  701. function TObjSection.WriteZeros(l:longword):aword;
  702. var
  703. empty : array[0..1023] of byte;
  704. begin
  705. if l>sizeof(empty) then
  706. internalerror(200404082);
  707. if l>0 then
  708. begin
  709. fillchar(empty,l,0);
  710. result:=Write(empty,l);
  711. end
  712. else
  713. result:=Size;
  714. end;
  715. { Writes relocation to (section+offset) without need to have a symbol at that location.
  716. Not an abstract method because not every backend needs this functionality. }
  717. procedure TObjSection.writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  718. begin
  719. InternalError(2012081501);
  720. end;
  721. procedure TObjSection.setDatapos(var dpos:aword);
  722. begin
  723. if oso_Data in secoptions then
  724. begin
  725. { get aligned Datapos }
  726. Datapos:=align(dpos,secalign);
  727. Dataalignbytes:=Datapos-dpos;
  728. { return updated Datapos }
  729. dpos:=Datapos+size;
  730. end
  731. else
  732. Datapos:=dpos;
  733. end;
  734. function TObjSection.setmempos(mpos:qword):qword;
  735. begin
  736. mempos:=align(mpos,secalign);
  737. { return updated mempos }
  738. result:=mempos+size;
  739. end;
  740. procedure TObjSection.alloc(l:aword);
  741. begin
  742. inc(size,l);
  743. end;
  744. procedure TObjSection.addsymReloc(ofs:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  745. begin
  746. ObjRelocations.Add(TObjRelocation.CreateSymbol(ofs,p,reloctype));
  747. end;
  748. procedure TObjSection.addsectionReloc(ofs:aword;aobjsec:TObjSection;Reloctype:TObjRelocationType);
  749. begin
  750. ObjRelocations.Add(TObjRelocation.CreateSection(ofs,aobjsec,reloctype));
  751. end;
  752. procedure TObjSection.addrawReloc(ofs:aword;p:TObjSymbol;RawReloctype:byte);
  753. begin
  754. ObjRelocations.Add(TObjRelocation.CreateRaw(ofs,p,RawReloctype));
  755. end;
  756. procedure TObjSection.ReleaseData;
  757. begin
  758. if assigned(FData) then
  759. begin
  760. FData.free;
  761. FData:=nil;
  762. end;
  763. ObjRelocations.free;
  764. ObjRelocations:=nil;
  765. if assigned(FCachedFullName) then
  766. begin
  767. stringdispose(FCachedFullName);
  768. FCachedFullName:=nil;
  769. end;
  770. end;
  771. function TObjSection.FullName:string;
  772. begin
  773. if not assigned(FCachedFullName) then
  774. begin
  775. if assigned(ObjData) then
  776. FCachedFullName:=stringdup(ObjData.Name+'('+Name+')')
  777. else
  778. FCachedFullName:=stringdup(Name);
  779. end;
  780. result:=FCachedFullName^;
  781. end;
  782. {****************************************************************************
  783. TObjData
  784. ****************************************************************************}
  785. constructor TObjData.create(const n:string);
  786. begin
  787. inherited create;
  788. FName:=ExtractFileName(n);
  789. FObjSectionList:=TFPHashObjectList.Create(true);
  790. FStabsObjSec:=nil;
  791. FStabStrObjSec:=nil;
  792. { symbols }
  793. FObjSymbolList:=TFPHashObjectList.Create(true);
  794. FCachedAsmSymbolList:=TFPObjectList.Create(false);
  795. { section class type for creating of new sections }
  796. FCObjSection:=TObjSection;
  797. end;
  798. destructor TObjData.destroy;
  799. begin
  800. { Symbols }
  801. {$ifdef MEMDEBUG}
  802. MemObjSymbols.Start;
  803. {$endif}
  804. ResetCachedAsmSymbols;
  805. FCachedAsmSymbolList.free;
  806. FObjSymbolList.free;
  807. {$ifdef MEMDEBUG}
  808. MemObjSymbols.Stop;
  809. {$endif}
  810. { Sections }
  811. {$ifdef MEMDEBUG}
  812. MemObjSections.Start;
  813. {$endif}
  814. FObjSectionList.free;
  815. {$ifdef MEMDEBUG}
  816. MemObjSections.Stop;
  817. {$endif}
  818. inherited destroy;
  819. end;
  820. function TObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  821. const
  822. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  823. 'code',
  824. 'Data',
  825. 'Data',
  826. 'roData',
  827. 'bss',
  828. 'threadvar',
  829. 'pdata',
  830. 'stub',
  831. 'data_nonlazy',
  832. 'data_lazy',
  833. 'init_func',
  834. 'term_func',
  835. 'stab','stabstr',
  836. 'iData2','iData4','iData5','iData6','iData7','eData',
  837. 'eh_frame',
  838. 'debug_frame','debug_info','debug_line','debug_abbrev',
  839. 'fpc',
  840. 'toc',
  841. 'init',
  842. 'fini',
  843. 'objc_class',
  844. 'objc_meta_class',
  845. 'objc_cat_cls_meth',
  846. 'objc_cat_inst_meth',
  847. 'objc_protocol',
  848. 'objc_string_object',
  849. 'objc_cls_meth',
  850. 'objc_inst_meth',
  851. 'objc_cls_refs',
  852. 'objc_message_refs',
  853. 'objc_symbols',
  854. 'objc_category',
  855. 'objc_class_vars',
  856. 'objc_instance_vars',
  857. 'objc_module_info',
  858. 'objc_class_names',
  859. 'objc_meth_var_types',
  860. 'objc_meth_var_names',
  861. 'objc_selector_strs',
  862. 'objc_protocol_ext',
  863. 'objc_class_ext',
  864. 'objc_property',
  865. 'objc_image_info',
  866. 'objc_cstring_object',
  867. 'objc_sel_fixup',
  868. '__DATA,__objc_data',
  869. '__DATA,__objc_const',
  870. '.objc_superrefs',
  871. '__DATA, __datacoal_nt,coalesced',
  872. '.objc_classlist',
  873. '.objc_nlclasslist',
  874. '.objc_catlist',
  875. '.obcj_nlcatlist',
  876. '.objc_protolist'
  877. );
  878. var
  879. sep : string[3];
  880. begin
  881. if aname<>'' then
  882. begin
  883. case aorder of
  884. secorder_begin :
  885. sep:='.b_';
  886. secorder_end :
  887. sep:='.z_';
  888. else
  889. sep:='.n_';
  890. end;
  891. result:=secnames[atype]+sep+aname
  892. end
  893. else
  894. result:=secnames[atype];
  895. end;
  896. function TObjData.sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;
  897. const
  898. secoptions : array[TAsmSectiontype] of TObjSectionOptions = ([],
  899. {user} [oso_Data,oso_load,oso_write,oso_keep],
  900. {code} [oso_Data,oso_load,oso_executable,oso_keep],
  901. {Data} [oso_Data,oso_load,oso_write,oso_keep],
  902. { TODO: Fix sec_rodata be read-only-with-relocs}
  903. {roData} [oso_Data,oso_load,oso_write,oso_keep],
  904. { TODO: Fix sec_rodata_norel be read-only/constant}
  905. {roData_norel} [oso_Data,oso_load,oso_write,oso_keep],
  906. {bss} [oso_load,oso_write,oso_keep],
  907. {threadvar} [oso_load,oso_write
  908. {$ifdef FPC_USE_TLS_DIRECTORY}
  909. ,oso_keep
  910. {$endif FPC_USE_TLS_DIRECTORY}
  911. ],
  912. {pdata} [oso_data,oso_load {$ifndef x86_64},oso_keep{$endif}],
  913. {stub} [oso_Data,oso_load,oso_executable],
  914. {data_nonlazy} [oso_Data,oso_load,oso_write],
  915. {data_lazy} [oso_Data,oso_load,oso_write],
  916. {init_func} [oso_Data,oso_load],
  917. {term_func} [oso_Data,oso_load],
  918. {stab} [oso_Data,oso_debug],
  919. {stabstr} [oso_Data,oso_strings,oso_debug],
  920. {iData2} [oso_Data,oso_load,oso_write],
  921. {iData4} [oso_Data,oso_load,oso_write],
  922. {iData5} [oso_Data,oso_load,oso_write],
  923. {iData6} [oso_Data,oso_load,oso_write],
  924. {iData7} [oso_Data,oso_load,oso_write],
  925. {eData} [oso_Data,oso_load],
  926. {eh_frame} [oso_Data,oso_load],
  927. {debug_frame} [oso_Data,oso_debug],
  928. {debug_info} [oso_Data,oso_debug],
  929. {debug_line} [oso_Data,oso_debug],
  930. {debug_abbrev} [oso_Data,oso_debug],
  931. {fpc} [oso_Data,oso_load,oso_write,oso_keep],
  932. {toc} [oso_Data,oso_load],
  933. {init} [oso_Data,oso_load,oso_executable,oso_keep],
  934. {fini} [oso_Data,oso_load,oso_executable,oso_keep],
  935. {objc_class} [oso_data,oso_load],
  936. {objc_meta_class} [oso_data,oso_load],
  937. {objc_cat_cls_meth} [oso_data,oso_load],
  938. {objc_cat_inst_meth} [oso_data,oso_load],
  939. {objc_protocol} [oso_data,oso_load],
  940. {objc_string_object} [oso_data,oso_load],
  941. {objc_cls_meth} [oso_data,oso_load],
  942. {objc_inst_meth} [oso_data,oso_load],
  943. {objc_cls_refs} [oso_data,oso_load],
  944. {objc_message_refs} [oso_data,oso_load],
  945. {objc_symbols} [oso_data,oso_load],
  946. {objc_category} [oso_data,oso_load],
  947. {objc_class_vars} [oso_data,oso_load],
  948. {objc_instance_vars} [oso_data,oso_load],
  949. {objc_module_info} [oso_data,oso_load],
  950. {objc_class_names} [oso_data,oso_load],
  951. {objc_meth_var_types} [oso_data,oso_load],
  952. {objc_meth_var_names} [oso_data,oso_load],
  953. {objc_selector_strs} [oso_data,oso_load],
  954. {objc_protocol_ext} [oso_data,oso_load],
  955. {objc_class_ext} [oso_data,oso_load],
  956. {objc_property} [oso_data,oso_load],
  957. {objc_image_info} [oso_data,oso_load],
  958. {objc_cstring_object} [oso_data,oso_load],
  959. {objc_sel_fixup} [oso_data,oso_load],
  960. {sec_objc_data} [oso_data,oso_load],
  961. {sec_objc_const} [oso_data,oso_load],
  962. {sec_objc_sup_refs} [oso_data,oso_load],
  963. {sec_data_coalesced} [oso_data,oso_load],
  964. {sec_objc_classlist} [oso_data,oso_load],
  965. {sec_objc_nlclasslist} [oso_data,oso_load],
  966. {sec_objc_catlist} [oso_data,oso_load],
  967. {sec_objc_nlcatlist} [oso_data,oso_load],
  968. {sec_objc_protolist'} [oso_data,oso_load]
  969. );
  970. begin
  971. result:=secoptions[atype];
  972. end;
  973. function TObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  974. begin
  975. case atype of
  976. sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev:
  977. result:=1;
  978. sec_code,
  979. sec_bss,
  980. sec_data:
  981. result:=16;
  982. { For idata (at least idata2) it must be 4 bytes, because
  983. an entry is always (also in win64) 20 bytes and aligning
  984. on 8 bytes will insert 4 bytes between the entries resulting
  985. in a corrupt idata section.
  986. Same story with .pdata, it has 4-byte elements which should
  987. be packed without gaps. }
  988. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_pdata:
  989. result:=4;
  990. else
  991. result:=sizeof(pint);
  992. end;
  993. end;
  994. function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
  995. begin
  996. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
  997. end;
  998. function TObjData.createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
  999. begin
  1000. if DiscardDuplicate then
  1001. result:=TObjSection(FObjSectionList.Find(aname))
  1002. else
  1003. result:=nil;
  1004. if not assigned(result) then
  1005. begin
  1006. result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
  1007. result.ObjData:=self;
  1008. end;
  1009. FCurrObjSec:=result;
  1010. end;
  1011. procedure TObjData.CreateDebugSections;
  1012. begin
  1013. end;
  1014. function TObjData.FindSection(const aname:string):TObjSection;
  1015. begin
  1016. result:=TObjSection(FObjSectionList.Find(aname));
  1017. end;
  1018. procedure TObjData.setsection(asec:TObjSection);
  1019. begin
  1020. if asec.ObjData<>self then
  1021. internalerror(200403041);
  1022. FCurrObjSec:=asec;
  1023. end;
  1024. function TObjData.createsymbol(const aname:string):TObjSymbol;
  1025. begin
  1026. result:=TObjSymbol(FObjSymbolList.Find(aname));
  1027. if not assigned(result) then
  1028. result:=TObjSymbol.Create(FObjSymbolList,aname);
  1029. end;
  1030. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  1031. begin
  1032. if assigned(asmsym) then
  1033. begin
  1034. if not assigned(asmsym.cachedObjSymbol) then
  1035. begin
  1036. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  1037. asmsym.cachedObjSymbol:=result;
  1038. FCachedAsmSymbolList.add(asmsym);
  1039. end
  1040. else
  1041. begin
  1042. result:=TObjSymbol(asmsym.cachedObjSymbol);
  1043. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  1044. end;
  1045. end
  1046. else
  1047. result:=nil;
  1048. end;
  1049. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  1050. begin
  1051. if not assigned(CurrObjSec) then
  1052. internalerror(200603051);
  1053. result:=CreateSymbol(aname);
  1054. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  1055. end;
  1056. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  1057. var
  1058. s:string;
  1059. begin
  1060. if assigned(asmsym) then
  1061. begin
  1062. if not assigned(asmsym.cachedObjSymbol) then
  1063. begin
  1064. s:=asmsym.name;
  1065. result:=TObjSymbol(FObjSymbolList.Find(s));
  1066. if result=nil then
  1067. begin
  1068. result:=TObjSymbol.Create(FObjSymbolList,s);
  1069. if asmsym.bind=AB_WEAK_EXTERNAL then
  1070. result.bind:=AB_WEAK_EXTERNAL;
  1071. end;
  1072. asmsym.cachedObjSymbol:=result;
  1073. FCachedAsmSymbolList.add(asmsym);
  1074. end
  1075. else
  1076. result:=TObjSymbol(asmsym.cachedObjSymbol);
  1077. { The weak bit could have been removed from asmsym. }
  1078. if (asmsym.bind=AB_EXTERNAL) and (result.bind=AB_WEAK_EXTERNAL) then
  1079. result.bind:=AB_EXTERNAL;
  1080. end
  1081. else
  1082. result:=nil;
  1083. end;
  1084. function TObjData.symbolref(const aname:string):TObjSymbol;
  1085. begin
  1086. if not assigned(CurrObjSec) then
  1087. internalerror(200603052);
  1088. result:=CreateSymbol(aname);
  1089. end;
  1090. procedure TObjData.ResetCachedAsmSymbols;
  1091. var
  1092. i : longint;
  1093. begin
  1094. for i:=0 to FCachedAsmSymbolList.Count-1 do
  1095. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  1096. FCachedAsmSymbolList.Clear;
  1097. end;
  1098. procedure TObjData.writebytes(const Data;len:aword);
  1099. begin
  1100. if not assigned(CurrObjSec) then
  1101. internalerror(200402251);
  1102. CurrObjSec.write(Data,len);
  1103. end;
  1104. procedure TObjData.alloc(len:aword);
  1105. begin
  1106. if not assigned(CurrObjSec) then
  1107. internalerror(200402252);
  1108. CurrObjSec.alloc(len);
  1109. end;
  1110. procedure TObjData.allocalign(len:shortint);
  1111. begin
  1112. if not assigned(CurrObjSec) then
  1113. internalerror(200402253);
  1114. CurrObjSec.alloc(align(CurrObjSec.size,len)-CurrObjSec.size);
  1115. end;
  1116. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  1117. begin
  1118. with TObjSection(p) do
  1119. alloc(align(size,secalign)-size);
  1120. end;
  1121. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  1122. begin
  1123. with TObjSection(p) do
  1124. begin
  1125. if assigned(Data) then
  1126. writezeros(align(size,secalign)-size);
  1127. end;
  1128. end;
  1129. procedure TObjData.section_reset(p:TObject;arg:pointer);
  1130. begin
  1131. with TObjSection(p) do
  1132. begin
  1133. Size:=0;
  1134. Datapos:=0;
  1135. mempos:=0;
  1136. end;
  1137. end;
  1138. procedure TObjData.beforealloc;
  1139. begin
  1140. { create stabs sections if debugging }
  1141. if assigned(StabsSec) then
  1142. begin
  1143. StabsSec.Alloc(sizeof(TObjStabEntry));
  1144. StabStrSec.Alloc(1);
  1145. end;
  1146. end;
  1147. procedure TObjData.beforewrite;
  1148. var
  1149. s : string[1];
  1150. hstab : TObjStabEntry;
  1151. begin
  1152. { create stabs sections if debugging }
  1153. if assigned(StabsSec) then
  1154. begin
  1155. { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
  1156. fillchar(hstab,sizeof(hstab),0);
  1157. StabsSec.Write(hstab,sizeof(hstab));
  1158. { start of stabstr }
  1159. s:=#0;
  1160. StabStrSec.write(s[1],length(s));
  1161. end;
  1162. end;
  1163. procedure TObjData.afteralloc;
  1164. begin
  1165. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  1166. end;
  1167. procedure TObjData.afterwrite;
  1168. var
  1169. s : string[1];
  1170. hstab : TObjStabEntry;
  1171. begin
  1172. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  1173. { For the stab section we need an HdrSym which can now be
  1174. calculated more easily }
  1175. if assigned(StabsSec) then
  1176. begin
  1177. { end of stabstr }
  1178. s:=#0;
  1179. StabStrSec.write(s[1],length(s));
  1180. { header stab }
  1181. hstab.strpos:=1;
  1182. hstab.ntype:=0;
  1183. hstab.nother:=0;
  1184. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  1185. hstab.nvalue:=StabStrSec.Size;
  1186. StabsSec.Data.seek(0);
  1187. StabsSec.Data.write(hstab,sizeof(hstab));
  1188. end;
  1189. end;
  1190. procedure TObjData.resetsections;
  1191. begin
  1192. FObjSectionList.ForEachCall(@section_reset,nil);
  1193. end;
  1194. procedure TObjData.layoutsections(var DataPos:aword);
  1195. var
  1196. i: longint;
  1197. begin
  1198. for i:=0 to FObjSectionList.Count-1 do
  1199. TObjSection(FObjSectionList[i]).setDatapos(DataPos);
  1200. end;
  1201. {****************************************************************************
  1202. TObjOutput
  1203. ****************************************************************************}
  1204. constructor TObjOutput.create(AWriter:TObjectWriter);
  1205. begin
  1206. FWriter:=AWriter;
  1207. CObjData:=TObjData;
  1208. end;
  1209. destructor TObjOutput.destroy;
  1210. begin
  1211. inherited destroy;
  1212. end;
  1213. function TObjOutput.newObjData(const n:string):TObjData;
  1214. begin
  1215. result:=CObjData.create(n);
  1216. if (cs_use_lineinfo in current_settings.globalswitches) or
  1217. (cs_debuginfo in current_settings.moduleswitches) then
  1218. result.CreateDebugSections;
  1219. end;
  1220. function TObjOutput.startObjectfile(const fn:string):boolean;
  1221. begin
  1222. result:=false;
  1223. { start the writer already, so the .a generation can initialize
  1224. the position of the current objectfile }
  1225. if not FWriter.createfile(fn) then
  1226. Comment(V_Fatal,'Can''t create object '+fn);
  1227. result:=true;
  1228. end;
  1229. function TObjOutput.writeobjectfile(Data:TObjData):boolean;
  1230. begin
  1231. if errorcount=0 then
  1232. result:=writeData(Data)
  1233. else
  1234. result:=true;
  1235. { close the writer }
  1236. FWriter.closefile;
  1237. end;
  1238. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  1239. begin
  1240. { export globals and common symbols, this is needed
  1241. for .a files }
  1242. if p.bind in [AB_GLOBAL,AB_COMMON] then
  1243. FWriter.writesym(p.name);
  1244. end;
  1245. procedure TObjOutput.WriteSectionContent(Data:TObjData);
  1246. var
  1247. i:longint;
  1248. sec:TObjSection;
  1249. begin
  1250. for i:=0 to Data.ObjSectionList.Count-1 do
  1251. begin
  1252. sec:=TObjSection(Data.ObjSectionList[i]);
  1253. if (oso_data in sec.SecOptions) then
  1254. begin
  1255. if sec.Data=nil then
  1256. internalerror(200403073);
  1257. FWriter.writezeros(sec.dataalignbytes);
  1258. if sec.Datapos<>FWriter.ObjSize then
  1259. internalerror(200604031);
  1260. FWriter.writearray(sec.data);
  1261. end;
  1262. end;
  1263. end;
  1264. {****************************************************************************
  1265. TExeVTable
  1266. ****************************************************************************}
  1267. constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
  1268. begin
  1269. ExeSymbol:=AExeSymbol;
  1270. if ExeSymbol.State=symstate_undefined then
  1271. internalerror(200604012);
  1272. ChildList:=TFPObjectList.Create(false);
  1273. end;
  1274. destructor TExeVTable.Destroy;
  1275. begin
  1276. ChildList.Free;
  1277. if assigned(EntryArray) then
  1278. Freemem(EntryArray);
  1279. end;
  1280. procedure TExeVTable.CheckIdx(VTableIdx:longint);
  1281. var
  1282. OldEntryCnt : longint;
  1283. begin
  1284. if VTableIdx>=EntryCnt then
  1285. begin
  1286. OldEntryCnt:=EntryCnt;
  1287. EntryCnt:=VTableIdx+1;
  1288. ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
  1289. FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
  1290. end;
  1291. end;
  1292. procedure TExeVTable.AddChild(vt:TExeVTable);
  1293. begin
  1294. ChildList.Add(vt);
  1295. end;
  1296. procedure TExeVTable.AddEntry(VTableIdx:Longint);
  1297. var
  1298. i : longint;
  1299. objreloc : TObjRelocation;
  1300. vtblentryoffset : aword;
  1301. begin
  1302. CheckIdx(VTableIdx);
  1303. vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+longword(VTableIdx)*sizeof(pint);
  1304. { Find and disable relocation }
  1305. for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
  1306. begin
  1307. objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
  1308. if objreloc.dataoffset=vtblentryoffset then
  1309. begin
  1310. EntryArray[VTableIdx].ObjRelocation:=objreloc;
  1311. EntryArray[VTableIdx].OrgRelocType:=objreloc.ftype;
  1312. EntryArray[VTableIdx].OrgRelocFlags:=objreloc.flags;
  1313. objreloc.typ:=RELOC_ZERO;
  1314. break;
  1315. end;
  1316. end;
  1317. if not assigned(EntryArray[VTableIdx].ObjRelocation) then
  1318. internalerror(200604011);
  1319. end;
  1320. procedure TExeVTable.SetVTableSize(ASize:longint);
  1321. begin
  1322. if EntryCnt<>0 then
  1323. internalerror(200603313);
  1324. EntryCnt:=ASize div sizeof(pint);
  1325. EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
  1326. end;
  1327. function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
  1328. begin
  1329. result:=nil;
  1330. CheckIdx(VTableIdx);
  1331. if EntryArray[VTableIdx].Used then
  1332. exit;
  1333. { Restore relocation if available }
  1334. if assigned(EntryArray[VTableIdx].ObjRelocation) then
  1335. begin
  1336. EntryArray[VTableIdx].ObjRelocation.ftype:=EntryArray[VTableIdx].OrgRelocType;
  1337. EntryArray[VTableIdx].ObjRelocation.flags:=EntryArray[VTableIdx].OrgRelocFlags;
  1338. result:=EntryArray[VTableIdx].ObjRelocation;
  1339. end;
  1340. EntryArray[VTableIdx].Used:=true;
  1341. end;
  1342. {****************************************************************************
  1343. TExeSection
  1344. ****************************************************************************}
  1345. constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
  1346. begin
  1347. inherited create(AList,AName);
  1348. Size:=0;
  1349. MemPos:=0;
  1350. DataPos:=0;
  1351. FSecSymIdx:=0;
  1352. FObjSectionList:=TFPObjectList.Create(false);
  1353. end;
  1354. destructor TExeSection.destroy;
  1355. begin
  1356. ObjSectionList.Free;
  1357. inherited destroy;
  1358. end;
  1359. procedure TExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  1360. begin
  1361. ObjSectionList.Add(objsec);
  1362. { relate ObjSection to ExeSection, and mark it Used by default }
  1363. objsec.ExeSection:=self;
  1364. objsec.Used:=true;
  1365. if ignoreprops then
  1366. exit;
  1367. if (SecOptions<>[]) then
  1368. begin
  1369. { Only if the section contains (un)initialized data the
  1370. data flag must match. }
  1371. if ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1372. Comment(V_Error,'Incompatible section options');
  1373. end
  1374. else
  1375. begin
  1376. { inherit section options }
  1377. SecOptions:=SecOptions+objsec.SecOptions;
  1378. end;
  1379. SecAlign:=max(objsec.SecAlign,SecAlign);
  1380. end;
  1381. {****************************************************************************
  1382. TStaticLibrary
  1383. ****************************************************************************}
  1384. constructor TStaticLibrary.create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  1385. begin
  1386. FName:=AName;
  1387. FPayload:=AReader;
  1388. FObjInputClass:=AObjInputClass;
  1389. FKind:=lkArchive;
  1390. end;
  1391. constructor TStaticLibrary.create_object(AObjData:TObjData);
  1392. begin
  1393. FPayload:=AObjData;
  1394. FKind:=lkObject;
  1395. end;
  1396. constructor TStaticLibrary.create_group;
  1397. begin
  1398. FPayload:=TFPObjectList.Create(true);
  1399. FKind:=lkGroup;
  1400. end;
  1401. destructor TStaticLibrary.destroy;
  1402. begin
  1403. FPayload.Free;
  1404. inherited destroy;
  1405. end;
  1406. function TStaticLibrary.GetArReader: TObjectReader;
  1407. begin
  1408. if (FKind<>lkArchive) then
  1409. InternalError(2012071501);
  1410. result:=TObjectReader(FPayload);
  1411. end;
  1412. function TStaticLibrary.GetGroupMembers: TFPObjectList;
  1413. begin
  1414. if (FKind<>lkGroup) then
  1415. InternalError(2012071502);
  1416. result:=TFPObjectList(FPayload);
  1417. end;
  1418. function TStaticLibrary.GetObjData: TObjData;
  1419. begin
  1420. if (FKind<>lkObject) then
  1421. InternalError(2012071503);
  1422. result:=TObjData(FPayload);
  1423. end;
  1424. {****************************************************************************
  1425. TImportLibrary
  1426. ****************************************************************************}
  1427. constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
  1428. begin
  1429. inherited create(AList,AName);
  1430. FImportSymbolList:=TFPHashObjectList.Create(true);
  1431. end;
  1432. destructor TImportLibrary.destroy;
  1433. begin
  1434. ImportSymbolList.Free;
  1435. inherited destroy;
  1436. end;
  1437. {****************************************************************************
  1438. TImportSymbol
  1439. ****************************************************************************}
  1440. constructor TImportSymbol.create(AList:TFPHashObjectList;
  1441. const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
  1442. begin
  1443. inherited Create(AList, AName);
  1444. FOrdNr:=AOrdNr;
  1445. FIsVar:=AIsVar;
  1446. FMangledName:=AMangledName;
  1447. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1448. { This allows to import VC++ mangled names from DLLs. }
  1449. if target_info.system in systems_all_windows then
  1450. begin
  1451. Replace(FMangledName,'?','__q$$');
  1452. {$ifdef arm}
  1453. { @ symbol is not allowed in ARM assembler only }
  1454. Replace(FMangledName,'@','__a$$');
  1455. {$endif arm}
  1456. end;
  1457. end;
  1458. {****************************************************************************
  1459. TExeOutput
  1460. ****************************************************************************}
  1461. constructor TExeOutput.create;
  1462. begin
  1463. { init writer }
  1464. FWriter:=TObjectwriter.create;
  1465. FExeWriteMode:=ewm_exefull;
  1466. { object files }
  1467. FObjDataList:=TFPObjectList.Create(true);
  1468. { symbols }
  1469. FExeSymbolList:=TFPHashObjectList.Create(true);
  1470. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  1471. FExternalObjSymbols:=TFPObjectList.Create(false);
  1472. FCommonObjSymbols:=TFPObjectList.Create(false);
  1473. FProvidedObjSymbols:=TFPObjectList.Create(false);
  1474. FIndirectObjSymbols:=TFPObjectList.Create(false);
  1475. FExeVTableList:=TFPObjectList.Create(false);
  1476. { sections }
  1477. FExeSectionList:=TFPHashObjectList.Create(true);
  1478. FImageBase:=0;
  1479. {$ifdef cpu16bitaddr}
  1480. SectionMemAlign:=$10;
  1481. SectionDataAlign:=$10;
  1482. {$else cpu16bitaddr}
  1483. SectionMemAlign:=$1000;
  1484. SectionDataAlign:=$200;
  1485. {$endif cpu16bitaddr}
  1486. FCExeSection:=TExeSection;
  1487. FCObjData:=TObjData;
  1488. end;
  1489. destructor TExeOutput.destroy;
  1490. begin
  1491. FExeSymbolList.free;
  1492. UnresolvedExeSymbols.free;
  1493. ExternalObjSymbols.free;
  1494. FProvidedObjSymbols.free;
  1495. FIndirectObjSymbols.free;
  1496. CommonObjSymbols.free;
  1497. ExeVTableList.free;
  1498. FExeSectionList.free;
  1499. ObjDatalist.free;
  1500. FWriter.free;
  1501. inherited destroy;
  1502. end;
  1503. function TExeOutput.WriteExeFile(const fn:string):boolean;
  1504. begin
  1505. result:=false;
  1506. if FWriter.createfile(fn) then
  1507. begin
  1508. { Only write the .o if there are no errors }
  1509. if errorcount=0 then
  1510. result:=writedata
  1511. else
  1512. result:=true;
  1513. { close the writer }
  1514. FWriter.closefile;
  1515. end
  1516. else
  1517. Comment(V_Fatal,'Can''t create executable '+fn);
  1518. end;
  1519. procedure TExeOutput.ParseScript (linkscript:TCmdStrList);
  1520. begin
  1521. end;
  1522. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1523. begin
  1524. result:=TExeSection(ExeSectionList.Find(aname));
  1525. end;
  1526. procedure TExeOutput.AddObjData(ObjData:TObjData);
  1527. begin
  1528. if ObjData.classtype<>FCObjData then
  1529. Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
  1530. ObjDataList.Add(ObjData);
  1531. ExecStack:=ExecStack or ObjData.ExecStack;
  1532. end;
  1533. procedure TExeOutput.Load_Start;
  1534. begin
  1535. ObjDataList.Clear;
  1536. { Globals defined in the linker script }
  1537. if not assigned(internalObjData) then
  1538. internalObjData:=CObjData.create('*Internal*');
  1539. AddObjData(internalObjData);
  1540. { Common Data section }
  1541. commonObjSection:=internalObjData.createsection(sec_bss,'');
  1542. end;
  1543. procedure TExeOutput.Load_EntryName(const aname:string);
  1544. begin
  1545. FEntryName:=aname;
  1546. end;
  1547. procedure TExeOutput.Load_IsSharedLibrary;
  1548. begin
  1549. IsSharedLibrary:=true;
  1550. end;
  1551. procedure TExeOutput.Load_ImageBase(const avalue:string);
  1552. var
  1553. code : integer;
  1554. objsec : TObjSection;
  1555. objsym : TObjSymbol;
  1556. exesym : TExeSymbol;
  1557. begin
  1558. val(avalue,FImageBase,code);
  1559. if code<>0 then
  1560. Comment(V_Error,'Invalid number '+avalue);
  1561. { Create __image_base__ symbol, create the symbol
  1562. in a section with adress 0 and at offset 0 }
  1563. objsec:=internalObjData.createsection('*__image_base__',0,[]);
  1564. internalObjData.setsection(objsec);
  1565. objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_DATA);
  1566. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1567. exesym.ObjSymbol:=objsym;
  1568. end;
  1569. procedure TExeOutput.Load_Symbol(const aname:string);
  1570. begin
  1571. internalObjData.createsection('*'+aname,0,[]);
  1572. internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_DATA);
  1573. end;
  1574. procedure TExeOutput.Load_ProvideSymbol(const aname:string);
  1575. begin
  1576. if assigned(ExeSymbolList.Find(aname)) then
  1577. exit;
  1578. internalObjData.createsection('*'+aname,0,[]);
  1579. // Use AB_COMMON to avoid muliple defined complaints
  1580. internalObjData.SymbolDefine(aname,AB_COMMON,AT_DATA);
  1581. end;
  1582. procedure TExeOutput.Load_DynamicObject(ObjData:TObjData);
  1583. begin
  1584. end;
  1585. procedure TExeOutput.Order_Start;
  1586. begin
  1587. end;
  1588. procedure TExeOutput.Order_End;
  1589. begin
  1590. internalObjData.afterwrite;
  1591. end;
  1592. procedure TExeOutput.Order_ExeSection(const aname:string);
  1593. var
  1594. sec : TExeSection;
  1595. begin
  1596. sec:=FindExeSection(aname);
  1597. if not assigned(sec) then
  1598. sec:=CExeSection.create(ExeSectionList,aname);
  1599. { Clear ExeSection contents }
  1600. FCurrExeSec:=sec;
  1601. end;
  1602. procedure TExeOutput.Order_EndExeSection;
  1603. begin
  1604. if not assigned(CurrExeSec) then
  1605. internalerror(200602184);
  1606. FCurrExeSec:=nil;
  1607. end;
  1608. procedure TExeOutput.Order_ObjSection(const aname:string);
  1609. var
  1610. i,j : longint;
  1611. ObjData : TObjData;
  1612. objsec : TObjSection;
  1613. TmpObjSectionList : TFPObjectList;
  1614. begin
  1615. if not assigned(CurrExeSec) then
  1616. internalerror(200602181);
  1617. TmpObjSectionList:=TFPObjectList.Create(false);
  1618. for i:=0 to ObjDataList.Count-1 do
  1619. begin
  1620. ObjData:=TObjData(ObjDataList[i]);
  1621. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1622. begin
  1623. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1624. if (not objsec.Used) and
  1625. MatchPattern(aname,objsec.name) then
  1626. TmpObjSectionList.Add(objsec);
  1627. end;
  1628. end;
  1629. { Order list if needed }
  1630. Order_ObjSectionList(TmpObjSectionList,aname);
  1631. { Add the (ordered) list to the current ExeSection }
  1632. for i:=0 to TmpObjSectionList.Count-1 do
  1633. begin
  1634. objsec:=TObjSection(TmpObjSectionList[i]);
  1635. CurrExeSec.AddObjSection(objsec);
  1636. end;
  1637. TmpObjSectionList.Free;
  1638. end;
  1639. procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);
  1640. begin
  1641. end;
  1642. procedure TExeOutput.Order_Symbol(const aname:string);
  1643. var
  1644. ObjSection : TObjSection;
  1645. begin
  1646. ObjSection:=internalObjData.findsection('*'+aname);
  1647. if not assigned(ObjSection) then
  1648. internalerror(200603041);
  1649. CurrExeSec.AddObjSection(ObjSection,True);
  1650. end;
  1651. procedure TExeOutput.Order_ProvideSymbol(const aname:string);
  1652. var
  1653. ObjSection : TObjSection;
  1654. exesym : TExeSymbol;
  1655. begin
  1656. ObjSection:=internalObjData.findsection('*'+aname);
  1657. if not assigned(ObjSection) then
  1658. internalerror(200603041);
  1659. exesym:=TExeSymbol(ExeSymbolList.Find(aname));
  1660. if not assigned(exesym) then
  1661. internalerror(201206301);
  1662. { Only include this section if it actually resolves
  1663. the symbol }
  1664. if exesym.objsymbol.objsection=objsection then
  1665. CurrExeSec.AddObjSection(ObjSection,True);
  1666. end;
  1667. procedure TExeOutput.Order_Align(const avalue:string);
  1668. var
  1669. code : integer;
  1670. alignval : shortint;
  1671. objsec : TObjSection;
  1672. begin
  1673. val(avalue,alignval,code);
  1674. if code<>0 then
  1675. Comment(V_Error,'Invalid number '+avalue);
  1676. if alignval<=0 then
  1677. exit;
  1678. { Create an empty section with the required aligning }
  1679. inc(Fzeronr);
  1680. objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1681. CurrExeSec.AddObjSection(objsec);
  1682. end;
  1683. procedure TExeOutput.Order_Zeros(const avalue:string);
  1684. var
  1685. zeros : array[0..1023] of byte;
  1686. code : integer;
  1687. len : longint;
  1688. objsec : TObjSection;
  1689. begin
  1690. val(avalue,len,code);
  1691. if code<>0 then
  1692. Comment(V_Error,'Invalid number '+avalue);
  1693. if len<=0 then
  1694. exit;
  1695. if len>sizeof(zeros) then
  1696. internalerror(200602254);
  1697. fillchar(zeros,len,0);
  1698. inc(Fzeronr);
  1699. objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1700. internalObjData.writebytes(zeros,len);
  1701. CurrExeSec.AddObjSection(objsec);
  1702. end;
  1703. procedure TExeOutput.Order_Values(bytesize : aword; const avalue:string);
  1704. const
  1705. MAXVAL = 128;
  1706. var
  1707. bytevalues : array[0..MAXVAL-1] of byte;
  1708. twobytevalues : array[0..MAXVAL-1] of word;
  1709. fourbytevalues : array[0..MAXVAL-1] of dword;
  1710. eightbytevalues : array[0..MAXVAL-1] of qword;
  1711. allvals, oneval : string;
  1712. len, commapos : longint;
  1713. indexpos, code : integer;
  1714. anumval : qword;
  1715. signedval : int64;
  1716. objsec : TObjSection;
  1717. begin
  1718. indexpos:=0;
  1719. allvals:=avalue;
  1720. repeat
  1721. commapos:=pos(',',allvals);
  1722. if commapos>0 then
  1723. begin
  1724. oneval:=trim(copy(allvals,1,commapos-1));
  1725. allvals:=copy(allvals,commapos+1,length(allvals));
  1726. end
  1727. else
  1728. begin
  1729. oneval:=trim(allvals);
  1730. allvals:='';
  1731. end;
  1732. if oneval<>'' then
  1733. begin
  1734. if oneval[1]='-' then
  1735. begin
  1736. val(oneval,signedval,code);
  1737. anumval:=qword(signedval);
  1738. end
  1739. else
  1740. val(oneval,anumval,code);
  1741. if code<>0 then
  1742. Comment(V_Error,'Invalid number '+avalue)
  1743. else
  1744. begin
  1745. if (indexpos<MAXVAL) then
  1746. begin
  1747. if source_info.endian<>target_info.endian then
  1748. swapendian(anumval);
  1749. { No range checking here }
  1750. if bytesize=1 then
  1751. bytevalues[indexpos]:=byte(anumval)
  1752. else if bytesize=2 then
  1753. twobytevalues[indexpos]:=word(anumval)
  1754. else if bytesize=4 then
  1755. fourbytevalues[indexpos]:=dword(anumval)
  1756. else if bytesize=8 then
  1757. eightbytevalues[indexpos]:=anumval;
  1758. inc(indexpos);
  1759. end
  1760. else
  1761. Comment(V_Error,'Buffer overrun in Order_values');
  1762. end;
  1763. end;
  1764. until allvals='';
  1765. if indexpos=0 then
  1766. begin
  1767. Comment(V_Error,'Invalid number '+avalue);
  1768. exit;
  1769. end;
  1770. if indexpos=MAXVAL then
  1771. begin
  1772. Comment(V_Error,'Too many values '+avalue);
  1773. internalerror(200602254);
  1774. end;
  1775. len:=bytesize*indexpos;
  1776. inc(Fvaluesnr);
  1777. objsec:=internalObjData.createsection('*values'+tostr(Fvaluesnr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1778. if bytesize=1 then
  1779. internalObjData.writebytes(bytevalues,len)
  1780. else if bytesize=2 then
  1781. internalObjData.writebytes(twobytevalues,len)
  1782. else if bytesize=4 then
  1783. internalObjData.writebytes(fourbytevalues,len)
  1784. else if bytesize=8 then
  1785. internalObjData.writebytes(eightbytevalues,len);
  1786. CurrExeSec.AddObjSection(objsec);
  1787. end;
  1788. procedure TExeOutput.MemPos_Start;
  1789. begin
  1790. CurrMemPos:=0;
  1791. RemoveDisabledSections;
  1792. end;
  1793. procedure TExeOutput.MemPos_Header;
  1794. begin
  1795. end;
  1796. procedure TExeOutput.MemPos_ExeSection(exesec:TExeSection);
  1797. var
  1798. i : longint;
  1799. objsec : TObjSection;
  1800. begin
  1801. { Alignment of ExeSection }
  1802. CurrMemPos:=align(CurrMemPos,SectionMemAlign);
  1803. exesec.MemPos:=CurrMemPos;
  1804. { set position of object ObjSections }
  1805. for i:=0 to exesec.ObjSectionList.Count-1 do
  1806. begin
  1807. objsec:=TObjSection(exesec.ObjSectionList[i]);
  1808. CurrMemPos:=objsec.setmempos(CurrMemPos);
  1809. end;
  1810. { calculate size of the section }
  1811. exesec.Size:=CurrMemPos-exesec.MemPos;
  1812. end;
  1813. procedure TExeOutput.MemPos_ExeSection(const aname:string);
  1814. begin
  1815. { Section can be removed }
  1816. FCurrExeSec:=FindExeSection(aname);
  1817. if not assigned(CurrExeSec) then
  1818. exit;
  1819. MemPos_ExeSection(CurrExeSec);
  1820. end;
  1821. procedure TExeOutput.MemPos_EndExeSection;
  1822. begin
  1823. if not assigned(CurrExeSec) then
  1824. exit;
  1825. FCurrExeSec:=nil;
  1826. end;
  1827. procedure TExeOutput.DataPos_Start;
  1828. begin
  1829. end;
  1830. procedure TExeOutput.DataPos_Header;
  1831. begin
  1832. end;
  1833. procedure TExeOutput.DataPos_ExeSection(exesec:TExeSection);
  1834. var
  1835. i : longint;
  1836. objsec : TObjSection;
  1837. begin
  1838. { don't write normal section if writing only debug info }
  1839. if (ExeWriteMode=ewm_dbgonly) and
  1840. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  1841. exit;
  1842. if (oso_Data in exesec.SecOptions) then
  1843. begin
  1844. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  1845. exesec.DataPos:=CurrDataPos;
  1846. end;
  1847. { set position of object ObjSections }
  1848. for i:=0 to exesec.ObjSectionList.Count-1 do
  1849. begin
  1850. objsec:=TObjSection(exesec.ObjSectionList[i]);
  1851. if (oso_Data in objsec.SecOptions) then
  1852. begin
  1853. if not(oso_Data in exesec.SecOptions) then
  1854. internalerror(200603043);
  1855. if not assigned(objsec.Data) then
  1856. internalerror(200603044);
  1857. objsec.setDatapos(CurrDataPos);
  1858. end;
  1859. end;
  1860. end;
  1861. procedure TExeOutput.DataPos_ExeSection(const aname:string);
  1862. begin
  1863. { Section can be removed }
  1864. FCurrExeSec:=FindExeSection(aname);
  1865. if not assigned(CurrExeSec) then
  1866. exit;
  1867. DataPos_ExeSection(CurrExeSec);
  1868. end;
  1869. procedure TExeOutput.DataPos_EndExeSection;
  1870. begin
  1871. if not assigned(CurrExeSec) then
  1872. exit;
  1873. FCurrExeSec:=nil;
  1874. end;
  1875. procedure TExeOutput.DataPos_Symbols;
  1876. begin
  1877. end;
  1878. procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  1879. var
  1880. hs : string;
  1881. code : integer;
  1882. i,k,
  1883. vtableidx : longint;
  1884. vtableexesym,
  1885. childexesym,
  1886. parentexesym : TExeSymbol;
  1887. objsym : TObjSymbol;
  1888. begin
  1889. { Build inheritance tree from VTINHERIT }
  1890. for i:=0 to VTInheritList.Count-1 do
  1891. begin
  1892. objsym:=TObjSymbol(VTInheritList[i]);
  1893. hs:=objsym.name;
  1894. { VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
  1895. Delete(hs,1,Pos('_',hs));
  1896. k:=Pos('$$',hs);
  1897. if k=0 then
  1898. internalerror(200603311);
  1899. childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1900. parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
  1901. if not assigned(childexesym) or
  1902. not assigned(parentexesym)then
  1903. internalerror(200603312);
  1904. if not assigned(childexesym.vtable) then
  1905. begin
  1906. childexesym.vtable:=TExeVTable.Create(childexesym);
  1907. ExeVTableList.Add(childexesym.vtable);
  1908. end;
  1909. if not assigned(parentexesym.vtable) then
  1910. begin
  1911. parentexesym.vtable:=TExeVTable.Create(parentexesym);
  1912. ExeVTableList.Add(parentexesym.vtable);
  1913. end;
  1914. childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
  1915. if parentexesym<>childexesym then
  1916. parentexesym.vtable.AddChild(childexesym.vtable);
  1917. end;
  1918. { Find VTable entries from VTENTRY }
  1919. for i:=0 to VTEntryList.Count-1 do
  1920. begin
  1921. objsym:=TObjSymbol(VTEntryList[i]);
  1922. hs:=objsym.name;
  1923. { VTENTRY_<VTableName>$$<Index> }
  1924. Delete(hs,1,Pos('_',hs));
  1925. k:=Pos('$$',hs);
  1926. if k=0 then
  1927. internalerror(200603319);
  1928. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1929. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  1930. if (code<>0) then
  1931. internalerror(200603318);
  1932. if not assigned(vtableexesym) then
  1933. internalerror(2006033110);
  1934. vtableexesym.vtable.AddEntry(vtableidx);
  1935. end;
  1936. end;
  1937. procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
  1938. var
  1939. i : longint;
  1940. exesym : TExeSymbol;
  1941. begin
  1942. { Generate a list of Unresolved External symbols }
  1943. for i:=0 to UnresolvedExeSymbols.count-1 do
  1944. begin
  1945. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1946. if exesym.State<>symstate_undefined then
  1947. UnresolvedExeSymbols[i]:=nil;
  1948. end;
  1949. UnresolvedExeSymbols.Pack;
  1950. Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
  1951. end;
  1952. procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPObjectList);
  1953. var
  1954. ObjData : TObjData;
  1955. exesym : TExeSymbol;
  1956. objsym,
  1957. commonsym : TObjSymbol;
  1958. firstarchive,
  1959. firstcommon : boolean;
  1960. i : longint;
  1961. VTEntryList,
  1962. VTInheritList : TFPObjectList;
  1963. procedure LoadObjDataSymbols(ObjData:TObjData);
  1964. var
  1965. j : longint;
  1966. hs : string;
  1967. exesym : TExeSymbol;
  1968. objsym : TObjSymbol;
  1969. begin
  1970. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  1971. begin
  1972. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  1973. { From the local symbols we are only interressed in the
  1974. VTENTRY and VTINHERIT symbols }
  1975. if objsym.bind=AB_LOCAL then
  1976. begin
  1977. if cs_link_opt_vtable in current_settings.globalswitches then
  1978. begin
  1979. hs:=objsym.name;
  1980. if (hs[1]='V') then
  1981. begin
  1982. if Copy(hs,1,5)='VTREF' then
  1983. begin
  1984. if not assigned(objsym.ObjSection.VTRefList) then
  1985. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  1986. objsym.ObjSection.VTRefList.Add(objsym);
  1987. end
  1988. else if Copy(hs,1,7)='VTENTRY' then
  1989. VTEntryList.Add(objsym)
  1990. else if Copy(hs,1,9)='VTINHERIT' then
  1991. VTInheritList.Add(objsym);
  1992. end;
  1993. end;
  1994. continue;
  1995. end;
  1996. { Search for existing exesymbol }
  1997. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  1998. if not assigned(exesym) then
  1999. begin
  2000. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  2001. exesym.ObjSymbol:=objsym;
  2002. end;
  2003. objsym.ExeSymbol:=exesym;
  2004. case objsym.bind of
  2005. AB_GLOBAL :
  2006. begin
  2007. if exesym.State<>symstate_defined then
  2008. begin
  2009. exesym.ObjSymbol:=objsym;
  2010. exesym.State:=symstate_defined;
  2011. end
  2012. else
  2013. Comment(V_Error,'Multiple defined symbol '+objsym.name);
  2014. end;
  2015. AB_EXTERNAL :
  2016. begin
  2017. ExternalObjSymbols.add(objsym);
  2018. { Register unresolved symbols only the first time they
  2019. are registered }
  2020. if exesym.ObjSymbol=objsym then
  2021. UnresolvedExeSymbols.Add(exesym);
  2022. end;
  2023. AB_COMMON :
  2024. begin
  2025. { A COMMON definition overrides weak one.
  2026. Also select the symbol with largest size. }
  2027. if (exesym.State in [symstate_undefined,symstate_defweak]) or
  2028. ((exesym.State=symstate_common) and (objsym.size>exesym.ObjSymbol.size)) then
  2029. begin
  2030. exesym.ObjSymbol:=objsym;
  2031. exesym.State:=symstate_common;
  2032. end;
  2033. if assigned(objsym.objsection) and
  2034. (objsym.objsection.objdata=internalObjData) then
  2035. FProvidedObjSymbols.add(objsym)
  2036. else
  2037. CommonObjSymbols.add(objsym);
  2038. end;
  2039. AB_WEAK_EXTERNAL :
  2040. begin
  2041. if objsym.objsection=nil then { a weak reference }
  2042. begin
  2043. ExternalObjSymbols.add(objsym);
  2044. if exesym.ObjSymbol=objsym then
  2045. UnresolvedExeSymbols.Add(exesym);
  2046. end
  2047. else { a weak definition }
  2048. begin
  2049. if exesym.State=symstate_undefined then
  2050. begin
  2051. exesym.ObjSymbol:=objsym;
  2052. exesym.state:=symstate_defweak;
  2053. end;
  2054. end;
  2055. end;
  2056. end;
  2057. end;
  2058. end;
  2059. procedure LoadLibrary(lib:TStaticLibrary);
  2060. var
  2061. j,k,oldcount: longint;
  2062. members: TFPObjectList;
  2063. exesym: TExeSymbol;
  2064. objinput: TObjInput;
  2065. begin
  2066. case lib.Kind of
  2067. lkArchive:
  2068. begin
  2069. { Process list of Unresolved External symbols, we need
  2070. to use a while loop because the list can be extended when
  2071. we load members from the library. }
  2072. j:=0;
  2073. while (j<UnresolvedExeSymbols.count) do
  2074. begin
  2075. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  2076. { Check first if the symbol is still undefined }
  2077. if (exesym.State=symstate_undefined) and (exesym.ObjSymbol.bind<>AB_WEAK_EXTERNAL) then
  2078. begin
  2079. if lib.ArReader.OpenFile(exesym.name) then
  2080. begin
  2081. if assigned(exemap) then
  2082. begin
  2083. if firstarchive then
  2084. begin
  2085. exemap.Add('');
  2086. exemap.Add('Archive member included because of file (symbol)');
  2087. exemap.Add('');
  2088. firstarchive:=false;
  2089. end;
  2090. exemap.Add(lib.ArReader.FileName+' - '+
  2091. {exesym.ObjSymbol.ObjSection.FullName+}
  2092. '('+exesym.Name+')');
  2093. end;
  2094. objinput:=lib.ObjInputClass.Create;
  2095. objdata:=objinput.newObjData(lib.ArReader.FileName);
  2096. objinput.ReadObjData(lib.ArReader,objdata);
  2097. objinput.free;
  2098. AddObjData(objdata);
  2099. LoadObjDataSymbols(objdata);
  2100. lib.ArReader.CloseFile;
  2101. end;
  2102. end;
  2103. inc(j);
  2104. end;
  2105. end;
  2106. lkGroup:
  2107. begin
  2108. { repeatedly process members of the group until no new
  2109. unresolved symbols appear }
  2110. members:=lib.GroupMembers;
  2111. repeat
  2112. oldcount:=UnresolvedExeSymbols.count;
  2113. for k:=0 to members.Count-1 do
  2114. LoadLibrary(TStaticLibrary(members[k]));
  2115. until UnresolvedExeSymbols.count=oldcount;
  2116. end;
  2117. lkObject:
  2118. { TODO: ownership of objdata }
  2119. //if lib.objdata.is_dynamic then
  2120. Load_DynamicObject(lib.objdata);
  2121. {else
  2122. begin
  2123. AddObjData(lib.objdata);
  2124. LoadObjDataSymbols(lib.objdata);
  2125. end;}
  2126. end;
  2127. end;
  2128. begin
  2129. VTEntryList:=TFPObjectList.Create(false);
  2130. VTInheritList:=TFPObjectList.Create(false);
  2131. {
  2132. The symbol resolving is done in 4 steps:
  2133. 1. Register symbols from objects
  2134. 2. Find symbols in static libraries
  2135. 3. Define symbols PROVIDEd by the link script
  2136. 4. Define still undefined common symbols
  2137. }
  2138. { Step 1, Register symbols from objects }
  2139. for i:=0 to ObjDataList.Count-1 do
  2140. begin
  2141. ObjData:=TObjData(ObjDataList[i]);
  2142. LoadObjDataSymbols(ObjData);
  2143. end;
  2144. PackUnresolvedExeSymbols('in objects');
  2145. { Step 2, Find unresolved symbols in the libraries }
  2146. firstarchive:=true;
  2147. for i:=0 to StaticLibraryList.Count-1 do
  2148. LoadLibrary(TStaticLibrary(StaticLibraryList[i]));
  2149. PackUnresolvedExeSymbols('after static libraries');
  2150. { Step 3, handle symbols provided in script }
  2151. for i:=0 to FProvidedObjSymbols.count-1 do
  2152. begin
  2153. objsym:=TObjSymbol(FProvidedObjSymbols[i]);
  2154. if objsym.exesymbol.State=symstate_defined then
  2155. continue;
  2156. objsym.exesymbol.objsymbol:=objsym;
  2157. objsym.bind:=AB_GLOBAL;
  2158. objsym.exesymbol.State:=symstate_defined;
  2159. end;
  2160. PackUnresolvedExeSymbols('after defining symbols provided by link script');
  2161. { Step 4, Match common symbols or add to the globals }
  2162. firstcommon:=true;
  2163. for i:=0 to CommonObjSymbols.count-1 do
  2164. begin
  2165. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2166. if objsym.exesymbol.State=symstate_defined then
  2167. begin
  2168. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  2169. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  2170. end
  2171. else
  2172. begin
  2173. { allocate new objsymbol in .bss of *COMMON* and assign
  2174. it to the exesymbol }
  2175. if firstcommon then
  2176. begin
  2177. if assigned(exemap) then
  2178. exemap.AddCommonSymbolsHeader;
  2179. firstcommon:=false;
  2180. end;
  2181. internalObjData.setsection(commonObjSection);
  2182. internalObjData.allocalign(var_align(objsym.size));
  2183. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_DATA);
  2184. commonsym.size:=objsym.size;
  2185. internalObjData.alloc(objsym.size);
  2186. if assigned(exemap) then
  2187. exemap.AddCommonSymbol(commonsym);
  2188. { Assign to the exesymbol }
  2189. objsym.exesymbol.objsymbol:=commonsym;
  2190. objsym.exesymbol.state:=symstate_defined;
  2191. end;
  2192. end;
  2193. PackUnresolvedExeSymbols('after defining COMMON symbols');
  2194. { Find entry symbol and print in map }
  2195. if (EntryName<>'') then
  2196. begin
  2197. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  2198. if assigned(exesym) then
  2199. begin
  2200. EntrySym:=exesym.ObjSymbol;
  2201. if assigned(exemap) then
  2202. begin
  2203. exemap.Add('');
  2204. exemap.Add('Entry symbol '+EntryName);
  2205. end;
  2206. end
  2207. else
  2208. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  2209. end;
  2210. { Generate VTable tree }
  2211. if cs_link_opt_vtable in current_settings.globalswitches then
  2212. BuildVTableTree(VTInheritList,VTEntryList);
  2213. VTInheritList.Free;
  2214. VTEntryList.Free;
  2215. end;
  2216. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  2217. var
  2218. debuglink : array[0..1023] of byte;
  2219. len : longint;
  2220. objsec : TObjSection;
  2221. exesec : TExeSection;
  2222. begin
  2223. { From the gdb manual chapter 15. GDB Files:
  2224. * A filename, with any leading directory components removed, followed by a zero byte,
  2225. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  2226. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  2227. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  2228. }
  2229. fillchar(debuglink,sizeof(debuglink),0);
  2230. len:=0;
  2231. move(dbgname[1],debuglink[len],length(dbgname));
  2232. inc(len,length(dbgname)+1);
  2233. len:=align(len,4);
  2234. if source_info.endian<>target_info.endian then
  2235. SwapEndian(dbgcrc);
  2236. move(dbgcrc,debuglink[len],sizeof(cardinal));
  2237. inc(len,4);
  2238. { Add section }
  2239. exesec:=FindExeSection(debuglinkname);
  2240. if not assigned(exesec) then
  2241. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  2242. exesec.SecOptions:=[oso_data,oso_keep];
  2243. exesec.SecAlign:=4;
  2244. objsec:=internalObjData.createsection(exesec.name,1,exesec.SecOptions);
  2245. internalObjData.writebytes(debuglink,len);
  2246. exesec.AddObjSection(objsec);
  2247. end;
  2248. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2249. begin
  2250. end;
  2251. function ByAddress(item1,item2:pointer):longint;
  2252. var
  2253. sym1:TObjSymbol absolute item1;
  2254. sym2:TObjSymbol absolute item2;
  2255. begin
  2256. result:=sym1.address-sym2.address;
  2257. end;
  2258. procedure TExeOutput.PrintMemoryMap;
  2259. var
  2260. exesec : TExeSection;
  2261. objsec : TObjSection;
  2262. objsym : TObjSymbol;
  2263. i,j,k,m: longint;
  2264. list : TFPList;
  2265. flag : boolean;
  2266. begin
  2267. if not assigned(exemap) then
  2268. exit;
  2269. { create a list of symbols sorted by address }
  2270. list:=TFPList.Create;
  2271. for i:=0 to ExeSymbolList.Count-1 do
  2272. list.Add(TExeSymbol(ExeSymbolList[i]).ObjSymbol);
  2273. list.Sort(@ByAddress);
  2274. exemap.AddMemoryMapHeader(ImageBase);
  2275. k:=0;
  2276. for i:=0 to ExeSectionList.Count-1 do
  2277. begin
  2278. exesec:=TExeSection(ExeSectionList[i]);
  2279. exemap.AddMemoryMapExeSection(exesec);
  2280. for j:=0 to exesec.ObjSectionList.count-1 do
  2281. begin
  2282. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2283. exemap.AddMemoryMapObjectSection(objsec);
  2284. while (k<list.Count) and (TObjSymbol(list[k]).Address<objsec.MemPos) do
  2285. inc(k);
  2286. while (k<list.Count) do
  2287. begin
  2288. objsym:=TObjSymbol(list[k]);
  2289. if objsym.address>objsec.MemPos+objsec.Size then
  2290. break;
  2291. if objsym.objsection=objsec then
  2292. exemap.AddMemoryMapSymbol(objsym)
  2293. else
  2294. begin
  2295. { Got a symbol with address falling into current section, but
  2296. belonging to a different section. This may happen for zero-length
  2297. sections because symbol list is sorted by address but not by section.
  2298. Do some look-ahead in this case. }
  2299. m:=k+1;
  2300. flag:=false;
  2301. while (m<list.Count) and (TObjSymbol(list[m]).Address=objsym.address) do
  2302. begin
  2303. if TObjSymbol(list[m]).objsection=objsec then
  2304. begin
  2305. flag:=true;
  2306. list.Exchange(k,m);
  2307. exemap.AddMemoryMapSymbol(TObjSymbol(list[k]));
  2308. break;
  2309. end;
  2310. inc(m);
  2311. end;
  2312. if not flag then
  2313. break;
  2314. end;
  2315. inc(k);
  2316. end;
  2317. end;
  2318. end;
  2319. list.Free;
  2320. end;
  2321. procedure TExeOutput.FixupSymbols;
  2322. procedure UpdateSymbol(objsym:TObjSymbol);
  2323. begin
  2324. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  2325. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2326. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  2327. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2328. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2329. end;
  2330. var
  2331. i : longint;
  2332. objsym : TObjSymbol;
  2333. exesym : TExeSymbol;
  2334. begin
  2335. { Print list of Unresolved External symbols }
  2336. for i:=0 to UnresolvedExeSymbols.count-1 do
  2337. begin
  2338. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2339. if (exesym.State<>symstate_defined) and
  2340. (exesym.objsymbol.bind<>AB_WEAK_EXTERNAL) then
  2341. Comment(V_Error,'Undefined symbol: '+exesym.name);
  2342. end;
  2343. {
  2344. Fixing up symbols is done in the following steps:
  2345. 1. Update common references
  2346. 2. Update external references
  2347. Symbols with objsection<>nil are removed from the lists,
  2348. remaining ones can be processed later by calling this method again.
  2349. }
  2350. { Step 1, Update commons. Preserve the original symbol size and bind,
  2351. this is needed for correct relocation of DJCOFF files. }
  2352. for i:=0 to CommonObjSymbols.count-1 do
  2353. begin
  2354. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2355. if objsym.bind<>AB_COMMON then
  2356. internalerror(200606241);
  2357. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2358. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2359. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2360. end;
  2361. { Step 2, Update externals }
  2362. for i:=0 to ExternalObjSymbols.count-1 do
  2363. begin
  2364. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  2365. if not (objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) then
  2366. internalerror(200606242);
  2367. UpdateSymbol(objsym);
  2368. { Collect symbols that resolve to indirect functions,
  2369. they will need additional target-specific processing. }
  2370. if objsym.typ=AT_GNU_IFUNC then
  2371. IndirectObjSymbols.Add(objsym)
  2372. else if assigned(objsym.objsection) then
  2373. ExternalObjSymbols[i]:=nil;
  2374. end;
  2375. CommonObjSymbols.Clear;
  2376. ExternalObjSymbols.Pack;
  2377. end;
  2378. procedure TExeOutput.MergeStabs;
  2379. var
  2380. stabexesec,
  2381. stabstrexesec : TExeSection;
  2382. relocsec,
  2383. currstabsec,
  2384. currstabstrsec,
  2385. mergedstabsec,
  2386. mergedstabstrsec : TObjSection;
  2387. hstabreloc,
  2388. currstabreloc : TObjRelocation;
  2389. i,j : longint;
  2390. currstabrelocidx,
  2391. mergestabcnt,
  2392. stabcnt : longword;
  2393. skipstab : boolean;
  2394. skipfun : boolean;
  2395. hstab : TObjStabEntry;
  2396. stabrelocofs : longword;
  2397. buf : array[0..1023] of byte;
  2398. bufend,
  2399. bufsize : longint;
  2400. begin
  2401. stabexesec:=FindExeSection('.stab');
  2402. stabstrexesec:=FindExeSection('.stabstr');
  2403. if (stabexesec=nil) or
  2404. (stabstrexesec=nil) or
  2405. (stabexesec.ObjSectionlist.count=0) then
  2406. exit;
  2407. { Create new stabsection }
  2408. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  2409. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  2410. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  2411. { write stab for hdrsym }
  2412. fillchar(hstab,sizeof(TObjStabEntry),0);
  2413. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  2414. mergestabcnt:=1;
  2415. { .stabstr starts with a #0 }
  2416. buf[0]:=0;
  2417. mergedstabstrsec.write(buf[0],1);
  2418. skipfun:=false;
  2419. { Copy stabs and corresponding Relocations }
  2420. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  2421. begin
  2422. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  2423. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  2424. if assigned(currstabstrsec) then
  2425. begin
  2426. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  2427. currstabsec.Data.seek(0);
  2428. currstabrelocidx:=0;
  2429. for j:=0 to stabcnt-1 do
  2430. begin
  2431. hstabreloc:=nil;
  2432. skipstab:=false;
  2433. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  2434. { Only include first hdrsym stab }
  2435. if hstab.ntype=0 then
  2436. skipstab:=true;
  2437. if skipfun then
  2438. begin
  2439. { Skip all stabs for function body until N_RBRAC }
  2440. skipfun:=hstab.ntype<>N_RBRAC;
  2441. skipstab:=true;
  2442. end;
  2443. if not skipstab then
  2444. begin
  2445. { Find corresponding Relocation }
  2446. currstabreloc:=nil;
  2447. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  2448. begin
  2449. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  2450. if assigned(currstabreloc) and
  2451. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2452. break;
  2453. inc(currstabrelocidx);
  2454. end;
  2455. if assigned(currstabreloc) and
  2456. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2457. begin
  2458. hstabReloc:=currstabReloc;
  2459. inc(currstabrelocidx);
  2460. end;
  2461. { Check if the stab is refering to a removed section }
  2462. if assigned(hstabreloc) then
  2463. begin
  2464. if assigned(hstabreloc.Symbol) then
  2465. relocsec:=hstabreloc.Symbol.ObjSection
  2466. else
  2467. relocsec:=hstabreloc.ObjSection;
  2468. if not assigned(relocsec) then
  2469. internalerror(200603302);
  2470. if not relocsec.Used then
  2471. begin
  2472. skipstab:=true;
  2473. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  2474. begin
  2475. currstabstrsec.Data.seek(hstab.strpos);
  2476. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2477. bufend:=indexbyte(buf,bufsize,Ord(':'));
  2478. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  2479. skipfun:=true;
  2480. end;
  2481. end;
  2482. end;
  2483. end;
  2484. if not skipstab then
  2485. begin
  2486. { Copy string in stabstr }
  2487. if hstab.strpos<>0 then
  2488. begin
  2489. currstabstrsec.Data.seek(hstab.strpos);
  2490. hstab.strpos:=mergedstabstrsec.Size;
  2491. repeat
  2492. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2493. bufend:=indexbyte(buf,bufsize,0);
  2494. if bufend=-1 then
  2495. bufend:=bufsize
  2496. else
  2497. begin
  2498. { include the #0 }
  2499. inc(bufend);
  2500. end;
  2501. mergedstabstrsec.write(buf,bufend);
  2502. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2503. end;
  2504. { Copy and Update the relocation }
  2505. if assigned(hstabreloc) then
  2506. begin
  2507. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2508. { Remove from List without freeing the object }
  2509. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2510. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2511. end;
  2512. { Write updated stab }
  2513. mergedstabsec.write(hstab,sizeof(hstab));
  2514. inc(mergestabcnt);
  2515. end;
  2516. end;
  2517. end;
  2518. { Unload stabs }
  2519. if assigned(currstabstrsec) then
  2520. begin
  2521. currstabstrsec.Used:=False;
  2522. currstabstrsec.ReleaseData;
  2523. end;
  2524. currstabsec.Used:=false;
  2525. currstabsec.ReleaseData;
  2526. end;
  2527. { Generate new HdrSym }
  2528. if mergedstabsec.Size>0 then
  2529. begin
  2530. hstab.strpos:=1;
  2531. hstab.ntype:=0;
  2532. hstab.nother:=0;
  2533. hstab.ndesc:=word(mergestabcnt-1);
  2534. hstab.nvalue:=mergedstabstrsec.Size;
  2535. mergedstabsec.Data.seek(0);
  2536. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2537. end;
  2538. { Replace all sections with our combined stabsec }
  2539. stabexesec.ObjSectionList.Clear;
  2540. stabstrexesec.ObjSectionList.Clear;
  2541. stabexesec.AddObjSection(mergedstabsec);
  2542. stabstrexesec.AddObjSection(mergedstabstrsec);
  2543. end;
  2544. procedure TExeOutput.MarkEmptySections;
  2545. var
  2546. i, j : longint;
  2547. exesec : TExeSection;
  2548. doremove : boolean;
  2549. begin
  2550. for i:=0 to ExeSectionList.Count-1 do
  2551. begin
  2552. exesec:=TExeSection(ExeSectionList[i]);
  2553. doremove:=not(oso_keep in exesec.SecOptions) and
  2554. (
  2555. (exesec.ObjSectionlist.count=0) or
  2556. (
  2557. (cs_link_strip in current_settings.globalswitches) and
  2558. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2559. (oso_debug in exesec.SecOptions)
  2560. )
  2561. );
  2562. if not doremove then
  2563. begin
  2564. { Check if section has no actual data }
  2565. doremove:=true;
  2566. for j:=0 to exesec.ObjSectionList.Count-1 do
  2567. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2568. begin
  2569. doremove:=false;
  2570. break;
  2571. end;
  2572. end;
  2573. if doremove then
  2574. begin
  2575. Comment(V_Debug,'Disabling empty section '+exesec.name);
  2576. exesec.SecOptions:=exesec.SecOptions+[oso_disabled];
  2577. end;
  2578. end;
  2579. end;
  2580. procedure TExeOutput.RemoveDisabledSections;
  2581. var
  2582. i: longint;
  2583. exesec: TExeSection;
  2584. begin
  2585. for i:=0 to ExeSectionList.Count-1 do
  2586. begin
  2587. exesec:=TExeSection(ExeSectionList[i]);
  2588. if (oso_disabled in exesec.SecOptions) then
  2589. ExeSectionList[i]:=nil;
  2590. end;
  2591. ExeSectionList.Pack;
  2592. end;
  2593. procedure TExeOutput.RemoveDebugInfo;
  2594. var
  2595. i : longint;
  2596. exesec : TExeSection;
  2597. begin
  2598. for i:=0 to ExeSectionList.Count-1 do
  2599. begin
  2600. exesec:=TExeSection(ExeSectionList[i]);
  2601. if (oso_debug in exesec.SecOptions) then
  2602. ExeSectionList[i]:=nil;
  2603. end;
  2604. ExeSectionList.Pack;
  2605. end;
  2606. procedure TExeOutput.RemoveUnreferencedSections;
  2607. var
  2608. ObjSectionWorkList : TFPObjectList;
  2609. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2610. begin
  2611. if not aobjsec.Used then
  2612. begin
  2613. aobjsec.Used:=true;
  2614. ObjSectionWorkList.Add(aobjsec);
  2615. end;
  2616. end;
  2617. procedure DoReloc(objreloc:TObjRelocation);
  2618. var
  2619. objsym : TObjSymbol;
  2620. refobjsec : TObjSection;
  2621. begin
  2622. { Disabled Relocation to 0 }
  2623. if objreloc.typ=RELOC_ZERO then
  2624. exit;
  2625. if assigned(objreloc.symbol) then
  2626. begin
  2627. objsym:=objreloc.symbol;
  2628. if objsym.bind<>AB_LOCAL then
  2629. begin
  2630. if not(assigned(objsym.exesymbol) and
  2631. (objsym.exesymbol.State in [symstate_defined,symstate_dynamic,symstate_defweak])) then
  2632. internalerror(200603063);
  2633. objsym:=objsym.exesymbol.objsymbol;
  2634. end;
  2635. if not assigned(objsym.objsection) then
  2636. begin
  2637. objsym.exesymbol.state:=symstate_dynamic;
  2638. exit;
  2639. end
  2640. else
  2641. refobjsec:=objsym.objsection;
  2642. end
  2643. else
  2644. if assigned(objreloc.objsection) then
  2645. refobjsec:=objreloc.objsection
  2646. else
  2647. internalerror(200603316);
  2648. if assigned(exemap) then
  2649. begin
  2650. objsym:=objreloc.symbol;
  2651. if assigned(objsym) then
  2652. exemap.Add(' References '+objsym.name+' in '
  2653. +refobjsec.fullname)
  2654. else
  2655. exemap.Add(' References '+refobjsec.fullname);
  2656. end;
  2657. AddToObjSectionWorkList(refobjsec);
  2658. end;
  2659. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  2660. var
  2661. i : longint;
  2662. objreloc : TObjRelocation;
  2663. begin
  2664. objreloc:=vtable.VTableRef(VTableIdx);
  2665. if assigned(objreloc) then
  2666. begin
  2667. { Process the relocation now if the ObjSection is
  2668. already processed and marked as used. Otherwise we leave it
  2669. unprocessed. It'll then be resolved when the ObjSection is
  2670. changed to Used }
  2671. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  2672. DoReloc(objreloc);
  2673. end;
  2674. { This recursive walking is done here instead of
  2675. in TExeVTable.VTableRef because we can now process
  2676. all needed relocations }
  2677. for i:=0 to vtable.ChildList.Count-1 do
  2678. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  2679. end;
  2680. var
  2681. hs : string;
  2682. i,j,k : longint;
  2683. exesec : TExeSection;
  2684. objdata : TObjData;
  2685. objsec : TObjSection;
  2686. objsym : TObjSymbol;
  2687. code : integer;
  2688. vtableidx : longint;
  2689. vtableexesym : TExeSymbol;
  2690. begin
  2691. ObjSectionWorkList:=TFPObjectList.Create(false);
  2692. if assigned(exemap) then
  2693. exemap.AddHeader('Removing unreferenced sections');
  2694. { Initialize by marking all sections unused and
  2695. adding the sections with oso_keep flags to the ObjSectionWorkList }
  2696. for i:=0 to ObjDataList.Count-1 do
  2697. begin
  2698. ObjData:=TObjData(ObjDataList[i]);
  2699. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2700. begin
  2701. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2702. objsec.Used:=false;
  2703. { TODO: remove debug section always keep}
  2704. if oso_debug in objsec.secoptions then
  2705. objsec.Used:=true;
  2706. if (oso_keep in objsec.secoptions) then
  2707. begin
  2708. AddToObjSectionWorkList(objsec);
  2709. if objsec.name='.fpc.n_links' then
  2710. objsec.Used:=false;
  2711. end;
  2712. end;
  2713. end;
  2714. if assigned(entrysym) then
  2715. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  2716. { Process all sections, add new sections to process based
  2717. on the symbol references }
  2718. while ObjSectionWorkList.Count>0 do
  2719. begin
  2720. objsec:=TObjSection(ObjSectionWorkList.Last);
  2721. if assigned(exemap) then
  2722. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  2723. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  2724. { Process Relocations }
  2725. for i:=0 to objsec.ObjRelocations.count-1 do
  2726. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  2727. { Process Virtual Entry calls }
  2728. if cs_link_opt_vtable in current_settings.globalswitches then
  2729. begin
  2730. for i:=0 to objsec.VTRefList.count-1 do
  2731. begin
  2732. objsym:=TObjSymbol(objsec.VTRefList[i]);
  2733. hs:=objsym.name;
  2734. Delete(hs,1,Pos('_',hs));
  2735. k:=Pos('$$',hs);
  2736. if k=0 then
  2737. internalerror(200603314);
  2738. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2739. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2740. if (code<>0) then
  2741. internalerror(200603317);
  2742. if not assigned(vtableexesym) then
  2743. internalerror(200603315);
  2744. if not assigned(vtableexesym.vtable) then
  2745. internalerror(200603316);
  2746. DoVTableRef(vtableexesym.vtable,vtableidx);
  2747. end;
  2748. end;
  2749. end;
  2750. ObjSectionWorkList.Free;
  2751. ObjSectionWorkList:=nil;
  2752. { Remove unused objsections from ExeSectionList }
  2753. for i:=0 to ExeSectionList.Count-1 do
  2754. begin
  2755. exesec:=TExeSection(ExeSectionList[i]);
  2756. for j:=0 to exesec.ObjSectionlist.count-1 do
  2757. begin
  2758. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2759. if not objsec.used then
  2760. begin
  2761. if assigned(exemap) then
  2762. exemap.Add('Removing '+objsec.FullName);
  2763. exesec.ObjSectionlist[j]:=nil;
  2764. objsec.ReleaseData;
  2765. end;
  2766. end;
  2767. exesec.ObjSectionlist.Pack;
  2768. end;
  2769. end;
  2770. procedure TExeOutput.FixupRelocations;
  2771. var
  2772. i,j : longint;
  2773. exesec : TExeSection;
  2774. objsec : TObjSection;
  2775. begin
  2776. for i:=0 to ExeSectionList.Count-1 do
  2777. begin
  2778. exesec:=TExeSection(ExeSectionList[i]);
  2779. if not assigned(exesec) then
  2780. continue;
  2781. for j:=0 to exesec.ObjSectionlist.count-1 do
  2782. begin
  2783. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2784. if not objsec.Used then
  2785. internalerror(200603301);
  2786. if (objsec.ObjRelocations.Count>0) and
  2787. not assigned(objsec.data) then
  2788. internalerror(200205183);
  2789. DoRelocationFixup(objsec);
  2790. {for size = 0 data is not valid PM }
  2791. if assigned(objsec.data) and (objsec.data.size<>objsec.size) then
  2792. internalerror(2010092801);
  2793. end;
  2794. end;
  2795. end;
  2796. procedure TExeOutput.RemoveUnusedExeSymbols;
  2797. var
  2798. i : longint;
  2799. sym : TExeSymbol;
  2800. begin
  2801. { Removing unused symbols }
  2802. for i:=0 to ExeSymbolList.Count-1 do
  2803. begin
  2804. sym:=TExeSymbol(ExeSymbolList[i]);
  2805. { an unresolved weak symbol has objsection=nil }
  2806. if assigned(sym.ObjSymbol.objsection) and
  2807. (not sym.ObjSymbol.objsection.Used) then
  2808. ExeSymbolList[i]:=nil;
  2809. end;
  2810. ExeSymbolList.Pack;
  2811. end;
  2812. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  2813. begin
  2814. if AValue>MaxMemPos then
  2815. Message1(link_f_executable_too_big, target_os_string);
  2816. FCurrMemPos:=AValue;
  2817. end;
  2818. procedure TExeOutput.WriteExeSectionContent;
  2819. var
  2820. exesec : TExeSection;
  2821. objsec : TObjSection;
  2822. i,j : longint;
  2823. begin
  2824. for j:=0 to ExeSectionList.Count-1 do
  2825. begin
  2826. exesec:=TExeSection(ExeSectionList[j]);
  2827. { don't write normal section if writing only debug info }
  2828. if (ExeWriteMode=ewm_dbgonly) and
  2829. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  2830. continue;
  2831. if oso_data in exesec.SecOptions then
  2832. begin
  2833. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  2834. for i:=0 to exesec.ObjSectionList.Count-1 do
  2835. begin
  2836. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2837. if oso_data in objsec.secoptions then
  2838. begin
  2839. if not assigned(objsec.data) then
  2840. internalerror(200603042);
  2841. FWriter.writezeros(objsec.dataalignbytes);
  2842. if objsec.DataPos<>FWriter.Size then
  2843. internalerror(200602251);
  2844. FWriter.writearray(objsec.data);
  2845. end;
  2846. end;
  2847. end;
  2848. end;
  2849. end;
  2850. {****************************************************************************
  2851. TObjInput
  2852. ****************************************************************************}
  2853. constructor TObjInput.create;
  2854. begin
  2855. end;
  2856. destructor TObjInput.destroy;
  2857. begin
  2858. inherited destroy;
  2859. end;
  2860. function TObjInput.newObjData(const n:string):TObjData;
  2861. begin
  2862. result:=CObjData.create(n);
  2863. end;
  2864. procedure TObjInput.inputerror(const s : string);
  2865. begin
  2866. Comment(V_Error,s+' while reading '+InputFileName);
  2867. end;
  2868. class function TObjInput.CanReadObjData(AReader:TObjectReader):boolean;
  2869. begin
  2870. result:=false;
  2871. end;
  2872. procedure TObjInput.ReadSectionContent(Data:TObjData);
  2873. var
  2874. i: longint;
  2875. sec: TObjSection;
  2876. begin
  2877. for i:=0 to Data.ObjSectionList.Count-1 do
  2878. begin
  2879. sec:=TObjSection(Data.ObjSectionList[i]);
  2880. { Skip debug sections }
  2881. if (oso_debug in sec.SecOptions) and
  2882. (cs_link_strip in current_settings.globalswitches) and
  2883. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  2884. continue;
  2885. if assigned(sec.Data) then
  2886. begin
  2887. FReader.Seek(sec.datapos);
  2888. if not FReader.ReadArray(sec.data,sec.Size) then
  2889. begin
  2890. InputError('Can''t read object data');
  2891. exit;
  2892. end;
  2893. end;
  2894. end;
  2895. end;
  2896. {$ifdef MEMDEBUG}
  2897. initialization
  2898. memobjsymbols:=TMemDebug.create('ObjSymbols');
  2899. memobjsymbols.stop;
  2900. memobjsections:=TMemDebug.create('ObjSections');
  2901. memobjsections.stop;
  2902. finalization
  2903. memobjsymbols.free;
  2904. memobjsections.free;
  2905. {$endif MEMDEBUG}
  2906. end.