ogbase.pas 106 KB

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