ogbase.pas 124 KB

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