ogbase.pas 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731
  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. tmpsym,
  2225. objsym : TObjSymbol;
  2226. grp : TObjSectionGroup;
  2227. makeexternal : boolean;
  2228. begin
  2229. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  2230. begin
  2231. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  2232. { From the local symbols we are only interessed in the
  2233. VTENTRY and VTINHERIT symbols }
  2234. if objsym.bind=AB_LOCAL then
  2235. begin
  2236. if cs_link_opt_vtable in current_settings.globalswitches then
  2237. begin
  2238. hs:=objsym.name;
  2239. if (hs[1]='V') then
  2240. begin
  2241. if Copy(hs,1,5)='VTREF' then
  2242. begin
  2243. if not assigned(objsym.ObjSection.VTRefList) then
  2244. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  2245. objsym.ObjSection.VTRefList.Add(objsym);
  2246. end
  2247. else if Copy(hs,1,7)='VTENTRY' then
  2248. VTEntryList.Add(objsym)
  2249. else if Copy(hs,1,9)='VTINHERIT' then
  2250. VTInheritList.Add(objsym);
  2251. end;
  2252. end;
  2253. continue;
  2254. end;
  2255. { If this symbol comes from COMDAT group, see if a group with
  2256. matching signature is already included. }
  2257. if assigned(objsym.objsection) and
  2258. assigned(objsym.objsection.group) then
  2259. begin
  2260. grp:=objsym.objsection.group;
  2261. if grp.IsComdat then
  2262. begin
  2263. if ComdatGroups.Find(grp.name)=nil then
  2264. ComdatGroups.Add(grp.name,grp)
  2265. else
  2266. begin
  2267. { Undefine the symbol, causing relocations to it from same
  2268. objdata to be redirected to the symbol in the actually
  2269. linked group. }
  2270. if objsym.bind=AB_GLOBAL then
  2271. objsym.bind:=AB_EXTERNAL;
  2272. { AB_WEAK_EXTERNAL remains unchanged }
  2273. objsym.objsection:=nil;
  2274. end;
  2275. end;
  2276. end;
  2277. { Search for existing exesymbol }
  2278. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  2279. if not assigned(exesym) then
  2280. begin
  2281. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  2282. exesym.ObjSymbol:=objsym;
  2283. end
  2284. else
  2285. begin
  2286. if assigned(objsym.objsection) and assigned(exesym.objsymbol.objsection) then
  2287. begin
  2288. if (oso_comdat in exesym.ObjSymbol.objsection.SecOptions) and
  2289. (oso_comdat in objsym.objsection.SecOptions) then
  2290. begin
  2291. if exesym.ObjSymbol.objsection.ComdatSelection=objsym.objsection.ComdatSelection then
  2292. begin
  2293. makeexternal:=true;
  2294. case objsym.objsection.ComdatSelection of
  2295. oscs_none:
  2296. makeexternal:=false;
  2297. oscs_any:
  2298. Message1(link_d_comdat_discard_any,objsym.name);
  2299. oscs_same_size:
  2300. if exesym.ObjSymbol.size<>objsym.size then
  2301. Message1(link_e_comdat_size_differs,objsym.name)
  2302. else
  2303. Message1(link_d_comdat_discard_size,objsym.name);
  2304. oscs_exact_match:
  2305. if (exesym.ObjSymbol.size<>objsym.size) and not exesym.ObjSymbol.objsection.Data.equal(objsym.objsection.Data) then
  2306. Message1(link_e_comdat_content_differs,objsym.name)
  2307. else
  2308. Message1(link_d_comdat_discard_content,objsym.name);
  2309. oscs_associative:
  2310. { this is handled in a different way }
  2311. makeexternal:=false;
  2312. oscs_largest:
  2313. if objsym.size>exesym.ObjSymbol.size then
  2314. begin
  2315. Message1(link_d_comdat_replace_size,objsym.name);
  2316. { we swap the symbols and turn the smaller one to an external
  2317. symbol }
  2318. tmpsym:=exesym.objsymbol;
  2319. exesym.objsymbol:=objsym;
  2320. objsym.exesymbol:=exesym;
  2321. objsym:=tmpsym;
  2322. end;
  2323. end;
  2324. if makeexternal then
  2325. begin
  2326. { Undefine the symbol, causing relocations to it from same
  2327. objdata to be redirected to the symbol that is actually
  2328. used }
  2329. if objsym.bind=AB_GLOBAL then
  2330. objsym.bind:=AB_EXTERNAL;
  2331. { AB_WEAK_EXTERNAL remains unchanged }
  2332. objsym.objsection:=nil;
  2333. end;
  2334. end
  2335. else
  2336. Message1(link_e_comdat_selection_differs,objsym.name);
  2337. end;
  2338. end;
  2339. end;
  2340. objsym.ExeSymbol:=exesym;
  2341. case objsym.bind of
  2342. AB_GLOBAL,
  2343. AB_PRIVATE_EXTERN:
  2344. begin
  2345. if exesym.State<>symstate_defined then
  2346. begin
  2347. exesym.ObjSymbol:=objsym;
  2348. exesym.State:=symstate_defined;
  2349. end
  2350. else
  2351. Message1(link_e_duplicate_symbol,objsym.name);
  2352. { hidden symbols must become local symbols in the executable }
  2353. if objsym.bind=AB_PRIVATE_EXTERN then
  2354. objsym.bind:=AB_LOCAL;
  2355. end;
  2356. AB_EXTERNAL :
  2357. begin
  2358. ExternalObjSymbols.add(objsym);
  2359. { Register unresolved symbols only the first time they
  2360. are registered }
  2361. if exesym.ObjSymbol=objsym then
  2362. UnresolvedExeSymbols.Add(exesym)
  2363. { Normal reference removes any existing "weakness" }
  2364. else if exesym.state=symstate_undefweak then
  2365. begin
  2366. exesym.state:=symstate_undefined;
  2367. exesym.ObjSymbol:=objsym;
  2368. end;
  2369. end;
  2370. AB_COMMON :
  2371. begin
  2372. { A COMMON definition overrides weak one.
  2373. Also select the symbol with largest size. }
  2374. if (exesym.State in [symstate_undefined,symstate_undefweak,symstate_defweak]) or
  2375. ((exesym.State=symstate_common) and (objsym.size>exesym.ObjSymbol.size)) then
  2376. begin
  2377. exesym.ObjSymbol:=objsym;
  2378. exesym.State:=symstate_common;
  2379. end;
  2380. if assigned(objsym.objsection) and
  2381. (objsym.objsection.objdata=internalObjData) then
  2382. FProvidedObjSymbols.add(objsym)
  2383. else
  2384. CommonObjSymbols.add(objsym);
  2385. end;
  2386. AB_WEAK_EXTERNAL :
  2387. begin
  2388. if objsym.objsection=nil then { a weak reference }
  2389. begin
  2390. ExternalObjSymbols.add(objsym);
  2391. if exesym.ObjSymbol=objsym then
  2392. begin
  2393. UnresolvedExeSymbols.Add(exesym);
  2394. exesym.state:=symstate_undefweak;
  2395. end;
  2396. end
  2397. else { a weak definition }
  2398. begin
  2399. if exesym.State in [symstate_undefined,symstate_undefweak] then
  2400. begin
  2401. exesym.ObjSymbol:=objsym;
  2402. exesym.state:=symstate_defweak;
  2403. end;
  2404. end;
  2405. end;
  2406. else
  2407. internalerror(2019050510);
  2408. end;
  2409. end;
  2410. end;
  2411. procedure LoadLibrary(lib:TStaticLibrary);
  2412. var
  2413. j,k,oldcount: longint;
  2414. members: TFPObjectList;
  2415. exesym: TExeSymbol;
  2416. objinput: TObjInput;
  2417. begin
  2418. case lib.Kind of
  2419. lkArchive:
  2420. begin
  2421. { Process list of Unresolved External symbols, we need
  2422. to use a while loop because the list can be extended when
  2423. we load members from the library. }
  2424. j:=0;
  2425. while (j<UnresolvedExeSymbols.count) do
  2426. begin
  2427. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  2428. { Check first if the symbol is still undefined }
  2429. if (exesym.State=symstate_undefined) then
  2430. begin
  2431. if lib.ArReader.OpenFile(exesym.name) then
  2432. begin
  2433. if assigned(exemap) then
  2434. begin
  2435. if firstarchive then
  2436. begin
  2437. exemap.Add('');
  2438. exemap.Add('Archive member included because of file (symbol)');
  2439. exemap.Add('');
  2440. firstarchive:=false;
  2441. end;
  2442. exemap.Add(lib.ArReader.FileName+' - '+
  2443. {exesym.ObjSymbol.ObjSection.FullName+}
  2444. '('+exesym.Name+')');
  2445. end;
  2446. objinput:=lib.ObjInputClass.Create;
  2447. objinput.ReadObjData(lib.ArReader,objdata);
  2448. objinput.free;
  2449. AddObjData(objdata);
  2450. LoadObjDataSymbols(objdata);
  2451. lib.ArReader.CloseFile;
  2452. end;
  2453. end;
  2454. inc(j);
  2455. end;
  2456. end;
  2457. lkGroup:
  2458. begin
  2459. { repeatedly process members of the group until no new
  2460. unresolved symbols appear }
  2461. members:=lib.GroupMembers;
  2462. repeat
  2463. oldcount:=UnresolvedExeSymbols.count;
  2464. for k:=0 to members.Count-1 do
  2465. LoadLibrary(TStaticLibrary(members[k]));
  2466. until UnresolvedExeSymbols.count=oldcount;
  2467. end;
  2468. lkObject:
  2469. { TODO: ownership of objdata }
  2470. //if lib.objdata.is_dynamic then
  2471. Load_DynamicObject(lib.objdata,lib.AsNeeded);
  2472. {else
  2473. begin
  2474. AddObjData(lib.objdata);
  2475. LoadObjDataSymbols(lib.objdata);
  2476. end;}
  2477. end;
  2478. end;
  2479. begin
  2480. VTEntryList:=TFPObjectList.Create(false);
  2481. VTInheritList:=TFPObjectList.Create(false);
  2482. {
  2483. The symbol resolving is done in 4 steps:
  2484. 1. Register symbols from objects
  2485. 2. Find symbols in static libraries
  2486. 3. Define symbols PROVIDEd by the link script
  2487. 4. Define still undefined common symbols
  2488. }
  2489. { Step 1, Register symbols from objects }
  2490. for i:=0 to ObjDataList.Count-1 do
  2491. begin
  2492. ObjData:=TObjData(ObjDataList[i]);
  2493. LoadObjDataSymbols(ObjData);
  2494. end;
  2495. PackUnresolvedExeSymbols('in objects');
  2496. { Step 2, Find unresolved symbols in the libraries }
  2497. firstarchive:=true;
  2498. for i:=0 to StaticLibraryList.Count-1 do
  2499. LoadLibrary(TStaticLibrary(StaticLibraryList[i]));
  2500. PackUnresolvedExeSymbols('after static libraries');
  2501. { Step 3, handle symbols provided in script }
  2502. for i:=0 to FProvidedObjSymbols.count-1 do
  2503. begin
  2504. objsym:=TObjSymbol(FProvidedObjSymbols[i]);
  2505. if objsym.exesymbol.State=symstate_defined then
  2506. continue;
  2507. objsym.exesymbol.objsymbol:=objsym;
  2508. objsym.bind:=AB_GLOBAL;
  2509. objsym.exesymbol.State:=symstate_defined;
  2510. end;
  2511. PackUnresolvedExeSymbols('after defining symbols provided by link script');
  2512. { Step 4, Match common symbols or add to the globals }
  2513. firstcommon:=true;
  2514. for i:=0 to CommonObjSymbols.count-1 do
  2515. begin
  2516. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2517. if objsym.exesymbol.State=symstate_defined then
  2518. begin
  2519. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  2520. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  2521. end
  2522. else
  2523. begin
  2524. { allocate new objsymbol in .bss of *COMMON* and assign
  2525. it to the exesymbol }
  2526. if firstcommon then
  2527. begin
  2528. if assigned(exemap) then
  2529. exemap.AddCommonSymbolsHeader;
  2530. firstcommon:=false;
  2531. end;
  2532. internalObjData.setsection(commonObjSection);
  2533. { TODO: support alignment of common symbols (ELF targets at least),
  2534. increase commonObjSection.SecAlign if necessary here. }
  2535. internalObjData.allocalign(used_align(size_2_align(objsym.size),0,commonObjSection.SecAlign));
  2536. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_DATA);
  2537. commonsym.size:=objsym.size;
  2538. internalObjData.alloc(objsym.size);
  2539. if assigned(exemap) then
  2540. exemap.AddCommonSymbol(objsym);
  2541. { Assign to the exesymbol }
  2542. objsym.exesymbol.objsymbol:=commonsym;
  2543. objsym.exesymbol.state:=symstate_defined;
  2544. end;
  2545. end;
  2546. PackUnresolvedExeSymbols('after defining COMMON symbols');
  2547. { Find entry symbol and print in map }
  2548. if (EntryName<>'') then
  2549. begin
  2550. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  2551. if assigned(exesym) then
  2552. begin
  2553. EntrySym:=exesym.ObjSymbol;
  2554. if assigned(exemap) then
  2555. begin
  2556. exemap.Add('');
  2557. exemap.Add('Entry symbol '+EntryName);
  2558. end;
  2559. end
  2560. else
  2561. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  2562. end;
  2563. { Generate VTable tree }
  2564. if cs_link_opt_vtable in current_settings.globalswitches then
  2565. BuildVTableTree(VTInheritList,VTEntryList);
  2566. VTInheritList.Free;
  2567. VTEntryList.Free;
  2568. end;
  2569. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  2570. var
  2571. debuglink : array[0..1023] of byte;
  2572. len : longint;
  2573. objsec : TObjSection;
  2574. exesec : TExeSection;
  2575. begin
  2576. { From the gdb manual chapter 15. GDB Files:
  2577. * A filename, with any leading directory components removed, followed by a zero byte,
  2578. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  2579. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  2580. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  2581. }
  2582. fillchar(debuglink,sizeof(debuglink),0);
  2583. len:=0;
  2584. move(dbgname[1],debuglink[len],length(dbgname));
  2585. inc(len,length(dbgname)+1);
  2586. len:=align(len,4);
  2587. if source_info.endian<>target_info.endian then
  2588. SwapEndian(dbgcrc);
  2589. move(dbgcrc,debuglink[len],sizeof(cardinal));
  2590. inc(len,4);
  2591. { Add section }
  2592. exesec:=FindExeSection(debuglinkname);
  2593. if not assigned(exesec) then
  2594. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  2595. exesec.SecOptions:=[oso_data,oso_keep];
  2596. exesec.SecAlign:=4;
  2597. objsec:=internalObjData.createsection(exesec.name,1,exesec.SecOptions);
  2598. internalObjData.writebytes(debuglink,len);
  2599. exesec.AddObjSection(objsec);
  2600. end;
  2601. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2602. begin
  2603. end;
  2604. procedure TExeOutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  2605. begin
  2606. end;
  2607. procedure TExeOutput.AfterUnusedSectionRemoval;
  2608. begin
  2609. end;
  2610. function ByAddress(item1,item2:pointer):longint;
  2611. var
  2612. sym1:TObjSymbol absolute item1;
  2613. sym2:TObjSymbol absolute item2;
  2614. begin
  2615. if sym1.address>sym2.address then
  2616. result:=1
  2617. else if sym1.address<sym2.address then
  2618. result:=-1
  2619. else
  2620. result:=0;
  2621. end;
  2622. procedure TExeOutput.PrintMemoryMap;
  2623. var
  2624. exesec : TExeSection;
  2625. objsec : TObjSection;
  2626. objsym : TObjSymbol;
  2627. i,j,k,m: longint;
  2628. list : TFPList;
  2629. flag : boolean;
  2630. begin
  2631. if not assigned(exemap) then
  2632. exit;
  2633. { create a list of symbols sorted by address }
  2634. list:=TFPList.Create;
  2635. for i:=0 to ExeSymbolList.Count-1 do
  2636. list.Add(TExeSymbol(ExeSymbolList[i]).ObjSymbol);
  2637. list.Sort(@ByAddress);
  2638. exemap.AddMemoryMapHeader(ImageBase);
  2639. k:=0;
  2640. for i:=0 to ExeSectionList.Count-1 do
  2641. begin
  2642. exesec:=TExeSection(ExeSectionList[i]);
  2643. exemap.AddMemoryMapExeSection(exesec);
  2644. for j:=0 to exesec.ObjSectionList.count-1 do
  2645. begin
  2646. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2647. exemap.AddMemoryMapObjectSection(objsec);
  2648. while (k<list.Count) and (TObjSymbol(list[k]).Address<objsec.MemPos) do
  2649. inc(k);
  2650. while (k<list.Count) do
  2651. begin
  2652. objsym:=TObjSymbol(list[k]);
  2653. if objsym.address>objsec.MemPos+objsec.Size then
  2654. break;
  2655. if objsym.objsection=objsec then
  2656. exemap.AddMemoryMapSymbol(objsym)
  2657. else
  2658. begin
  2659. { Got a symbol with address falling into current section, but
  2660. belonging to a different section. This may happen for zero-length
  2661. sections because symbol list is sorted by address but not by section.
  2662. Do some look-ahead in this case. }
  2663. m:=k+1;
  2664. flag:=false;
  2665. while (m<list.Count) and (TObjSymbol(list[m]).Address=objsym.address) do
  2666. begin
  2667. if TObjSymbol(list[m]).objsection=objsec then
  2668. begin
  2669. flag:=true;
  2670. list.Exchange(k,m);
  2671. exemap.AddMemoryMapSymbol(TObjSymbol(list[k]));
  2672. break;
  2673. end;
  2674. inc(m);
  2675. end;
  2676. if not flag then
  2677. break;
  2678. end;
  2679. inc(k);
  2680. end;
  2681. end;
  2682. end;
  2683. list.Free;
  2684. end;
  2685. procedure TExeOutput.FixupSymbols;
  2686. procedure UpdateSymbol(objsym:TObjSymbol);
  2687. begin
  2688. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  2689. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2690. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  2691. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2692. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2693. objsym.group:=objsym.ExeSymbol.ObjSymbol.group;
  2694. end;
  2695. var
  2696. i : longint;
  2697. objsym : TObjSymbol;
  2698. exesym : TExeSymbol;
  2699. begin
  2700. { Print list of Unresolved External symbols }
  2701. if not AllowUndefinedSymbols then
  2702. for i:=0 to UnresolvedExeSymbols.count-1 do
  2703. begin
  2704. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2705. if (exesym.State=symstate_undefined) then
  2706. Comment(V_Error,'Undefined symbol: '+exesym.name);
  2707. end;
  2708. {
  2709. Fixing up symbols is done in the following steps:
  2710. 1. Update common references
  2711. 2. Update external references
  2712. Symbols with objsection<>nil are removed from the lists,
  2713. remaining ones can be processed later by calling this method again.
  2714. }
  2715. { Step 1, Update commons. Preserve the original symbol size and bind,
  2716. this is needed for correct relocation of DJCOFF files. }
  2717. for i:=0 to CommonObjSymbols.count-1 do
  2718. begin
  2719. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2720. if objsym.bind<>AB_COMMON then
  2721. internalerror(200606241);
  2722. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2723. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2724. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2725. end;
  2726. { Step 2, Update externals }
  2727. for i:=0 to ExternalObjSymbols.count-1 do
  2728. begin
  2729. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  2730. if not (objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) then
  2731. internalerror(200606242);
  2732. UpdateSymbol(objsym);
  2733. { Collect symbols that resolve to indirect functions,
  2734. they will need additional target-specific processing. }
  2735. if objsym.typ=AT_GNU_IFUNC then
  2736. IndirectObjSymbols.Add(objsym)
  2737. else if assigned(objsym.objsection) then
  2738. ExternalObjSymbols[i]:=nil;
  2739. end;
  2740. CommonObjSymbols.Clear;
  2741. ExternalObjSymbols.Pack;
  2742. end;
  2743. procedure TExeOutput.MergeStabs;
  2744. var
  2745. stabexesec,
  2746. stabstrexesec : TExeSection;
  2747. relocsec,
  2748. currstabsec,
  2749. currstabstrsec,
  2750. mergedstabsec,
  2751. mergedstabstrsec : TObjSection;
  2752. hstabreloc,
  2753. currstabreloc : TObjRelocation;
  2754. i,j : longint;
  2755. currstabrelocidx,
  2756. mergestabcnt,
  2757. stabcnt : longword;
  2758. skipstab : boolean;
  2759. skipfun : boolean;
  2760. hstab : TObjStabEntry;
  2761. stabrelocofs : longword;
  2762. buf : array[0..1023] of byte;
  2763. bufend,
  2764. bufsize : longint;
  2765. begin
  2766. stabexesec:=FindExeSection('.stab');
  2767. stabstrexesec:=FindExeSection('.stabstr');
  2768. if (stabexesec=nil) or
  2769. (stabstrexesec=nil) or
  2770. (stabexesec.ObjSectionlist.count=0) then
  2771. exit;
  2772. { Create new stabsection }
  2773. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  2774. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  2775. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  2776. { write stab for hdrsym }
  2777. mergedstabsec.writeZeros(sizeof(TObjStabEntry));
  2778. mergestabcnt:=1;
  2779. { .stabstr starts with a #0 }
  2780. buf[0]:=0;
  2781. mergedstabstrsec.write(buf[0],1);
  2782. skipfun:=false;
  2783. { Copy stabs and corresponding Relocations }
  2784. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  2785. begin
  2786. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  2787. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  2788. if assigned(currstabstrsec) then
  2789. begin
  2790. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  2791. currstabsec.Data.seek(0);
  2792. currstabrelocidx:=0;
  2793. for j:=0 to stabcnt-1 do
  2794. begin
  2795. hstabreloc:=nil;
  2796. skipstab:=false;
  2797. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  2798. MaybeSwapStab(hstab);
  2799. { Only include first hdrsym stab }
  2800. if hstab.ntype=0 then
  2801. skipstab:=true;
  2802. if skipfun then
  2803. begin
  2804. { Skip all stabs for function body until N_RBRAC }
  2805. skipfun:=hstab.ntype<>N_RBRAC;
  2806. skipstab:=true;
  2807. end;
  2808. if not skipstab then
  2809. begin
  2810. { Find corresponding Relocation }
  2811. currstabreloc:=nil;
  2812. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  2813. begin
  2814. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  2815. if assigned(currstabreloc) and
  2816. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2817. break;
  2818. inc(currstabrelocidx);
  2819. end;
  2820. if assigned(currstabreloc) and
  2821. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2822. begin
  2823. hstabReloc:=currstabReloc;
  2824. inc(currstabrelocidx);
  2825. end;
  2826. { Check if the stab is refering to a removed section }
  2827. if assigned(hstabreloc) then
  2828. begin
  2829. if assigned(hstabreloc.Symbol) then
  2830. relocsec:=hstabreloc.Symbol.ObjSection
  2831. else
  2832. relocsec:=hstabreloc.ObjSection;
  2833. if not assigned(relocsec) then
  2834. internalerror(200603302);
  2835. if not relocsec.Used then
  2836. begin
  2837. skipstab:=true;
  2838. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  2839. begin
  2840. currstabstrsec.Data.seek(hstab.strpos);
  2841. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2842. bufend:=indexbyte(buf,bufsize,Ord(':'));
  2843. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  2844. skipfun:=true;
  2845. end;
  2846. end;
  2847. end;
  2848. end;
  2849. if not skipstab then
  2850. begin
  2851. { Copy string in stabstr }
  2852. if hstab.strpos<>0 then
  2853. begin
  2854. currstabstrsec.Data.seek(hstab.strpos);
  2855. hstab.strpos:=mergedstabstrsec.Size;
  2856. repeat
  2857. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2858. bufend:=indexbyte(buf,bufsize,0);
  2859. if bufend=-1 then
  2860. bufend:=bufsize
  2861. else
  2862. begin
  2863. { include the #0 }
  2864. inc(bufend);
  2865. end;
  2866. mergedstabstrsec.write(buf,bufend);
  2867. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2868. end;
  2869. { Copy and Update the relocation }
  2870. if assigned(hstabreloc) then
  2871. begin
  2872. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2873. { Remove from List without freeing the object }
  2874. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2875. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2876. end;
  2877. { Write updated stab }
  2878. MaybeSwapStab(hstab);
  2879. mergedstabsec.write(hstab,sizeof(hstab));
  2880. inc(mergestabcnt);
  2881. end;
  2882. end;
  2883. end;
  2884. { Unload stabs }
  2885. if assigned(currstabstrsec) then
  2886. begin
  2887. currstabstrsec.Used:=False;
  2888. currstabstrsec.ReleaseData;
  2889. end;
  2890. currstabsec.Used:=false;
  2891. currstabsec.ReleaseData;
  2892. end;
  2893. { Generate new HdrSym }
  2894. if mergedstabsec.Size>0 then
  2895. begin
  2896. hstab.strpos:=1;
  2897. hstab.ntype:=0;
  2898. hstab.nother:=0;
  2899. hstab.ndesc:=word(mergestabcnt-1);
  2900. hstab.nvalue:=mergedstabstrsec.Size;
  2901. MaybeSwapStab(hstab);
  2902. mergedstabsec.Data.seek(0);
  2903. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2904. end;
  2905. { Replace all sections with our combined stabsec }
  2906. stabexesec.ObjSectionList.Clear;
  2907. stabstrexesec.ObjSectionList.Clear;
  2908. stabexesec.AddObjSection(mergedstabsec);
  2909. stabstrexesec.AddObjSection(mergedstabstrsec);
  2910. end;
  2911. procedure TExeOutput.MarkEmptySections;
  2912. var
  2913. i, j : longint;
  2914. exesec : TExeSection;
  2915. doremove : boolean;
  2916. begin
  2917. for i:=0 to ExeSectionList.Count-1 do
  2918. begin
  2919. exesec:=TExeSection(ExeSectionList[i]);
  2920. doremove:=not(oso_keep in exesec.SecOptions) and
  2921. (
  2922. (exesec.ObjSectionlist.count=0) or
  2923. (
  2924. (cs_link_strip in current_settings.globalswitches) and
  2925. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2926. (oso_debug in exesec.SecOptions)
  2927. )
  2928. );
  2929. if not doremove then
  2930. begin
  2931. { Check if section has no actual data }
  2932. doremove:=true;
  2933. for j:=0 to exesec.ObjSectionList.Count-1 do
  2934. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2935. begin
  2936. doremove:=false;
  2937. break;
  2938. end;
  2939. end;
  2940. if doremove then
  2941. begin
  2942. Comment(V_Debug,'Disabling empty section '+exesec.name);
  2943. exesec.Disabled:=true;
  2944. end;
  2945. end;
  2946. end;
  2947. procedure TExeOutput.RemoveDisabledSections;
  2948. var
  2949. i: longint;
  2950. exesec: TExeSection;
  2951. begin
  2952. for i:=0 to ExeSectionList.Count-1 do
  2953. begin
  2954. exesec:=TExeSection(ExeSectionList[i]);
  2955. if exesec.Disabled then
  2956. ExeSectionList[i]:=nil;
  2957. end;
  2958. ExeSectionList.Pack;
  2959. end;
  2960. procedure TExeOutput.RemoveDebugInfo;
  2961. var
  2962. i : longint;
  2963. exesec : TExeSection;
  2964. begin
  2965. for i:=0 to ExeSectionList.Count-1 do
  2966. begin
  2967. exesec:=TExeSection(ExeSectionList[i]);
  2968. if (oso_debug in exesec.SecOptions) then
  2969. ExeSectionList[i]:=nil;
  2970. end;
  2971. ExeSectionList.Pack;
  2972. end;
  2973. procedure TExeOutput.RemoveUnreferencedSections;
  2974. var
  2975. ObjSectionWorkList : TFPObjectList;
  2976. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2977. begin
  2978. if not aobjsec.Used then
  2979. begin
  2980. aobjsec.Used:=true;
  2981. ObjSectionWorkList.Add(aobjsec);
  2982. end;
  2983. end;
  2984. procedure DoReloc(objreloc:TObjRelocation);
  2985. var
  2986. objsym : TObjSymbol;
  2987. refobjsec : TObjSection;
  2988. refgrp : TObjSectionGroup;
  2989. begin
  2990. { Disabled Relocation to 0 }
  2991. if (objreloc.flags and rf_nosymbol)<>0 then
  2992. exit;
  2993. refobjsec:=nil;
  2994. refgrp:=nil;
  2995. if assigned(objreloc.symbol) then
  2996. begin
  2997. objsym:=objreloc.symbol;
  2998. if objsym.bind<>AB_LOCAL then
  2999. begin
  3000. if not assigned(objsym.exesymbol) then
  3001. internalerror(200603063);
  3002. objsym.exesymbol.used:=true;
  3003. objsym:=objsym.exesymbol.objsymbol;
  3004. end;
  3005. if not assigned(objsym.objsection) then
  3006. exit
  3007. else
  3008. refobjsec:=objsym.objsection;
  3009. end
  3010. else if assigned(objreloc.objsection) then
  3011. refobjsec:=objreloc.objsection
  3012. else if assigned(objreloc.group) then
  3013. refgrp:=objreloc.group
  3014. else
  3015. internalerror(200603316);
  3016. if assigned(exemap) then
  3017. begin
  3018. objsym:=objreloc.symbol;
  3019. if assigned(objsym) and (objsym.typ<>AT_SECTION) then
  3020. exemap.Add(' References '+objsym.name+' in '
  3021. +refobjsec.fullname)
  3022. else if assigned(refobjsec) then
  3023. exemap.Add(' References '+refobjsec.fullname)
  3024. else if assigned(refgrp) then
  3025. exemap.Add(' References '+refgrp.Name)
  3026. else
  3027. internalerror(200603316);
  3028. end;
  3029. if assigned(refobjsec) then
  3030. AddToObjSectionWorkList(refobjsec);
  3031. end;
  3032. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  3033. var
  3034. i : longint;
  3035. objreloc : TObjRelocation;
  3036. begin
  3037. objreloc:=vtable.VTableRef(VTableIdx);
  3038. if assigned(objreloc) then
  3039. begin
  3040. { Process the relocation now if the ObjSection is
  3041. already processed and marked as used. Otherwise we leave it
  3042. unprocessed. It'll then be resolved when the ObjSection is
  3043. changed to Used }
  3044. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  3045. DoReloc(objreloc);
  3046. end;
  3047. { This recursive walking is done here instead of
  3048. in TExeVTable.VTableRef because we can now process
  3049. all needed relocations }
  3050. for i:=0 to vtable.ChildList.Count-1 do
  3051. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  3052. end;
  3053. procedure ProcessWorkList;
  3054. var
  3055. hs : string;
  3056. i,k : longint;
  3057. objsec : TObjSection;
  3058. objsym : TObjSymbol;
  3059. code : integer;
  3060. vtableidx : longint;
  3061. vtableexesym : TExeSymbol;
  3062. begin
  3063. while ObjSectionWorkList.Count>0 do
  3064. begin
  3065. objsec:=TObjSection(ObjSectionWorkList.Last);
  3066. if assigned(exemap) then
  3067. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  3068. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  3069. { Process Relocations }
  3070. for i:=0 to objsec.ObjRelocations.count-1 do
  3071. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  3072. { Process Virtual Entry calls }
  3073. if cs_link_opt_vtable in current_settings.globalswitches then
  3074. begin
  3075. for i:=0 to objsec.VTRefList.count-1 do
  3076. begin
  3077. objsym:=TObjSymbol(objsec.VTRefList[i]);
  3078. hs:=objsym.name;
  3079. Delete(hs,1,Pos('_',hs));
  3080. k:=Pos('$$',hs);
  3081. if k=0 then
  3082. internalerror(200603314);
  3083. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  3084. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  3085. if (code<>0) then
  3086. internalerror(200603317);
  3087. if not assigned(vtableexesym) then
  3088. internalerror(200603315);
  3089. if not assigned(vtableexesym.vtable) then
  3090. internalerror(200603316);
  3091. DoVTableRef(vtableexesym.vtable,vtableidx);
  3092. end;
  3093. end;
  3094. end;
  3095. end;
  3096. var
  3097. i,j : longint;
  3098. exesec : TExeSection;
  3099. objdata : TObjData;
  3100. objsec : TObjSection;
  3101. begin
  3102. ObjSectionWorkList:=TFPObjectList.Create(false);
  3103. if assigned(exemap) then
  3104. exemap.AddHeader('Removing unreferenced sections');
  3105. { Initialize by marking all sections unused and
  3106. adding the sections with oso_keep flags to the ObjSectionWorkList }
  3107. for i:=0 to ObjDataList.Count-1 do
  3108. begin
  3109. ObjData:=TObjData(ObjDataList[i]);
  3110. for j:=0 to ObjData.ObjSectionList.Count-1 do
  3111. begin
  3112. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  3113. objsec.Used:=false;
  3114. { TODO: remove debug section always keep}
  3115. if oso_debug in objsec.secoptions then
  3116. objsec.Used:=true;
  3117. if (oso_keep in objsec.secoptions) then
  3118. begin
  3119. AddToObjSectionWorkList(objsec);
  3120. if objsec.name='.fpc.n_links' then
  3121. objsec.Used:=false;
  3122. end;
  3123. end;
  3124. end;
  3125. if assigned(entrysym) then
  3126. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  3127. { Process all sections, add new sections to process based
  3128. on the symbol references }
  3129. ProcessWorkList;
  3130. { Handle stuff like .pdata, i.e. sections that are not referenced
  3131. but must be included if sections they reference are included.
  3132. Loop is necessary because .pdata can reference (via .xdata)
  3133. more text sections, VMTs of exception classes, etc. }
  3134. repeat
  3135. MarkTargetSpecificSections(ObjSectionWorkList);
  3136. if (ObjSectionWorkList.Count=0) then
  3137. break;
  3138. ProcessWorkList;
  3139. until False;
  3140. ObjSectionWorkList.Free;
  3141. ObjSectionWorkList:=nil;
  3142. { Remove unused objsections from ExeSectionList }
  3143. for i:=0 to ExeSectionList.Count-1 do
  3144. begin
  3145. exesec:=TExeSection(ExeSectionList[i]);
  3146. for j:=0 to exesec.ObjSectionlist.count-1 do
  3147. begin
  3148. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  3149. if not objsec.used then
  3150. begin
  3151. if assigned(exemap) then
  3152. exemap.Add('Removing '+objsec.FullName);
  3153. exesec.ObjSectionlist[j]:=nil;
  3154. objsec.ReleaseData;
  3155. end;
  3156. end;
  3157. exesec.ObjSectionlist.Pack;
  3158. end;
  3159. end;
  3160. procedure TExeOutput.FixupRelocations;
  3161. var
  3162. i,j : longint;
  3163. exesec : TExeSection;
  3164. objsec : TObjSection;
  3165. begin
  3166. for i:=0 to ExeSectionList.Count-1 do
  3167. begin
  3168. exesec:=TExeSection(ExeSectionList[i]);
  3169. if not assigned(exesec) then
  3170. continue;
  3171. for j:=0 to exesec.ObjSectionlist.count-1 do
  3172. begin
  3173. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  3174. if not objsec.Used then
  3175. internalerror(200603301);
  3176. if (objsec.ObjRelocations.Count>0) and
  3177. not assigned(objsec.data) then
  3178. internalerror(200205183);
  3179. DoRelocationFixup(objsec);
  3180. {for size = 0 data is not valid PM }
  3181. if assigned(objsec.data) and (objsec.data.size<>objsec.size) then
  3182. internalerror(2010092801);
  3183. end;
  3184. end;
  3185. end;
  3186. procedure TExeOutput.RemoveUnusedExeSymbols;
  3187. var
  3188. i : longint;
  3189. sym : TExeSymbol;
  3190. begin
  3191. { Removing unused symbols }
  3192. for i:=0 to ExeSymbolList.Count-1 do
  3193. begin
  3194. sym:=TExeSymbol(ExeSymbolList[i]);
  3195. { an unresolved weak symbol has objsection=nil }
  3196. if assigned(sym.ObjSymbol.objsection) and
  3197. (not sym.ObjSymbol.objsection.Used) then
  3198. ExeSymbolList[i]:=nil;
  3199. end;
  3200. ExeSymbolList.Pack;
  3201. end;
  3202. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  3203. begin
  3204. if AValue>MaxMemPos then
  3205. Message1(link_f_executable_too_big, target_os_string);
  3206. FCurrMemPos:=AValue;
  3207. end;
  3208. procedure TExeOutput.WriteExeSectionContent;
  3209. var
  3210. exesec : TExeSection;
  3211. objsec : TObjSection;
  3212. i,j : longint;
  3213. dpos,pad: aword;
  3214. begin
  3215. for j:=0 to ExeSectionList.Count-1 do
  3216. begin
  3217. exesec:=TExeSection(ExeSectionList[j]);
  3218. { don't write normal section if writing only debug info }
  3219. if (ExeWriteMode=ewm_dbgonly) and
  3220. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  3221. continue;
  3222. if oso_data in exesec.SecOptions then
  3223. begin
  3224. if exesec.DataPos<FWriter.Size then
  3225. InternalError(2012103001);
  3226. FWriter.Writezeros(exesec.DataPos-FWriter.Size);
  3227. for i:=0 to exesec.ObjSectionList.Count-1 do
  3228. begin
  3229. objsec:=TObjSection(exesec.ObjSectionList[i]);
  3230. if oso_data in objsec.secoptions then
  3231. begin
  3232. if not assigned(objsec.data) then
  3233. internalerror(200603042);
  3234. dpos:=objsec.MemPos-exesec.MemPos+exesec.DataPos;
  3235. pad:=dpos-FWriter.Size;
  3236. { objsection must be within SecAlign bytes from the previous one }
  3237. if (dpos<FWriter.Size) or
  3238. (pad>=max(objsec.SecAlign,1)) then
  3239. internalerror(200602251);
  3240. FWriter.writeZeros(pad);
  3241. FWriter.writearray(objsec.data);
  3242. end;
  3243. end;
  3244. end;
  3245. end;
  3246. end;
  3247. procedure TExeOutput.ReplaceExeSectionList(newlist: TFPList);
  3248. var
  3249. tmp: TFPHashObjectList;
  3250. i: longint;
  3251. begin
  3252. tmp:=TFPHashObjectList.Create(true);
  3253. for i:=0 to newlist.count-1 do
  3254. TFPHashObject(newlist[i]).ChangeOwner(tmp);
  3255. { prevent destruction of existing sections }
  3256. for i:=0 to ExeSectionList.count-1 do
  3257. ExeSectionList.List[i]:=nil;
  3258. FExeSectionList.Free;
  3259. FExeSectionList:=tmp;
  3260. end;
  3261. {****************************************************************************
  3262. TObjInput
  3263. ****************************************************************************}
  3264. constructor TObjInput.create;
  3265. begin
  3266. end;
  3267. procedure TObjInput.inputerror(const s : string);
  3268. begin
  3269. Comment(V_Error,s+' while reading '+InputFileName);
  3270. end;
  3271. class function TObjInput.CanReadObjData(AReader:TObjectReader):boolean;
  3272. begin
  3273. result:=false;
  3274. end;
  3275. procedure TObjInput.ReadSectionContent(Data:TObjData);
  3276. var
  3277. i: longint;
  3278. sec: TObjSection;
  3279. begin
  3280. for i:=0 to Data.ObjSectionList.Count-1 do
  3281. begin
  3282. sec:=TObjSection(Data.ObjSectionList[i]);
  3283. { Skip debug sections }
  3284. if (oso_debug in sec.SecOptions) and
  3285. (cs_link_strip in current_settings.globalswitches) and
  3286. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  3287. continue;
  3288. if assigned(sec.Data) then
  3289. begin
  3290. FReader.Seek(sec.datapos);
  3291. if not FReader.ReadArray(sec.data,sec.Size) then
  3292. begin
  3293. InputError('Can''t read object data');
  3294. exit;
  3295. end;
  3296. end;
  3297. end;
  3298. end;
  3299. {$ifdef MEMDEBUG}
  3300. initialization
  3301. memobjsymbols:=TMemDebug.create('ObjSymbols');
  3302. memobjsymbols.stop;
  3303. memobjsections:=TMemDebug.create('ObjSections');
  3304. memobjsections.stop;
  3305. finalization
  3306. memobjsymbols.free;
  3307. memobjsections.free;
  3308. {$endif MEMDEBUG}
  3309. end.