ogbase.pas 110 KB

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