ogbase.pas 128 KB

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