ogbase.pas 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784
  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 (aType in [sec_rodata,sec_rodata_norel]) then
  1190. begin
  1191. if (target_info.system in systems_all_windows) then
  1192. aType:=sec_rodata_norel
  1193. else
  1194. aType:=sec_data;
  1195. end;
  1196. result:=secoptions[atype];
  1197. {$ifdef OMFOBJSUPPORT}
  1198. { in the huge memory model, BSS data is actually written in the regular
  1199. FAR_DATA segment of the module }
  1200. if omf_segclass(atype)='FAR_DATA' then
  1201. Result:=Result+[oso_data,oso_sparse_data];
  1202. {$endif OMFOBJSUPPORT}
  1203. end;
  1204. function TObjData.sectiontype2align(atype:TAsmSectiontype):longint;
  1205. begin
  1206. case atype of
  1207. sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges,
  1208. sec_arm_attribute:
  1209. result:=1;
  1210. sec_code,
  1211. sec_bss,
  1212. sec_data:
  1213. result:=16;
  1214. { For idata (at least idata2) it must be 4 bytes, because
  1215. an entry is always (also in win64) 20 bytes and aligning
  1216. on 8 bytes will insert 4 bytes between the entries resulting
  1217. in a corrupt idata section.
  1218. Same story with .pdata, it has 4-byte elements which should
  1219. be packed without gaps. }
  1220. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_pdata:
  1221. result:=4;
  1222. else
  1223. result:=sizeof(pint);
  1224. end;
  1225. end;
  1226. class procedure TObjData.sectiontype2progbitsandflags(atype:TAsmSectiontype;out progbits:TSectionProgbits;out flags:TSectionFlags);
  1227. var
  1228. options : TObjSectionOptions;
  1229. begin
  1230. { this is essentially the inverse of the createsection overload that takes
  1231. both progbits and flags as parameters }
  1232. options:=sectiontype2options(atype);
  1233. flags:=[];
  1234. progbits:=SPB_None;
  1235. if oso_load in options then
  1236. include(flags,SF_A);
  1237. if oso_write in options then
  1238. include(flags,SF_W);
  1239. if oso_executable in options then
  1240. include(flags,SF_X);
  1241. if not (oso_data in options) then
  1242. progbits:=SPB_NOBITS
  1243. else if oso_note in options then
  1244. progbits:=SPB_NOTE
  1245. else if oso_arm_attributes in options then
  1246. progbits:=SPB_ARM_ATTRIBUTES;
  1247. end;
  1248. function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
  1249. begin
  1250. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
  1251. end;
  1252. function TObjData.createsection(atype: TAsmSectionType; secflags: TSectionFlags; aprogbits: TSectionProgbits; const aname: string; aorder: TAsmSectionOrder): TObjSection;
  1253. var
  1254. flags : TObjSectionOptions;
  1255. begin
  1256. flags:=[oso_data];
  1257. if SF_A in secflags then
  1258. Include(flags,oso_load);
  1259. if SF_W in secflags then
  1260. Include(flags,oso_write);
  1261. if SF_X in secflags then
  1262. Include(flags,oso_executable);
  1263. if aprogbits=SPB_NOBITS then
  1264. Exclude(flags,oso_data);
  1265. if aprogbits=SPB_NOTE then
  1266. Include(flags,oso_note);
  1267. if aprogbits=SPB_ARM_ATTRIBUTES then
  1268. Include(flags,oso_arm_attributes);
  1269. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),flags);
  1270. end;
  1271. function TObjData.createsection(const aname:string;aalign:longint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
  1272. begin
  1273. if DiscardDuplicate then
  1274. result:=TObjSection(FObjSectionList.Find(aname))
  1275. else
  1276. result:=nil;
  1277. if not assigned(result) then
  1278. begin
  1279. result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
  1280. result.ObjData:=self;
  1281. end;
  1282. FCurrObjSec:=result;
  1283. end;
  1284. function TObjData.CreateSectionGroup(const aname:string):TObjSectionGroup;
  1285. begin
  1286. if FGroupsList=nil then
  1287. FGroupsList:=TFPHashObjectList.Create(true);
  1288. result:=CObjSectionGroup.Create(FGroupsList,aname);
  1289. end;
  1290. procedure TObjData.CreateDebugSections;
  1291. begin
  1292. end;
  1293. function TObjData.FindSection(const aname:string):TObjSection;
  1294. begin
  1295. result:=TObjSection(FObjSectionList.Find(aname));
  1296. end;
  1297. procedure TObjData.setsection(asec:TObjSection);
  1298. begin
  1299. if asec.ObjData<>self then
  1300. internalerror(200403041);
  1301. FCurrObjSec:=asec;
  1302. end;
  1303. function TObjData.createsymbol(const aname:string):TObjSymbol;
  1304. begin
  1305. result:=TObjSymbol(FObjSymbolList.Find(aname));
  1306. if not assigned(result) then
  1307. result:=CObjSymbol.Create(FObjSymbolList,aname);
  1308. {$ifdef ARM}
  1309. result.ThumbFunc:=ThumbFunc;
  1310. ThumbFunc:=false;
  1311. {$endif ARM}
  1312. end;
  1313. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  1314. begin
  1315. if assigned(asmsym) then
  1316. begin
  1317. if asmsym.typ = AT_NONE then
  1318. InternalError(2018062800);
  1319. if not assigned(asmsym.cachedObjSymbol) then
  1320. begin
  1321. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  1322. asmsym.cachedObjSymbol:=result;
  1323. FCachedAsmSymbolList.add(asmsym);
  1324. end
  1325. else
  1326. begin
  1327. result:=TObjSymbol(asmsym.cachedObjSymbol);
  1328. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  1329. end;
  1330. end
  1331. else
  1332. result:=nil;
  1333. end;
  1334. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  1335. begin
  1336. if not assigned(CurrObjSec) then
  1337. internalerror(200603051);
  1338. result:=CreateSymbol(aname);
  1339. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  1340. end;
  1341. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  1342. var
  1343. s:string;
  1344. begin
  1345. if assigned(asmsym) then
  1346. begin
  1347. if not assigned(asmsym.cachedObjSymbol) then
  1348. begin
  1349. s:=asmsym.name;
  1350. result:=TObjSymbol(FObjSymbolList.Find(s));
  1351. if result=nil then
  1352. begin
  1353. result:=CObjSymbol.Create(FObjSymbolList,s);
  1354. if asmsym.bind=AB_WEAK_EXTERNAL then
  1355. result.bind:=AB_WEAK_EXTERNAL;
  1356. end;
  1357. asmsym.cachedObjSymbol:=result;
  1358. FCachedAsmSymbolList.add(asmsym);
  1359. end
  1360. else
  1361. result:=TObjSymbol(asmsym.cachedObjSymbol);
  1362. { The weak bit could have been removed from asmsym. }
  1363. if (asmsym.bind=AB_EXTERNAL) and (result.bind=AB_WEAK_EXTERNAL) then
  1364. result.bind:=AB_EXTERNAL;
  1365. { the TLS type needs to be inherited }
  1366. if asmsym.typ=AT_TLS then
  1367. result.typ:=AT_TLS;
  1368. end
  1369. else
  1370. result:=nil;
  1371. end;
  1372. function TObjData.symbolref(const aname:string):TObjSymbol;
  1373. begin
  1374. if not assigned(CurrObjSec) then
  1375. internalerror(200603052);
  1376. result:=CreateSymbol(aname);
  1377. end;
  1378. procedure TObjData.ResetCachedAsmSymbols;
  1379. var
  1380. i : longint;
  1381. begin
  1382. for i:=0 to FCachedAsmSymbolList.Count-1 do
  1383. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  1384. FCachedAsmSymbolList.Clear;
  1385. end;
  1386. procedure TObjData.writebytes(const Data;len:TObjSectionOfs);
  1387. begin
  1388. if not assigned(CurrObjSec) then
  1389. internalerror(200402251);
  1390. CurrObjSec.write(Data,len);
  1391. end;
  1392. procedure TObjData.alloc(len:TObjSectionOfs);
  1393. begin
  1394. if not assigned(CurrObjSec) then
  1395. internalerror(200402252);
  1396. CurrObjSec.alloc(len);
  1397. end;
  1398. procedure TObjData.allocalign(len:longint);
  1399. begin
  1400. if not assigned(CurrObjSec) then
  1401. internalerror(200402253);
  1402. CurrObjSec.alloc(align_objsecofs(CurrObjSec.size,len)-CurrObjSec.size);
  1403. end;
  1404. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  1405. begin
  1406. with TObjSection(p) do
  1407. alloc(align_objsecofs(size,secalign)-size);
  1408. end;
  1409. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  1410. begin
  1411. with TObjSection(p) do
  1412. begin
  1413. if assigned(Data) then
  1414. writezeros(align_objsecofs(size,secalign)-size);
  1415. end;
  1416. end;
  1417. procedure TObjData.section_reset(p:TObject;arg:pointer);
  1418. begin
  1419. with TObjSection(p) do
  1420. begin
  1421. Size:=0;
  1422. Datapos:=0;
  1423. mempos:=0;
  1424. if assigned(Data) then
  1425. Data.reset;
  1426. end;
  1427. end;
  1428. procedure TObjData.beforealloc;
  1429. begin
  1430. FCPUType:=current_settings.cputype;
  1431. { create stabs sections if debugging }
  1432. if assigned(StabsSec) then
  1433. begin
  1434. StabsSec.Alloc(sizeof(TObjStabEntry));
  1435. StabStrSec.Alloc(1);
  1436. end;
  1437. end;
  1438. procedure TObjData.beforewrite;
  1439. begin
  1440. FCPUType:=current_settings.cputype;
  1441. { create stabs sections if debugging }
  1442. if assigned(StabsSec) then
  1443. begin
  1444. { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
  1445. StabsSec.WriteZeros(sizeof(TObjStabEntry));
  1446. { start of stabstr }
  1447. StabStrSec.writeZeros(1);
  1448. end;
  1449. end;
  1450. procedure TObjData.afteralloc;
  1451. begin
  1452. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  1453. end;
  1454. procedure TObjData.afterwrite;
  1455. var
  1456. hstab : TObjStabEntry;
  1457. begin
  1458. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  1459. { For the stab section we need an HdrSym which can now be
  1460. calculated more easily }
  1461. if assigned(StabsSec) then
  1462. begin
  1463. { end of stabstr }
  1464. StabStrSec.writeZeros(1);
  1465. { header stab }
  1466. hstab.strpos:=1;
  1467. hstab.ntype:=0;
  1468. hstab.nother:=0;
  1469. {$push}{$R-}
  1470. { for jwawindows.pas, this causes an range check error, it contains too much stab symbols }
  1471. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  1472. {$pop}
  1473. hstab.nvalue:=StabStrSec.Size;
  1474. MaybeSwapStab(hstab);
  1475. StabsSec.Data.seek(0);
  1476. StabsSec.Data.write(hstab,sizeof(hstab));
  1477. end;
  1478. end;
  1479. procedure TObjData.resetsections;
  1480. begin
  1481. FObjSectionList.ForEachCall(@section_reset,nil);
  1482. end;
  1483. procedure TObjData.layoutsections(var DataPos:TObjSectionOfs);
  1484. var
  1485. i: longint;
  1486. begin
  1487. for i:=0 to FObjSectionList.Count-1 do
  1488. TObjSection(FObjSectionList[i]).setDatapos(DataPos);
  1489. end;
  1490. {****************************************************************************
  1491. TObjOutput
  1492. ****************************************************************************}
  1493. constructor TObjOutput.create(AWriter:TObjectWriter);
  1494. begin
  1495. FWriter:=AWriter;
  1496. CObjData:=TObjData;
  1497. end;
  1498. destructor TObjOutput.destroy;
  1499. begin
  1500. inherited destroy;
  1501. end;
  1502. function TObjOutput.newObjData(const n:string):TObjData;
  1503. begin
  1504. result:=CObjData.create(n);
  1505. if (cs_use_lineinfo in current_settings.globalswitches) or
  1506. (cs_debuginfo in current_settings.moduleswitches) then
  1507. result.CreateDebugSections;
  1508. end;
  1509. function TObjOutput.startObjectfile(const fn:string):boolean;
  1510. begin
  1511. result:=false;
  1512. { start the writer already, so the .a generation can initialize
  1513. the position of the current objectfile }
  1514. if not FWriter.createfile(fn) then
  1515. Comment(V_Fatal,'Can''t create object '+fn);
  1516. result:=true;
  1517. end;
  1518. function TObjOutput.writeobjectfile(Data:TObjData):boolean;
  1519. begin
  1520. if errorcount=0 then
  1521. result:=writeData(Data)
  1522. else
  1523. result:=true;
  1524. { close the writer }
  1525. FWriter.closefile;
  1526. end;
  1527. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  1528. begin
  1529. { export globals and common symbols, this is needed
  1530. for .a files }
  1531. if p.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN,AB_COMMON] then
  1532. FWriter.writesym(ApplyAsmSymbolRestrictions(p.name));
  1533. end;
  1534. procedure TObjOutput.WriteSectionContent(Data:TObjData);
  1535. var
  1536. i:longint;
  1537. sec:TObjSection;
  1538. begin
  1539. for i:=0 to Data.ObjSectionList.Count-1 do
  1540. begin
  1541. sec:=TObjSection(Data.ObjSectionList[i]);
  1542. if (oso_data in sec.SecOptions) then
  1543. begin
  1544. if sec.Data=nil then
  1545. internalerror(200403073);
  1546. FWriter.writezeros(sec.dataalignbytes);
  1547. if sec.Datapos<>FWriter.ObjSize then
  1548. internalerror(200604031);
  1549. FWriter.writearray(sec.data);
  1550. end;
  1551. end;
  1552. end;
  1553. {****************************************************************************
  1554. TExeVTable
  1555. ****************************************************************************}
  1556. constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
  1557. begin
  1558. ExeSymbol:=AExeSymbol;
  1559. if ExeSymbol.State=symstate_undefined then
  1560. internalerror(200604012);
  1561. ChildList:=TFPObjectList.Create(false);
  1562. end;
  1563. destructor TExeVTable.Destroy;
  1564. begin
  1565. ChildList.Free;
  1566. if assigned(EntryArray) then
  1567. Freemem(EntryArray);
  1568. end;
  1569. procedure TExeVTable.CheckIdx(VTableIdx:longint);
  1570. var
  1571. OldEntryCnt : longint;
  1572. begin
  1573. if VTableIdx>=EntryCnt then
  1574. begin
  1575. OldEntryCnt:=EntryCnt;
  1576. EntryCnt:=VTableIdx+1;
  1577. ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
  1578. FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
  1579. end;
  1580. end;
  1581. procedure TExeVTable.AddChild(vt:TExeVTable);
  1582. begin
  1583. ChildList.Add(vt);
  1584. end;
  1585. procedure TExeVTable.AddEntry(VTableIdx:Longint);
  1586. var
  1587. i : longint;
  1588. objreloc : TObjRelocation;
  1589. vtblentryoffset : aword;
  1590. begin
  1591. CheckIdx(VTableIdx);
  1592. vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+longword(VTableIdx)*sizeof(pint);
  1593. { Find and disable relocation }
  1594. for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
  1595. begin
  1596. objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
  1597. if objreloc.dataoffset=vtblentryoffset then
  1598. begin
  1599. EntryArray[VTableIdx].ObjRelocation:=objreloc;
  1600. EntryArray[VTableIdx].OrgRelocType:=objreloc.ftype;
  1601. EntryArray[VTableIdx].OrgRelocFlags:=objreloc.flags;
  1602. objreloc.typ:=RELOC_ZERO;
  1603. objreloc.flags:=objreloc.flags or rf_nosymbol;
  1604. break;
  1605. end;
  1606. end;
  1607. if not assigned(EntryArray[VTableIdx].ObjRelocation) then
  1608. internalerror(200604011);
  1609. end;
  1610. procedure TExeVTable.SetVTableSize(ASize:longint);
  1611. begin
  1612. if EntryCnt<>0 then
  1613. internalerror(200603313);
  1614. EntryCnt:=ASize div sizeof(pint);
  1615. EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
  1616. end;
  1617. function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
  1618. begin
  1619. result:=nil;
  1620. CheckIdx(VTableIdx);
  1621. if EntryArray[VTableIdx].Used then
  1622. exit;
  1623. { Restore relocation if available }
  1624. if assigned(EntryArray[VTableIdx].ObjRelocation) then
  1625. begin
  1626. EntryArray[VTableIdx].ObjRelocation.ftype:=EntryArray[VTableIdx].OrgRelocType;
  1627. EntryArray[VTableIdx].ObjRelocation.flags:=EntryArray[VTableIdx].OrgRelocFlags;
  1628. result:=EntryArray[VTableIdx].ObjRelocation;
  1629. end;
  1630. EntryArray[VTableIdx].Used:=true;
  1631. end;
  1632. {****************************************************************************
  1633. TExeSection
  1634. ****************************************************************************}
  1635. constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
  1636. begin
  1637. inherited create(AList,AName);
  1638. Size:=0;
  1639. MemPos:=0;
  1640. DataPos:=0;
  1641. FSecSymIdx:=0;
  1642. FObjSectionList:=TFPObjectList.Create(false);
  1643. end;
  1644. destructor TExeSection.destroy;
  1645. begin
  1646. ObjSectionList.Free;
  1647. inherited destroy;
  1648. end;
  1649. procedure TExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  1650. begin
  1651. ObjSectionList.Add(objsec);
  1652. { relate ObjSection to ExeSection, and mark it Used by default }
  1653. objsec.ExeSection:=self;
  1654. objsec.Used:=true;
  1655. if ignoreprops then
  1656. exit;
  1657. if (SecOptions<>[]) then
  1658. begin
  1659. { Only if the section contains (un)initialized data the
  1660. data flag must match. }
  1661. if ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1662. Comment(V_Error,'Incompatible section options');
  1663. end
  1664. else
  1665. begin
  1666. { inherit section options }
  1667. SecOptions:=SecOptions+objsec.SecOptions;
  1668. end;
  1669. SecAlign:=max(objsec.SecAlign,SecAlign);
  1670. end;
  1671. function TExeSection.MemPosStr(AImageBase: qword): string;
  1672. begin
  1673. result:='0x'+HexStr(mempos+AImageBase,sizeof(pint)*2);
  1674. end;
  1675. {****************************************************************************
  1676. TStaticLibrary
  1677. ****************************************************************************}
  1678. constructor TStaticLibrary.create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  1679. begin
  1680. FName:=AName;
  1681. FPayload:=AReader;
  1682. FObjInputClass:=AObjInputClass;
  1683. FKind:=lkArchive;
  1684. end;
  1685. constructor TStaticLibrary.create_object(AObjData:TObjData);
  1686. begin
  1687. FPayload:=AObjData;
  1688. FKind:=lkObject;
  1689. end;
  1690. constructor TStaticLibrary.create_group;
  1691. begin
  1692. FPayload:=TFPObjectList.Create(true);
  1693. FKind:=lkGroup;
  1694. end;
  1695. destructor TStaticLibrary.destroy;
  1696. begin
  1697. FPayload.Free;
  1698. inherited destroy;
  1699. end;
  1700. function TStaticLibrary.GetArReader: TObjectReader;
  1701. begin
  1702. if (FKind<>lkArchive) then
  1703. InternalError(2012071501);
  1704. result:=TObjectReader(FPayload);
  1705. end;
  1706. function TStaticLibrary.GetGroupMembers: TFPObjectList;
  1707. begin
  1708. if (FKind<>lkGroup) then
  1709. InternalError(2012071502);
  1710. result:=TFPObjectList(FPayload);
  1711. end;
  1712. function TStaticLibrary.GetObjData: TObjData;
  1713. begin
  1714. if (FKind<>lkObject) then
  1715. InternalError(2012071503);
  1716. result:=TObjData(FPayload);
  1717. end;
  1718. {****************************************************************************
  1719. TImportLibrary
  1720. ****************************************************************************}
  1721. constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
  1722. begin
  1723. inherited create(AList,AName);
  1724. FImportSymbolList:=TFPHashObjectList.Create(true);
  1725. end;
  1726. destructor TImportLibrary.destroy;
  1727. begin
  1728. ImportSymbolList.Free;
  1729. inherited destroy;
  1730. end;
  1731. {****************************************************************************
  1732. TImportSymbol
  1733. ****************************************************************************}
  1734. constructor TImportSymbol.create(AList:TFPHashObjectList;
  1735. const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
  1736. begin
  1737. inherited Create(AList, AName);
  1738. FOrdNr:=AOrdNr;
  1739. FIsVar:=AIsVar;
  1740. FMangledName:=AMangledName;
  1741. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1742. { This allows to import VC++ mangled names from DLLs. }
  1743. if target_info.system in systems_all_windows then
  1744. begin
  1745. Replace(FMangledName,'?','__q$$');
  1746. {$ifdef arm}
  1747. { @ symbol is not allowed in ARM assembler only }
  1748. Replace(FMangledName,'@','__a$$');
  1749. {$endif arm}
  1750. end;
  1751. end;
  1752. {****************************************************************************
  1753. TExeOutput
  1754. ****************************************************************************}
  1755. constructor TExeOutput.create;
  1756. begin
  1757. { init writer }
  1758. FWriter:=TObjectwriter.create;
  1759. FExeWriteMode:=ewm_exefull;
  1760. { object files }
  1761. FObjDataList:=TFPObjectList.Create(true);
  1762. { symbols }
  1763. FExeSymbolList:=TFPHashObjectList.Create(true);
  1764. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  1765. FExternalObjSymbols:=TFPObjectList.Create(false);
  1766. FCommonObjSymbols:=TFPObjectList.Create(false);
  1767. FProvidedObjSymbols:=TFPObjectList.Create(false);
  1768. FIndirectObjSymbols:=TFPObjectList.Create(false);
  1769. FExeVTableList:=TFPObjectList.Create(false);
  1770. ComdatGroups:=TFPHashList.Create;
  1771. { sections }
  1772. FExeSectionList:=TFPHashObjectList.Create(true);
  1773. FImageBase:=0;
  1774. {$ifdef cpu16bitaddr}
  1775. SectionMemAlign:=$10;
  1776. SectionDataAlign:=$10;
  1777. {$else cpu16bitaddr}
  1778. SectionMemAlign:=$1000;
  1779. SectionDataAlign:=$200;
  1780. {$endif cpu16bitaddr}
  1781. FixedSectionAlign:=True;
  1782. FCExeSection:=TExeSection;
  1783. FCObjData:=TObjData;
  1784. FCObjSymbol:=TObjSymbol;
  1785. end;
  1786. destructor TExeOutput.destroy;
  1787. begin
  1788. FExeSymbolList.free;
  1789. UnresolvedExeSymbols.free;
  1790. ExternalObjSymbols.free;
  1791. FProvidedObjSymbols.free;
  1792. FIndirectObjSymbols.free;
  1793. CommonObjSymbols.free;
  1794. ExeVTableList.free;
  1795. FExeSectionList.free;
  1796. ComdatGroups.free;
  1797. ObjDatalist.free;
  1798. FWriter.free;
  1799. inherited destroy;
  1800. end;
  1801. function TExeOutput.MemAlign(exesec:TExeSection):longword;
  1802. begin
  1803. if FixedSectionAlign then
  1804. result:=SectionMemAlign
  1805. else
  1806. result:=exesec.SecAlign;
  1807. end;
  1808. function TExeOutput.DataAlign(exesec:TExeSection):longword;
  1809. begin
  1810. if FixedSectionAlign then
  1811. result:=SectionDataAlign
  1812. else
  1813. result:=exesec.SecAlign;
  1814. end;
  1815. function TExeOutput.WriteExeFile(const fn:string):boolean;
  1816. begin
  1817. result:=false;
  1818. if FWriter.createfile(fn) then
  1819. begin
  1820. { Only write the .o if there are no errors }
  1821. if errorcount=0 then
  1822. result:=writedata
  1823. else
  1824. result:=true;
  1825. { close the writer }
  1826. FWriter.closefile;
  1827. end
  1828. else
  1829. Comment(V_Fatal,'Can''t create executable '+fn);
  1830. end;
  1831. procedure TExeOutput.ParseScript (linkscript:TCmdStrList);
  1832. begin
  1833. end;
  1834. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1835. begin
  1836. result:=TExeSection(ExeSectionList.Find(aname));
  1837. end;
  1838. procedure TExeOutput.AddObjData(ObjData:TObjData);
  1839. begin
  1840. if ObjData.classtype<>FCObjData then
  1841. Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
  1842. ObjDataList.Add(ObjData);
  1843. ExecStack:=ExecStack or ObjData.ExecStack;
  1844. end;
  1845. procedure TExeOutput.Load_Start;
  1846. begin
  1847. ObjDataList.Clear;
  1848. { Globals defined in the linker script }
  1849. if not assigned(internalObjData) then
  1850. internalObjData:=CObjData.create('*Internal*');
  1851. AddObjData(internalObjData);
  1852. { Common Data section }
  1853. commonObjSection:=internalObjData.createsection(sec_bss,'');
  1854. end;
  1855. procedure TExeOutput.Load_EntryName(const aname:string);
  1856. begin
  1857. FEntryName:=aname;
  1858. end;
  1859. procedure TExeOutput.Load_IsSharedLibrary;
  1860. begin
  1861. IsSharedLibrary:=true;
  1862. end;
  1863. procedure TExeOutput.Load_ImageBase(const avalue:string);
  1864. var
  1865. code : integer;
  1866. objsec : TObjSection;
  1867. objsym : TObjSymbol;
  1868. exesym : TExeSymbol;
  1869. begin
  1870. val(avalue,FImageBase,code);
  1871. if code<>0 then
  1872. Comment(V_Error,'Invalid number '+avalue);
  1873. { Create __image_base__ symbol, create the symbol
  1874. in a section with adress 0 and at offset 0 }
  1875. objsec:=internalObjData.createsection('*__image_base__',0,[]);
  1876. internalObjData.setsection(objsec);
  1877. objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_DATA);
  1878. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1879. exesym.ObjSymbol:=objsym;
  1880. end;
  1881. procedure TExeOutput.Load_Symbol(const aname:string);
  1882. begin
  1883. internalObjData.createsection('*'+aname,0,[]);
  1884. internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_DATA);
  1885. end;
  1886. procedure TExeOutput.Load_ProvideSymbol(const aname:string);
  1887. begin
  1888. if assigned(ExeSymbolList.Find(aname)) then
  1889. exit;
  1890. internalObjData.createsection('*'+aname,0,[]);
  1891. // Use AB_COMMON to avoid muliple defined complaints
  1892. internalObjData.SymbolDefine(aname,AB_COMMON,AT_DATA);
  1893. end;
  1894. procedure TExeOutput.Load_DynamicObject(ObjData:TObjData;asneeded:boolean);
  1895. begin
  1896. end;
  1897. procedure TExeOutput.Order_Start;
  1898. begin
  1899. end;
  1900. procedure TExeOutput.Order_End;
  1901. begin
  1902. internalObjData.afterwrite;
  1903. end;
  1904. procedure TExeOutput.Order_ExeSection(const aname:string);
  1905. var
  1906. sec : TExeSection;
  1907. begin
  1908. sec:=FindExeSection(aname);
  1909. if not assigned(sec) then
  1910. sec:=CExeSection.create(ExeSectionList,aname);
  1911. { Clear ExeSection contents }
  1912. FCurrExeSec:=sec;
  1913. end;
  1914. procedure TExeOutput.Order_EndExeSection;
  1915. begin
  1916. if not assigned(CurrExeSec) then
  1917. internalerror(200602184);
  1918. FCurrExeSec:=nil;
  1919. end;
  1920. procedure TExeOutput.Order_ObjSection(const aname:string);
  1921. var
  1922. i,j : longint;
  1923. ObjData : TObjData;
  1924. objsec : TObjSection;
  1925. TmpObjSectionList : TFPObjectList;
  1926. begin
  1927. if not assigned(CurrExeSec) then
  1928. internalerror(200602181);
  1929. TmpObjSectionList:=TFPObjectList.Create(false);
  1930. for i:=0 to ObjDataList.Count-1 do
  1931. begin
  1932. ObjData:=TObjData(ObjDataList[i]);
  1933. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1934. begin
  1935. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1936. if (not objsec.Used) and
  1937. MatchPattern(aname,objsec.name) then
  1938. TmpObjSectionList.Add(objsec);
  1939. end;
  1940. end;
  1941. { Order list if needed }
  1942. Order_ObjSectionList(TmpObjSectionList,aname);
  1943. { Add the (ordered) list to the current ExeSection }
  1944. for i:=0 to TmpObjSectionList.Count-1 do
  1945. begin
  1946. objsec:=TObjSection(TmpObjSectionList[i]);
  1947. CurrExeSec.AddObjSection(objsec);
  1948. end;
  1949. TmpObjSectionList.Free;
  1950. end;
  1951. procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);
  1952. begin
  1953. end;
  1954. procedure TExeOutput.Order_Symbol(const aname:string);
  1955. var
  1956. objsym: TObjSymbol;
  1957. begin
  1958. objsym:=TObjSymbol(internalObjData.ObjSymbolList.Find(aname));
  1959. if (objsym=nil) or (objsym.ObjSection.ObjData<>internalObjData) then
  1960. internalerror(200603041);
  1961. CurrExeSec.AddObjSection(objsym.ObjSection,True);
  1962. end;
  1963. procedure TExeOutput.Order_ProvideSymbol(const aname:string);
  1964. var
  1965. objsym : TObjSymbol;
  1966. exesym : TExeSymbol;
  1967. begin
  1968. objsym:=TObjSymbol(internalObjData.ObjSymbolList.Find(aname));
  1969. if (objsym=nil) or (objsym.ObjSection.ObjData<>internalObjData) then
  1970. internalerror(200603041);
  1971. exesym:=TExeSymbol(ExeSymbolList.Find(aname));
  1972. if not assigned(exesym) then
  1973. internalerror(201206301);
  1974. { Only include this section if it actually resolves
  1975. the symbol }
  1976. if exesym.objsymbol=objsym then
  1977. CurrExeSec.AddObjSection(objsym.ObjSection,True);
  1978. end;
  1979. procedure TExeOutput.Order_Align(const avalue:string);
  1980. var
  1981. code : integer;
  1982. alignval : shortint;
  1983. objsec : TObjSection;
  1984. begin
  1985. val(avalue,alignval,code);
  1986. if code<>0 then
  1987. Comment(V_Error,'Invalid number '+avalue);
  1988. if alignval<=0 then
  1989. exit;
  1990. { Create an empty section with the required aligning }
  1991. inc(Fzeronr);
  1992. objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1993. CurrExeSec.AddObjSection(objsec);
  1994. end;
  1995. procedure TExeOutput.Order_Zeros(const avalue:string);
  1996. var
  1997. zeros : array[0..1023] of byte;
  1998. code : integer;
  1999. len : longint;
  2000. objsec : TObjSection;
  2001. begin
  2002. val(avalue,len,code);
  2003. if code<>0 then
  2004. Comment(V_Error,'Invalid number '+avalue);
  2005. if len<=0 then
  2006. exit;
  2007. if len>sizeof(zeros) then
  2008. internalerror(200602254);
  2009. fillchar(zeros,len,0);
  2010. inc(Fzeronr);
  2011. objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  2012. internalObjData.writebytes(zeros,len);
  2013. CurrExeSec.AddObjSection(objsec);
  2014. end;
  2015. procedure TExeOutput.Order_Values(bytesize : aword; const avalue:string);
  2016. const
  2017. MAXVAL = 128;
  2018. var
  2019. bytevalues : array[0..MAXVAL-1] of byte;
  2020. twobytevalues : array[0..MAXVAL-1] of word;
  2021. fourbytevalues : array[0..MAXVAL-1] of dword;
  2022. eightbytevalues : array[0..MAXVAL-1] of qword;
  2023. allvals, oneval : string;
  2024. len, commapos : longint;
  2025. indexpos, code : integer;
  2026. anumval : qword;
  2027. signedval : int64;
  2028. objsec : TObjSection;
  2029. begin
  2030. indexpos:=0;
  2031. allvals:=avalue;
  2032. { avoid warnings }
  2033. bytevalues[0]:=0;
  2034. twobytevalues[0]:=0;
  2035. fourbytevalues[0]:=0;
  2036. eightbytevalues[0]:=0;
  2037. repeat
  2038. commapos:=pos(',',allvals);
  2039. if commapos>0 then
  2040. begin
  2041. oneval:=trim(copy(allvals,1,commapos-1));
  2042. allvals:=copy(allvals,commapos+1,length(allvals));
  2043. end
  2044. else
  2045. begin
  2046. oneval:=trim(allvals);
  2047. allvals:='';
  2048. end;
  2049. if oneval<>'' then
  2050. begin
  2051. if oneval[1]='-' then
  2052. begin
  2053. val(oneval,signedval,code);
  2054. anumval:=qword(signedval);
  2055. end
  2056. else
  2057. val(oneval,anumval,code);
  2058. if code<>0 then
  2059. Comment(V_Error,'Invalid number '+avalue)
  2060. else
  2061. begin
  2062. if (indexpos<MAXVAL) then
  2063. begin
  2064. if source_info.endian<>target_info.endian then
  2065. swapendian(anumval);
  2066. { No range checking here }
  2067. if bytesize=1 then
  2068. bytevalues[indexpos]:=byte(anumval)
  2069. else if bytesize=2 then
  2070. twobytevalues[indexpos]:=word(anumval)
  2071. else if bytesize=4 then
  2072. fourbytevalues[indexpos]:=dword(anumval)
  2073. else if bytesize=8 then
  2074. eightbytevalues[indexpos]:=anumval;
  2075. inc(indexpos);
  2076. end
  2077. else
  2078. Comment(V_Error,'Buffer overrun in Order_values');
  2079. end;
  2080. end;
  2081. until allvals='';
  2082. if indexpos=0 then
  2083. begin
  2084. Comment(V_Error,'Invalid number '+avalue);
  2085. exit;
  2086. end;
  2087. if indexpos=MAXVAL then
  2088. begin
  2089. Comment(V_Error,'Too many values '+avalue);
  2090. internalerror(200602254);
  2091. end;
  2092. len:=bytesize*indexpos;
  2093. inc(Fvaluesnr);
  2094. objsec:=internalObjData.createsection('*values'+tostr(Fvaluesnr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  2095. if bytesize=1 then
  2096. internalObjData.writebytes(bytevalues,len)
  2097. else if bytesize=2 then
  2098. internalObjData.writebytes(twobytevalues,len)
  2099. else if bytesize=4 then
  2100. internalObjData.writebytes(fourbytevalues,len)
  2101. else if bytesize=8 then
  2102. internalObjData.writebytes(eightbytevalues,len);
  2103. CurrExeSec.AddObjSection(objsec);
  2104. end;
  2105. procedure TExeOutput.MemPos_Start;
  2106. begin
  2107. CurrMemPos:=0;
  2108. RemoveDisabledSections;
  2109. end;
  2110. procedure TExeOutput.MemPos_Header;
  2111. begin
  2112. end;
  2113. procedure TExeOutput.MemPos_ExeSection(exesec:TExeSection);
  2114. var
  2115. i : longint;
  2116. objsec : TObjSection;
  2117. begin
  2118. { Alignment of ExeSection }
  2119. CurrMemPos:=align_qword(CurrMemPos,MemAlign(exesec));
  2120. exesec.MemPos:=CurrMemPos;
  2121. { set position of object ObjSections }
  2122. for i:=0 to exesec.ObjSectionList.Count-1 do
  2123. begin
  2124. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2125. CurrMemPos:=objsec.setmempos(CurrMemPos);
  2126. end;
  2127. { calculate size of the section }
  2128. exesec.Size:=CurrMemPos-exesec.MemPos;
  2129. end;
  2130. procedure TExeOutput.MemPos_ExeSection(const aname:string);
  2131. begin
  2132. { Section can be removed }
  2133. FCurrExeSec:=FindExeSection(aname);
  2134. if not assigned(CurrExeSec) then
  2135. exit;
  2136. MemPos_ExeSection(CurrExeSec);
  2137. end;
  2138. procedure TExeOutput.MemPos_EndExeSection;
  2139. begin
  2140. if not assigned(CurrExeSec) then
  2141. exit;
  2142. FCurrExeSec:=nil;
  2143. end;
  2144. procedure TExeOutput.DataPos_Start;
  2145. begin
  2146. end;
  2147. procedure TExeOutput.DataPos_Header;
  2148. begin
  2149. end;
  2150. procedure TExeOutput.DataPos_ExeSection(exesec:TExeSection);
  2151. begin
  2152. { don't write normal section if writing only debug info }
  2153. if (ExeWriteMode=ewm_dbgonly) and
  2154. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  2155. exit;
  2156. if (oso_Data in exesec.SecOptions) then
  2157. begin
  2158. CurrDataPos:=align_aword(CurrDataPos,DataAlign(exesec));
  2159. exesec.DataPos:=CurrDataPos;
  2160. CurrDataPos:=CurrDataPos+exesec.Size;
  2161. end;
  2162. end;
  2163. procedure TExeOutput.DataPos_ExeSection(const aname:string);
  2164. begin
  2165. { Section can be removed }
  2166. FCurrExeSec:=FindExeSection(aname);
  2167. if not assigned(CurrExeSec) then
  2168. exit;
  2169. DataPos_ExeSection(CurrExeSec);
  2170. end;
  2171. procedure TExeOutput.DataPos_EndExeSection;
  2172. begin
  2173. if not assigned(CurrExeSec) then
  2174. exit;
  2175. FCurrExeSec:=nil;
  2176. end;
  2177. procedure TExeOutput.DataPos_Symbols;
  2178. begin
  2179. end;
  2180. procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  2181. var
  2182. hs : string;
  2183. code : integer;
  2184. i,k,
  2185. vtableidx : longint;
  2186. vtableexesym,
  2187. childexesym,
  2188. parentexesym : TExeSymbol;
  2189. objsym : TObjSymbol;
  2190. begin
  2191. { Build inheritance tree from VTINHERIT }
  2192. for i:=0 to VTInheritList.Count-1 do
  2193. begin
  2194. objsym:=TObjSymbol(VTInheritList[i]);
  2195. hs:=objsym.name;
  2196. { VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
  2197. Delete(hs,1,Pos('_',hs));
  2198. k:=Pos('$$',hs);
  2199. if k=0 then
  2200. internalerror(200603311);
  2201. childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2202. parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
  2203. if not assigned(childexesym) or
  2204. not assigned(parentexesym)then
  2205. internalerror(200603312);
  2206. if not assigned(childexesym.vtable) then
  2207. begin
  2208. childexesym.vtable:=TExeVTable.Create(childexesym);
  2209. ExeVTableList.Add(childexesym.vtable);
  2210. end;
  2211. if not assigned(parentexesym.vtable) then
  2212. begin
  2213. parentexesym.vtable:=TExeVTable.Create(parentexesym);
  2214. ExeVTableList.Add(parentexesym.vtable);
  2215. end;
  2216. childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
  2217. if parentexesym<>childexesym then
  2218. parentexesym.vtable.AddChild(childexesym.vtable);
  2219. end;
  2220. { Find VTable entries from VTENTRY }
  2221. for i:=0 to VTEntryList.Count-1 do
  2222. begin
  2223. objsym:=TObjSymbol(VTEntryList[i]);
  2224. hs:=objsym.name;
  2225. { VTENTRY_<VTableName>$$<Index> }
  2226. Delete(hs,1,Pos('_',hs));
  2227. k:=Pos('$$',hs);
  2228. if k=0 then
  2229. internalerror(200603319);
  2230. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2231. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2232. if (code<>0) then
  2233. internalerror(200603318);
  2234. if not assigned(vtableexesym) then
  2235. internalerror(2006033110);
  2236. vtableexesym.vtable.AddEntry(vtableidx);
  2237. end;
  2238. end;
  2239. procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
  2240. var
  2241. i : longint;
  2242. exesym : TExeSymbol;
  2243. begin
  2244. { Generate a list of Unresolved External symbols }
  2245. for i:=0 to UnresolvedExeSymbols.count-1 do
  2246. begin
  2247. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2248. if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
  2249. UnresolvedExeSymbols[i]:=nil;
  2250. end;
  2251. UnresolvedExeSymbols.Pack;
  2252. Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
  2253. end;
  2254. procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPObjectList);
  2255. var
  2256. ObjData : TObjData;
  2257. exesym : TExeSymbol;
  2258. objsym,
  2259. commonsym : TObjSymbol;
  2260. firstarchive,
  2261. firstcommon : boolean;
  2262. i : longint;
  2263. VTEntryList,
  2264. VTInheritList : TFPObjectList;
  2265. procedure LoadObjDataSymbols(ObjData:TObjData);
  2266. var
  2267. j : longint;
  2268. hs : string;
  2269. exesym : TExeSymbol;
  2270. tmpsym,
  2271. objsym : TObjSymbol;
  2272. grp : TObjSectionGroup;
  2273. makeexternal : boolean;
  2274. begin
  2275. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  2276. begin
  2277. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  2278. { From the local symbols we are only interessed in the
  2279. VTENTRY and VTINHERIT symbols }
  2280. if objsym.bind=AB_LOCAL then
  2281. begin
  2282. if cs_link_opt_vtable in current_settings.globalswitches then
  2283. begin
  2284. hs:=objsym.name;
  2285. if (hs[1]='V') then
  2286. begin
  2287. if Copy(hs,1,5)='VTREF' then
  2288. begin
  2289. if not assigned(objsym.ObjSection.VTRefList) then
  2290. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  2291. objsym.ObjSection.VTRefList.Add(objsym);
  2292. end
  2293. else if Copy(hs,1,7)='VTENTRY' then
  2294. VTEntryList.Add(objsym)
  2295. else if Copy(hs,1,9)='VTINHERIT' then
  2296. VTInheritList.Add(objsym);
  2297. end;
  2298. end;
  2299. continue;
  2300. end;
  2301. { If this symbol comes from COMDAT group, see if a group with
  2302. matching signature is already included. }
  2303. if assigned(objsym.objsection) and
  2304. assigned(objsym.objsection.group) then
  2305. begin
  2306. grp:=objsym.objsection.group;
  2307. if grp.IsComdat then
  2308. begin
  2309. if ComdatGroups.Find(grp.name)=nil then
  2310. ComdatGroups.Add(grp.name,grp)
  2311. else
  2312. begin
  2313. { Undefine the symbol, causing relocations to it from same
  2314. objdata to be redirected to the symbol in the actually
  2315. linked group. }
  2316. if objsym.bind=AB_GLOBAL then
  2317. objsym.bind:=AB_EXTERNAL;
  2318. { AB_WEAK_EXTERNAL remains unchanged }
  2319. objsym.objsection:=nil;
  2320. end;
  2321. end;
  2322. end;
  2323. { Search for existing exesymbol }
  2324. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  2325. if not assigned(exesym) then
  2326. begin
  2327. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  2328. exesym.ObjSymbol:=objsym;
  2329. end
  2330. else
  2331. begin
  2332. if assigned(objsym.objsection) and assigned(exesym.objsymbol.objsection) then
  2333. begin
  2334. if (oso_comdat in exesym.ObjSymbol.objsection.SecOptions) and
  2335. (oso_comdat in objsym.objsection.SecOptions) then
  2336. begin
  2337. if exesym.ObjSymbol.objsection.ComdatSelection=objsym.objsection.ComdatSelection then
  2338. begin
  2339. makeexternal:=true;
  2340. case objsym.objsection.ComdatSelection of
  2341. oscs_none:
  2342. makeexternal:=false;
  2343. oscs_any:
  2344. Message1(link_d_comdat_discard_any,objsym.name);
  2345. oscs_same_size:
  2346. if exesym.ObjSymbol.size<>objsym.size then
  2347. Message1(link_e_comdat_size_differs,objsym.name)
  2348. else
  2349. Message1(link_d_comdat_discard_size,objsym.name);
  2350. oscs_exact_match:
  2351. if (exesym.ObjSymbol.size<>objsym.size) and not exesym.ObjSymbol.objsection.Data.equal(objsym.objsection.Data) then
  2352. Message1(link_e_comdat_content_differs,objsym.name)
  2353. else
  2354. Message1(link_d_comdat_discard_content,objsym.name);
  2355. oscs_associative:
  2356. { this is handled in a different way }
  2357. makeexternal:=false;
  2358. oscs_largest:
  2359. if objsym.size>exesym.ObjSymbol.size then
  2360. begin
  2361. Message1(link_d_comdat_replace_size,objsym.name);
  2362. { we swap the symbols and turn the smaller one to an external
  2363. symbol }
  2364. tmpsym:=exesym.objsymbol;
  2365. exesym.objsymbol:=objsym;
  2366. objsym.exesymbol:=exesym;
  2367. objsym:=tmpsym;
  2368. end;
  2369. end;
  2370. if makeexternal then
  2371. begin
  2372. { Undefine the symbol, causing relocations to it from same
  2373. objdata to be redirected to the symbol that is actually
  2374. used }
  2375. if objsym.bind=AB_GLOBAL then
  2376. objsym.bind:=AB_EXTERNAL;
  2377. { AB_WEAK_EXTERNAL remains unchanged }
  2378. objsym.objsection:=nil;
  2379. end;
  2380. end
  2381. else
  2382. Message1(link_e_comdat_selection_differs,objsym.name);
  2383. end;
  2384. end;
  2385. end;
  2386. objsym.ExeSymbol:=exesym;
  2387. case objsym.bind of
  2388. AB_GLOBAL,
  2389. AB_PRIVATE_EXTERN:
  2390. begin
  2391. if exesym.State<>symstate_defined then
  2392. begin
  2393. exesym.ObjSymbol:=objsym;
  2394. exesym.State:=symstate_defined;
  2395. end
  2396. else
  2397. Message1(link_e_duplicate_symbol,objsym.name);
  2398. { hidden symbols must become local symbols in the executable }
  2399. if objsym.bind=AB_PRIVATE_EXTERN then
  2400. objsym.bind:=AB_LOCAL;
  2401. end;
  2402. AB_EXTERNAL :
  2403. begin
  2404. ExternalObjSymbols.add(objsym);
  2405. { Register unresolved symbols only the first time they
  2406. are registered }
  2407. if exesym.ObjSymbol=objsym then
  2408. UnresolvedExeSymbols.Add(exesym)
  2409. { Normal reference removes any existing "weakness" }
  2410. else if exesym.state=symstate_undefweak then
  2411. begin
  2412. exesym.state:=symstate_undefined;
  2413. exesym.ObjSymbol:=objsym;
  2414. end;
  2415. end;
  2416. AB_COMMON :
  2417. begin
  2418. { A COMMON definition overrides weak one.
  2419. Also select the symbol with largest size. }
  2420. if (exesym.State in [symstate_undefined,symstate_undefweak,symstate_defweak]) or
  2421. ((exesym.State=symstate_common) and (objsym.size>exesym.ObjSymbol.size)) then
  2422. begin
  2423. exesym.ObjSymbol:=objsym;
  2424. exesym.State:=symstate_common;
  2425. end;
  2426. if assigned(objsym.objsection) and
  2427. (objsym.objsection.objdata=internalObjData) then
  2428. FProvidedObjSymbols.add(objsym)
  2429. else
  2430. CommonObjSymbols.add(objsym);
  2431. end;
  2432. AB_WEAK_EXTERNAL :
  2433. begin
  2434. if objsym.objsection=nil then { a weak reference }
  2435. begin
  2436. ExternalObjSymbols.add(objsym);
  2437. if exesym.ObjSymbol=objsym then
  2438. begin
  2439. UnresolvedExeSymbols.Add(exesym);
  2440. exesym.state:=symstate_undefweak;
  2441. end;
  2442. end
  2443. else { a weak definition }
  2444. begin
  2445. if exesym.State in [symstate_undefined,symstate_undefweak] then
  2446. begin
  2447. exesym.ObjSymbol:=objsym;
  2448. exesym.state:=symstate_defweak;
  2449. end;
  2450. end;
  2451. end;
  2452. else
  2453. internalerror(2019050510);
  2454. end;
  2455. end;
  2456. end;
  2457. procedure LoadLibrary(lib:TStaticLibrary);
  2458. var
  2459. j,k,oldcount: longint;
  2460. members: TFPObjectList;
  2461. exesym: TExeSymbol;
  2462. objinput: TObjInput;
  2463. begin
  2464. case lib.Kind of
  2465. lkArchive:
  2466. begin
  2467. { Process list of Unresolved External symbols, we need
  2468. to use a while loop because the list can be extended when
  2469. we load members from the library. }
  2470. j:=0;
  2471. while (j<UnresolvedExeSymbols.count) do
  2472. begin
  2473. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  2474. { Check first if the symbol is still undefined }
  2475. if (exesym.State=symstate_undefined) then
  2476. begin
  2477. if lib.ArReader.OpenFile(exesym.name) then
  2478. begin
  2479. if assigned(exemap) then
  2480. begin
  2481. if firstarchive then
  2482. begin
  2483. exemap.Add('');
  2484. exemap.Add('Archive member included because of file (symbol)');
  2485. exemap.Add('');
  2486. firstarchive:=false;
  2487. end;
  2488. exemap.Add(lib.ArReader.FileName+' - '+
  2489. {exesym.ObjSymbol.ObjSection.FullName+}
  2490. '('+exesym.Name+')');
  2491. end;
  2492. objinput:=lib.ObjInputClass.Create;
  2493. objinput.ReadObjData(lib.ArReader,objdata);
  2494. objinput.free;
  2495. AddObjData(objdata);
  2496. LoadObjDataSymbols(objdata);
  2497. lib.ArReader.CloseFile;
  2498. end;
  2499. end;
  2500. inc(j);
  2501. end;
  2502. end;
  2503. lkGroup:
  2504. begin
  2505. { repeatedly process members of the group until no new
  2506. unresolved symbols appear }
  2507. members:=lib.GroupMembers;
  2508. repeat
  2509. oldcount:=UnresolvedExeSymbols.count;
  2510. for k:=0 to members.Count-1 do
  2511. LoadLibrary(TStaticLibrary(members[k]));
  2512. until UnresolvedExeSymbols.count=oldcount;
  2513. end;
  2514. lkObject:
  2515. { TODO: ownership of objdata }
  2516. //if lib.objdata.is_dynamic then
  2517. Load_DynamicObject(lib.objdata,lib.AsNeeded);
  2518. {else
  2519. begin
  2520. AddObjData(lib.objdata);
  2521. LoadObjDataSymbols(lib.objdata);
  2522. end;}
  2523. end;
  2524. end;
  2525. begin
  2526. VTEntryList:=TFPObjectList.Create(false);
  2527. VTInheritList:=TFPObjectList.Create(false);
  2528. {
  2529. The symbol resolving is done in 4 steps:
  2530. 1. Register symbols from objects
  2531. 2. Find symbols in static libraries
  2532. 3. Define symbols PROVIDEd by the link script
  2533. 4. Define still undefined common symbols
  2534. }
  2535. { Step 1, Register symbols from objects }
  2536. for i:=0 to ObjDataList.Count-1 do
  2537. begin
  2538. ObjData:=TObjData(ObjDataList[i]);
  2539. LoadObjDataSymbols(ObjData);
  2540. end;
  2541. PackUnresolvedExeSymbols('in objects');
  2542. { Step 2, Find unresolved symbols in the libraries }
  2543. firstarchive:=true;
  2544. for i:=0 to StaticLibraryList.Count-1 do
  2545. LoadLibrary(TStaticLibrary(StaticLibraryList[i]));
  2546. PackUnresolvedExeSymbols('after static libraries');
  2547. { Step 3, handle symbols provided in script }
  2548. for i:=0 to FProvidedObjSymbols.count-1 do
  2549. begin
  2550. objsym:=TObjSymbol(FProvidedObjSymbols[i]);
  2551. if objsym.exesymbol.State=symstate_defined then
  2552. continue;
  2553. objsym.exesymbol.objsymbol:=objsym;
  2554. objsym.bind:=AB_GLOBAL;
  2555. objsym.exesymbol.State:=symstate_defined;
  2556. end;
  2557. PackUnresolvedExeSymbols('after defining symbols provided by link script');
  2558. { Step 4, Match common symbols or add to the globals }
  2559. firstcommon:=true;
  2560. for i:=0 to CommonObjSymbols.count-1 do
  2561. begin
  2562. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2563. if objsym.exesymbol.State=symstate_defined then
  2564. begin
  2565. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  2566. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  2567. end
  2568. else
  2569. begin
  2570. { allocate new objsymbol in .bss of *COMMON* and assign
  2571. it to the exesymbol }
  2572. if firstcommon then
  2573. begin
  2574. if assigned(exemap) then
  2575. exemap.AddCommonSymbolsHeader;
  2576. firstcommon:=false;
  2577. end;
  2578. internalObjData.setsection(commonObjSection);
  2579. { TODO: support alignment of common symbols (ELF targets at least),
  2580. increase commonObjSection.SecAlign if necessary here. }
  2581. internalObjData.allocalign(used_align(size_2_align(objsym.size),0,commonObjSection.SecAlign));
  2582. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_DATA);
  2583. commonsym.size:=objsym.size;
  2584. internalObjData.alloc(objsym.size);
  2585. if assigned(exemap) then
  2586. exemap.AddCommonSymbol(objsym);
  2587. { Assign to the exesymbol }
  2588. objsym.exesymbol.objsymbol:=commonsym;
  2589. objsym.exesymbol.state:=symstate_defined;
  2590. end;
  2591. end;
  2592. PackUnresolvedExeSymbols('after defining COMMON symbols');
  2593. { Find entry symbol and print in map }
  2594. if (EntryName<>'') then
  2595. begin
  2596. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  2597. if assigned(exesym) then
  2598. begin
  2599. EntrySym:=exesym.ObjSymbol;
  2600. if assigned(exemap) then
  2601. begin
  2602. exemap.Add('');
  2603. exemap.Add('Entry symbol '+EntryName);
  2604. end;
  2605. end
  2606. else
  2607. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  2608. end;
  2609. { Generate VTable tree }
  2610. if cs_link_opt_vtable in current_settings.globalswitches then
  2611. BuildVTableTree(VTInheritList,VTEntryList);
  2612. VTInheritList.Free;
  2613. VTEntryList.Free;
  2614. end;
  2615. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  2616. var
  2617. debuglink : array[0..1023] of byte;
  2618. len : longint;
  2619. objsec : TObjSection;
  2620. exesec : TExeSection;
  2621. begin
  2622. { From the gdb manual chapter 15. GDB Files:
  2623. * A filename, with any leading directory components removed, followed by a zero byte,
  2624. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  2625. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  2626. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  2627. }
  2628. fillchar(debuglink,sizeof(debuglink),0);
  2629. len:=0;
  2630. move(dbgname[1],debuglink[len],length(dbgname));
  2631. inc(len,length(dbgname)+1);
  2632. len:=align(len,4);
  2633. if source_info.endian<>target_info.endian then
  2634. SwapEndian(dbgcrc);
  2635. move(dbgcrc,debuglink[len],sizeof(cardinal));
  2636. inc(len,4);
  2637. { Add section }
  2638. exesec:=FindExeSection(debuglinkname);
  2639. if not assigned(exesec) then
  2640. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  2641. exesec.SecOptions:=[oso_data,oso_keep];
  2642. exesec.SecAlign:=4;
  2643. objsec:=internalObjData.createsection(exesec.name,1,exesec.SecOptions);
  2644. internalObjData.writebytes(debuglink,len);
  2645. exesec.AddObjSection(objsec);
  2646. end;
  2647. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2648. begin
  2649. end;
  2650. procedure TExeOutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  2651. begin
  2652. end;
  2653. procedure TExeOutput.AfterUnusedSectionRemoval;
  2654. begin
  2655. end;
  2656. function ByAddress(item1,item2:pointer):longint;
  2657. var
  2658. sym1:TObjSymbol absolute item1;
  2659. sym2:TObjSymbol absolute item2;
  2660. begin
  2661. if sym1.address>sym2.address then
  2662. result:=1
  2663. else if sym1.address<sym2.address then
  2664. result:=-1
  2665. else
  2666. result:=0;
  2667. end;
  2668. procedure TExeOutput.PrintMemoryMap;
  2669. var
  2670. exesec : TExeSection;
  2671. objsec : TObjSection;
  2672. objsym : TObjSymbol;
  2673. i,j,k,m: longint;
  2674. list : TFPList;
  2675. flag : boolean;
  2676. begin
  2677. if not assigned(exemap) then
  2678. exit;
  2679. { create a list of symbols sorted by address }
  2680. list:=TFPList.Create;
  2681. for i:=0 to ExeSymbolList.Count-1 do
  2682. list.Add(TExeSymbol(ExeSymbolList[i]).ObjSymbol);
  2683. list.Sort(@ByAddress);
  2684. exemap.AddMemoryMapHeader(ImageBase);
  2685. k:=0;
  2686. for i:=0 to ExeSectionList.Count-1 do
  2687. begin
  2688. exesec:=TExeSection(ExeSectionList[i]);
  2689. exemap.AddMemoryMapExeSection(exesec);
  2690. for j:=0 to exesec.ObjSectionList.count-1 do
  2691. begin
  2692. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2693. exemap.AddMemoryMapObjectSection(objsec);
  2694. while (k<list.Count) and (TObjSymbol(list[k]).Address<objsec.MemPos) do
  2695. inc(k);
  2696. while (k<list.Count) do
  2697. begin
  2698. objsym:=TObjSymbol(list[k]);
  2699. if objsym.address>objsec.MemPos+objsec.Size then
  2700. break;
  2701. if objsym.objsection=objsec then
  2702. exemap.AddMemoryMapSymbol(objsym)
  2703. else
  2704. begin
  2705. { Got a symbol with address falling into current section, but
  2706. belonging to a different section. This may happen for zero-length
  2707. sections because symbol list is sorted by address but not by section.
  2708. Do some look-ahead in this case. }
  2709. m:=k+1;
  2710. flag:=false;
  2711. while (m<list.Count) and (TObjSymbol(list[m]).Address=objsym.address) do
  2712. begin
  2713. if TObjSymbol(list[m]).objsection=objsec then
  2714. begin
  2715. flag:=true;
  2716. list.Exchange(k,m);
  2717. exemap.AddMemoryMapSymbol(TObjSymbol(list[k]));
  2718. break;
  2719. end;
  2720. inc(m);
  2721. end;
  2722. if not flag then
  2723. break;
  2724. end;
  2725. inc(k);
  2726. end;
  2727. end;
  2728. end;
  2729. list.Free;
  2730. end;
  2731. procedure TExeOutput.FixupSymbols;
  2732. procedure UpdateSymbol(objsym:TObjSymbol);
  2733. begin
  2734. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  2735. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2736. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  2737. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2738. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2739. objsym.group:=objsym.ExeSymbol.ObjSymbol.group;
  2740. end;
  2741. var
  2742. i : longint;
  2743. objsym : TObjSymbol;
  2744. exesym : TExeSymbol;
  2745. begin
  2746. { Print list of Unresolved External symbols }
  2747. if not AllowUndefinedSymbols then
  2748. for i:=0 to UnresolvedExeSymbols.count-1 do
  2749. begin
  2750. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2751. if (exesym.State=symstate_undefined) then
  2752. begin
  2753. if assigned(exesym.ObjSymbol) and assigned(exesym.ObjSymbol.ObjData) then
  2754. Message2(link_e_undefined_symbol_in_obj,exesym.name,exesym.objsymbol.ObjData.Name)
  2755. else
  2756. Message1(link_e_undefined_symbol,exesym.name);
  2757. end;
  2758. end;
  2759. {
  2760. Fixing up symbols is done in the following steps:
  2761. 1. Update common references
  2762. 2. Update external references
  2763. Symbols with objsection<>nil are removed from the lists,
  2764. remaining ones can be processed later by calling this method again.
  2765. }
  2766. { Step 1, Update commons. Preserve the original symbol size and bind,
  2767. this is needed for correct relocation of DJCOFF files. }
  2768. for i:=0 to CommonObjSymbols.count-1 do
  2769. begin
  2770. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2771. if objsym.bind<>AB_COMMON then
  2772. internalerror(200606241);
  2773. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2774. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2775. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2776. end;
  2777. { Step 2, Update externals }
  2778. for i:=0 to ExternalObjSymbols.count-1 do
  2779. begin
  2780. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  2781. if not (objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) then
  2782. internalerror(200606242);
  2783. UpdateSymbol(objsym);
  2784. { Collect symbols that resolve to indirect functions,
  2785. they will need additional target-specific processing. }
  2786. if objsym.typ=AT_GNU_IFUNC then
  2787. IndirectObjSymbols.Add(objsym)
  2788. else if assigned(objsym.objsection) then
  2789. ExternalObjSymbols[i]:=nil;
  2790. end;
  2791. CommonObjSymbols.Clear;
  2792. ExternalObjSymbols.Pack;
  2793. end;
  2794. procedure TExeOutput.MergeStabs;
  2795. var
  2796. stabexesec,
  2797. stabstrexesec : TExeSection;
  2798. relocsec,
  2799. currstabsec,
  2800. currstabstrsec,
  2801. mergedstabsec,
  2802. mergedstabstrsec : TObjSection;
  2803. hstabreloc,
  2804. currstabreloc : TObjRelocation;
  2805. i,j : longint;
  2806. currstabrelocidx,
  2807. mergestabcnt,
  2808. stabcnt : longword;
  2809. skipstab : boolean;
  2810. skipfun : boolean;
  2811. hstab : TObjStabEntry;
  2812. stabrelocofs : longword;
  2813. buf : array[0..1023] of byte;
  2814. bufend,
  2815. bufsize : longint;
  2816. begin
  2817. stabexesec:=FindExeSection('.stab');
  2818. stabstrexesec:=FindExeSection('.stabstr');
  2819. if (stabexesec=nil) or
  2820. (stabstrexesec=nil) or
  2821. (stabexesec.ObjSectionlist.count=0) then
  2822. exit;
  2823. { Create new stabsection }
  2824. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  2825. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  2826. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  2827. { write stab for hdrsym }
  2828. mergedstabsec.writeZeros(sizeof(TObjStabEntry));
  2829. mergestabcnt:=1;
  2830. { .stabstr starts with a #0 }
  2831. buf[0]:=0;
  2832. mergedstabstrsec.write(buf[0],1);
  2833. skipfun:=false;
  2834. { Copy stabs and corresponding Relocations }
  2835. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  2836. begin
  2837. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  2838. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  2839. if assigned(currstabstrsec) then
  2840. begin
  2841. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  2842. currstabsec.Data.seek(0);
  2843. currstabrelocidx:=0;
  2844. for j:=0 to stabcnt-1 do
  2845. begin
  2846. hstabreloc:=nil;
  2847. skipstab:=false;
  2848. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  2849. MaybeSwapStab(hstab);
  2850. { Only include first hdrsym stab }
  2851. if hstab.ntype=0 then
  2852. skipstab:=true;
  2853. if skipfun then
  2854. begin
  2855. { Skip all stabs for function body until N_RBRAC }
  2856. skipfun:=hstab.ntype<>N_RBRAC;
  2857. skipstab:=true;
  2858. end;
  2859. if not skipstab then
  2860. begin
  2861. { Find corresponding Relocation }
  2862. currstabreloc:=nil;
  2863. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  2864. begin
  2865. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  2866. if assigned(currstabreloc) and
  2867. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2868. break;
  2869. inc(currstabrelocidx);
  2870. end;
  2871. if assigned(currstabreloc) and
  2872. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2873. begin
  2874. hstabReloc:=currstabReloc;
  2875. inc(currstabrelocidx);
  2876. end;
  2877. { Check if the stab is refering to a removed section }
  2878. if assigned(hstabreloc) then
  2879. begin
  2880. if assigned(hstabreloc.Symbol) then
  2881. relocsec:=hstabreloc.Symbol.ObjSection
  2882. else
  2883. relocsec:=hstabreloc.ObjSection;
  2884. if not assigned(relocsec) then
  2885. internalerror(200603302);
  2886. if not relocsec.Used then
  2887. begin
  2888. skipstab:=true;
  2889. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  2890. begin
  2891. currstabstrsec.Data.seek(hstab.strpos);
  2892. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2893. bufend:=indexbyte(buf,bufsize,Ord(':'));
  2894. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  2895. skipfun:=true;
  2896. end;
  2897. end;
  2898. end;
  2899. end;
  2900. if not skipstab then
  2901. begin
  2902. { Copy string in stabstr }
  2903. if hstab.strpos<>0 then
  2904. begin
  2905. currstabstrsec.Data.seek(hstab.strpos);
  2906. hstab.strpos:=mergedstabstrsec.Size;
  2907. repeat
  2908. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2909. bufend:=indexbyte(buf,bufsize,0);
  2910. if bufend=-1 then
  2911. bufend:=bufsize
  2912. else
  2913. begin
  2914. { include the #0 }
  2915. inc(bufend);
  2916. end;
  2917. mergedstabstrsec.write(buf,bufend);
  2918. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2919. end;
  2920. { Copy and Update the relocation }
  2921. if assigned(hstabreloc) then
  2922. begin
  2923. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2924. { Remove from List without freeing the object }
  2925. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2926. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2927. end;
  2928. { Write updated stab }
  2929. MaybeSwapStab(hstab);
  2930. mergedstabsec.write(hstab,sizeof(hstab));
  2931. inc(mergestabcnt);
  2932. end;
  2933. end;
  2934. end;
  2935. { Unload stabs }
  2936. if assigned(currstabstrsec) then
  2937. begin
  2938. currstabstrsec.Used:=False;
  2939. currstabstrsec.ReleaseData;
  2940. end;
  2941. currstabsec.Used:=false;
  2942. currstabsec.ReleaseData;
  2943. end;
  2944. { Generate new HdrSym }
  2945. if mergedstabsec.Size>0 then
  2946. begin
  2947. hstab.strpos:=1;
  2948. hstab.ntype:=0;
  2949. hstab.nother:=0;
  2950. hstab.ndesc:=word(mergestabcnt-1);
  2951. hstab.nvalue:=mergedstabstrsec.Size;
  2952. MaybeSwapStab(hstab);
  2953. mergedstabsec.Data.seek(0);
  2954. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2955. end;
  2956. { Replace all sections with our combined stabsec }
  2957. stabexesec.ObjSectionList.Clear;
  2958. stabstrexesec.ObjSectionList.Clear;
  2959. stabexesec.AddObjSection(mergedstabsec);
  2960. stabstrexesec.AddObjSection(mergedstabstrsec);
  2961. end;
  2962. procedure TExeOutput.MarkEmptySections;
  2963. var
  2964. i, j : longint;
  2965. exesec : TExeSection;
  2966. doremove : boolean;
  2967. begin
  2968. for i:=0 to ExeSectionList.Count-1 do
  2969. begin
  2970. exesec:=TExeSection(ExeSectionList[i]);
  2971. doremove:=not(oso_keep in exesec.SecOptions) and
  2972. (
  2973. (exesec.ObjSectionlist.count=0) or
  2974. (
  2975. (cs_link_strip in current_settings.globalswitches) and
  2976. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2977. (oso_debug in exesec.SecOptions)
  2978. )
  2979. );
  2980. if not doremove then
  2981. begin
  2982. { Check if section has no actual data }
  2983. doremove:=true;
  2984. for j:=0 to exesec.ObjSectionList.Count-1 do
  2985. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2986. begin
  2987. doremove:=false;
  2988. break;
  2989. end;
  2990. end;
  2991. if doremove then
  2992. begin
  2993. Comment(V_Debug,'Disabling empty section '+exesec.name);
  2994. exesec.Disabled:=true;
  2995. end;
  2996. end;
  2997. end;
  2998. procedure TExeOutput.RemoveDisabledSections;
  2999. var
  3000. i: longint;
  3001. exesec: TExeSection;
  3002. begin
  3003. for i:=0 to ExeSectionList.Count-1 do
  3004. begin
  3005. exesec:=TExeSection(ExeSectionList[i]);
  3006. if exesec.Disabled then
  3007. ExeSectionList[i]:=nil;
  3008. end;
  3009. ExeSectionList.Pack;
  3010. end;
  3011. procedure TExeOutput.RemoveDebugInfo;
  3012. var
  3013. i : longint;
  3014. exesec : TExeSection;
  3015. begin
  3016. for i:=0 to ExeSectionList.Count-1 do
  3017. begin
  3018. exesec:=TExeSection(ExeSectionList[i]);
  3019. if (oso_debug in exesec.SecOptions) then
  3020. ExeSectionList[i]:=nil;
  3021. end;
  3022. ExeSectionList.Pack;
  3023. end;
  3024. procedure TExeOutput.RemoveUnreferencedSections;
  3025. var
  3026. ObjSectionWorkList : TFPObjectList;
  3027. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  3028. begin
  3029. if not aobjsec.Used then
  3030. begin
  3031. aobjsec.Used:=true;
  3032. ObjSectionWorkList.Add(aobjsec);
  3033. end;
  3034. end;
  3035. procedure DoReloc(objreloc:TObjRelocation);
  3036. var
  3037. objsym : TObjSymbol;
  3038. refobjsec : TObjSection;
  3039. refgrp : TObjSectionGroup;
  3040. begin
  3041. { Disabled Relocation to 0 }
  3042. if (objreloc.flags and rf_nosymbol)<>0 then
  3043. exit;
  3044. refobjsec:=nil;
  3045. refgrp:=nil;
  3046. if assigned(objreloc.symbol) then
  3047. begin
  3048. objsym:=objreloc.symbol;
  3049. if objsym.bind<>AB_LOCAL then
  3050. begin
  3051. if not assigned(objsym.exesymbol) then
  3052. internalerror(200603063);
  3053. objsym.exesymbol.used:=true;
  3054. objsym:=objsym.exesymbol.objsymbol;
  3055. end;
  3056. if not assigned(objsym.objsection) then
  3057. exit
  3058. else
  3059. refobjsec:=objsym.objsection;
  3060. end
  3061. else if assigned(objreloc.objsection) then
  3062. refobjsec:=objreloc.objsection
  3063. else if assigned(objreloc.group) then
  3064. refgrp:=objreloc.group
  3065. else
  3066. internalerror(200603316);
  3067. if assigned(exemap) then
  3068. begin
  3069. objsym:=objreloc.symbol;
  3070. if assigned(objsym) and (objsym.typ<>AT_SECTION) then
  3071. exemap.Add(' References '+objsym.name+' in '
  3072. +refobjsec.fullname)
  3073. else if assigned(refobjsec) then
  3074. exemap.Add(' References '+refobjsec.fullname)
  3075. else if assigned(refgrp) then
  3076. exemap.Add(' References '+refgrp.Name)
  3077. else
  3078. internalerror(200603316);
  3079. end;
  3080. if assigned(refobjsec) then
  3081. AddToObjSectionWorkList(refobjsec);
  3082. end;
  3083. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  3084. var
  3085. i : longint;
  3086. objreloc : TObjRelocation;
  3087. begin
  3088. objreloc:=vtable.VTableRef(VTableIdx);
  3089. if assigned(objreloc) then
  3090. begin
  3091. { Process the relocation now if the ObjSection is
  3092. already processed and marked as used. Otherwise we leave it
  3093. unprocessed. It'll then be resolved when the ObjSection is
  3094. changed to Used }
  3095. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  3096. DoReloc(objreloc);
  3097. end;
  3098. { This recursive walking is done here instead of
  3099. in TExeVTable.VTableRef because we can now process
  3100. all needed relocations }
  3101. for i:=0 to vtable.ChildList.Count-1 do
  3102. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  3103. end;
  3104. procedure ProcessWorkList;
  3105. var
  3106. hs : string;
  3107. i,k : longint;
  3108. objsec : TObjSection;
  3109. objsym : TObjSymbol;
  3110. code : integer;
  3111. vtableidx : longint;
  3112. vtableexesym : TExeSymbol;
  3113. begin
  3114. while ObjSectionWorkList.Count>0 do
  3115. begin
  3116. objsec:=TObjSection(ObjSectionWorkList.Last);
  3117. if assigned(exemap) then
  3118. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  3119. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  3120. { Process Relocations }
  3121. for i:=0 to objsec.ObjRelocations.count-1 do
  3122. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  3123. { Process Virtual Entry calls }
  3124. if cs_link_opt_vtable in current_settings.globalswitches then
  3125. begin
  3126. for i:=0 to objsec.VTRefList.count-1 do
  3127. begin
  3128. objsym:=TObjSymbol(objsec.VTRefList[i]);
  3129. hs:=objsym.name;
  3130. Delete(hs,1,Pos('_',hs));
  3131. k:=Pos('$$',hs);
  3132. if k=0 then
  3133. internalerror(200603314);
  3134. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  3135. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  3136. if (code<>0) then
  3137. internalerror(200603317);
  3138. if not assigned(vtableexesym) then
  3139. internalerror(200603315);
  3140. if not assigned(vtableexesym.vtable) then
  3141. internalerror(200603316);
  3142. DoVTableRef(vtableexesym.vtable,vtableidx);
  3143. end;
  3144. end;
  3145. end;
  3146. end;
  3147. var
  3148. i,j : longint;
  3149. exesec : TExeSection;
  3150. objdata : TObjData;
  3151. objsec : TObjSection;
  3152. begin
  3153. ObjSectionWorkList:=TFPObjectList.Create(false);
  3154. if assigned(exemap) then
  3155. exemap.AddHeader('Removing unreferenced sections');
  3156. { Initialize by marking all sections unused and
  3157. adding the sections with oso_keep flags to the ObjSectionWorkList }
  3158. for i:=0 to ObjDataList.Count-1 do
  3159. begin
  3160. ObjData:=TObjData(ObjDataList[i]);
  3161. for j:=0 to ObjData.ObjSectionList.Count-1 do
  3162. begin
  3163. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  3164. objsec.Used:=false;
  3165. { TODO: remove debug section always keep}
  3166. if oso_debug in objsec.secoptions then
  3167. objsec.Used:=true;
  3168. if (oso_keep in objsec.secoptions) then
  3169. begin
  3170. AddToObjSectionWorkList(objsec);
  3171. if objsec.name='.fpc.n_links' then
  3172. objsec.Used:=false;
  3173. end;
  3174. end;
  3175. end;
  3176. if assigned(entrysym) then
  3177. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  3178. { Process all sections, add new sections to process based
  3179. on the symbol references }
  3180. ProcessWorkList;
  3181. { Handle stuff like .pdata, i.e. sections that are not referenced
  3182. but must be included if sections they reference are included.
  3183. Loop is necessary because .pdata can reference (via .xdata)
  3184. more text sections, VMTs of exception classes, etc. }
  3185. repeat
  3186. MarkTargetSpecificSections(ObjSectionWorkList);
  3187. if (ObjSectionWorkList.Count=0) then
  3188. break;
  3189. ProcessWorkList;
  3190. until False;
  3191. ObjSectionWorkList.Free;
  3192. ObjSectionWorkList:=nil;
  3193. { Remove unused objsections from ExeSectionList }
  3194. for i:=0 to ExeSectionList.Count-1 do
  3195. begin
  3196. exesec:=TExeSection(ExeSectionList[i]);
  3197. for j:=0 to exesec.ObjSectionlist.count-1 do
  3198. begin
  3199. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  3200. if not objsec.used then
  3201. begin
  3202. if assigned(exemap) then
  3203. exemap.Add('Removing '+objsec.FullName);
  3204. exesec.ObjSectionlist[j]:=nil;
  3205. objsec.ReleaseData;
  3206. end;
  3207. end;
  3208. exesec.ObjSectionlist.Pack;
  3209. end;
  3210. end;
  3211. procedure TExeOutput.FixupRelocations;
  3212. var
  3213. i,j : longint;
  3214. exesec : TExeSection;
  3215. objsec : TObjSection;
  3216. begin
  3217. for i:=0 to ExeSectionList.Count-1 do
  3218. begin
  3219. exesec:=TExeSection(ExeSectionList[i]);
  3220. if not assigned(exesec) then
  3221. continue;
  3222. for j:=0 to exesec.ObjSectionlist.count-1 do
  3223. begin
  3224. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  3225. if not objsec.Used then
  3226. internalerror(200603301);
  3227. if (objsec.ObjRelocations.Count>0) and
  3228. not assigned(objsec.data) then
  3229. internalerror(200205183);
  3230. DoRelocationFixup(objsec);
  3231. {for size = 0 data is not valid PM }
  3232. if assigned(objsec.data) and (objsec.data.size<>objsec.size) then
  3233. internalerror(2010092801);
  3234. end;
  3235. end;
  3236. end;
  3237. procedure TExeOutput.RemoveUnusedExeSymbols;
  3238. var
  3239. i : longint;
  3240. sym : TExeSymbol;
  3241. begin
  3242. { Removing unused symbols }
  3243. for i:=0 to ExeSymbolList.Count-1 do
  3244. begin
  3245. sym:=TExeSymbol(ExeSymbolList[i]);
  3246. { an unresolved weak symbol has objsection=nil }
  3247. if assigned(sym.ObjSymbol.objsection) and
  3248. (not sym.ObjSymbol.objsection.Used) then
  3249. ExeSymbolList[i]:=nil;
  3250. end;
  3251. ExeSymbolList.Pack;
  3252. end;
  3253. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  3254. begin
  3255. if AValue>MaxMemPos then
  3256. Message1(link_f_executable_too_big, target_os_string);
  3257. FCurrMemPos:=AValue;
  3258. end;
  3259. procedure TExeOutput.WriteExeSectionContent;
  3260. var
  3261. exesec : TExeSection;
  3262. objsec : TObjSection;
  3263. i,j : longint;
  3264. dpos,pad: aword;
  3265. begin
  3266. for j:=0 to ExeSectionList.Count-1 do
  3267. begin
  3268. exesec:=TExeSection(ExeSectionList[j]);
  3269. { don't write normal section if writing only debug info }
  3270. if (ExeWriteMode=ewm_dbgonly) and
  3271. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  3272. continue;
  3273. if oso_data in exesec.SecOptions then
  3274. begin
  3275. if exesec.DataPos<FWriter.Size then
  3276. InternalError(2012103001);
  3277. FWriter.Writezeros(exesec.DataPos-FWriter.Size);
  3278. for i:=0 to exesec.ObjSectionList.Count-1 do
  3279. begin
  3280. objsec:=TObjSection(exesec.ObjSectionList[i]);
  3281. if oso_data in objsec.secoptions then
  3282. begin
  3283. if not assigned(objsec.data) then
  3284. internalerror(200603042);
  3285. dpos:=objsec.MemPos-exesec.MemPos+exesec.DataPos;
  3286. pad:=dpos-FWriter.Size;
  3287. { objsection must be within SecAlign bytes from the previous one }
  3288. if (dpos<FWriter.Size) or
  3289. (pad>=max(objsec.SecAlign,1)) then
  3290. internalerror(200602251);
  3291. FWriter.writeZeros(pad);
  3292. FWriter.writearray(objsec.data);
  3293. end;
  3294. end;
  3295. end;
  3296. end;
  3297. end;
  3298. procedure TExeOutput.ReplaceExeSectionList(newlist: TFPList);
  3299. var
  3300. tmp: TFPHashObjectList;
  3301. i: longint;
  3302. begin
  3303. tmp:=TFPHashObjectList.Create(true);
  3304. for i:=0 to newlist.count-1 do
  3305. TFPHashObject(newlist[i]).ChangeOwner(tmp);
  3306. { prevent destruction of existing sections }
  3307. for i:=0 to ExeSectionList.count-1 do
  3308. ExeSectionList.List[i]:=nil;
  3309. FExeSectionList.Free;
  3310. FExeSectionList:=tmp;
  3311. end;
  3312. {****************************************************************************
  3313. TObjInput
  3314. ****************************************************************************}
  3315. constructor TObjInput.create;
  3316. begin
  3317. end;
  3318. procedure TObjInput.inputerror(const s : string);
  3319. begin
  3320. Comment(V_Error,s+' while reading '+InputFileName);
  3321. end;
  3322. class function TObjInput.CanReadObjData(AReader:TObjectReader):boolean;
  3323. begin
  3324. result:=false;
  3325. end;
  3326. procedure TObjInput.ReadSectionContent(Data:TObjData);
  3327. var
  3328. i: longint;
  3329. sec: TObjSection;
  3330. begin
  3331. for i:=0 to Data.ObjSectionList.Count-1 do
  3332. begin
  3333. sec:=TObjSection(Data.ObjSectionList[i]);
  3334. { Skip debug sections }
  3335. if (oso_debug in sec.SecOptions) and
  3336. (cs_link_strip in current_settings.globalswitches) and
  3337. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  3338. continue;
  3339. if assigned(sec.Data) then
  3340. begin
  3341. FReader.Seek(sec.datapos);
  3342. if not FReader.ReadArray(sec.data,sec.Size) then
  3343. begin
  3344. InputError('Can''t read object data');
  3345. exit;
  3346. end;
  3347. end;
  3348. end;
  3349. end;
  3350. {$ifdef MEMDEBUG}
  3351. initialization
  3352. memobjsymbols:=TMemDebug.create('ObjSymbols');
  3353. memobjsymbols.stop;
  3354. memobjsections:=TMemDebug.create('ObjSections');
  3355. memobjsections.stop;
  3356. finalization
  3357. memobjsymbols.free;
  3358. memobjsections.free;
  3359. {$endif MEMDEBUG}
  3360. end.