ogbase.pas 128 KB

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