ogelf.pas 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary elf writer
  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 ogelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. cpuinfo,cpubase,aasmbase,aasmtai,aasmdata,assemble,
  27. { ELF definitions }
  28. elfbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. {$ifdef cpu64bitaddr}
  34. TElfsechdr = TElf64sechdr;
  35. {$else cpu64bitaddr}
  36. TElfsechdr = TElf32sechdr;
  37. {$endif cpu64bitaddr}
  38. TElfObjSection = class(TObjSection)
  39. public
  40. shstridx,
  41. shtype,
  42. shflags,
  43. shlink,
  44. shinfo,
  45. shentsize : longint;
  46. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  47. constructor create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags:longint;Aalign:shortint;Aentsize:longint);
  48. constructor create_reloc(aobjdata:TObjData;const Aname:string;allocflag:boolean);
  49. procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);override;
  50. end;
  51. TElfSymtabKind = (esk_obj,esk_exe,esk_dyn);
  52. TElfSymtab = class(TElfObjSection)
  53. public
  54. kind: TElfSymtabKind;
  55. fstrsec: TObjSection;
  56. symidx: longint;
  57. tlsbase: aword;
  58. constructor create(aObjData:TObjData;aKind:TElfSymtabKind);reintroduce;
  59. procedure writeSymbol(objsym:TObjSymbol;nameidx:longword=0);
  60. procedure writeInternalSymbol(avalue:aword;astridx:longword;ainfo:byte;ashndx:word);
  61. end;
  62. TElfObjData = class(TObjData)
  63. public
  64. ident: TElfIdent;
  65. flags: longword;
  66. {$ifdef mips}
  67. gp_value: longword;
  68. {$endif mips}
  69. constructor create(const n:string);override;
  70. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  71. procedure CreateDebugSections;override;
  72. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);override;
  73. end;
  74. TElfObjectOutput = class(tObjOutput)
  75. private
  76. symtabsect: TElfSymtab;
  77. shstrtabsect: TElfObjSection;
  78. procedure createrelocsection(s:TElfObjSection;data:TObjData);
  79. procedure createshstrtab(data:TObjData);
  80. procedure createsymtab(data: TObjData);
  81. procedure writesectionheader(s:TElfObjSection);
  82. procedure section_write_symbol(p:TObject;arg:pointer);
  83. procedure section_count_sections(p:TObject;arg:pointer);
  84. procedure section_create_relocsec(p:TObject;arg:pointer);
  85. procedure section_write_sechdr(p:TObject;arg:pointer);
  86. protected
  87. function writedata(data:TObjData):boolean;override;
  88. public
  89. constructor Create(AWriter:TObjectWriter);override;
  90. end;
  91. TElfAssembler = class(tinternalassembler)
  92. constructor create(smart:boolean);override;
  93. end;
  94. PSectionRec=^TSectionRec;
  95. TSectionRec=record
  96. sec: TObjSection;
  97. relocpos: aword;
  98. relocs: longint;
  99. relentsize: longint;
  100. end;
  101. TElfsecheaderarray=array of TElfsechdr;
  102. TObjSymbolClass=class of TObjSymbol;
  103. TElfObjInput=class(TObjInput)
  104. private
  105. FSecTbl: PSectionRec;
  106. FSymTbl: PPointer;
  107. FLoaded: PBoolean;
  108. shdrs: TElfsecheaderarray;
  109. nsects: longword;
  110. shoffset: aword;
  111. shstrndx: longword;
  112. symtabndx: longword;
  113. shstrtab: PChar;
  114. strtab: PChar;
  115. shstrtablen: longword;
  116. strtablen: longword;
  117. symtaboffset: aword;
  118. syms: longword;
  119. localsyms: longword;
  120. symversions: PWord;
  121. dynobj: boolean;
  122. CObjSymbol: TObjSymbolClass;
  123. verdefs: TFPHashObjectList;
  124. function LoadHeader(out objdata:TObjData):boolean;
  125. procedure LoadSection(const shdr:TElfsechdr;index:longint;objdata:TObjData);
  126. procedure LoadRelocations(const secrec:TSectionRec);
  127. procedure LoadSymbols(objdata:TObjData;count,locals:longword);
  128. procedure LoadDynamic(const shdr:TElfsechdr;objdata:TObjData);
  129. public
  130. constructor Create;override;
  131. destructor Destroy;override;
  132. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  133. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  134. function CreateSection(const shdr:TElfsechdr;index:longint;objdata:TObjData;
  135. out secname:string):TElfObjSection;
  136. function ReadBytes(offs:longint;out buf;len:longint):boolean;
  137. end;
  138. TElfVersionDef = class(TFPHashObject)
  139. public
  140. index: longword;
  141. end;
  142. TElfDynamicObjData = class(TElfObjData)
  143. private
  144. FVersionDefs: TFPHashObjectList;
  145. public
  146. soname_strofs: longword;
  147. vernaux_count: longword;
  148. constructor create(const n:string);override;
  149. destructor destroy;override;
  150. property versiondefs:TFPHashObjectList read FVersionDefs;
  151. end;
  152. TVersionedObjSymbol = class(TObjSymbol)
  153. private
  154. FVersion: TElfVersionDef;
  155. public
  156. property version: TElfVersionDef read FVersion write FVersion;
  157. end;
  158. TRelocNameProc=function(reltyp:byte):string;
  159. TEncodeRelocProc=function(objrel:TObjRelocation):byte;
  160. TLoadRelocProc=procedure(objrel:TObjRelocation);
  161. TLoadSectionProc=function(objinput:TElfObjInput;objdata:TObjData;const shdr:TElfsechdr;shindex:longint):boolean;
  162. TDynamicReloc=(
  163. dr_relative,
  164. dr_glob_dat,
  165. dr_jump_slot,
  166. dr_copy,
  167. dr_irelative
  168. );
  169. TElfTarget=record
  170. max_page_size: longword;
  171. exe_image_base: longword;
  172. machine_code: word;
  173. relocs_use_addend: boolean;
  174. dyn_reloc_codes: array[TDynamicReloc] of byte;
  175. relocname: TRelocNameProc;
  176. encodereloc: TEncodeRelocProc;
  177. loadreloc: TLoadRelocProc;
  178. loadsection: TLoadSectionProc;
  179. end;
  180. TElfExeSection=class(TExeSection)
  181. public
  182. secshidx : longword; { index of the section header }
  183. shstridx,
  184. shtype,
  185. shflags,
  186. shlink,
  187. shinfo,
  188. shentsize : longword;
  189. procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);override;
  190. end;
  191. TElfSegment=class
  192. public
  193. ptype: longword;
  194. pflags: longword;
  195. DataPos: aword;
  196. DataSize: aword;
  197. MemPos: aword;
  198. MemSize: aword;
  199. align: longword;
  200. //physaddr: aword;
  201. FSectionList: TFPObjectList;
  202. constructor Create(atype,aflags,aalign:longword);
  203. destructor Destroy; override;
  204. procedure Add(exesec:TExeSection);
  205. end;
  206. TElfExeOutput=class(TExeOutput)
  207. private
  208. segmentlist: TFPObjectList;
  209. textseg,
  210. dataseg,
  211. noteseg,
  212. phdrseg: TElfSegment;
  213. shstrtabsect: TElfObjSection;
  214. symtab: TElfSymtab;
  215. shoffset: aword;
  216. gotwritten: boolean;
  217. { dynamic linking }
  218. dynsymnames: Plongword;
  219. dynsymtable: TElfSymtab;
  220. interpobjsec: TObjSection;
  221. FInterpreter: pshortstring;
  222. verneedcount,
  223. verdefcount: longword;
  224. symversec,
  225. verdefsec,
  226. verneedsec,
  227. hashobjsec: TElfObjSection;
  228. neededlist: TFPHashList;
  229. dyncopysyms: TFPObjectList;
  230. function AttachSection(objsec:TObjSection):TElfExeSection;
  231. function CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  232. procedure WriteHeader;
  233. procedure WriteDynamicSymbolsHash;
  234. procedure WriteVersionSections;
  235. procedure WriteDynamicTags;
  236. procedure FinishDynamicTags;
  237. procedure exesection_write_header(p:TObject;arg:Pointer);
  238. procedure segment_write_header(p:TObject;arg:Pointer);
  239. procedure mempos_segment(seg:TElfSegment);
  240. procedure datapos_segment(seg:TElfSegment);
  241. procedure MapSectionsToSegments;
  242. procedure WriteStaticSymtable;
  243. procedure WriteShstrtab;
  244. procedure FixupSectionLinks;
  245. procedure InitDynlink;
  246. procedure OrderOrphanSections;
  247. protected
  248. dynamiclink: boolean;
  249. hastextrelocs: boolean;
  250. gotsymbol: TObjSymbol;
  251. dynsymlist: TFPObjectList;
  252. dynamicsec,
  253. gotobjsec: TObjSection;
  254. dynbssobjsec,
  255. pltobjsec,
  256. gotpltobjsec,
  257. pltrelocsec,
  258. ipltrelocsec,
  259. dynrelocsec: TElfObjSection;
  260. dynreloclist: TFPObjectList;
  261. tlsseg: TElfSegment;
  262. relative_reloc_count: longint;
  263. gotsize: aword;
  264. dynrelsize: aword;
  265. procedure PrepareGOT;virtual;
  266. function AllocGOTSlot(objsym: TObjSymbol):boolean;virtual;
  267. procedure CreateGOTSection;virtual;
  268. procedure make_dynamic_if_undefweak(exesym:TExeSymbol);
  269. procedure WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  270. procedure CreatePLT;virtual;
  271. procedure WriteFirstPLTEntry;virtual;abstract;
  272. procedure WritePLTEntry(exesym:TExeSymbol);virtual;
  273. procedure WriteIndirectPLTEntry(exesym:TExeSymbol);virtual;
  274. procedure WriteTargetDynamicTags;virtual;
  275. procedure GOTRelocPass1(objsec:TObjSection;var idx:longint);virtual;abstract;
  276. procedure ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  277. procedure ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  278. procedure WriteDynTag(aTag:longword;aValue:longword);
  279. procedure WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword=0);
  280. procedure Do_Mempos;virtual;
  281. public
  282. constructor Create;override;
  283. destructor Destroy;override;
  284. procedure Load_Start;override;
  285. procedure Load_DynamicObject(ObjData:TObjData;asneeded:boolean);override;
  286. procedure Order_Start;override;
  287. procedure Order_end;override;
  288. procedure AfterUnusedSectionRemoval;override;
  289. procedure MemPos_Start;override;
  290. procedure MemPos_ExeSection(const aname:string);override;
  291. procedure DataPos_Start;override;
  292. procedure DataPos_ExeSection(const aname:string);override;
  293. function writeData:boolean;override;
  294. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  295. property interpreter:pshortstring read FInterpreter write FInterpreter;
  296. end;
  297. var
  298. ElfExeOutputClass: TExeOutputClass;
  299. ElfTarget: TElfTarget;
  300. const
  301. { Bits of TObjSymbol.refs field }
  302. symref_plt = 1;
  303. symref_from_text = 2;
  304. implementation
  305. uses
  306. SysUtils,
  307. verbose,
  308. export,expunix,
  309. cutils,globals,fmodule;
  310. const
  311. symbolresize = 200*18;
  312. {$ifdef cpu64bitaddr}
  313. const
  314. ELFCLASS = ELFCLASS64;
  315. type
  316. telfheader = telf64header;
  317. telfreloc = telf64reloc;
  318. telfsymbol = telf64symbol;
  319. telfproghdr = telf64proghdr;
  320. telfdyn = telf64dyn;
  321. function ELF_R_INFO(sym:longword;typ:byte):qword;inline;
  322. begin
  323. result:=(qword(sym) shl 32) or typ;
  324. end;
  325. {$else cpu64bitaddr}
  326. const
  327. ELFCLASS = ELFCLASS32;
  328. type
  329. telfheader = telf32header;
  330. telfreloc = telf32reloc;
  331. telfsymbol = telf32symbol;
  332. telfproghdr = telf32proghdr;
  333. telfdyn = telf32dyn;
  334. function ELF_R_INFO(sym:longword;typ:byte):longword;inline;
  335. begin
  336. result:=(sym shl 8) or typ;
  337. end;
  338. {$endif cpu64bitaddr}
  339. procedure MayBeSwapHeader(var h : telf32header);
  340. begin
  341. if source_info.endian<>target_info.endian then
  342. with h do
  343. begin
  344. e_type:=swapendian(e_type);
  345. e_machine:=swapendian(e_machine);
  346. e_version:=swapendian(e_version);
  347. e_entry:=swapendian(e_entry);
  348. e_phoff:=swapendian(e_phoff);
  349. e_shoff:=swapendian(e_shoff);
  350. e_flags:=swapendian(e_flags);
  351. e_ehsize:=swapendian(e_ehsize);
  352. e_phentsize:=swapendian(e_phentsize);
  353. e_phnum:=swapendian(e_phnum);
  354. e_shentsize:=swapendian(e_shentsize);
  355. e_shnum:=swapendian(e_shnum);
  356. e_shstrndx:=swapendian(e_shstrndx);
  357. end;
  358. end;
  359. procedure MayBeSwapHeader(var h : telf64header);
  360. begin
  361. if source_info.endian<>target_info.endian then
  362. with h do
  363. begin
  364. e_type:=swapendian(e_type);
  365. e_machine:=swapendian(e_machine);
  366. e_version:=swapendian(e_version);
  367. e_entry:=swapendian(e_entry);
  368. e_phoff:=swapendian(e_phoff);
  369. e_shoff:=swapendian(e_shoff);
  370. e_flags:=swapendian(e_flags);
  371. e_ehsize:=swapendian(e_ehsize);
  372. e_phentsize:=swapendian(e_phentsize);
  373. e_phnum:=swapendian(e_phnum);
  374. e_shentsize:=swapendian(e_shentsize);
  375. e_shnum:=swapendian(e_shnum);
  376. e_shstrndx:=swapendian(e_shstrndx);
  377. end;
  378. end;
  379. procedure MayBeSwapHeader(var h : telf32proghdr);
  380. begin
  381. if source_info.endian<>target_info.endian then
  382. with h do
  383. begin
  384. p_align:=swapendian(p_align);
  385. p_filesz:=swapendian(p_filesz);
  386. p_flags:=swapendian(p_flags);
  387. p_memsz:=swapendian(p_memsz);
  388. p_offset:=swapendian(p_offset);
  389. p_paddr:=swapendian(p_paddr);
  390. p_type:=swapendian(p_type);
  391. p_vaddr:=swapendian(p_vaddr);
  392. end;
  393. end;
  394. procedure MayBeSwapHeader(var h : telf64proghdr);
  395. begin
  396. if source_info.endian<>target_info.endian then
  397. with h do
  398. begin
  399. p_align:=swapendian(p_align);
  400. p_filesz:=swapendian(p_filesz);
  401. p_flags:=swapendian(p_flags);
  402. p_memsz:=swapendian(p_memsz);
  403. p_offset:=swapendian(p_offset);
  404. p_paddr:=swapendian(p_paddr);
  405. p_type:=swapendian(p_type);
  406. p_vaddr:=swapendian(p_vaddr);
  407. end;
  408. end;
  409. procedure MaybeSwapSecHeader(var h : telf32sechdr);
  410. begin
  411. if source_info.endian<>target_info.endian then
  412. with h do
  413. begin
  414. sh_name:=swapendian(sh_name);
  415. sh_type:=swapendian(sh_type);
  416. sh_flags:=swapendian(sh_flags);
  417. sh_addr:=swapendian(sh_addr);
  418. sh_offset:=swapendian(sh_offset);
  419. sh_size:=swapendian(sh_size);
  420. sh_link:=swapendian(sh_link);
  421. sh_info:=swapendian(sh_info);
  422. sh_addralign:=swapendian(sh_addralign);
  423. sh_entsize:=swapendian(sh_entsize);
  424. end;
  425. end;
  426. procedure MaybeSwapSecHeader(var h : telf64sechdr);
  427. begin
  428. if source_info.endian<>target_info.endian then
  429. with h do
  430. begin
  431. sh_name:=swapendian(sh_name);
  432. sh_type:=swapendian(sh_type);
  433. sh_flags:=swapendian(sh_flags);
  434. sh_addr:=swapendian(sh_addr);
  435. sh_offset:=swapendian(sh_offset);
  436. sh_size:=swapendian(sh_size);
  437. sh_link:=swapendian(sh_link);
  438. sh_info:=swapendian(sh_info);
  439. sh_addralign:=swapendian(sh_addralign);
  440. sh_entsize:=swapendian(sh_entsize);
  441. end;
  442. end;
  443. procedure MaybeSwapElfSymbol(var h : telf32symbol);
  444. begin
  445. if source_info.endian<>target_info.endian then
  446. with h do
  447. begin
  448. st_name:=swapendian(st_name);
  449. st_value:=swapendian(st_value);
  450. st_size:=swapendian(st_size);
  451. st_shndx:=swapendian(st_shndx);
  452. end;
  453. end;
  454. procedure MaybeSwapElfSymbol(var h : telf64symbol);
  455. begin
  456. if source_info.endian<>target_info.endian then
  457. with h do
  458. begin
  459. st_name:=swapendian(st_name);
  460. st_value:=swapendian(st_value);
  461. st_size:=swapendian(st_size);
  462. st_shndx:=swapendian(st_shndx);
  463. end;
  464. end;
  465. procedure MaybeSwapElfReloc(var h : telf32reloc);
  466. begin
  467. if source_info.endian<>target_info.endian then
  468. with h do
  469. begin
  470. address:=swapendian(address);
  471. info:=swapendian(info);
  472. addend:=swapendian(addend);
  473. end;
  474. end;
  475. procedure MaybeSwapElfReloc(var h : telf64reloc);
  476. begin
  477. if source_info.endian<>target_info.endian then
  478. with h do
  479. begin
  480. address:=swapendian(address);
  481. info:=swapendian(info);
  482. addend:=swapendian(addend);
  483. end;
  484. end;
  485. procedure MaybeSwapElfDyn(var h : telf32dyn);
  486. begin
  487. if source_info.endian<>target_info.endian then
  488. with h do
  489. begin
  490. d_tag:=swapendian(d_tag);
  491. d_val:=swapendian(d_val);
  492. end;
  493. end;
  494. procedure MaybeSwapElfDyn(var h : telf64dyn);
  495. begin
  496. if source_info.endian<>target_info.endian then
  497. with h do
  498. begin
  499. d_tag:=swapendian(d_tag);
  500. d_val:=swapendian(d_val);
  501. end;
  502. end;
  503. procedure MaybeSwapElfverdef(var h: TElfverdef);
  504. begin
  505. if source_info.endian<>target_info.endian then
  506. with h do
  507. begin
  508. vd_version:=swapendian(vd_version);
  509. vd_flags:=swapendian(vd_flags);
  510. vd_ndx:=swapendian(vd_ndx);
  511. vd_cnt:=swapendian(vd_cnt);
  512. vd_hash:=swapendian(vd_hash);
  513. vd_aux:=swapendian(vd_aux);
  514. vd_next:=swapendian(vd_next);
  515. end;
  516. end;
  517. procedure MaybeSwapElfverdaux(var h: TElfverdaux);
  518. begin
  519. if source_info.endian<>target_info.endian then
  520. with h do
  521. begin
  522. vda_name:=swapendian(vda_name);
  523. vda_next:=swapendian(vda_next);
  524. end;
  525. end;
  526. procedure MaybeSwapElfverneed(var h: TElfverneed);
  527. begin
  528. if source_info.endian<>target_info.endian then
  529. with h do
  530. begin
  531. vn_version:=swapendian(vn_version);
  532. vn_cnt:=swapendian(vn_cnt);
  533. vn_file:=swapendian(vn_file);
  534. vn_aux:=swapendian(vn_aux);
  535. vn_next:=swapendian(vn_next);
  536. end;
  537. end;
  538. procedure MaybeSwapElfvernaux(var h: TElfvernaux);
  539. begin
  540. if source_info.endian<>target_info.endian then
  541. with h do
  542. begin
  543. vna_hash:=swapendian(vna_hash);
  544. vna_flags:=swapendian(vna_flags);
  545. vna_other:=swapendian(vna_other);
  546. vna_name:=swapendian(vna_name);
  547. vna_next:=swapendian(vna_next);
  548. end;
  549. end;
  550. {****************************************************************************
  551. Helpers
  552. ****************************************************************************}
  553. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  554. begin
  555. { Section Type }
  556. AshType:=SHT_PROGBITS;
  557. if oso_strings in aoptions then
  558. AshType:=SHT_STRTAB
  559. else if not(oso_data in aoptions) then
  560. AshType:=SHT_NOBITS;
  561. { Section Flags }
  562. Ashflags:=0;
  563. if oso_load in aoptions then
  564. Ashflags:=Ashflags or SHF_ALLOC;
  565. if oso_executable in aoptions then
  566. Ashflags:=Ashflags or SHF_EXECINSTR;
  567. if oso_write in aoptions then
  568. Ashflags:=Ashflags or SHF_WRITE;
  569. end;
  570. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  571. begin
  572. aoptions:=[];
  573. { Section Type }
  574. if AshType<>SHT_NOBITS then
  575. include(aoptions,oso_data);
  576. if AshType=SHT_STRTAB then
  577. include(aoptions,oso_strings);
  578. { Section Flags }
  579. if Ashflags and SHF_ALLOC<>0 then
  580. include(aoptions,oso_load);
  581. if Ashflags and SHF_WRITE<>0 then
  582. include(aoptions,oso_write);
  583. if Ashflags and SHF_EXECINSTR<>0 then
  584. include(aoptions,oso_executable);
  585. end;
  586. {****************************************************************************
  587. TElfObjSection
  588. ****************************************************************************}
  589. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  590. begin
  591. inherited create(AList,Aname,Aalign,aoptions);
  592. index:=0;
  593. shstridx:=0;
  594. encodesechdrflags(aoptions,shtype,shflags);
  595. shlink:=0;
  596. shinfo:=0;
  597. if name='.stab' then
  598. shentsize:=sizeof(TObjStabEntry);
  599. end;
  600. constructor TElfObjSection.create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags:longint;Aalign:shortint;Aentsize:longint);
  601. var
  602. aoptions : TObjSectionOptions;
  603. begin
  604. decodesechdrflags(Ashtype,Ashflags,aoptions);
  605. inherited create(aobjdata.ObjSectionList,Aname,Aalign,aoptions);
  606. objdata:=aobjdata;
  607. index:=0;
  608. shstridx:=0;
  609. shtype:=AshType;
  610. shflags:=AshFlags;
  611. shentsize:=Aentsize;
  612. end;
  613. const
  614. relsec_prefix:array[boolean] of string[5] = ('.rel','.rela');
  615. relsec_shtype:array[boolean] of longword = (SHT_REL,SHT_RELA);
  616. constructor TElfObjSection.create_reloc(aobjdata:TObjData;const Aname:string;allocflag:boolean);
  617. begin
  618. create_ext(aobjdata,
  619. relsec_prefix[ElfTarget.relocs_use_addend]+aname,
  620. relsec_shtype[ElfTarget.relocs_use_addend],
  621. SHF_ALLOC*ord(allocflag),
  622. sizeof(pint),
  623. (2+ord(ElfTarget.relocs_use_addend))*sizeof(pint));
  624. end;
  625. procedure TElfObjSection.writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  626. var
  627. reloc: TObjRelocation;
  628. begin
  629. reloc:=TObjRelocation.CreateSection(Size,aTarget,reltype);
  630. reloc.size:=len;
  631. ObjRelocations.Add(reloc);
  632. if reltype=RELOC_RELATIVE then
  633. { ARM does not require this adjustment, other CPUs must be checked }
  634. {$if defined(i386) or defined(x86_64)}
  635. dec(offset,len)
  636. {$endif i386 or x86_64}
  637. else if reltype<>RELOC_ABSOLUTE then
  638. InternalError(2012062401);
  639. if ElfTarget.relocs_use_addend then
  640. begin
  641. reloc.orgsize:=offset;
  642. offset:=0;
  643. end;
  644. write(offset,len);
  645. end;
  646. {****************************************************************************
  647. TElfObjData
  648. ****************************************************************************}
  649. constructor TElfObjData.create(const n:string);
  650. begin
  651. inherited create(n);
  652. CObjSection:=TElfObjSection;
  653. end;
  654. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  655. const
  656. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  657. { TODO: sec_rodata is still writable }
  658. '.text','.data','.data','.rodata','.bss','.threadvar',
  659. '.pdata',
  660. '.text', { darwin stubs }
  661. '__DATA,__nl_symbol_ptr',
  662. '__DATA,__la_symbol_ptr',
  663. '__DATA,__mod_init_func',
  664. '__DATA,__mod_term_func',
  665. '.stab','.stabstr',
  666. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  667. '.eh_frame',
  668. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  669. '.fpc',
  670. '.toc',
  671. '.init',
  672. '.fini',
  673. '.objc_class',
  674. '.objc_meta_class',
  675. '.objc_cat_cls_meth',
  676. '.objc_cat_inst_meth',
  677. '.objc_protocol',
  678. '.objc_string_object',
  679. '.objc_cls_meth',
  680. '.objc_inst_meth',
  681. '.objc_cls_refs',
  682. '.objc_message_refs',
  683. '.objc_symbols',
  684. '.objc_category',
  685. '.objc_class_vars',
  686. '.objc_instance_vars',
  687. '.objc_module_info',
  688. '.objc_class_names',
  689. '.objc_meth_var_types',
  690. '.objc_meth_var_names',
  691. '.objc_selector_strs',
  692. '.objc_protocol_ext',
  693. '.objc_class_ext',
  694. '.objc_property',
  695. '.objc_image_info',
  696. '.objc_cstring_object',
  697. '.objc_sel_fixup',
  698. '__DATA,__objc_data',
  699. '__DATA,__objc_const',
  700. '.objc_superrefs',
  701. '__DATA, __datacoal_nt,coalesced',
  702. '.objc_classlist',
  703. '.objc_nlclasslist',
  704. '.objc_catlist',
  705. '.obcj_nlcatlist',
  706. '.objc_protolist'
  707. );
  708. var
  709. sep : string[3];
  710. secname : string;
  711. begin
  712. { section type user gives the user full controll on the section name }
  713. if atype=sec_user then
  714. result:=aname
  715. else
  716. begin
  717. secname:=secnames[atype];
  718. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  719. begin
  720. result:=secname+'.'+aname;
  721. exit;
  722. end;
  723. if create_smartlink_sections and (aname<>'') then
  724. begin
  725. case aorder of
  726. secorder_begin :
  727. sep:='.b_';
  728. secorder_end :
  729. sep:='.z_';
  730. else
  731. sep:='.n_';
  732. end;
  733. result:=secname+sep+aname
  734. end
  735. else
  736. result:=secname;
  737. end;
  738. end;
  739. procedure TElfObjData.CreateDebugSections;
  740. begin
  741. if target_dbg.id=dbg_stabs then
  742. begin
  743. stabssec:=createsection(sec_stab);
  744. stabstrsec:=createsection(sec_stabstr);
  745. end;
  746. end;
  747. procedure TElfObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);
  748. var
  749. symaddr : aint;
  750. objreloc: TObjRelocation;
  751. begin
  752. if CurrObjSec=nil then
  753. internalerror(200403292);
  754. objreloc:=nil;
  755. if assigned(p) then
  756. begin
  757. { real address of the symbol }
  758. symaddr:=p.address;
  759. { Local ObjSymbols can be resolved already or need a section reloc }
  760. if (p.bind=AB_LOCAL) and
  761. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32{$endif x86_64}]) then
  762. begin
  763. { For a reltype relocation in the same section the
  764. value can be calculated }
  765. if (p.objsection=CurrObjSec) and
  766. (reltype=RELOC_RELATIVE) then
  767. inc(data,symaddr-len-CurrObjSec.Size)
  768. else
  769. begin
  770. objreloc:=TObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,reltype);
  771. CurrObjSec.ObjRelocations.Add(objreloc);
  772. inc(data,symaddr);
  773. end;
  774. end
  775. else
  776. begin
  777. objreloc:=TObjRelocation.CreateSymbol(CurrObjSec.Size,p,reltype);
  778. CurrObjSec.ObjRelocations.Add(objreloc);
  779. { If target is a local label and it isn't handled above,
  780. patch its type in order to get it written to symtable.
  781. This may happen e.g. when taking address of Pascal label in PIC mode. }
  782. if (p.bind=AB_LOCAL) and (p.typ=AT_LABEL) then
  783. p.typ:=AT_ADDR;
  784. end;
  785. end;
  786. if assigned(objreloc) then
  787. begin
  788. objreloc.size:=len;
  789. if reltype in [RELOC_RELATIVE{$ifdef x86},RELOC_PLT32{$endif}{$ifdef x86_64},RELOC_GOTPCREL{$endif}] then
  790. dec(data,len);
  791. if ElfTarget.relocs_use_addend then
  792. begin
  793. objreloc.orgsize:=aword(data);
  794. data:=0;
  795. end;
  796. end;
  797. CurrObjSec.write(data,len);
  798. end;
  799. {****************************************************************************
  800. TElfDynamicObjData
  801. ****************************************************************************}
  802. constructor TElfDynamicObjData.create(const n:string);
  803. begin
  804. inherited Create(n);
  805. FVersionDefs:=TFPHashObjectList.create(true);
  806. { Default symversions with indices 0 and 1 }
  807. TElfVersionDef.create(FVersionDefs,'*local*');
  808. TElfVersionDef.create(FVersionDefs,'*global*');
  809. end;
  810. destructor TElfDynamicObjData.destroy;
  811. begin
  812. FVersionDefs.free;
  813. inherited Destroy;
  814. end;
  815. {****************************************************************************
  816. TElfSymtab
  817. ****************************************************************************}
  818. const
  819. symsecnames: array[boolean] of string[8] = ('.symtab','.dynsym');
  820. strsecnames: array[boolean] of string[8] = ('.strtab','.dynstr');
  821. symsectypes: array[boolean] of longword = (SHT_SYMTAB,SHT_DYNSYM);
  822. symsecattrs: array[boolean] of longword = (0,SHF_ALLOC);
  823. constructor TElfSymtab.create(aObjData:TObjData;aKind:TElfSymtabKind);
  824. var
  825. dyn:boolean;
  826. begin
  827. dyn:=(aKind=esk_dyn);
  828. create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],sizeof(pint),sizeof(TElfSymbol));
  829. fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],1,0);
  830. fstrsec.writezeros(1);
  831. writezeros(sizeof(TElfSymbol));
  832. symidx:=1;
  833. shinfo:=1;
  834. kind:=aKind;
  835. end;
  836. procedure TElfSymtab.writeInternalSymbol(avalue:aword;astridx:longword;ainfo:byte;ashndx:word);
  837. var
  838. elfsym:TElfSymbol;
  839. begin
  840. fillchar(elfsym,sizeof(elfsym),0);
  841. elfsym.st_value:=avalue;
  842. elfsym.st_name:=astridx;
  843. elfsym.st_info:=ainfo;
  844. elfsym.st_shndx:=ashndx;
  845. inc(symidx);
  846. inc(shinfo);
  847. MaybeSwapElfSymbol(elfsym);
  848. write(elfsym,sizeof(elfsym));
  849. end;
  850. procedure TElfSymtab.writeSymbol(objsym:TObjSymbol;nameidx:longword);
  851. var
  852. elfsym:TElfSymbol;
  853. begin
  854. fillchar(elfsym,sizeof(elfsym),0);
  855. if nameidx=0 then
  856. elfsym.st_name:=fstrsec.writestr(objsym.name)
  857. else
  858. elfsym.st_name:=nameidx;
  859. elfsym.st_size:=objsym.size;
  860. elfsym.st_value:=objsym.address;
  861. case objsym.bind of
  862. AB_LOCAL :
  863. begin
  864. elfsym.st_info:=STB_LOCAL shl 4;
  865. inc(shinfo);
  866. end;
  867. AB_COMMON :
  868. begin
  869. elfsym.st_value:=size_2_align(objsym.size);
  870. elfsym.st_info:=STB_GLOBAL shl 4;
  871. elfsym.st_shndx:=SHN_COMMON;
  872. end;
  873. AB_EXTERNAL :
  874. elfsym.st_info:=STB_GLOBAL shl 4;
  875. AB_WEAK_EXTERNAL :
  876. elfsym.st_info:=STB_WEAK shl 4;
  877. AB_GLOBAL :
  878. elfsym.st_info:=STB_GLOBAL shl 4;
  879. else
  880. InternalError(2012111801);
  881. end;
  882. { External symbols must be NOTYPE in relocatable files }
  883. if (objsym.bind<>AB_EXTERNAL) or (kind<>esk_obj) then
  884. begin
  885. case objsym.typ of
  886. AT_FUNCTION :
  887. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  888. AT_DATA :
  889. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  890. AT_TLS:
  891. elfsym.st_info:=elfsym.st_info or STT_TLS;
  892. AT_GNU_IFUNC:
  893. elfsym.st_info:=elfsym.st_info or STT_GNU_IFUNC;
  894. { other types are implicitly mapped to STT_NOTYPE }
  895. end;
  896. end;
  897. if objsym.bind<>AB_COMMON then
  898. begin
  899. if kind<>esk_obj then
  900. begin
  901. if assigned(objsym.objsection) and assigned(objsym.objsection.ExeSection) then
  902. begin
  903. if (objsym.typ=AT_TLS) then
  904. elfsym.st_value:=elfsym.st_value-tlsbase
  905. else if (oso_plt in objsym.objsection.SecOptions) then
  906. elfsym.st_value:=0
  907. else
  908. elfsym.st_shndx:=TElfExeSection(objsym.objsection.ExeSection).secshidx;
  909. end;
  910. end
  911. else
  912. begin
  913. if assigned(objsym.objsection) then
  914. elfsym.st_shndx:=objsym.objsection.index
  915. else
  916. elfsym.st_shndx:=SHN_UNDEF;
  917. objsym.symidx:=symidx;
  918. end;
  919. end;
  920. inc(symidx);
  921. MaybeSwapElfSymbol(elfsym);
  922. write(elfsym,sizeof(TElfSymbol));
  923. end;
  924. {****************************************************************************
  925. TElfObjectOutput
  926. ****************************************************************************}
  927. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  928. begin
  929. inherited Create(AWriter);
  930. CObjData:=TElfObjData;
  931. end;
  932. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection;data:TObjData);
  933. var
  934. i : longint;
  935. rel : telfreloc;
  936. objreloc : TObjRelocation;
  937. relsym : longint;
  938. relocsect : TElfObjSection;
  939. begin
  940. { create the reloc section }
  941. relocsect:=TElfObjSection.create_reloc(data,s.name,false);
  942. relocsect.shlink:=symtabsect.index;
  943. relocsect.shinfo:=s.index;
  944. { add the relocations }
  945. for i:=0 to s.Objrelocations.count-1 do
  946. begin
  947. objreloc:=TObjRelocation(s.Objrelocations[i]);
  948. { Symbol }
  949. if assigned(objreloc.symbol) then
  950. begin
  951. if objreloc.symbol.symidx=-1 then
  952. begin
  953. writeln(objreloc.symbol.Name);
  954. internalerror(200603012);
  955. end;
  956. relsym:=objreloc.symbol.symidx;
  957. end
  958. else
  959. begin
  960. if objreloc.objsection<>nil then
  961. relsym:=objreloc.objsection.secsymidx
  962. else
  963. relsym:=SHN_UNDEF;
  964. end;
  965. rel.address:=objreloc.dataoffset;
  966. rel.info:=ELF_R_INFO(relsym,ElfTarget.encodereloc(objreloc));
  967. {$push}{$r-}
  968. rel.addend:=objreloc.orgsize;
  969. {$pop}
  970. { write reloc }
  971. { ElfXX_Rel is essentially ElfXX_Rela without the addend field. }
  972. MaybeSwapElfReloc(rel);
  973. relocsect.write(rel,relocsect.shentsize);
  974. end;
  975. end;
  976. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  977. begin
  978. { Must not write symbols for internal sections like .symtab }
  979. { TODO: maybe use inclusive list of section types instead }
  980. if (TElfObjSection(p).shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  981. exit;
  982. TObjSection(p).secsymidx:=symtabsect.symidx;
  983. symtabsect.writeInternalSymbol(0,0,STT_SECTION,TObjSection(p).index);
  984. end;
  985. procedure TElfObjectOutput.createsymtab(data: TObjData);
  986. var
  987. i : longint;
  988. objsym : TObjSymbol;
  989. begin
  990. with data do
  991. begin
  992. { section symbols }
  993. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  994. { First the Local Symbols, this is required by ELF. The localsyms
  995. count stored in shinfo is used to skip the local symbols
  996. when traversing the symtab }
  997. for i:=0 to ObjSymbolList.Count-1 do
  998. begin
  999. objsym:=TObjSymbol(ObjSymbolList[i]);
  1000. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  1001. symtabsect.WriteSymbol(objsym);
  1002. end;
  1003. { Global Symbols }
  1004. for i:=0 to ObjSymbolList.Count-1 do
  1005. begin
  1006. objsym:=TObjSymbol(ObjSymbolList[i]);
  1007. if (objsym.bind<>AB_LOCAL) then
  1008. symtabsect.WriteSymbol(objsym);
  1009. end;
  1010. { update the .symtab section header }
  1011. symtabsect.shlink:=symtabsect.fstrsec.index;
  1012. end;
  1013. end;
  1014. procedure TElfObjectOutput.createshstrtab(data: TObjData);
  1015. var
  1016. i,prefixlen:longint;
  1017. objsec,target:TElfObjSection;
  1018. begin
  1019. shstrtabsect.writezeros(1);
  1020. prefixlen:=length('.rel')+ord(ElfTarget.relocs_use_addend);
  1021. for i:=0 to data.ObjSectionList.Count-1 do
  1022. begin
  1023. objsec:=TElfObjSection(data.ObjSectionList[i]);
  1024. { Alias section names into names of corresponding reloc sections,
  1025. this is allowed by ELF specs and saves good half of .shstrtab space. }
  1026. if objsec.shtype=relsec_shtype[ElfTarget.relocs_use_addend] then
  1027. begin
  1028. target:=TElfObjSection(data.ObjSectionList[objsec.shinfo-1]);
  1029. if (target.ObjRelocations.Count=0) or
  1030. (target.shstridx<prefixlen) then
  1031. InternalError(2012101204);
  1032. objsec.shstridx:=target.shstridx-prefixlen;
  1033. end
  1034. else
  1035. begin
  1036. if objsec.ObjRelocations.Count<>0 then
  1037. shstrtabsect.write(relsec_prefix[true][1],prefixlen);
  1038. objsec.shstridx:=shstrtabsect.writestr(objsec.name);
  1039. end;
  1040. end;
  1041. end;
  1042. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  1043. var
  1044. sechdr : telfsechdr;
  1045. begin
  1046. fillchar(sechdr,sizeof(sechdr),0);
  1047. sechdr.sh_name:=s.shstridx;
  1048. sechdr.sh_type:=s.shtype;
  1049. sechdr.sh_flags:=s.shflags;
  1050. sechdr.sh_offset:=s.datapos;
  1051. sechdr.sh_size:=s.Size;
  1052. sechdr.sh_link:=s.shlink;
  1053. sechdr.sh_info:=s.shinfo;
  1054. sechdr.sh_addralign:=s.secalign;
  1055. sechdr.sh_entsize:=s.shentsize;
  1056. MaybeSwapSecHeader(sechdr);
  1057. writer.write(sechdr,sizeof(sechdr));
  1058. end;
  1059. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  1060. begin
  1061. TElfObjSection(p).index:=pword(arg)^;
  1062. inc(pword(arg)^);
  1063. end;
  1064. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  1065. begin
  1066. if (TElfObjSection(p).ObjRelocations.count>0) then
  1067. createrelocsection(TElfObjSection(p),TObjData(arg));
  1068. end;
  1069. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  1070. begin
  1071. writesectionheader(TElfObjSection(p));
  1072. end;
  1073. function TElfObjectOutput.writedata(data:TObjData):boolean;
  1074. var
  1075. header : telfheader;
  1076. shoffset,
  1077. datapos : aword;
  1078. nsections : word;
  1079. begin
  1080. result:=false;
  1081. with data do
  1082. begin
  1083. { default sections }
  1084. symtabsect:=TElfSymtab.create(data,esk_obj);
  1085. shstrtabsect:=TElfObjSection.create_ext(data,'.shstrtab',SHT_STRTAB,0,1,0);
  1086. { "no executable stack" marker for Linux }
  1087. if (target_info.system in (systems_linux + systems_android)) and
  1088. not(cs_executable_stack in current_settings.moduleswitches) then
  1089. TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,1,0);
  1090. { symbol for filename }
  1091. symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
  1092. symtabsect.writeInternalSymbol(0,1,STT_FILE,SHN_ABS);
  1093. { calc amount of sections we have }
  1094. nsections:=1;
  1095. { also create the index in the section header table }
  1096. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1097. { create .symtab and .strtab }
  1098. createsymtab(data);
  1099. { Create the relocation sections, this needs valid secidx and symidx }
  1100. ObjSectionList.ForEachCall(@section_create_relocsec,data);
  1101. { recalc nsections to incude the reloc sections }
  1102. nsections:=1;
  1103. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1104. { create .shstrtab }
  1105. createshstrtab(data);
  1106. { Calculate the filepositions }
  1107. datapos:=$40; { elfheader + alignment }
  1108. { section data }
  1109. layoutsections(datapos);
  1110. { section headers }
  1111. shoffset:=datapos;
  1112. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  1113. { Write ELF Header }
  1114. fillchar(header,sizeof(header),0);
  1115. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  1116. header.e_ident[EI_MAG1]:=ELFMAG1;
  1117. header.e_ident[EI_MAG2]:=ELFMAG2;
  1118. header.e_ident[EI_MAG3]:=ELFMAG3;
  1119. header.e_ident[EI_CLASS]:=ELFCLASS;
  1120. if target_info.endian=endian_big then
  1121. header.e_ident[EI_DATA]:=ELFDATA2MSB
  1122. else
  1123. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  1124. header.e_ident[EI_VERSION]:=1;
  1125. if target_info.system in systems_openbsd then
  1126. header.e_ident[EI_OSABI]:=ELFOSABI_OPENBSD;
  1127. header.e_type:=ET_REL;
  1128. header.e_machine:=ElfTarget.machine_code;
  1129. header.e_version:=1;
  1130. header.e_shoff:=shoffset;
  1131. header.e_shstrndx:=shstrtabsect.index;
  1132. header.e_shnum:=nsections;
  1133. header.e_ehsize:=sizeof(telfheader);
  1134. header.e_shentsize:=sizeof(telfsechdr);
  1135. MaybeSwapHeader(header);
  1136. writer.write(header,sizeof(header));
  1137. writer.writezeros($40-sizeof(header)); { align }
  1138. { Sections }
  1139. WriteSectionContent(data);
  1140. { section headers, start with an empty header for sh_undef }
  1141. writer.writezeros(sizeof(telfsechdr));
  1142. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1143. end;
  1144. result:=true;
  1145. end;
  1146. {****************************************************************************
  1147. TELFAssembler
  1148. ****************************************************************************}
  1149. constructor TElfAssembler.Create(smart:boolean);
  1150. begin
  1151. inherited Create(smart);
  1152. CObjOutput:=TElfObjectOutput;
  1153. end;
  1154. {****************************************************************************
  1155. TELFObjectInput
  1156. ****************************************************************************}
  1157. constructor TElfObjInput.Create;
  1158. begin
  1159. inherited Create;
  1160. CObjData:=TElfObjData;
  1161. CObjSymbol:=TObjSymbol;
  1162. end;
  1163. destructor TElfObjInput.Destroy;
  1164. begin
  1165. if Assigned(FSymTbl) then
  1166. FreeMem(FSymTbl);
  1167. if Assigned(FSecTbl) then
  1168. FreeMem(FSecTbl);
  1169. if Assigned(strtab) then
  1170. FreeMem(strtab);
  1171. if Assigned(shstrtab) then
  1172. FreeMem(shstrtab);
  1173. if Assigned(symversions) then
  1174. FreeMem(symversions);
  1175. inherited Destroy;
  1176. end;
  1177. procedure TElfObjInput.LoadRelocations(const secrec:TSectionRec);
  1178. var
  1179. i: longint;
  1180. rel: TElfReloc;
  1181. reltyp: byte;
  1182. relsym: longint;
  1183. objrel: TObjRelocation;
  1184. p: TObjSymbol;
  1185. begin
  1186. FReader.Seek(secrec.relocpos);
  1187. if secrec.sec=nil then
  1188. InternalError(2012060203);
  1189. for i:=0 to secrec.relocs-1 do
  1190. begin
  1191. FReader.Read(rel,secrec.relentsize);
  1192. MaybeSwapElfReloc(rel);
  1193. reltyp:=rel.info and $FF;
  1194. {$ifdef cpu64bitaddr}
  1195. relsym:=rel.info shr 32;
  1196. {$else cpu64bitaddr}
  1197. relsym:=(rel.info shr 8) and $FFFFFF;
  1198. {$endif cpu64bitaddr}
  1199. if relsym>=syms then
  1200. InternalError(2012060204);
  1201. p:=TObjSymbol(FSymTbl[relsym]);
  1202. { Some relocations (e.g. R_ARM_V4BX) don't use a symbol at all }
  1203. if assigned(p) or (relsym=0) then
  1204. begin
  1205. objrel:=TObjRelocation.CreateRaw(rel.address-secrec.sec.mempos,p,reltyp);
  1206. if (secrec.relentsize=3*sizeof(pint)) then
  1207. objrel.orgsize:=rel.addend;
  1208. { perform target-specific actions }
  1209. if Assigned(ElfTarget.loadreloc) then
  1210. ElfTarget.loadreloc(objrel);
  1211. secrec.sec.ObjRelocations.add(objrel);
  1212. end
  1213. else
  1214. begin
  1215. InputError('Unable to resolve symbol of relocation');
  1216. exit;
  1217. end;
  1218. end;
  1219. end;
  1220. procedure TElfObjInput.LoadSymbols(objdata:TObjData;count,locals:longword);
  1221. var
  1222. i: longint;
  1223. sym: TElfSymbol;
  1224. bind: TAsmSymBind;
  1225. typ: TAsmSymType;
  1226. objsym: TObjSymbol;
  1227. ver: word;
  1228. begin
  1229. FSymTbl:=AllocMem(count*sizeof(Pointer));
  1230. for i:=1 to count-1 do
  1231. begin
  1232. FReader.Read(sym,sizeof(TElfSymbol));
  1233. MaybeSwapElfSymbol(sym);
  1234. if sym.st_name>=strtablen then
  1235. InternalError(2012060205);
  1236. if sym.st_shndx=SHN_ABS then { ignore absolute symbols (should we really do it???) }
  1237. Continue
  1238. else if sym.st_shndx=SHN_COMMON then
  1239. bind:=AB_COMMON
  1240. else if (sym.st_shndx>=nsects) then
  1241. InternalError(2012060206)
  1242. else
  1243. case (sym.st_info shr 4) of
  1244. STB_LOCAL:
  1245. bind:=AB_LOCAL;
  1246. STB_GLOBAL:
  1247. if sym.st_shndx=SHN_UNDEF then
  1248. bind:=AB_EXTERNAL
  1249. else
  1250. bind:=AB_GLOBAL;
  1251. STB_WEAK:
  1252. bind:=AB_WEAK_EXTERNAL;
  1253. else
  1254. InternalError(2012060207);
  1255. end;
  1256. { Ignore section symbol if we didn't create the corresponding objsection
  1257. (examples are SHT_GROUP or .note.GNU-stack sections). }
  1258. if (sym.st_shndx>0) and (sym.st_shndx<SHN_LORESERVE) and
  1259. (FSecTbl[sym.st_shndx].sec=nil) and
  1260. (not dynobj) then
  1261. if ((sym.st_info and $0F)=STT_SECTION) then
  1262. Continue
  1263. else
  1264. begin
  1265. writeln(objdata.name,' ',i);
  1266. InternalError(2012110701)
  1267. end;
  1268. case (sym.st_info and $0F) of
  1269. STT_NOTYPE:
  1270. typ:=AT_NONE;
  1271. STT_OBJECT:
  1272. typ:=AT_DATA;
  1273. STT_FUNC:
  1274. typ:=AT_FUNCTION;
  1275. STT_SECTION:
  1276. typ:=AT_SECTION;
  1277. STT_FILE:
  1278. continue;
  1279. STT_TLS:
  1280. typ:=AT_TLS;
  1281. STT_GNU_IFUNC:
  1282. typ:=AT_GNU_IFUNC;
  1283. else
  1284. writeln(objdata.name,' ',sym.st_info and $0F);
  1285. InternalError(2012060208);
  1286. end;
  1287. { If reading DSO, we're interested only in global symbols defined there.
  1288. Symbols with non-current version should also be ignored. }
  1289. ver:=0;
  1290. if dynobj then
  1291. begin
  1292. if assigned(symversions) then
  1293. begin
  1294. ver:=symversions[i];
  1295. if (ver=VER_NDX_LOCAL) or (ver>VERSYM_VERSION) then
  1296. continue;
  1297. end;
  1298. if (bind=AB_LOCAL) or (sym.st_shndx=SHN_UNDEF) then
  1299. continue;
  1300. if ver>=verdefs.count then
  1301. InternalError(2012120505);
  1302. end;
  1303. { validity of name and objsection has been checked above }
  1304. { !! all AT_SECTION symbols have duplicate (null) name,
  1305. therefore TObjSection.CreateSymbol cannot be used here }
  1306. objsym:=CObjSymbol.Create(objdata.ObjSymbolList,string(PChar(@strtab[sym.st_name])));
  1307. objsym.bind:=bind;
  1308. objsym.typ:=typ;
  1309. if bind<>AB_COMMON then
  1310. objsym.objsection:=FSecTbl[sym.st_shndx].sec;
  1311. objsym.offset:=sym.st_value;
  1312. objsym.size:=sym.st_size;
  1313. FSymTbl[i]:=objsym;
  1314. if (ver>VER_NDX_GLOBAL) then
  1315. TVersionedObjSymbol(objsym).version:=TElfVersionDef(verdefs[ver]);
  1316. end;
  1317. end;
  1318. function TElfObjInput.CreateSection(const shdr:TElfsechdr;index:longint;objdata:tobjdata;
  1319. out secname:string):TElfObjSection;
  1320. begin
  1321. secname:=string(PChar(@shstrtab[shdr.sh_name]));
  1322. result:=TElfObjSection.create_ext(objdata,secname,
  1323. shdr.sh_type,shdr.sh_flags,shdr.sh_addralign,shdr.sh_entsize);
  1324. result.index:=index;
  1325. result.DataPos:=shdr.sh_offset;
  1326. result.MemPos:=shdr.sh_addr;
  1327. result.Size:=shdr.sh_size;
  1328. FSecTbl[index].sec:=result;
  1329. end;
  1330. function TElfObjInput.ReadBytes(offs:longint;out buf;len:longint):boolean;
  1331. begin
  1332. FReader.Seek(offs);
  1333. result:=FReader.Read(buf,len);
  1334. end;
  1335. procedure TElfObjInput.LoadSection(const shdr:TElfsechdr;index:longint;objdata:tobjdata);
  1336. var
  1337. sec: TElfObjSection;
  1338. sym: TElfSymbol;
  1339. secname: string;
  1340. begin
  1341. if shdr.sh_name>=shstrtablen then
  1342. InternalError(2012060210);
  1343. case shdr.sh_type of
  1344. SHT_NULL:
  1345. {ignore};
  1346. { SHT_STRTAB may appear for .stabstr and other debug sections.
  1347. .shstrtab and .strtab are processed separately and don't appear here. }
  1348. SHT_PROGBITS,SHT_NOBITS,SHT_NOTE,SHT_STRTAB,
  1349. SHT_INIT_ARRAY,SHT_FINI_ARRAY,SHT_PREINIT_ARRAY:
  1350. begin
  1351. sec:=CreateSection(shdr,index,objdata,secname);
  1352. if (Length(secname)>3) and (secname[2] in ['d','f','n','s']) then
  1353. begin
  1354. if (Pos('.stub',secname)=1) or
  1355. (Pos('.fpc',secname)=1) then
  1356. sec.SecOptions:=[oso_keep]
  1357. { ELF does not have any flags specific to debug sections,
  1358. but reserves names starting with '.debug' for this purpose }
  1359. else if (Pos('.debug',secname)=1) or
  1360. (secname='.stab') or
  1361. (secname='.stabstr') then
  1362. sec.SecOptions:=[oso_debug]
  1363. else if (secname='.note.GNU-stack') and (shdr.sh_type=SHT_PROGBITS) then
  1364. begin
  1365. if (shdr.sh_flags and SHF_EXECINSTR)=0 then
  1366. objdata.ExecStack:=False;
  1367. end;
  1368. end;
  1369. if (shdr.sh_type=SHT_NOTE) and (shdr.sh_size<>0) then
  1370. sec.SecOptions:=[oso_keep];
  1371. end;
  1372. SHT_REL,SHT_RELA:
  1373. begin
  1374. if shdr.sh_info>=nsects then
  1375. InternalError(2012060211);
  1376. if shdr.sh_entsize<>longword((2+ord(shdr.sh_type=SHT_RELA))*sizeof(pint)) then
  1377. InternalError(2012060212);
  1378. with FSecTbl[shdr.sh_info] do
  1379. begin
  1380. relocpos:=shdr.sh_offset;
  1381. relocs:=shdr.sh_size div shdr.sh_entsize;
  1382. relentsize:=shdr.sh_entsize;
  1383. end;
  1384. end;
  1385. SHT_GROUP:
  1386. if (shdr.sh_size>=2*sizeof(longword)) and
  1387. (shdr.sh_entsize=sizeof(longword)) and
  1388. ((shdr.sh_size mod shdr.sh_entsize)=0) then
  1389. begin
  1390. { Groups are identified by name of symbol pointed to by
  1391. sh_link and sh_info, not by sh_name. This symbol
  1392. may as well be STT_SECTION symbol of this section,
  1393. in which case we end up using sh_name. }
  1394. if dynobj then
  1395. InternalError(2012110801);
  1396. if (shdr.sh_link<>symtabndx) then
  1397. InternalError(2012110703);
  1398. if (shdr.sh_info>=syms) then
  1399. InternalError(2012110704);
  1400. FReader.Seek(symtaboffset+shdr.sh_info*sizeof(TElfSymbol));
  1401. FReader.Read(sym,sizeof(TElfSymbol));
  1402. MaybeSwapElfSymbol(sym);
  1403. if sym.st_name>=strtablen then
  1404. InternalError(2012110705);
  1405. if (sym.st_shndx=index) and (sym.st_info=((STB_LOCAL shl 4) or STT_SECTION)) then
  1406. secname:=string(PChar(@shstrtab[shdr.sh_name]))
  1407. else
  1408. secname:=string(PChar(@strtab[sym.st_name]));
  1409. { Postpone further processing until all sections are loaded,
  1410. we'll need to access correct section header.
  1411. Since ABI requires SHT_GROUP sections to come first in the file,
  1412. we assume that group number x has header index x+1.
  1413. If we ever encounter files where this is not true, we'll have
  1414. to maintain a separate index. }
  1415. objdata.CreateSectionGroup(secname);
  1416. if (index<>objdata.GroupsList.Count) then
  1417. InternalError(2012110802);
  1418. end
  1419. else
  1420. InternalError(2012110706);
  1421. SHT_GNU_ATTRIBUTES:
  1422. { TODO: must not be ignored };
  1423. else
  1424. if not (assigned(ElfTarget.loadsection) and
  1425. ElfTarget.loadsection(self,objdata,shdr,index)) then
  1426. InternalError(2012072603);
  1427. end;
  1428. FLoaded[index]:=True;
  1429. end;
  1430. function TElfObjInput.LoadHeader(out objdata:TObjData):boolean;
  1431. var
  1432. header:TElfHeader;
  1433. begin
  1434. result:=false;
  1435. if not FReader.read(header,sizeof(header)) then
  1436. begin
  1437. InputError('Can''t read ELF header');
  1438. exit;
  1439. end;
  1440. if (header.e_ident[EI_MAG0]<>ELFMAG0) or (header.e_ident[EI_MAG1]<>ELFMAG1) or
  1441. (header.e_ident[EI_MAG2]<>ELFMAG2) or (header.e_ident[EI_MAG3]<>ELFMAG3) then
  1442. begin
  1443. InputError('Illegal ELF magic');
  1444. exit;
  1445. end;
  1446. if (header.e_ident[EI_VERSION]<>1) then
  1447. begin
  1448. InputError('Unknown ELF file version');
  1449. exit;
  1450. end;
  1451. if (header.e_ident[EI_CLASS]<>ELFCLASS) then
  1452. begin
  1453. InputError('Wrong ELF file class (32/64 bit mismatch)');
  1454. exit;
  1455. end;
  1456. if (header.e_ident[EI_DATA]<>1+ord(target_info.endian=endian_big)) then
  1457. begin
  1458. InputError('ELF endianness does not match target');
  1459. exit;
  1460. end;
  1461. MaybeSwapHeader(header);
  1462. if (header.e_version<>1) then
  1463. begin
  1464. InputError('Unknown ELF data version');
  1465. exit;
  1466. end;
  1467. if (header.e_machine<>ElfTarget.machine_code) then
  1468. begin
  1469. InputError('ELF file is for different CPU');
  1470. exit;
  1471. end;
  1472. if (header.e_type<>ET_REL) and (header.e_type<>ET_DYN) then
  1473. begin
  1474. InputError('Not a relocatable or dynamic ELF file');
  1475. exit;
  1476. end;
  1477. if header.e_shentsize<>sizeof(TElfsechdr) then
  1478. InternalError(2012062701);
  1479. nsects:=header.e_shnum;
  1480. dynobj:=(header.e_type=ET_DYN);
  1481. shoffset:=header.e_shoff;
  1482. shstrndx:=header.e_shstrndx;
  1483. if dynobj then
  1484. begin
  1485. objdata:=TElfDynamicObjData.Create(InputFilename);
  1486. verdefs:=TElfDynamicObjData(objdata).versiondefs;
  1487. CObjSymbol:=TVersionedObjSymbol;
  1488. end
  1489. else
  1490. objdata:=CObjData.Create(InputFilename);
  1491. TElfObjData(objdata).ident:=header.e_ident;
  1492. TElfObjData(objdata).flags:=header.e_flags;
  1493. result:=true;
  1494. end;
  1495. procedure TElfObjInput.LoadDynamic(const shdr:TElfsechdr;objdata:TObjData);
  1496. var
  1497. dt: TElfDyn;
  1498. i: longint;
  1499. begin
  1500. if (shdr.sh_entsize<>sizeof(TElfDyn)) then
  1501. InternalError(2012071403);
  1502. FReader.Seek(shdr.sh_offset);
  1503. for i:=0 to (shdr.sh_size div shdr.sh_entsize)-1 do
  1504. begin
  1505. FReader.Read(dt,sizeof(TElfDyn));
  1506. MaybeSwapElfDyn(dt);
  1507. case dt.d_tag of
  1508. DT_NULL:
  1509. break;
  1510. DT_SONAME:
  1511. TElfObjData(objdata).FName:=string(PChar(@strtab[dt.d_ptr]));
  1512. DT_NEEDED:
  1513. ;
  1514. end;
  1515. end;
  1516. end;
  1517. function TElfObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  1518. var
  1519. i,j,strndx,dynndx,
  1520. versymndx,verdefndx,verneedndx: longint;
  1521. objsec: TObjSection;
  1522. grp: TObjSectionGroup;
  1523. tmp: longword;
  1524. count: longint;
  1525. vd: TElfverdef;
  1526. vda: TElfverdaux;
  1527. vdoffset: aword;
  1528. begin
  1529. FReader:=AReader;
  1530. InputFileName:=AReader.FileName;
  1531. result:=false;
  1532. if not LoadHeader(objData) then
  1533. exit;
  1534. FSecTbl:=AllocMem(nsects*sizeof(TSectionRec));
  1535. FLoaded:=AllocMem(nsects*sizeof(boolean));
  1536. SetLength(shdrs,nsects);
  1537. FReader.Seek(shoffset);
  1538. if not FReader.Read(shdrs[0],nsects*sizeof(TElfsechdr)) then
  1539. begin
  1540. InputError('Can''t read ELF section headers');
  1541. exit;
  1542. end;
  1543. if source_info.endian<>target_info.endian then
  1544. for i:=0 to nsects-1 do
  1545. MaybeSwapSecHeader(shdrs[i]);
  1546. { First, load the .shstrtab section }
  1547. if shstrndx>=nsects then
  1548. InternalError(2012060201);
  1549. if shdrs[shstrndx].sh_type<>SHT_STRTAB then
  1550. InternalError(2012060202);
  1551. shstrtablen:=shdrs[shstrndx].sh_size;
  1552. GetMem(shstrtab,shstrtablen);
  1553. FReader.seek(shdrs[shstrndx].sh_offset);
  1554. FReader.read(shstrtab^,shstrtablen);
  1555. FLoaded[shstrndx]:=True;
  1556. { Locate the symtable, it is typically at the end so loop backwards.
  1557. Load the strings, postpone symtable itself until done with sections.
  1558. Note that is is legal to have no symtable.
  1559. For DSO, locate .dynsym instead, this one is near the beginning, but
  1560. overall number of sections won't be big. }
  1561. symtabndx:=0;
  1562. for i:=nsects-1 downto 1 do
  1563. begin
  1564. if (shdrs[i].sh_type<>symsectypes[dynobj]) then
  1565. continue;
  1566. if (shdrs[i].sh_entsize<>sizeof(TElfSymbol)) then
  1567. InternalError(2012060213);
  1568. if shdrs[i].sh_link>=nsects then
  1569. InternalError(2012062702);
  1570. strndx:=shdrs[i].sh_link;
  1571. if shdrs[strndx].sh_type<>SHT_STRTAB then
  1572. InternalError(2012062703);
  1573. strtablen:=shdrs[strndx].sh_size;
  1574. GetMem(strtab,strtablen);
  1575. FReader.seek(shdrs[strndx].sh_offset);
  1576. FReader.read(strtab^,strtablen);
  1577. symtaboffset:=shdrs[i].sh_offset;
  1578. syms:=shdrs[i].sh_size div sizeof(TElfSymbol);
  1579. localsyms:=shdrs[i].sh_info;
  1580. FLoaded[i]:=True;
  1581. FLoaded[strndx]:=True;
  1582. symtabndx:=i;
  1583. break;
  1584. end;
  1585. if dynobj then
  1586. begin
  1587. if symtabndx=0 then
  1588. InternalError(2012110707);
  1589. { Locate .dynamic and version sections. Expect a single one of a kind. }
  1590. dynndx:=0;
  1591. versymndx:=0;
  1592. verdefndx:=0;
  1593. verneedndx:=0;
  1594. for i:=nsects-1 downto 0 do
  1595. begin
  1596. case shdrs[i].sh_type of
  1597. SHT_DYNAMIC:
  1598. begin
  1599. if dynndx<>0 then
  1600. InternalError(2012102001);
  1601. dynndx:=i;
  1602. if (shdrs[dynndx].sh_link<>strndx) then
  1603. InternalError(2012071402);
  1604. LoadDynamic(shdrs[dynndx],objdata);
  1605. end;
  1606. SHT_GNU_versym:
  1607. begin
  1608. if versymndx<>0 then
  1609. InternalError(2012102002);
  1610. versymndx:=i;
  1611. if shdrs[i].sh_entsize<>sizeof(word) then
  1612. InternalError(2012102003);
  1613. if shdrs[i].sh_link<>symtabndx then
  1614. InternalError(2012102004);
  1615. if shdrs[i].sh_size<>syms*sizeof(word) then
  1616. InternalError(2012102005);
  1617. GetMem(symversions,shdrs[i].sh_size);
  1618. FReader.seek(shdrs[i].sh_offset);
  1619. FReader.read(symversions^,shdrs[i].sh_size);
  1620. if source_info.endian<>target_info.endian then
  1621. for j:=0 to syms-1 do
  1622. symversions[j]:=SwapEndian(symversions[j]);
  1623. end;
  1624. SHT_GNU_verdef:
  1625. begin
  1626. if verdefndx<>0 then
  1627. InternalError(2012102006);
  1628. verdefndx:=i;
  1629. if shdrs[i].sh_link<>strndx then
  1630. InternalError(2012120501);
  1631. vdoffset:=shdrs[i].sh_offset;
  1632. { TODO: can we rely on sh_info, or must read until vd_next=0? }
  1633. for j:=1 to shdrs[i].sh_info do
  1634. begin
  1635. FReader.seek(vdoffset);
  1636. FReader.Read(vd,sizeof(TElfverdef));
  1637. MaybeSwapElfverdef(vd);
  1638. if vd.vd_version<>VER_DEF_CURRENT then
  1639. InternalError(2012120502);
  1640. FReader.seek(vdoffset+vd.vd_aux);
  1641. vdoffset:=vdoffset+vd.vd_next;
  1642. { First verdaux entry holds name of version (unless VER_FLG_BASE flag is set),
  1643. subsequent one(s) point to parent(s). For our purposes, version hierarchy
  1644. looks irrelevant. }
  1645. FReader.Read(vda,sizeof(TElfverdaux));
  1646. MaybeSwapElfverdaux(vda);
  1647. if vda.vda_name>=strtablen then
  1648. InternalError(2012120503);
  1649. if (vd.vd_flags and VER_FLG_BASE)<>0 then
  1650. continue;
  1651. { Assuming verdef indices assigned continuously starting from 2,
  1652. at least BFD produces files that way. }
  1653. if verdefs.count<>vd.vd_ndx then
  1654. InternalError(2012120504);
  1655. TElfVersionDef.Create(verdefs,string(PChar(@strtab[vda.vda_name])));
  1656. end;
  1657. end;
  1658. SHT_GNU_verneed:
  1659. begin
  1660. if verneedndx<>0 then
  1661. InternalError(2012102007);
  1662. verneedndx:=i;
  1663. //sh_link->.dynstr
  1664. //sh_info->number of entries
  1665. end;
  1666. end;
  1667. end;
  1668. if dynndx=0 then
  1669. InternalError(2012071401);
  1670. { load the symtable }
  1671. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1672. LoadSymbols(objdata,syms,localsyms);
  1673. result:=True;
  1674. exit;
  1675. end;
  1676. { assume stack is executable until proven otherwise }
  1677. objdata.ExecStack:=True;
  1678. { Process section headers }
  1679. for i:=1 to nsects-1 do
  1680. if not FLoaded[i] then
  1681. LoadSection(shdrs[i],i,objdata);
  1682. { load the content }
  1683. ReadSectionContent(objdata);
  1684. { load the symtable }
  1685. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1686. LoadSymbols(objdata,syms,localsyms);
  1687. { finish relocations }
  1688. for i:=0 to objdata.ObjSectionList.Count-1 do
  1689. begin
  1690. objsec:=TObjSection(objdata.ObjsectionList[i]);
  1691. { skip debug sections }
  1692. if (oso_debug in objsec.SecOptions) and
  1693. (cs_link_strip in current_settings.globalswitches) and
  1694. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1695. continue;
  1696. if FSecTbl[objsec.index].relocpos>0 then
  1697. LoadRelocations(FSecTbl[objsec.index]);
  1698. end;
  1699. { finish processing section groups, if any }
  1700. if Assigned(objdata.GroupsList) then
  1701. begin
  1702. for i:=0 to objdata.GroupsList.Count-1 do
  1703. begin
  1704. grp:=TObjSectionGroup(objData.GroupsList[i]);
  1705. FReader.Seek(shdrs[i+1].sh_offset);
  1706. { first dword is flags }
  1707. FReader.Read(tmp,sizeof(longword));
  1708. if source_info.endian<>target_info.endian then
  1709. tmp:=SwapEndian(tmp);
  1710. if (tmp and GRP_COMDAT)<>0 then
  1711. grp.IsComdat:=true;
  1712. count:=(shdrs[i+1].sh_size div sizeof(longword))-1;
  1713. SetLength(grp.members,count);
  1714. for j:=0 to count-1 do
  1715. begin
  1716. FReader.Read(tmp,sizeof(longword));
  1717. if source_info.endian<>target_info.endian then
  1718. tmp:=SwapEndian(tmp);
  1719. if (tmp>=nsects) then
  1720. InternalError(2012110805);
  1721. objsec:=FSecTbl[tmp].sec;
  1722. if (objsec=nil) then
  1723. InternalError(2012110806);
  1724. if (TElfObjSection(objsec).shflags and SHF_GROUP)=0 then
  1725. InternalError(2012110807);
  1726. grp.members[j]:=objsec;
  1727. objsec.Group:=grp;
  1728. end;
  1729. end;
  1730. end;
  1731. result:=True;
  1732. end;
  1733. class function TElfObjInput.CanReadObjData(AReader:TObjectreader):boolean;
  1734. var
  1735. header: TElfHeader;
  1736. begin
  1737. result:=false;
  1738. if AReader.Read(header,sizeof(header)) then
  1739. begin;
  1740. if (header.e_ident[EI_MAG0]=ELFMAG0) and (header.e_ident[EI_MAG1]=ELFMAG1) and
  1741. (header.e_ident[EI_MAG2]=ELFMAG2) and (header.e_ident[EI_MAG3]=ELFMAG3) then
  1742. { TODO: check additional fields }
  1743. result:=true;
  1744. end;
  1745. AReader.Seek(0);
  1746. end;
  1747. {*****************************************************************************
  1748. TElfExeOutput
  1749. *****************************************************************************}
  1750. constructor TElfExeOutput.Create;
  1751. begin
  1752. inherited Create;
  1753. CObjData:=TElfObjData;
  1754. CExeSection:=TElfExeSection;
  1755. {$ifdef cpu64}
  1756. MaxMemPos:=Qword($FFFFFFFFFFFFFFFF);
  1757. //MaxMemPos:=$7EFFFFFF; { As specified by SysV AMD64 ABI for small memory model }
  1758. {$else cpu64}
  1759. MaxMemPos:=$7FFFFFFF;
  1760. {$endif cpu64}
  1761. SectionMemAlign:=$20;
  1762. SectionDataAlign:=$20;
  1763. segmentlist:=TFPObjectList.Create(True);
  1764. neededlist:=TFPHashList.Create;
  1765. end;
  1766. destructor TElfExeOutput.Destroy;
  1767. begin
  1768. dyncopysyms.Free;
  1769. neededlist.Free;
  1770. segmentlist.Free;
  1771. dynsymlist.Free;
  1772. dynreloclist.Free;
  1773. if assigned(dynsymnames) then
  1774. FreeMem(dynsymnames);
  1775. stringdispose(FInterpreter);
  1776. inherited Destroy;
  1777. end;
  1778. function TElfExeOutput.AttachSection(objsec:TObjSection):TElfExeSection;
  1779. begin
  1780. objsec.SecOptions:=[oso_keep];
  1781. result:=TElfExeSection(FindExeSection(objsec.name));
  1782. if result=nil then
  1783. result:=TElfExeSection.Create(ExeSectionList,objsec.name);
  1784. result.AddObjSection(objsec);
  1785. end;
  1786. function TElfExeOutput.CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  1787. begin
  1788. result:=TElfSegment.Create(atype,aflags,aalign);
  1789. segmentlist.add(result);
  1790. end;
  1791. procedure TElfExeOutput.WriteHeader;
  1792. var
  1793. header: TElfHeader;
  1794. begin
  1795. FillChar(header,sizeof(header),0);
  1796. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  1797. header.e_ident[EI_MAG1]:=ELFMAG1;
  1798. header.e_ident[EI_MAG2]:=ELFMAG2;
  1799. header.e_ident[EI_MAG3]:=ELFMAG3;
  1800. header.e_ident[EI_CLASS]:=ELFCLASS;
  1801. if target_info.endian=endian_big then
  1802. header.e_ident[EI_DATA]:=ELFDATA2MSB
  1803. else
  1804. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  1805. header.e_ident[EI_VERSION]:=1;
  1806. if IsSharedLibrary then
  1807. header.e_type:=ET_DYN
  1808. else
  1809. header.e_type:=ET_EXEC;
  1810. header.e_machine:=ElfTarget.machine_code;
  1811. header.e_version:=1;
  1812. header.e_phoff:=sizeof(TElfHeader);
  1813. header.e_shoff:=shoffset;
  1814. header.e_shstrndx:=ExeSectionList.IndexOf(shstrtabsect.ExeSection)+1;
  1815. header.e_shnum:=ExeSectionList.Count+1;
  1816. header.e_phnum:=segmentlist.count;
  1817. header.e_ehsize:=sizeof(telfheader);
  1818. if assigned(EntrySym) then
  1819. header.e_entry:=EntrySym.Address;
  1820. header.e_shentsize:=sizeof(telfsechdr);
  1821. header.e_phentsize:=sizeof(telfproghdr);
  1822. MaybeSwapHeader(header);
  1823. FWriter.Write(header,sizeof(header));
  1824. end;
  1825. procedure TElfExeOutput.exesection_write_header(p:TObject;arg:Pointer);
  1826. var
  1827. shdr: TElfsechdr;
  1828. exesec: TElfExeSection absolute p;
  1829. begin
  1830. FillChar(shdr,sizeof(shdr),0);
  1831. shdr.sh_name:=exesec.shstridx;
  1832. if (ExeWriteMode=ewm_dbgonly) and
  1833. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  1834. shdr.sh_type:=SHT_NOBITS
  1835. else
  1836. shdr.sh_type:=exesec.shtype;
  1837. shdr.sh_flags:=exesec.shflags;
  1838. if (oso_load in exesec.SecOptions) then
  1839. shdr.sh_addr:=exesec.MemPos;
  1840. shdr.sh_offset:=exesec.DataPos;
  1841. shdr.sh_size:=exesec.Size;
  1842. shdr.sh_link:=exesec.shlink;
  1843. shdr.sh_info:=exesec.shinfo;
  1844. shdr.sh_addralign:=exesec.SecAlign;
  1845. shdr.sh_entsize:=exesec.shentsize;
  1846. MaybeSwapSecHeader(shdr);
  1847. FWriter.Write(shdr,sizeof(shdr));
  1848. end;
  1849. procedure TElfExeOutput.segment_write_header(p:TObject;arg:Pointer);
  1850. var
  1851. phdr: TElfproghdr;
  1852. seg: TElfSegment absolute p;
  1853. begin
  1854. FillChar(phdr,sizeof(phdr),0);
  1855. phdr.p_type:=seg.ptype;
  1856. phdr.p_flags:=seg.pflags;
  1857. phdr.p_align:=seg.align;
  1858. phdr.p_offset:=seg.DataPos;
  1859. phdr.p_filesz:=seg.DataSize;
  1860. phdr.p_memsz:=seg.MemSize;
  1861. phdr.p_vaddr:=seg.MemPos;
  1862. phdr.p_paddr:=seg.MemPos;
  1863. MaybeSwapHeader(phdr);
  1864. FWriter.Write(phdr,sizeof(phdr));
  1865. end;
  1866. procedure TElfExeOutput.WriteStaticSymtable;
  1867. var
  1868. i: longint;
  1869. sec: TElfExeSection;
  1870. exesym: TExeSymbol;
  1871. begin
  1872. if assigned(tlsseg) then
  1873. symtab.tlsbase:=tlsseg.MemPos;
  1874. for i:=0 to ExeSectionList.Count-1 do
  1875. begin
  1876. sec:=TElfExeSection(ExeSectionList[i]);
  1877. { Must not write symbols for internal sections like .symtab }
  1878. if (sec.shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  1879. continue;
  1880. sec.secsymidx:=symtab.symidx;
  1881. symtab.writeInternalSymbol(sec.mempos,0,STT_SECTION,sec.secshidx);
  1882. end;
  1883. { local symbols first }
  1884. for i:=0 to ExeSymbolList.Count-1 do
  1885. begin
  1886. exesym:=TExeSymbol(ExeSymbolList[i]);
  1887. if (exesym.objsymbol.bind=AB_LOCAL) and (exesym.objsymbol.typ<>AT_LABEL) then
  1888. symtab.WriteSymbol(exesym.objsymbol);
  1889. end;
  1890. { Global Symbols }
  1891. for i:=0 to ExeSymbolList.Count-1 do
  1892. begin
  1893. exesym:=TExeSymbol(ExeSymbolList[i]);
  1894. if (exesym.objsymbol.bind<>AB_LOCAL) then
  1895. symtab.WriteSymbol(exesym.objsymbol);
  1896. end;
  1897. { update exe section properties }
  1898. symtab.ExeSection.size:=symtab.size;
  1899. TElfExeSection(symtab.ExeSection).shinfo:=symtab.shinfo;
  1900. TElfExeSection(symtab.ExeSection).shlink:=ExeSectionList.IndexOf(symtab.fstrsec.ExeSection)+1;
  1901. symtab.fstrsec.ExeSection.Size:=symtab.fstrsec.size;
  1902. end;
  1903. procedure TElfExeOutput.MapSectionsToSegments;
  1904. var
  1905. seg: TElfSegment;
  1906. exesec: TExeSection;
  1907. i: longint;
  1908. begin
  1909. if (not IsSharedLibrary) and assigned(interpobjsec) then
  1910. begin
  1911. phdrseg:=CreateSegment(PT_PHDR,PF_R or PF_X,sizeof(pint));
  1912. seg:=CreateSegment(PT_INTERP,PF_R,1);
  1913. seg.Add(interpobjsec.ExeSection);
  1914. end;
  1915. textseg:=CreateSegment(PT_LOAD,PF_X or PF_R,ElfTarget.max_page_size);
  1916. dataseg:=CreateSegment(PT_LOAD,PF_R or PF_W,ElfTarget.max_page_size);
  1917. for i:=0 to ExeSectionList.Count-1 do
  1918. begin
  1919. exesec:=TExeSection(ExeSectionList[i]);
  1920. if (oso_load in exesec.SecOptions) then
  1921. begin
  1922. if (TElfExeSection(exesec).shflags and SHF_TLS)<>0 then
  1923. begin
  1924. if tlsseg=nil then
  1925. tlsseg:=CreateSegment(PT_TLS,PF_R,sizeof(pint));
  1926. tlsseg.add(exesec);
  1927. end;
  1928. { TODO: at least on Linux, ld seems to drop .note.ABI-tag for static executables.
  1929. (Logic is as follows: there is no .note.ABI-tag section in ld script, so it
  1930. is processed as orphan section. As such, it is placed after .interp.
  1931. For static executables .interp is dropped, and it looks like there's nowhere to
  1932. place .note.ABI-tag in this case)
  1933. Always including it doesn't harm though (except increasing file size). }
  1934. if TElfExeSection(exesec).shtype=SHT_NOTE then
  1935. begin
  1936. if noteseg=nil then
  1937. noteseg:=CreateSegment(PT_NOTE,PF_R,4);
  1938. noteseg.Add(exesec);
  1939. Include(exesec.SecOptions,oso_debug_copy);
  1940. end;
  1941. if (oso_executable in exesec.SecOptions) or
  1942. not (oso_write in exesec.SecOptions) then
  1943. textseg.add(exesec)
  1944. else
  1945. dataseg.add(exesec);
  1946. end;
  1947. end;
  1948. if dynamiclink then
  1949. begin
  1950. seg:=CreateSegment(PT_DYNAMIC,PF_R or PF_W,sizeof(pint));
  1951. seg.add(dynamicsec.ExeSection);
  1952. end;
  1953. { stack flags }
  1954. CreateSegment(PT_GNU_STACK,PF_R or PF_W or (PF_X*ord(ExecStack)),sizeof(pint));
  1955. end;
  1956. procedure TElfExeOutput.make_dynamic_if_undefweak(exesym:TExeSymbol);
  1957. begin
  1958. if (exesym.dynindex=0) and (exesym.state=symstate_undefweak) and
  1959. not (cs_link_staticflag in current_settings.globalswitches) then
  1960. exesym.dynindex:=dynsymlist.add(exesym)+1;
  1961. end;
  1962. function TElfExeOutput.AllocGOTSlot(objsym:TObjSymbol):boolean;
  1963. var
  1964. exesym: TExeSymbol;
  1965. begin
  1966. result:=false;
  1967. exesym:=objsym.exesymbol;
  1968. { Although local symbols should not be accessed through GOT,
  1969. this isn't strictly forbidden. In this case we need to fake up
  1970. the exesym to store the GOT offset in it.
  1971. TODO: name collision; maybe use a different symbol list object? }
  1972. if exesym=nil then
  1973. begin
  1974. exesym:=TExeSymbol.Create(ExeSymbolList,objsym.name+'*local*');
  1975. exesym.objsymbol:=objsym;
  1976. objsym.exesymbol:=exesym;
  1977. end;
  1978. if exesym.GotOffset>0 then
  1979. exit;
  1980. gotobjsec.alloc(sizeof(pint));
  1981. exesym.GotOffset:=gotobjsec.size;
  1982. make_dynamic_if_undefweak(exesym);
  1983. { In shared library, every GOT entry needs a RELATIVE dynamic reloc,
  1984. imported/exported symbols need GLOB_DAT instead. For executables,
  1985. only the latter applies. }
  1986. if IsSharedLibrary or (exesym.dynindex>0) then
  1987. dynrelocsec.alloc(dynrelocsec.shentsize);
  1988. result:=true;
  1989. end;
  1990. procedure TElfExeOutput.PrepareGOT;
  1991. var
  1992. i,j,k: longint;
  1993. objsec: TElfObjSection;
  1994. exesec: TExeSection;
  1995. begin
  1996. for i:=0 to ExeSectionList.Count-1 do
  1997. begin
  1998. exesec:=TExeSection(ExeSectionList[i]);
  1999. for j:=0 to exesec.ObjSectionlist.count-1 do
  2000. begin
  2001. objsec:=TElfObjSection(exesec.ObjSectionlist[j]);
  2002. { ignore linker-generated and debug sections }
  2003. if (objsec.objdata=internalobjdata) or (oso_debug in objsec.SecOptions) then
  2004. continue;
  2005. if not objsec.Used then
  2006. internalerror(2012060901);
  2007. k:=0;
  2008. while k<objsec.ObjRelocations.Count do
  2009. begin
  2010. GOTRelocPass1(objsec,k);
  2011. inc(k);
  2012. end;
  2013. end;
  2014. end;
  2015. { remember sizes for sanity checking }
  2016. gotsize:=gotobjsec.size;
  2017. if assigned(dynrelocsec) then
  2018. dynrelsize:=dynrelocsec.size
  2019. else
  2020. dynrelsize:=0;
  2021. end;
  2022. procedure TElfExeOutput.CreateGOTSection;
  2023. begin
  2024. gotpltobjsec:=TElfObjSection.create_ext(internalObjData,'.got.plt',
  2025. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  2026. gotobjsec:=TElfObjSection.create_ext(internalObjData,'.got',
  2027. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  2028. gotobjsec.SecOptions:=[oso_keep];
  2029. { GOT symbol and reserved .got.plt entries }
  2030. internalObjData.SetSection(gotpltobjsec);
  2031. gotsymbol:=internalObjData.SymbolDefine('_GLOBAL_OFFSET_TABLE_',AB_GLOBAL,AT_DATA);
  2032. gotpltobjsec.writeZeros(3*sizeof(pint));
  2033. end;
  2034. procedure TElfExeOutput.Load_Start;
  2035. begin
  2036. inherited Load_Start;
  2037. dynsymlist:=TFPObjectList.Create(False);
  2038. CreateGOTSection;
  2039. end;
  2040. procedure TElfExeOutput.Load_DynamicObject(objdata:TObjData;asneeded:boolean);
  2041. var
  2042. i: longint;
  2043. exesym: TExeSymbol;
  2044. objsym: TObjSymbol;
  2045. needed: boolean;
  2046. begin
  2047. Comment(v_debug,'Dynamic object: '+objdata.name);
  2048. needed:=false;
  2049. for i:=0 to UnresolvedExeSymbols.Count-1 do
  2050. begin
  2051. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2052. if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
  2053. continue;
  2054. objsym:=TObjSymbol(objdata.ObjSymbolList.Find(exesym.name));
  2055. if assigned(objsym) then
  2056. begin
  2057. exesym.State:=symstate_defined;
  2058. exesym.dynindex:=dynsymlist.Add(exesym)+1;
  2059. { The original binding, value and section of external symbol
  2060. must be preserved, therefore resolving directly to .so symbol
  2061. hurts more than it helps. Copy type and size, and store .so
  2062. symbol in objsym.indsymbol for later use. }
  2063. exesym.ObjSymbol.typ:=objsym.typ;
  2064. if objsym.typ<>AT_FUNCTION then
  2065. exesym.ObjSymbol.size:=objsym.size;
  2066. exesym.ObjSymbol.indsymbol:=objsym;
  2067. objsym.ExeSymbol:=exesym;
  2068. needed:=true;
  2069. end;
  2070. end;
  2071. if (needed or (not asneeded)) and
  2072. (neededlist.Find(objdata.name)=nil) then
  2073. neededlist.Add(objdata.name,objdata);
  2074. end;
  2075. procedure TElfExeOutput.Order_Start;
  2076. begin
  2077. inherited Order_Start;
  2078. dynamiclink:=IsSharedLibrary or (dynsymlist.count>0) or
  2079. (
  2080. (UnresolvedExeSymbols.Count>0) and
  2081. not (cs_link_staticflag in current_settings.globalswitches)
  2082. );
  2083. if dynamiclink then
  2084. InitDynlink;
  2085. if dynamiclink or (IndirectObjSymbols.Count>0) then
  2086. CreatePLT;
  2087. end;
  2088. procedure TElfExeOutput.Order_end;
  2089. procedure set_oso_keep(const s:string);
  2090. var
  2091. exesec:TExeSection;
  2092. objsec:TObjSection;
  2093. i:longint;
  2094. begin
  2095. exesec:=TExeSection(ExeSectionList.Find(s));
  2096. if assigned(exesec) then
  2097. begin
  2098. for i:=0 to exesec.ObjSectionList.Count-1 do
  2099. begin
  2100. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2101. { ignore sections used for symbol definition }
  2102. if oso_data in objsec.SecOptions then
  2103. objsec.SecOptions:=[oso_keep];
  2104. end;
  2105. end;
  2106. end;
  2107. begin
  2108. OrderOrphanSections;
  2109. inherited Order_end;
  2110. set_oso_keep('.init');
  2111. set_oso_keep('.fini');
  2112. set_oso_keep('.jcr');
  2113. set_oso_keep('.ctors');
  2114. set_oso_keep('.dtors');
  2115. set_oso_keep('.preinit_array');
  2116. set_oso_keep('.init_array');
  2117. set_oso_keep('.fini_array');
  2118. set_oso_keep('.eh_frame');
  2119. { let .dynamic reference other dynamic sections so they aren't marked
  2120. for removal as unused }
  2121. if dynamiclink then
  2122. WriteDynamicTags;
  2123. end;
  2124. procedure TElfExeOutput.OrderOrphanSections;
  2125. var
  2126. i,j:longint;
  2127. objdata:TObjData;
  2128. objsec:TObjSection;
  2129. exesec:TExeSection;
  2130. opts:TObjSectionOptions;
  2131. s:string;
  2132. newsections,tmp:TFPHashObjectList;
  2133. allsections:TFPList;
  2134. inserts:array[0..6] of TExeSection;
  2135. idx,inspos:longint;
  2136. begin
  2137. newsections:=TFPHashObjectList.Create(false);
  2138. allsections:=TFPList.Create;
  2139. { copy existing sections }
  2140. for i:=0 to ExeSectionList.Count-1 do
  2141. allsections.add(ExeSectionList[i]);
  2142. inserts[0]:=FindExeSection('.comment');
  2143. inserts[1]:=nil;
  2144. inserts[2]:=FindExeSection('.interp');
  2145. inserts[3]:=FindExeSection('.bss');
  2146. inserts[4]:=FindExeSection('.data');
  2147. inserts[5]:=FindExeSection('.rodata');
  2148. inserts[6]:=FindExeSection('.text');
  2149. for i:=0 to ObjDataList.Count-1 do
  2150. begin
  2151. ObjData:=TObjData(ObjDataList[i]);
  2152. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2153. begin
  2154. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2155. if objsec.Used then
  2156. continue;
  2157. s:=objsec.name;
  2158. exesec:=TExeSection(newsections.Find(s));
  2159. if assigned(exesec) then
  2160. begin
  2161. exesec.AddObjSection(objsec);
  2162. continue;
  2163. end;
  2164. opts:=objsec.SecOptions*[oso_data,oso_load,oso_write,oso_executable];
  2165. if (objsec.SecOptions*[oso_load,oso_debug]=[]) then
  2166. { non-alloc, after .comment
  2167. GNU ld places .comment between stabs and dwarf debug info }
  2168. inspos:=0
  2169. else if not (oso_load in objsec.SecOptions) then
  2170. inspos:=1 { debugging, skip }
  2171. else if (oso_load in objsec.SecOptions) and
  2172. (TElfObjSection(objsec).shtype=SHT_NOTE) then
  2173. inspos:=2 { after .interp }
  2174. else if (opts=[oso_load,oso_write]) then
  2175. inspos:=3 { after .bss }
  2176. else if (opts=[oso_data,oso_load,oso_write]) then
  2177. inspos:=4 { after .data }
  2178. else if (opts=[oso_data,oso_load]) then
  2179. inspos:=5 { rodata, relocs=??? }
  2180. else if (opts=[oso_data,oso_load,oso_executable]) then
  2181. inspos:=6 { text }
  2182. else
  2183. begin
  2184. Comment(v_debug,'Orphan section '+objsec.fullname+' has attributes that are not handled!');
  2185. continue;
  2186. end;
  2187. if (inserts[inspos]=nil) then
  2188. begin
  2189. Comment(v_debug,'Orphan section '+objsec.fullname+': nowhere to insert, ignored');
  2190. continue;
  2191. end;
  2192. idx:=allsections.IndexOf(inserts[inspos]);
  2193. exesec:=CExeSection.Create(newsections,s);
  2194. allsections.Insert(idx+1,exesec);
  2195. inserts[inspos]:=exesec;
  2196. exesec.AddObjSection(objsec);
  2197. end;
  2198. end;
  2199. { Now replace the ExeSectionList with content of allsections }
  2200. if (newsections.count<>0) then
  2201. ReplaceExeSectionList(allsections);
  2202. newsections.Free;
  2203. allsections.Free;
  2204. end;
  2205. procedure TElfExeOutput.AfterUnusedSectionRemoval;
  2206. var
  2207. i:longint;
  2208. exesym:TExeSymbol;
  2209. objsym:TObjSymbol;
  2210. objsec: TObjSection;
  2211. begin
  2212. { Unused section removal sets Used property of referenced exesymbols.
  2213. Remaining ones can be removed. }
  2214. for i:=0 to dynsymlist.count-1 do
  2215. begin
  2216. exesym:=TExeSymbol(dynsymlist[i]);
  2217. if assigned(exesym.ObjSymbol.ObjSection) then // an exported symbol
  2218. continue;
  2219. if not exesym.used then
  2220. begin
  2221. dynsymlist[i]:=nil;
  2222. exesym.dynindex:=0;
  2223. end;
  2224. end;
  2225. dynsymlist.Pack;
  2226. { reindex }
  2227. for i:=0 to dynsymlist.count-1 do
  2228. TExeSymbol(dynsymlist[i]).dynindex:=i+1;
  2229. { Drop unresolved symbols that aren't referenced, assign dynamic
  2230. indices to remaining ones, but not if linking with -Xt.
  2231. TODO: behavior of .so with -Xt ? }
  2232. if (cs_link_staticflag in current_settings.globalswitches) then
  2233. UnresolvedExeSymbols.Clear
  2234. else
  2235. for i:=0 to UnresolvedExeSymbols.Count-1 do
  2236. begin
  2237. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2238. if exesym.used then
  2239. begin
  2240. if exesym.dynindex<>0 then
  2241. InternalError(2012062301);
  2242. { Weak-referenced symbols are changed into dynamic ones
  2243. only if referenced through GOT or PLT (this is BFD-compatible) }
  2244. if exesym.state<>symstate_undefweak then
  2245. exesym.dynindex:=dynsymlist.add(exesym)+1;
  2246. end
  2247. else
  2248. UnresolvedExeSymbols[i]:=nil;
  2249. end;
  2250. UnresolvedExeSymbols.Pack;
  2251. { Scan relocations to determine size of GOT, dynamic reloc section, etc. }
  2252. PrepareGOT;
  2253. { Write required PLT entries }
  2254. for i:=0 to dynsymlist.Count-1 do
  2255. begin
  2256. exesym:=TExeSymbol(dynsymlist[i]);
  2257. if assigned(exesym.ObjSymbol.objsection) then // an exported symbol
  2258. continue;
  2259. if ((exesym.ObjSymbol.refs and symref_plt)<>0) or
  2260. ((exesym.ObjSymbol.typ=AT_FUNCTION) and (not IsSharedLibrary)) then
  2261. begin
  2262. make_dynamic_if_undefweak(exesym);
  2263. { This symbol has a valid address to which relocations are resolved,
  2264. but it remains (weak)external when written to dynamic symtable. }
  2265. objsym:=internalobjdata.CreateSymbol(exesym.name);
  2266. objsym.typ:=AT_FUNCTION;
  2267. objsym.bind:=exesym.ObjSymbol.bind; { AB_EXTERNAL or AB_WEAK_EXTERNAL }
  2268. objsym.indsymbol:=exesym.ObjSymbol.indsymbol;
  2269. objsym.offset:=pltobjsec.size;
  2270. objsym.objsection:=pltobjsec;
  2271. objsym.exesymbol:=exesym;
  2272. exesym.ObjSymbol:=objsym;
  2273. WritePLTEntry(exesym);
  2274. end
  2275. else if ((exesym.ObjSymbol.refs and symref_from_text)<>0) and
  2276. (exesym.ObjSymbol.typ<>AT_FUNCTION) and (not IsSharedLibrary) and
  2277. (exesym.state<>symstate_undefweak) then
  2278. begin
  2279. if exesym.ObjSymbol.size=0 then
  2280. Comment(v_error,'Dynamic variable '+exesym.name+' has zero size');
  2281. internalobjdata.setSection(dynbssobjsec);
  2282. internalobjdata.allocalign(size_2_align(exesym.ObjSymbol.size));
  2283. objsym:=internalobjdata.SymbolDefine(exesym.name,AB_GLOBAL,AT_DATA);
  2284. objsym.size:=exesym.ObjSymbol.size;
  2285. objsym.indsymbol:=exesym.ObjSymbol.indsymbol;
  2286. exesym.ObjSymbol:=objsym;
  2287. objsym.exesymbol:=exesym;
  2288. dynbssobjsec.alloc(objsym.size);
  2289. { allocate space for R_xx_COPY relocation for this symbol;
  2290. we'll create it later, to be consistent with "-z combreloc" semantics }
  2291. dyncopysyms.add(objsym);
  2292. dynrelocsec.alloc(dynrelocsec.shentsize);
  2293. inc(dynrelsize,dynrelocsec.shentsize);
  2294. end;
  2295. end;
  2296. { Handle indirect symbols }
  2297. for i:=0 to IndirectObjSymbols.Count-1 do
  2298. begin
  2299. objsym:=TObjSymbol(IndirectObjSymbols[i]);
  2300. objsec:=objsym.ExeSymbol.ObjSymbol.objsection;
  2301. objsym.bind:=AB_EXTERNAL; { cheat FixupSymbols }
  2302. if (oso_plt in objsec.SecOptions) then
  2303. continue;
  2304. WriteIndirectPLTEntry(objsym.ExeSymbol);
  2305. end;
  2306. FixupSymbols;
  2307. if dynamiclink then
  2308. begin
  2309. WriteVersionSections;
  2310. WriteDynamicSymbolsHash;
  2311. end;
  2312. { Create .shstrtab section, which is needed in both exe and .dbg files }
  2313. shstrtabsect:=TElfObjSection.Create_ext(internalObjData,'.shstrtab',SHT_STRTAB,0,1,0);
  2314. shstrtabsect.SecOptions:=[oso_debug_copy];
  2315. AttachSection(shstrtabsect);
  2316. { Create the static symtable (.symtab and .strtab) }
  2317. if (cs_link_separate_dbg_file in current_settings.globalswitches) or
  2318. not(cs_link_strip in current_settings.globalswitches) then
  2319. begin
  2320. symtab:=TElfSymtab.Create(internalObjData,esk_exe);
  2321. symtab.SecOptions:=[oso_debug];
  2322. symtab.fstrsec.SecOptions:=[oso_debug];
  2323. AttachSection(symtab);
  2324. AttachSection(symtab.fstrsec);
  2325. end;
  2326. { Re-enable sections which end up to contain some data
  2327. (.got, .rel[a].dyn, .rel[a].plt (includes .rel[a].iplt) and .hash }
  2328. if gotobjsec.size<>0 then
  2329. gotobjsec.ExeSection.Disabled:=false;
  2330. if assigned(dynrelocsec) and
  2331. ((dynrelocsec.size<>0) or (dyncopysyms.count<>0)) then
  2332. dynrelocsec.ExeSection.Disabled:=false;
  2333. if assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2334. pltrelocsec.ExeSection.Disabled:=false;
  2335. if assigned(ipltrelocsec) and (ipltrelocsec.size>0) then
  2336. ipltrelocsec.ExeSection.Disabled:=false;
  2337. if assigned(hashobjsec) then
  2338. hashobjsec.ExeSection.Disabled:=false;
  2339. if assigned(symversec) and (symversec.size<>0) then
  2340. symversec.ExeSection.Disabled:=false;
  2341. if assigned(verneedsec) and (verneedsec.size<>0) then
  2342. verneedsec.ExeSection.Disabled:=false;
  2343. if assigned(verdefsec) and (verdefsec.size<>0) then
  2344. verdefsec.ExeSection.Disabled:=false;
  2345. RemoveDisabledSections;
  2346. MapSectionsToSegments;
  2347. if dynamiclink then
  2348. FinishDynamicTags;
  2349. end;
  2350. procedure TElfExeOutput.WriteShStrtab;
  2351. var
  2352. i: longint;
  2353. exesec: TElfExeSection;
  2354. begin
  2355. { Remove any existing .shstrtab contents }
  2356. if (shstrtabsect.size>0) then
  2357. begin
  2358. shstrtabsect.ReleaseData;
  2359. shstrtabsect.Size:=0;
  2360. shstrtabsect.SecOptions:=[oso_data];
  2361. end;
  2362. shstrtabsect.writezeros(1);
  2363. for i:=0 to ExeSectionList.Count-1 do
  2364. begin
  2365. exesec:=TElfExeSection(ExeSectionList[i]);
  2366. exesec.shstridx:=shstrtabsect.writestr(exesec.Name);
  2367. exesec.secshidx:=i+1;
  2368. end;
  2369. end;
  2370. procedure TElfExeOutput.FixupSectionLinks;
  2371. var
  2372. dynstrndx,dynsymndx: longword;
  2373. begin
  2374. if dynamiclink then
  2375. begin
  2376. dynstrndx:=TElfExeSection(dynsymtable.fstrsec.ExeSection).secshidx;
  2377. dynsymndx:=TElfExeSection(dynsymtable.ExeSection).secshidx;
  2378. TElfExeSection(hashobjsec.ExeSection).shlink:=dynsymndx;
  2379. TElfExeSection(dynamicsec.ExeSection).shlink:=dynstrndx;
  2380. TElfExeSection(dynsymtable.ExeSection).shlink:=dynstrndx;
  2381. if assigned(pltrelocsec) then
  2382. begin
  2383. TElfExeSection(pltrelocsec.ExeSection).shlink:=dynsymndx;
  2384. TElfExeSection(pltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2385. end;
  2386. if assigned(dynrelocsec) and assigned(dynrelocsec.ExeSection) then
  2387. TElfExeSection(dynrelocsec.ExeSection).shlink:=dynsymndx;
  2388. if symversec.size>0 then
  2389. TElfExeSection(symversec.ExeSection).shlink:=dynsymndx;
  2390. if verdefsec.size>0 then
  2391. TElfExeSection(verdefsec.ExeSection).shlink:=dynstrndx;
  2392. if verneedsec.size>0 then
  2393. TElfExeSection(verneedsec.ExeSection).shlink:=dynstrndx;
  2394. end
  2395. else if assigned(ipltrelocsec) then
  2396. TElfExeSection(ipltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2397. end;
  2398. procedure TElfExeOutput.Do_Mempos;
  2399. var
  2400. i,j: longint;
  2401. seg: TElfSegment;
  2402. exesec: TElfExeSection;
  2403. objsec: TObjSection;
  2404. tempmempos: qword;
  2405. begin
  2406. if IsSharedLibrary then
  2407. CurrMemPos:=0
  2408. else
  2409. CurrMemPos:=ElfTarget.exe_image_base;
  2410. textseg.MemPos:=CurrMemPos;
  2411. if assigned(phdrseg) then
  2412. begin
  2413. phdrseg.Mempos:=CurrMemPos+sizeof(TElfHeader);
  2414. phdrseg.Memsize:=sizeof(TElfproghdr)*segmentlist.count;
  2415. end;
  2416. CurrMemPos:=CurrMemPos+sizeof(TElfHeader)+segmentlist.count*sizeof(TElfproghdr);
  2417. MemPos_Segment(textseg);
  2418. CurrMemPos:=Align(CurrMemPos,SectionDataAlign); {! Data,not MemAlign}
  2419. CurrMemPos:=CurrMemPos+ElfTarget.max_page_size;
  2420. dataseg.MemPos:=CurrMemPos;
  2421. MemPos_Segment(dataseg);
  2422. { Mempos of unmapped sections is forced to zero, but we have to set positions
  2423. of its objsections and update sizes }
  2424. for i:=0 to ExeSectionList.Count-1 do
  2425. begin
  2426. exesec:=TElfExeSection(ExeSectionList[i]);
  2427. if not (oso_load in exesec.SecOptions) then
  2428. begin
  2429. tempmempos:=0;
  2430. exesec.MemPos:=tempmempos;
  2431. for j:=0 to exesec.ObjSectionList.Count-1 do
  2432. begin
  2433. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2434. tempmempos:=objsec.setmempos(tempmempos);
  2435. end;
  2436. exesec.Size:=tempmempos;
  2437. end;
  2438. end;
  2439. { Update MemPos and MemSize of non-load segments,
  2440. in particular, TLS sizes are needed to resolve relocations }
  2441. for i:=0 to segmentlist.count-1 do
  2442. begin
  2443. seg:=TElfSegment(segmentlist[i]);
  2444. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2445. continue;
  2446. seg.MemPos:=TExeSection(seg.FSectionList.First).MemPos;
  2447. for j:=0 to seg.FSectionList.Count-1 do
  2448. begin
  2449. exesec:=TElfExeSection(seg.FSectionList[j]);
  2450. seg.MemSize:=exesec.MemPos+exesec.Size-seg.MemPos;
  2451. end;
  2452. end;
  2453. end;
  2454. procedure TElfExeOutput.MemPos_Start;
  2455. var
  2456. i: longint;
  2457. begin
  2458. { Assign section indices and fill .shstrtab
  2459. List of sections cannot be modified after this point. }
  2460. WriteShStrtab;
  2461. { fixup sh_link/sh_info members of various dynamic sections }
  2462. FixupSectionLinks;
  2463. { The actual layout }
  2464. Do_Mempos;
  2465. if (not gotwritten) then
  2466. begin
  2467. { Reset size of .got and .rel[a].dyn, they will be refilled while fixing up relocations.
  2468. For .got, consider already written reserved entries. }
  2469. if assigned(gotobjsec) then
  2470. gotobjsec.size:=gotobjsec.data.size;
  2471. if assigned(dynrelocsec) then
  2472. begin
  2473. dynrelocsec.size:=0;
  2474. { write actual .dynsym content (needs valid symbol addresses) }
  2475. if assigned(tlsseg) then
  2476. dynsymtable.tlsbase:=tlsseg.MemPos;
  2477. dynsymtable.size:=sizeof(TElfsymbol);
  2478. for i:=0 to dynsymlist.count-1 do
  2479. dynsymtable.writeSymbol(TExeSymbol(dynsymlist[i]).objsymbol,dynsymnames[i]);
  2480. end;
  2481. end;
  2482. end;
  2483. procedure TElfExeOutput.MemPos_Segment(seg:TElfSegment);
  2484. var
  2485. i: longint;
  2486. exesec: TElfExeSection;
  2487. begin
  2488. for i:=0 to seg.FSectionList.Count-1 do
  2489. begin
  2490. exesec:=TElfExeSection(seg.FSectionList[i]);
  2491. inherited MemPos_ExeSection(exesec);
  2492. { .tbss should not contribute to address space }
  2493. if (exesec.shtype=SHT_NOBITS) and ((exesec.shflags and SHF_TLS)<>0) then
  2494. CurrMemPos:=exesec.MemPos;
  2495. end;
  2496. { calculate size of the segment }
  2497. seg.MemSize:=CurrMemPos-seg.MemPos;
  2498. end;
  2499. procedure TElfExeOutput.MemPos_ExeSection(const aname:string);
  2500. begin
  2501. // Ignore. All layout is done in mempos_start
  2502. end;
  2503. procedure TElfExeOutput.DataPos_Start;
  2504. var
  2505. i,j: longint;
  2506. exesec: TExeSection;
  2507. seg: TElfSegment;
  2508. objreloc: TObjRelocation;
  2509. objsym: TObjSymbol;
  2510. begin
  2511. gotwritten:=true;
  2512. { If target does not support sorted relocations, it is expected to write the
  2513. entire .rel[a].dyn section during FixupRelocations, and leave dynreloclist empty.
  2514. Otherwise, only RELATIVE ones should be written, space for non-relative relocations
  2515. should remain. }
  2516. if assigned(dynrelocsec) then
  2517. begin
  2518. { Append R_xx_COPY relocations }
  2519. for i:=0 to dyncopysyms.count-1 do
  2520. begin
  2521. objsym:=TObjSymbol(dyncopysyms[i]);
  2522. dynreloclist.Add(TObjRelocation.CreateRaw(objsym.address,objsym,ElfTarget.dyn_reloc_codes[dr_copy]));
  2523. end;
  2524. dyncopysyms.Clear;
  2525. if (dynrelocsec.size+(dynreloclist.count*dynrelocsec.shentsize)<>dynrelsize) then
  2526. InternalError(2012110601);
  2527. { Write out non-RELATIVE dynamic relocations
  2528. TODO: additional sorting? }
  2529. for i:=0 to dynreloclist.count-1 do
  2530. begin
  2531. objreloc:=TObjRelocation(dynreloclist[i]);
  2532. WriteDynRelocEntry(objreloc.dataoffset,objreloc.ftype,objreloc.symbol.exesymbol.dynindex,0);
  2533. end;
  2534. end;
  2535. { sanity checks }
  2536. if assigned(gotobjsec) and (gotsize<>gotobjsec.size) then
  2537. InternalError(2012092501);
  2538. if assigned(dynrelocsec) and (dynrelsize<>dynrelocsec.size) then
  2539. InternalError(2012092502);
  2540. if (ExeWriteMode=ewm_dbgonly) or
  2541. (
  2542. (ExeWriteMode=ewm_exefull) and
  2543. not(cs_link_strip in current_settings.globalswitches)
  2544. ) then
  2545. WriteStaticSymtable;
  2546. { first handle primary segments }
  2547. textseg.DataPos:=0;
  2548. CurrDataPos:=sizeof(TElfHeader)+sizeof(TElfproghdr)*segmentlist.count;
  2549. if assigned(phdrseg) then
  2550. begin
  2551. phdrseg.DataPos:=sizeof(TElfHeader);
  2552. phdrseg.DataSize:=sizeof(TElfproghdr)*segmentlist.count;
  2553. end;
  2554. DataPos_Segment(textseg);
  2555. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2556. dataseg.DataPos:=CurrDataPos;
  2557. DataPos_Segment(dataseg);
  2558. { then unmapped sections }
  2559. for i:=0 to ExeSectionList.Count-1 do
  2560. begin
  2561. exesec:=TExeSection(ExeSectionList[i]);
  2562. if not (oso_load in exesec.SecOptions) then
  2563. inherited DataPos_ExeSection(exesec);
  2564. end;
  2565. { finally, update size/position of non-load segments }
  2566. for i:=0 to segmentlist.count-1 do
  2567. begin
  2568. seg:=TElfSegment(segmentlist[i]);
  2569. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2570. continue;
  2571. seg.DataPos:=TExeSection(seg.FSectionList.First).DataPos;
  2572. for j:=0 to seg.FSectionList.Count-1 do
  2573. begin
  2574. exesec:=TExeSection(seg.FSectionList[j]);
  2575. if oso_data in exesec.SecOptions then
  2576. seg.DataSize:=exesec.DataPos+exesec.Size-seg.DataPos;
  2577. end;
  2578. end;
  2579. { place section headers after the data }
  2580. shoffset:=CurrDataPos;
  2581. CurrDataPos:=CurrDataPos+ExeSectionList.Count*sizeof(TElfsechdr);
  2582. end;
  2583. procedure TElfExeOutput.DataPos_Segment(seg:TElfSegment);
  2584. var
  2585. i: longint;
  2586. exesec: TElfExeSection;
  2587. begin
  2588. for i:=0 to seg.FSectionList.Count-1 do
  2589. begin
  2590. exesec:=TElfExeSection(seg.FSectionList[i]);
  2591. { ELF needs DataPos set to 'would-be' value for sections that
  2592. don't have data, and for non-debug sections in .dbg file, too.
  2593. This slightly differs from generic approach. }
  2594. if not (oso_data in exesec.SecOptions) or
  2595. (
  2596. (ExeWriteMode=ewm_dbgonly) and
  2597. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[])
  2598. ) then
  2599. begin
  2600. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2601. exesec.DataPos:=CurrDataPos;
  2602. end
  2603. else
  2604. inherited DataPos_ExeSection(exesec);
  2605. end;
  2606. { calculate size of the segment }
  2607. seg.DataSize:=CurrDataPos-seg.DataPos;
  2608. end;
  2609. procedure TElfExeOutput.DataPos_ExeSection(const aname:string);
  2610. begin
  2611. // Ignore. Work is done entirely in datapos_start.
  2612. end;
  2613. procedure TElfExeOutput.InitDynlink;
  2614. begin
  2615. if not IsSharedLibrary then
  2616. begin
  2617. interpobjsec:=internalObjData.createsection('.interp',1,[oso_data,oso_load,oso_keep]);
  2618. interpobjsec.writestr(interpreter^);
  2619. end;
  2620. hashobjsec:=TElfObjSection.create_ext(internalObjData,'.hash',
  2621. SHT_HASH,SHF_ALLOC,sizeof(pint),4);
  2622. hashobjsec.secoptions:=[oso_keep];
  2623. dynsymtable:=TElfSymtab.create(internalObjData,esk_dyn);
  2624. dynamicsec:=TElfObjSection.create_ext(internalObjData,'.dynamic',
  2625. SHT_DYNAMIC,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(TElfDyn));
  2626. dynamicsec.SecOptions:=[oso_keep];
  2627. dynrelocsec:=TElfObjSection.create_reloc(internalObjData,'.dyn',true);
  2628. dynrelocsec.SecOptions:=[oso_keep];
  2629. dynbssobjsec:=TElfObjSection.create_ext(internalObjData,'.dynbss',
  2630. SHT_NOBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint){16??},0);
  2631. dynbssobjsec.SecOptions:=[oso_keep];
  2632. dynreloclist:=TFPObjectList.Create(true);
  2633. symversec:=TElfObjSection.create_ext(internalObjData,'.gnu.version',
  2634. SHT_GNU_VERSYM,SHF_ALLOC,sizeof(word),sizeof(word));
  2635. symversec.SecOptions:=[oso_keep];
  2636. verdefsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_d',
  2637. SHT_GNU_VERDEF,SHF_ALLOC,sizeof(pint),0);
  2638. verdefsec.SecOptions:=[oso_keep];
  2639. verneedsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_r',
  2640. SHT_GNU_VERNEED,SHF_ALLOC,sizeof(pint),0);
  2641. verneedsec.SecOptions:=[oso_keep];
  2642. dyncopysyms:=TFPObjectList.Create(False);
  2643. end;
  2644. const
  2645. hashbuckets: array[0..15] of longint=(
  2646. 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  2647. 16411, 32771);
  2648. {$push}{$r-,q-}
  2649. function elfhash(const name:string):longword;
  2650. var
  2651. g: longword;
  2652. i: longint;
  2653. begin
  2654. result:=0;
  2655. for i:=1 to length(name) do
  2656. begin
  2657. result:=(result shl 4)+ord(name[i]);
  2658. g:=result and $F0000000;
  2659. if g>0 then
  2660. result:=result xor (g shr 24);
  2661. result:=result and (not g);
  2662. end;
  2663. end;
  2664. {$pop}
  2665. procedure TElfExeOutput.WriteDynamicSymbolsHash;
  2666. var
  2667. nchains,nbuckets: longint;
  2668. i,j: longint;
  2669. hashdata: plongint;
  2670. sym: TExeSymbol;
  2671. begin
  2672. dynsymnames:=AllocMem(dynsymlist.count*sizeof(longword));
  2673. nchains:=dynsymlist.Count+1;
  2674. { determine suitable bucket count }
  2675. i:=high(hashbuckets);
  2676. while (i>=0) and (nchains<hashbuckets[i]) do
  2677. dec(i);
  2678. nbuckets:=hashbuckets[i];
  2679. hashdata:=AllocMem((2+nchains+nbuckets)*sizeof(longint));
  2680. hashdata[0]:=nbuckets;
  2681. hashdata[1]:=nchains;
  2682. { The contents of .dynsym can be written only after mempos pass
  2683. because it needs valid symbol virtual addresses and section indices.
  2684. Here we preset .dynsym size and write names, in order to get
  2685. correct size of .dynstr section. }
  2686. dynsymtable.size:=(dynsymlist.count+1)*sizeof(TElfsymbol);
  2687. for i:=0 to dynsymlist.Count-1 do
  2688. begin
  2689. sym:=TExeSymbol(dynsymlist[i]);
  2690. dynsymnames[i]:=dynsymtable.fstrsec.writestr(sym.objsymbol.name);
  2691. j:=(elfhash(sym.objsymbol.name) mod nbuckets)+2;
  2692. while hashdata[j]<>0 do
  2693. j:=2+nbuckets+hashdata[j];
  2694. hashdata[j]:=i+1;
  2695. end;
  2696. if source_info.endian<>target_info.endian then
  2697. for i:=0 to nchains+nbuckets+1 do
  2698. hashdata[i]:=swapendian(hashdata[i]);
  2699. hashobjsec.write(hashdata^,(2+nchains+nbuckets)*sizeof(longint));
  2700. freemem(hashdata);
  2701. end;
  2702. procedure TElfExeOutput.WriteVersionSections;
  2703. var
  2704. i,j: longint;
  2705. idx,auxidx: longword;
  2706. exesym: TExeSymbol;
  2707. dynobj: TElfDynamicObjData;
  2708. ver: TElfVersionDef;
  2709. vn: TElfverneed;
  2710. vna: TElfvernaux;
  2711. symversions: pword;
  2712. begin
  2713. symversions:=AllocMem((dynsymlist.count+1)*sizeof(word));
  2714. { Assign version indices }
  2715. idx:=VER_NDX_GLOBAL+1;
  2716. for i:=0 to dynsymlist.count-1 do
  2717. begin
  2718. exesym:=TExeSymbol(dynsymlist[i]);
  2719. if (exesym.objsymbol.indsymbol is TVersionedObjSymbol) then
  2720. ver:=TVersionedObjSymbol(exesym.objsymbol.indsymbol).version
  2721. else
  2722. ver:=nil;
  2723. if assigned(ver) then
  2724. begin
  2725. if ver.index=0 then
  2726. begin
  2727. ver.index:=idx;
  2728. inc(idx);
  2729. end;
  2730. symversions[i+1]:=ver.index;
  2731. end
  2732. else if exesym.state in [symstate_undefined,symstate_undefweak] then
  2733. symversions[i+1]:=VER_NDX_LOCAL
  2734. else
  2735. symversions[i+1]:=VER_NDX_GLOBAL;
  2736. end;
  2737. { Count entries to be written }
  2738. verneedcount:=0;
  2739. for i:=0 to neededlist.count-1 do
  2740. begin
  2741. dynobj:=TElfDynamicObjData(neededlist[i]);
  2742. dynobj.vernaux_count:=0;
  2743. for j:=2 to dynobj.versiondefs.count-1 do
  2744. begin
  2745. ver:=TElfVersionDef(dynobj.versiondefs[j]);
  2746. if ver.index>VER_NDX_GLOBAL then
  2747. inc(dynobj.vernaux_count);
  2748. end;
  2749. if (dynobj.vernaux_count>0) then
  2750. inc(verneedcount);
  2751. end;
  2752. { Now write }
  2753. idx:=0;
  2754. for i:=0 to neededlist.count-1 do
  2755. begin
  2756. dynobj:=TElfDynamicObjData(neededlist[i]);
  2757. if dynobj.vernaux_count=0 then
  2758. continue;
  2759. inc(idx);
  2760. vn.vn_version:=VER_NEED_CURRENT;
  2761. vn.vn_cnt:=dynobj.vernaux_count;
  2762. vn.vn_file:=dynobj.soname_strofs;
  2763. vn.vn_aux:=sizeof(TElfverneed);
  2764. vn.vn_next:=ord(idx<verneedcount)*(sizeof(TElfverneed)+vn.vn_cnt*sizeof(TElfvernaux));
  2765. MaybeSwapElfverneed(vn);
  2766. verneedsec.write(vn,sizeof(TElfverneed));
  2767. auxidx:=0;
  2768. for j:=2 to dynobj.versiondefs.count-1 do
  2769. begin
  2770. ver:=TElfVersionDef(dynobj.versiondefs[j]);
  2771. if ver.index<=VER_NDX_GLOBAL then
  2772. continue;
  2773. inc(auxidx);
  2774. vna.vna_hash:=elfhash(ver.name);
  2775. vna.vna_flags:=0; { BFD copies this from verdef.vd_flags?? }
  2776. vna.vna_other:=ver.index;
  2777. vna.vna_name:=dynsymtable.fstrsec.writestr(ver.name);
  2778. vna.vna_next:=ord(auxidx<dynobj.vernaux_count)*sizeof(TElfvernaux);
  2779. MaybeSwapElfvernaux(vna);
  2780. verneedsec.write(vna,sizeof(TElfvernaux));
  2781. end;
  2782. end;
  2783. TElfExeSection(verneedsec.ExeSection).shinfo:=verneedcount;
  2784. { If there are no needed versions, .gnu.version section is not needed }
  2785. if verneedcount>0 then
  2786. begin
  2787. if source_info.endian<>target_info.endian then
  2788. for i:=0 to dynsymlist.count+1 do
  2789. symversions[i]:=swapendian(symversions[i]);
  2790. symversec.write(symversions^,(dynsymlist.count+1)*sizeof(word));
  2791. end;
  2792. FreeMem(symversions);
  2793. end;
  2794. procedure TElfExeOutput.WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  2795. var
  2796. rel:telfreloc;
  2797. begin
  2798. rel.address:=dataofs;
  2799. rel.info:=ELF_R_INFO(symidx,typ);
  2800. {$push}{$r-}
  2801. rel.addend:=addend;
  2802. {$pop}
  2803. MaybeSwapElfReloc(rel);
  2804. dynrelocsec.write(rel,dynrelocsec.shentsize);
  2805. end;
  2806. procedure TElfExeOutput.WriteDynTag(aTag:longword;aValue:longword);
  2807. var
  2808. d: TElfDyn;
  2809. begin
  2810. d.d_tag:=aTag;
  2811. d.d_val:=aValue;
  2812. MaybeSwapElfDyn(d);
  2813. dynamicsec.write(d,sizeof(TElfDyn));
  2814. end;
  2815. procedure TElfExeOutput.WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword);
  2816. var
  2817. d: TElfDyn;
  2818. begin
  2819. d.d_tag:=aTag;
  2820. if source_info.endian<>target_info.endian then
  2821. d.d_tag:=swapendian(d.d_tag);
  2822. dynamicsec.write(d.d_tag,sizeof(d.d_tag));
  2823. { TODO: ignores endianness! }
  2824. dynamicsec.writeReloc_internal(aSection,aOffs,sizeof(d.d_ptr),RELOC_ABSOLUTE);
  2825. end;
  2826. procedure TElfExeOutput.WriteTargetDynamicTags;
  2827. begin
  2828. { to be overridden by CPU-specific descendants }
  2829. end;
  2830. procedure TElfExeOutput.WriteDynamicTags;
  2831. var
  2832. s: aword;
  2833. i: longint;
  2834. sym: TExeSymbol;
  2835. hs:string;
  2836. dynobj: TElfDynamicObjData;
  2837. begin
  2838. for i:=0 to neededlist.Count-1 do
  2839. begin
  2840. dynobj:=TElfDynamicObjData(neededlist[i]);
  2841. s:=dynsymtable.fstrsec.writestr(dynobj.name);
  2842. dynobj.soname_strofs:=s;
  2843. WriteDynTag(DT_NEEDED,s);
  2844. end;
  2845. if IsSharedLibrary then
  2846. begin
  2847. s:=dynsymtable.fstrsec.writestr(ExtractFileName(current_module.sharedlibfilename));
  2848. WriteDynTag(DT_SONAME,s);
  2849. { TODO: names hardcoded here }
  2850. sym:=TExeSymbol(ExeSymbolList.Find('FPC_SHARED_LIB_START'));
  2851. if assigned(sym) then
  2852. WriteDynTag(DT_INIT,sym.objsymbol.objsection,sym.objsymbol.offset);
  2853. sym:=TExeSymbol(ExeSymbolList.Find('FPC_LIB_EXIT'));
  2854. if assigned(sym) then
  2855. WriteDynTag(DT_FINI,sym.objsymbol.objsection,sym.objsymbol.offset);
  2856. end;
  2857. { TODO: we need a dedicated parameter to pass runpath, instead of this hack
  2858. (-Xr is a different thing, it passes "-rpath-link"). }
  2859. if (ParaLinkOptions<>'') then
  2860. begin
  2861. hs:=ParaLinkOptions;
  2862. while (hs<>'') do
  2863. begin
  2864. if (GetToken(hs,' ')='-rpath') then
  2865. begin
  2866. s:=dynsymtable.fstrsec.writestr(GetToken(hs,' '));
  2867. WriteDynTag(DT_RPATH,s);
  2868. end;
  2869. end;
  2870. end;
  2871. writeDynTag(DT_HASH,hashobjsec);
  2872. writeDynTag(DT_STRTAB,dynsymtable.fstrsec);
  2873. writeDynTag(DT_SYMTAB,dynsymtable);
  2874. writeDynTag(DT_SYMENT,sizeof(TElfSymbol));
  2875. if Assigned(gotpltobjsec) then
  2876. writeDynTag(DT_PLTGOT,gotpltobjsec);
  2877. end;
  2878. const
  2879. pltreltags: array[boolean] of longword=(DT_REL,DT_RELA);
  2880. relsztags: array[boolean] of longword=(DT_RELSZ,DT_RELASZ);
  2881. relenttags: array[boolean] of longword=(DT_RELENT,DT_RELAENT);
  2882. relcnttags: array[boolean] of longword=(DT_RELCOUNT,DT_RELACOUNT);
  2883. procedure TElfExeOutput.FinishDynamicTags;
  2884. var
  2885. rela: boolean;
  2886. begin
  2887. if assigned(dynsymtable) then
  2888. writeDynTag(DT_STRSZ,dynsymtable.fstrsec.size);
  2889. if hastextrelocs then
  2890. writeDynTag(DT_TEXTREL,0);
  2891. if Assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2892. begin
  2893. writeDynTag(DT_PLTRELSZ,pltrelocsec.Size);
  2894. writeDynTag(DT_PLTREL,pltreltags[pltrelocsec.shtype=SHT_RELA]);
  2895. writeDynTag(DT_JMPREL,pltrelocsec);
  2896. end;
  2897. if Assigned(dynrelocsec) and (dynrelocsec.size>0) then
  2898. begin
  2899. rela:=(dynrelocsec.shtype=SHT_RELA);
  2900. writeDynTag(pltreltags[rela],dynrelocsec);
  2901. writeDynTag(relsztags[rela],dynrelocsec.Size);
  2902. writeDynTag(relenttags[rela],dynrelocsec.shentsize);
  2903. {$ifndef MIPS}
  2904. if (relative_reloc_count>0) then
  2905. writeDynTag(relcnttags[rela],relative_reloc_count);
  2906. {$endif MIPS}
  2907. end;
  2908. WriteTargetDynamicTags;
  2909. if (verdefcount>0) or (verneedcount>0) then
  2910. begin
  2911. if (verdefcount>0) then
  2912. begin
  2913. writeDynTag(DT_VERDEF,verdefsec);
  2914. writeDynTag(DT_VERDEFNUM,verdefcount);
  2915. end;
  2916. if (verneedcount>0) then
  2917. begin
  2918. writeDynTag(DT_VERNEED,verneedsec);
  2919. writeDynTag(DT_VERNEEDNUM,verneedcount);
  2920. end;
  2921. writeDynTag(DT_VERSYM,symversec);
  2922. end;
  2923. writeDynTag(DT_NULL,0);
  2924. end;
  2925. procedure TElfExeOutput.CreatePLT;
  2926. var
  2927. reloc: TObjRelocation;
  2928. begin
  2929. pltobjsec:=TElfObjSection.create_ext(internalObjData,'.plt',
  2930. SHT_PROGBITS,SHF_ALLOC or SHF_EXECINSTR,4,16);
  2931. pltobjsec.SecOptions:=[oso_keep,oso_plt];
  2932. pltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.plt',true);
  2933. pltrelocsec.SecOptions:=[oso_keep];
  2934. ipltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.iplt',true);
  2935. ipltrelocsec.SecOptions:=[oso_keep];
  2936. { reference .dynamic from .got.plt, this isn't necessary if linking statically }
  2937. { TODO: maybe move writing initial .got.plt entries here completely
  2938. (needs testing --- GOT symbol may get lost if .got.plt is empty)}
  2939. if dynamiclink then
  2940. begin
  2941. reloc:=TObjRelocation.CreateSection(0,dynamicsec,RELOC_ABSOLUTE);
  2942. reloc.size:=sizeof(pint);
  2943. gotpltobjsec.ObjRelocations.Add(reloc);
  2944. end;
  2945. { Initial PLT entry, CPU-specific }
  2946. WriteFirstPLTEntry;
  2947. end;
  2948. procedure TElfExeOutput.WritePLTEntry(exesym:TExeSymbol);
  2949. begin
  2950. // must be implemented by CPU-specific descendant
  2951. InternalError(2012092102);
  2952. end;
  2953. procedure TElfExeOutput.WriteIndirectPLTEntry(exesym:TExeSymbol);
  2954. begin
  2955. // must be implemented by CPU-specific descendant
  2956. InternalError(2012092101);
  2957. end;
  2958. function TElfExeOutput.WriteData:boolean;
  2959. begin
  2960. WriteHeader;
  2961. segmentlist.ForEachCall(@segment_write_header,nil);
  2962. WriteExeSectionContent;
  2963. FWriter.WriteZeros(sizeof(TElfsechdr));
  2964. ExeSectionList.ForEachCall(@exesection_write_header,nil);
  2965. result:=true;
  2966. end;
  2967. procedure TElfExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2968. var
  2969. exportlist: TCmdStrList;
  2970. sym: TExeSymbol;
  2971. begin
  2972. AllowUndefinedSymbols:=IsSharedLibrary;
  2973. { add exported symbols to dynamic list }
  2974. exportlist:=texportlibunix(exportlib).exportedsymnames;
  2975. if not exportlist.empty then
  2976. repeat
  2977. sym:=TExeSymbol(ExeSymbolList.Find(exportlist.getfirst));
  2978. if assigned(sym) then
  2979. begin
  2980. if assigned(sym.objsymbol.objsection) then
  2981. sym.objsymbol.objsection.SecOptions:=[oso_keep];
  2982. sym.dynindex:=dynsymlist.add(sym)+1
  2983. end
  2984. else
  2985. InternalError(2012071801);
  2986. until exportlist.empty;
  2987. end;
  2988. procedure TElfExeOutput.ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2989. begin
  2990. { TODO: include objsec properties into message }
  2991. Comment(v_error,'Relocation '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+''' cannot be used when linking a shared object; recompile with -Cg');
  2992. end;
  2993. procedure TElfExeOutput.ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2994. begin
  2995. { TODO: include objsec properties into message }
  2996. Comment(v_error,'Relocation truncated to fit: '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+'''');
  2997. end;
  2998. {****************************************************************************
  2999. TElfExeSection
  3000. ****************************************************************************}
  3001. procedure TElfExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  3002. begin
  3003. inherited AddObjSection(objsec,ignoreprops);
  3004. if ignoreprops then
  3005. exit;
  3006. if (shtype=SHT_NULL) then
  3007. begin
  3008. shtype:=TElfObjSection(objsec).shtype;
  3009. shflags:=TElfObjSection(objsec).shflags;
  3010. shentsize:=TElfObjSection(objsec).shentsize;
  3011. end;
  3012. end;
  3013. {****************************************************************************
  3014. TElfSegment
  3015. ****************************************************************************}
  3016. constructor TElfSegment.Create(atype,aflags,aalign:longword);
  3017. begin
  3018. ptype:=atype;
  3019. pflags:=aflags;
  3020. align:=aalign;
  3021. FSectionList:=TFPObjectList.Create(false);
  3022. end;
  3023. destructor TElfSegment.Destroy;
  3024. begin
  3025. FSectionList.Free;
  3026. inherited Destroy;
  3027. end;
  3028. procedure TElfSegment.Add(exesec:TExeSection);
  3029. begin
  3030. FSectionList.Add(exesec);
  3031. end;
  3032. end.