ogbase.pas 107 KB

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