ogbase.pas 110 KB

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