ogbase.pas 125 KB

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