ogbase.pas 110 KB

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