ogbase.pas 107 KB

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