ogbase.pas 110 KB

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