ogbase.pas 128 KB

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