ogbase.pas 106 KB

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