ogbase.pas 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169
  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);
  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);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);
  1300. begin
  1301. ObjSectionList.Add(objsec);
  1302. if (SecOptions<>[]) then
  1303. begin
  1304. { Only if the section contains (un)initialized data the
  1305. data flag must match. This check is not needed if the
  1306. section is empty for a symbol allocation }
  1307. if (objsec.size>0) and
  1308. ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1309. Comment(V_Error,'Incompatible section options');
  1310. end
  1311. else
  1312. begin
  1313. { inherit section options }
  1314. SecOptions:=SecOptions+objsec.SecOptions;
  1315. end;
  1316. { relate ObjSection to ExeSection, and mark it Used by default }
  1317. SecAlign:=max(objsec.SecAlign,SecAlign);
  1318. objsec.ExeSection:=self;
  1319. objsec.Used:=true;
  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);
  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);
  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. if exesym.State=symstate_undefined then
  1966. begin
  1967. exesym.ObjSymbol:=objsym;
  1968. exesym.State:=symstate_common;
  1969. end;
  1970. if assigned(objsym.objsection) and
  1971. (objsym.objsection.objdata=internalObjData) then
  1972. FProvidedObjSymbols.add(objsym)
  1973. else
  1974. CommonObjSymbols.add(objsym);
  1975. end;
  1976. end;
  1977. end;
  1978. end;
  1979. procedure LoadLibrary(lib:TStaticLibrary);
  1980. var
  1981. j,k,oldcount: longint;
  1982. members: TFPObjectList;
  1983. exesym: TExeSymbol;
  1984. objinput: TObjInput;
  1985. begin
  1986. case lib.Kind of
  1987. lkArchive:
  1988. begin
  1989. { Process list of Unresolved External symbols, we need
  1990. to use a while loop because the list can be extended when
  1991. we load members from the library. }
  1992. j:=0;
  1993. while (j<UnresolvedExeSymbols.count) do
  1994. begin
  1995. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  1996. { Check first if the symbol is still undefined }
  1997. if (exesym.State=symstate_undefined) and (exesym.ObjSymbol.bind<>AB_WEAK_EXTERNAL) then
  1998. begin
  1999. if lib.ArReader.OpenFile(exesym.name) then
  2000. begin
  2001. if assigned(exemap) then
  2002. begin
  2003. if firstarchive then
  2004. begin
  2005. exemap.Add('');
  2006. exemap.Add('Archive member included because of file (symbol)');
  2007. exemap.Add('');
  2008. firstarchive:=false;
  2009. end;
  2010. exemap.Add(lib.ArReader.FileName+' - '+
  2011. {exesym.ObjSymbol.ObjSection.FullName+}
  2012. '('+exesym.Name+')');
  2013. end;
  2014. objinput:=lib.ObjInputClass.Create;
  2015. objdata:=objinput.newObjData(lib.ArReader.FileName);
  2016. objinput.ReadObjData(lib.ArReader,objdata);
  2017. objinput.free;
  2018. AddObjData(objdata);
  2019. LoadObjDataSymbols(objdata);
  2020. lib.ArReader.CloseFile;
  2021. end;
  2022. end;
  2023. inc(j);
  2024. end;
  2025. end;
  2026. lkGroup:
  2027. begin
  2028. { repeatedly process members of the group until no new
  2029. unresolved symbols appear }
  2030. members:=lib.GroupMembers;
  2031. repeat
  2032. oldcount:=UnresolvedExeSymbols.count;
  2033. for k:=0 to members.Count-1 do
  2034. LoadLibrary(TStaticLibrary(members[k]));
  2035. until UnresolvedExeSymbols.count=oldcount;
  2036. end;
  2037. lkObject:
  2038. { TODO: ownership of objdata }
  2039. //if lib.objdata.is_dynamic then
  2040. Load_DynamicObject(lib.objdata);
  2041. {else
  2042. begin
  2043. AddObjData(lib.objdata);
  2044. LoadObjDataSymbols(lib.objdata);
  2045. end;}
  2046. end;
  2047. end;
  2048. begin
  2049. VTEntryList:=TFPObjectList.Create(false);
  2050. VTInheritList:=TFPObjectList.Create(false);
  2051. {
  2052. The symbol resolving is done in 4 steps:
  2053. 1. Register symbols from objects
  2054. 2. Find symbols in static libraries
  2055. 3. Define symbols PROVIDEd by the link script
  2056. 4. Define still undefined common symbols
  2057. }
  2058. { Step 1, Register symbols from objects }
  2059. for i:=0 to ObjDataList.Count-1 do
  2060. begin
  2061. ObjData:=TObjData(ObjDataList[i]);
  2062. LoadObjDataSymbols(ObjData);
  2063. end;
  2064. PackUnresolvedExeSymbols('in objects');
  2065. { Step 2, Find unresolved symbols in the libraries }
  2066. firstarchive:=true;
  2067. for i:=0 to StaticLibraryList.Count-1 do
  2068. LoadLibrary(TStaticLibrary(StaticLibraryList[i]));
  2069. PackUnresolvedExeSymbols('after static libraries');
  2070. { Step 3, handle symbols provided in script }
  2071. for i:=0 to FProvidedObjSymbols.count-1 do
  2072. begin
  2073. objsym:=TObjSymbol(FProvidedObjSymbols[i]);
  2074. if objsym.exesymbol.State=symstate_defined then
  2075. continue;
  2076. objsym.exesymbol.objsymbol:=objsym;
  2077. objsym.bind:=AB_GLOBAL;
  2078. objsym.exesymbol.State:=symstate_defined;
  2079. end;
  2080. PackUnresolvedExeSymbols('after defining symbols provided by link script');
  2081. { Step 4, Match common symbols or add to the globals }
  2082. firstcommon:=true;
  2083. for i:=0 to CommonObjSymbols.count-1 do
  2084. begin
  2085. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2086. if objsym.exesymbol.State=symstate_defined then
  2087. begin
  2088. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  2089. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  2090. end
  2091. else
  2092. begin
  2093. { allocate new objsymbol in .bss of *COMMON* and assign
  2094. it to the exesymbol }
  2095. if firstcommon then
  2096. begin
  2097. if assigned(exemap) then
  2098. exemap.AddCommonSymbolsHeader;
  2099. firstcommon:=false;
  2100. end;
  2101. internalObjData.setsection(commonObjSection);
  2102. internalObjData.allocalign(var_align(objsym.size));
  2103. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_DATA);
  2104. commonsym.size:=objsym.size;
  2105. internalObjData.alloc(objsym.size);
  2106. if assigned(exemap) then
  2107. exemap.AddCommonSymbol(commonsym);
  2108. { Assign to the exesymbol }
  2109. objsym.exesymbol.objsymbol:=commonsym;
  2110. objsym.exesymbol.state:=symstate_defined;
  2111. end;
  2112. end;
  2113. PackUnresolvedExeSymbols('after defining COMMON symbols');
  2114. { Find entry symbol and print in map }
  2115. if (EntryName<>'') then
  2116. begin
  2117. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  2118. if assigned(exesym) then
  2119. begin
  2120. EntrySym:=exesym.ObjSymbol;
  2121. if assigned(exemap) then
  2122. begin
  2123. exemap.Add('');
  2124. exemap.Add('Entry symbol '+EntryName);
  2125. end;
  2126. end
  2127. else
  2128. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  2129. end;
  2130. { Generate VTable tree }
  2131. if cs_link_opt_vtable in current_settings.globalswitches then
  2132. BuildVTableTree(VTInheritList,VTEntryList);
  2133. VTInheritList.Free;
  2134. VTEntryList.Free;
  2135. end;
  2136. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  2137. var
  2138. debuglink : array[0..1023] of byte;
  2139. len : longint;
  2140. objsec : TObjSection;
  2141. exesec : TExeSection;
  2142. begin
  2143. { From the gdb manual chapter 15. GDB Files:
  2144. * A filename, with any leading directory components removed, followed by a zero byte,
  2145. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  2146. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  2147. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  2148. }
  2149. fillchar(debuglink,sizeof(debuglink),0);
  2150. len:=0;
  2151. move(dbgname[1],debuglink[len],length(dbgname));
  2152. inc(len,length(dbgname)+1);
  2153. len:=align(len,4);
  2154. if source_info.endian<>target_info.endian then
  2155. SwapEndian(dbgcrc);
  2156. move(dbgcrc,debuglink[len],sizeof(cardinal));
  2157. inc(len,4);
  2158. { Add section }
  2159. exesec:=FindExeSection(debuglinkname);
  2160. if not assigned(exesec) then
  2161. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  2162. exesec.SecOptions:=[oso_data,oso_keep];
  2163. exesec.SecAlign:=4;
  2164. objsec:=internalObjData.createsection(exesec.name,1,exesec.SecOptions);
  2165. internalObjData.writebytes(debuglink,len);
  2166. exesec.AddObjSection(objsec);
  2167. end;
  2168. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2169. begin
  2170. end;
  2171. function ByAddress(item1,item2:pointer):longint;
  2172. var
  2173. sym1:TObjSymbol absolute item1;
  2174. sym2:TObjSymbol absolute item2;
  2175. begin
  2176. result:=sym1.address-sym2.address;
  2177. end;
  2178. procedure TExeOutput.PrintMemoryMap;
  2179. var
  2180. exesec : TExeSection;
  2181. objsec : TObjSection;
  2182. objsym : TObjSymbol;
  2183. i,j,k,m: longint;
  2184. list : TFPList;
  2185. flag : boolean;
  2186. begin
  2187. if not assigned(exemap) then
  2188. exit;
  2189. { create a list of symbols sorted by address }
  2190. list:=TFPList.Create;
  2191. for i:=0 to ExeSymbolList.Count-1 do
  2192. list.Add(TExeSymbol(ExeSymbolList[i]).ObjSymbol);
  2193. list.Sort(@ByAddress);
  2194. exemap.AddMemoryMapHeader(ImageBase);
  2195. k:=0;
  2196. for i:=0 to ExeSectionList.Count-1 do
  2197. begin
  2198. exesec:=TExeSection(ExeSectionList[i]);
  2199. exemap.AddMemoryMapExeSection(exesec);
  2200. for j:=0 to exesec.ObjSectionList.count-1 do
  2201. begin
  2202. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2203. exemap.AddMemoryMapObjectSection(objsec);
  2204. while (k<list.Count) and (TObjSymbol(list[k]).Address<objsec.MemPos) do
  2205. inc(k);
  2206. while (k<list.Count) do
  2207. begin
  2208. objsym:=TObjSymbol(list[k]);
  2209. if objsym.address>objsec.MemPos+objsec.Size then
  2210. break;
  2211. if objsym.objsection=objsec then
  2212. exemap.AddMemoryMapSymbol(objsym)
  2213. else
  2214. begin
  2215. { Got a symbol with address falling into current section, but
  2216. belonging to a different section. This may happen for zero-length
  2217. sections because symbol list is sorted by address but not by section.
  2218. Do some look-ahead in this case. }
  2219. m:=k+1;
  2220. flag:=false;
  2221. while (m<list.Count) and (TObjSymbol(list[m]).Address=objsym.address) do
  2222. begin
  2223. if TObjSymbol(list[m]).objsection=objsec then
  2224. begin
  2225. flag:=true;
  2226. list.Exchange(k,m);
  2227. exemap.AddMemoryMapSymbol(TObjSymbol(list[k]));
  2228. break;
  2229. end;
  2230. inc(m);
  2231. end;
  2232. if not flag then
  2233. break;
  2234. end;
  2235. inc(k);
  2236. end;
  2237. end;
  2238. end;
  2239. list.Free;
  2240. end;
  2241. procedure TExeOutput.FixupSymbols;
  2242. procedure UpdateSymbol(objsym:TObjSymbol);
  2243. begin
  2244. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  2245. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2246. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  2247. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2248. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2249. end;
  2250. var
  2251. i : longint;
  2252. objsym : TObjSymbol;
  2253. exesym : TExeSymbol;
  2254. begin
  2255. { Print list of Unresolved External symbols }
  2256. for i:=0 to UnresolvedExeSymbols.count-1 do
  2257. begin
  2258. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2259. if exesym.State<>symstate_defined then
  2260. Comment(V_Error,'Undefined symbol: '+exesym.name);
  2261. end;
  2262. {
  2263. Fixing up symbols is done in the following steps:
  2264. 1. Update common references
  2265. 2. Update external references
  2266. }
  2267. { Step 1, Update commons }
  2268. for i:=0 to CommonObjSymbols.count-1 do
  2269. begin
  2270. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2271. if objsym.bind<>AB_COMMON then
  2272. internalerror(200606241);
  2273. UpdateSymbol(objsym);
  2274. end;
  2275. { Step 2, Update externals }
  2276. for i:=0 to ExternalObjSymbols.count-1 do
  2277. begin
  2278. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  2279. if objsym.bind<>AB_EXTERNAL then
  2280. internalerror(200606242);
  2281. UpdateSymbol(objsym);
  2282. end;
  2283. end;
  2284. procedure TExeOutput.MergeStabs;
  2285. var
  2286. stabexesec,
  2287. stabstrexesec : TExeSection;
  2288. relocsec,
  2289. currstabsec,
  2290. currstabstrsec,
  2291. mergedstabsec,
  2292. mergedstabstrsec : TObjSection;
  2293. hstabreloc,
  2294. currstabreloc : TObjRelocation;
  2295. i,j : longint;
  2296. currstabrelocidx,
  2297. mergestabcnt,
  2298. stabcnt : longword;
  2299. skipstab : boolean;
  2300. skipfun : boolean;
  2301. hstab : TObjStabEntry;
  2302. stabrelocofs : longword;
  2303. buf : array[0..1023] of byte;
  2304. bufend,
  2305. bufsize : longint;
  2306. begin
  2307. stabexesec:=FindExeSection('.stab');
  2308. stabstrexesec:=FindExeSection('.stabstr');
  2309. if (stabexesec=nil) or
  2310. (stabstrexesec=nil) or
  2311. (stabexesec.ObjSectionlist.count=0) then
  2312. exit;
  2313. { Create new stabsection }
  2314. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  2315. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  2316. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  2317. { write stab for hdrsym }
  2318. fillchar(hstab,sizeof(TObjStabEntry),0);
  2319. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  2320. mergestabcnt:=1;
  2321. { .stabstr starts with a #0 }
  2322. buf[0]:=0;
  2323. mergedstabstrsec.write(buf[0],1);
  2324. skipfun:=false;
  2325. { Copy stabs and corresponding Relocations }
  2326. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  2327. begin
  2328. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  2329. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  2330. if assigned(currstabstrsec) then
  2331. begin
  2332. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  2333. currstabsec.Data.seek(0);
  2334. currstabrelocidx:=0;
  2335. for j:=0 to stabcnt-1 do
  2336. begin
  2337. hstabreloc:=nil;
  2338. skipstab:=false;
  2339. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  2340. { Only include first hdrsym stab }
  2341. if hstab.ntype=0 then
  2342. skipstab:=true;
  2343. if skipfun then
  2344. begin
  2345. { Skip all stabs for function body until N_RBRAC }
  2346. skipfun:=hstab.ntype<>N_RBRAC;
  2347. skipstab:=true;
  2348. end;
  2349. if not skipstab then
  2350. begin
  2351. { Find corresponding Relocation }
  2352. currstabreloc:=nil;
  2353. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  2354. begin
  2355. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  2356. if assigned(currstabreloc) and
  2357. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2358. break;
  2359. inc(currstabrelocidx);
  2360. end;
  2361. if assigned(currstabreloc) and
  2362. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2363. begin
  2364. hstabReloc:=currstabReloc;
  2365. inc(currstabrelocidx);
  2366. end;
  2367. { Check if the stab is refering to a removed section }
  2368. if assigned(hstabreloc) then
  2369. begin
  2370. if assigned(hstabreloc.Symbol) then
  2371. relocsec:=hstabreloc.Symbol.ObjSection
  2372. else
  2373. relocsec:=hstabreloc.ObjSection;
  2374. if not assigned(relocsec) then
  2375. internalerror(200603302);
  2376. if not relocsec.Used then
  2377. begin
  2378. skipstab:=true;
  2379. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  2380. begin
  2381. currstabstrsec.Data.seek(hstab.strpos);
  2382. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2383. bufend:=indexbyte(buf,bufsize,Ord(':'));
  2384. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  2385. skipfun:=true;
  2386. end;
  2387. end;
  2388. end;
  2389. end;
  2390. if not skipstab then
  2391. begin
  2392. { Copy string in stabstr }
  2393. if hstab.strpos<>0 then
  2394. begin
  2395. currstabstrsec.Data.seek(hstab.strpos);
  2396. hstab.strpos:=mergedstabstrsec.Size;
  2397. repeat
  2398. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2399. bufend:=indexbyte(buf,bufsize,0);
  2400. if bufend=-1 then
  2401. bufend:=bufsize
  2402. else
  2403. begin
  2404. { include the #0 }
  2405. inc(bufend);
  2406. end;
  2407. mergedstabstrsec.write(buf,bufend);
  2408. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2409. end;
  2410. { Copy and Update the relocation }
  2411. if assigned(hstabreloc) then
  2412. begin
  2413. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2414. { Remove from List without freeing the object }
  2415. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2416. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2417. end;
  2418. { Write updated stab }
  2419. mergedstabsec.write(hstab,sizeof(hstab));
  2420. inc(mergestabcnt);
  2421. end;
  2422. end;
  2423. end;
  2424. { Unload stabs }
  2425. if assigned(currstabstrsec) then
  2426. begin
  2427. currstabstrsec.Used:=False;
  2428. currstabstrsec.ReleaseData;
  2429. end;
  2430. currstabsec.Used:=false;
  2431. currstabsec.ReleaseData;
  2432. end;
  2433. { Generate new HdrSym }
  2434. if mergedstabsec.Size>0 then
  2435. begin
  2436. hstab.strpos:=1;
  2437. hstab.ntype:=0;
  2438. hstab.nother:=0;
  2439. hstab.ndesc:=word(mergestabcnt-1);
  2440. hstab.nvalue:=mergedstabstrsec.Size;
  2441. mergedstabsec.Data.seek(0);
  2442. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2443. end;
  2444. { Replace all sections with our combined stabsec }
  2445. stabexesec.ObjSectionList.Clear;
  2446. stabstrexesec.ObjSectionList.Clear;
  2447. stabexesec.AddObjSection(mergedstabsec);
  2448. stabstrexesec.AddObjSection(mergedstabstrsec);
  2449. end;
  2450. procedure TExeOutput.MarkEmptySections;
  2451. var
  2452. i, j : longint;
  2453. exesec : TExeSection;
  2454. doremove : boolean;
  2455. begin
  2456. for i:=0 to ExeSectionList.Count-1 do
  2457. begin
  2458. exesec:=TExeSection(ExeSectionList[i]);
  2459. doremove:=not(oso_keep in exesec.SecOptions) and
  2460. (
  2461. (exesec.ObjSectionlist.count=0) or
  2462. (
  2463. (cs_link_strip in current_settings.globalswitches) and
  2464. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2465. (oso_debug in exesec.SecOptions)
  2466. )
  2467. );
  2468. if not doremove then
  2469. begin
  2470. { Check if section has no actual data }
  2471. doremove:=true;
  2472. for j:=0 to exesec.ObjSectionList.Count-1 do
  2473. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2474. begin
  2475. doremove:=false;
  2476. break;
  2477. end;
  2478. end;
  2479. if doremove then
  2480. begin
  2481. Comment(V_Debug,'Disabling empty section '+exesec.name);
  2482. exesec.SecOptions:=exesec.SecOptions+[oso_disabled];
  2483. end;
  2484. end;
  2485. end;
  2486. procedure TExeOutput.RemoveDisabledSections;
  2487. var
  2488. i: longint;
  2489. exesec: TExeSection;
  2490. begin
  2491. for i:=0 to ExeSectionList.Count-1 do
  2492. begin
  2493. exesec:=TExeSection(ExeSectionList[i]);
  2494. if (oso_disabled in exesec.SecOptions) then
  2495. ExeSectionList[i]:=nil;
  2496. end;
  2497. ExeSectionList.Pack;
  2498. end;
  2499. procedure TExeOutput.RemoveDebugInfo;
  2500. var
  2501. i : longint;
  2502. exesec : TExeSection;
  2503. begin
  2504. for i:=0 to ExeSectionList.Count-1 do
  2505. begin
  2506. exesec:=TExeSection(ExeSectionList[i]);
  2507. if (oso_debug in exesec.SecOptions) then
  2508. ExeSectionList[i]:=nil;
  2509. end;
  2510. ExeSectionList.Pack;
  2511. end;
  2512. procedure TExeOutput.RemoveUnreferencedSections;
  2513. var
  2514. ObjSectionWorkList : TFPObjectList;
  2515. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2516. begin
  2517. if not aobjsec.Used then
  2518. begin
  2519. aobjsec.Used:=true;
  2520. ObjSectionWorkList.Add(aobjsec);
  2521. end;
  2522. end;
  2523. procedure DoReloc(objreloc:TObjRelocation);
  2524. var
  2525. objsym : TObjSymbol;
  2526. refobjsec : TObjSection;
  2527. begin
  2528. { Disabled Relocation to 0 }
  2529. if objreloc.typ=RELOC_ZERO then
  2530. exit;
  2531. if assigned(objreloc.symbol) then
  2532. begin
  2533. objsym:=objreloc.symbol;
  2534. if objsym.bind<>AB_LOCAL then
  2535. begin
  2536. if not(assigned(objsym.exesymbol) and
  2537. (objsym.exesymbol.State=symstate_defined)) then
  2538. internalerror(200603063);
  2539. objsym:=objsym.exesymbol.objsymbol;
  2540. end;
  2541. if not assigned(objsym.objsection) then
  2542. internalerror(200603062);
  2543. refobjsec:=objsym.objsection;
  2544. end
  2545. else
  2546. if assigned(objreloc.objsection) then
  2547. refobjsec:=objreloc.objsection
  2548. else
  2549. internalerror(200603316);
  2550. if assigned(exemap) then
  2551. begin
  2552. objsym:=objreloc.symbol;
  2553. if assigned(objsym) then
  2554. exemap.Add(' References '+objsym.name+' in '
  2555. +refobjsec.fullname)
  2556. else
  2557. exemap.Add(' References '+refobjsec.fullname);
  2558. end;
  2559. AddToObjSectionWorkList(refobjsec);
  2560. end;
  2561. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  2562. var
  2563. i : longint;
  2564. objreloc : TObjRelocation;
  2565. begin
  2566. objreloc:=vtable.VTableRef(VTableIdx);
  2567. if assigned(objreloc) then
  2568. begin
  2569. { Process the relocation now if the ObjSection is
  2570. already processed and marked as used. Otherwise we leave it
  2571. unprocessed. It'll then be resolved when the ObjSection is
  2572. changed to Used }
  2573. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  2574. DoReloc(objreloc);
  2575. end;
  2576. { This recursive walking is done here instead of
  2577. in TExeVTable.VTableRef because we can now process
  2578. all needed relocations }
  2579. for i:=0 to vtable.ChildList.Count-1 do
  2580. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  2581. end;
  2582. var
  2583. hs : string;
  2584. i,j,k : longint;
  2585. exesec : TExeSection;
  2586. objdata : TObjData;
  2587. objsec : TObjSection;
  2588. objsym : TObjSymbol;
  2589. code : integer;
  2590. vtableidx : longint;
  2591. vtableexesym : TExeSymbol;
  2592. begin
  2593. ObjSectionWorkList:=TFPObjectList.Create(false);
  2594. if assigned(exemap) then
  2595. exemap.AddHeader('Removing unreferenced sections');
  2596. { Initialize by marking all sections unused and
  2597. adding the sections with oso_keep flags to the ObjSectionWorkList }
  2598. for i:=0 to ObjDataList.Count-1 do
  2599. begin
  2600. ObjData:=TObjData(ObjDataList[i]);
  2601. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2602. begin
  2603. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2604. objsec.Used:=false;
  2605. { TODO: remove debug section always keep}
  2606. if oso_debug in objsec.secoptions then
  2607. objsec.Used:=true;
  2608. if (oso_keep in objsec.secoptions) then
  2609. begin
  2610. AddToObjSectionWorkList(objsec);
  2611. if objsec.name='.fpc.n_links' then
  2612. objsec.Used:=false;
  2613. end;
  2614. end;
  2615. end;
  2616. if assigned(entrysym) then
  2617. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  2618. { Process all sections, add new sections to process based
  2619. on the symbol references }
  2620. while ObjSectionWorkList.Count>0 do
  2621. begin
  2622. objsec:=TObjSection(ObjSectionWorkList.Last);
  2623. if assigned(exemap) then
  2624. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  2625. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  2626. { Process Relocations }
  2627. for i:=0 to objsec.ObjRelocations.count-1 do
  2628. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  2629. { Process Virtual Entry calls }
  2630. if cs_link_opt_vtable in current_settings.globalswitches then
  2631. begin
  2632. for i:=0 to objsec.VTRefList.count-1 do
  2633. begin
  2634. objsym:=TObjSymbol(objsec.VTRefList[i]);
  2635. hs:=objsym.name;
  2636. Delete(hs,1,Pos('_',hs));
  2637. k:=Pos('$$',hs);
  2638. if k=0 then
  2639. internalerror(200603314);
  2640. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2641. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2642. if (code<>0) then
  2643. internalerror(200603317);
  2644. if not assigned(vtableexesym) then
  2645. internalerror(200603315);
  2646. if not assigned(vtableexesym.vtable) then
  2647. internalerror(200603316);
  2648. DoVTableRef(vtableexesym.vtable,vtableidx);
  2649. end;
  2650. end;
  2651. end;
  2652. ObjSectionWorkList.Free;
  2653. ObjSectionWorkList:=nil;
  2654. { Remove unused objsections from ExeSectionList }
  2655. for i:=0 to ExeSectionList.Count-1 do
  2656. begin
  2657. exesec:=TExeSection(ExeSectionList[i]);
  2658. for j:=0 to exesec.ObjSectionlist.count-1 do
  2659. begin
  2660. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2661. if not objsec.used then
  2662. begin
  2663. if assigned(exemap) then
  2664. exemap.Add('Removing '+objsec.FullName);
  2665. exesec.ObjSectionlist[j]:=nil;
  2666. objsec.ReleaseData;
  2667. end;
  2668. end;
  2669. exesec.ObjSectionlist.Pack;
  2670. end;
  2671. end;
  2672. procedure TExeOutput.FixupRelocations;
  2673. var
  2674. i,j : longint;
  2675. exesec : TExeSection;
  2676. objsec : TObjSection;
  2677. begin
  2678. for i:=0 to ExeSectionList.Count-1 do
  2679. begin
  2680. exesec:=TExeSection(ExeSectionList[i]);
  2681. if not assigned(exesec) then
  2682. continue;
  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. internalerror(200603301);
  2688. if (objsec.ObjRelocations.Count>0) and
  2689. not assigned(objsec.data) then
  2690. internalerror(200205183);
  2691. DoRelocationFixup(objsec);
  2692. {for size = 0 data is not valid PM }
  2693. if assigned(objsec.data) and (objsec.data.size<>objsec.size) then
  2694. internalerror(2010092801);
  2695. end;
  2696. end;
  2697. end;
  2698. procedure TExeOutput.RemoveUnusedExeSymbols;
  2699. var
  2700. i : longint;
  2701. sym : TExeSymbol;
  2702. begin
  2703. { Removing unused symbols }
  2704. for i:=0 to ExeSymbolList.Count-1 do
  2705. begin
  2706. sym:=TExeSymbol(ExeSymbolList[i]);
  2707. if not sym.ObjSymbol.objsection.Used then
  2708. ExeSymbolList[i]:=nil;
  2709. end;
  2710. ExeSymbolList.Pack;
  2711. end;
  2712. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  2713. begin
  2714. if AValue>MaxMemPos then
  2715. Message1(link_f_executable_too_big, target_os_string);
  2716. FCurrMemPos:=AValue;
  2717. end;
  2718. procedure TExeOutput.WriteExeSectionContent;
  2719. var
  2720. exesec : TExeSection;
  2721. objsec : TObjSection;
  2722. i,j : longint;
  2723. begin
  2724. for j:=0 to ExeSectionList.Count-1 do
  2725. begin
  2726. exesec:=TExeSection(ExeSectionList[j]);
  2727. { don't write normal section if writing only debug info }
  2728. if (ExeWriteMode=ewm_dbgonly) and
  2729. not(oso_debug in exesec.SecOptions) then
  2730. continue;
  2731. if oso_data in exesec.SecOptions then
  2732. begin
  2733. FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
  2734. for i:=0 to exesec.ObjSectionList.Count-1 do
  2735. begin
  2736. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2737. if oso_data in objsec.secoptions then
  2738. begin
  2739. if not assigned(objsec.data) then
  2740. internalerror(200603042);
  2741. FWriter.writezeros(objsec.dataalignbytes);
  2742. if objsec.DataPos<>FWriter.Size then
  2743. internalerror(200602251);
  2744. FWriter.writearray(objsec.data);
  2745. end;
  2746. end;
  2747. end;
  2748. end;
  2749. end;
  2750. {****************************************************************************
  2751. TObjInput
  2752. ****************************************************************************}
  2753. constructor TObjInput.create;
  2754. begin
  2755. end;
  2756. destructor TObjInput.destroy;
  2757. begin
  2758. inherited destroy;
  2759. end;
  2760. function TObjInput.newObjData(const n:string):TObjData;
  2761. begin
  2762. result:=CObjData.create(n);
  2763. end;
  2764. procedure TObjInput.inputerror(const s : string);
  2765. begin
  2766. Comment(V_Error,s+' while reading '+InputFileName);
  2767. end;
  2768. class function TObjInput.CanReadObjData(AReader:TObjectReader):boolean;
  2769. begin
  2770. result:=false;
  2771. end;
  2772. procedure TObjInput.ReadSectionContent(Data:TObjData);
  2773. var
  2774. i: longint;
  2775. sec: TObjSection;
  2776. begin
  2777. for i:=0 to Data.ObjSectionList.Count-1 do
  2778. begin
  2779. sec:=TObjSection(Data.ObjSectionList[i]);
  2780. { Skip debug sections }
  2781. if (oso_debug in sec.SecOptions) and
  2782. (cs_link_strip in current_settings.globalswitches) and
  2783. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  2784. continue;
  2785. if assigned(sec.Data) then
  2786. begin
  2787. FReader.Seek(sec.datapos);
  2788. if not FReader.ReadArray(sec.data,sec.Size) then
  2789. begin
  2790. InputError('Can''t read object data');
  2791. exit;
  2792. end;
  2793. end;
  2794. end;
  2795. end;
  2796. {$ifdef MEMDEBUG}
  2797. initialization
  2798. memobjsymbols:=TMemDebug.create('ObjSymbols');
  2799. memobjsymbols.stop;
  2800. memobjsections:=TMemDebug.create('ObjSections');
  2801. memobjsections.stop;
  2802. finalization
  2803. memobjsymbols.free;
  2804. memobjsections.free;
  2805. {$endif MEMDEBUG}
  2806. end.