ogelf.pas 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400
  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. strndx:=0;
  1533. if not LoadHeader(objData) then
  1534. exit;
  1535. FSecTbl:=AllocMem(nsects*sizeof(TSectionRec));
  1536. FLoaded:=AllocMem(nsects*sizeof(boolean));
  1537. SetLength(shdrs,nsects);
  1538. FReader.Seek(shoffset);
  1539. if not FReader.Read(shdrs[0],nsects*sizeof(TElfsechdr)) then
  1540. begin
  1541. InputError('Can''t read ELF section headers');
  1542. exit;
  1543. end;
  1544. if source_info.endian<>target_info.endian then
  1545. for i:=0 to nsects-1 do
  1546. MaybeSwapSecHeader(shdrs[i]);
  1547. { First, load the .shstrtab section }
  1548. if shstrndx>=nsects then
  1549. InternalError(2012060201);
  1550. if shdrs[shstrndx].sh_type<>SHT_STRTAB then
  1551. InternalError(2012060202);
  1552. shstrtablen:=shdrs[shstrndx].sh_size;
  1553. GetMem(shstrtab,shstrtablen);
  1554. FReader.seek(shdrs[shstrndx].sh_offset);
  1555. FReader.read(shstrtab^,shstrtablen);
  1556. FLoaded[shstrndx]:=True;
  1557. { Locate the symtable, it is typically at the end so loop backwards.
  1558. Load the strings, postpone symtable itself until done with sections.
  1559. Note that is is legal to have no symtable.
  1560. For DSO, locate .dynsym instead, this one is near the beginning, but
  1561. overall number of sections won't be big. }
  1562. symtabndx:=0;
  1563. for i:=nsects-1 downto 1 do
  1564. begin
  1565. if (shdrs[i].sh_type<>symsectypes[dynobj]) then
  1566. continue;
  1567. if (shdrs[i].sh_entsize<>sizeof(TElfSymbol)) then
  1568. InternalError(2012060213);
  1569. if shdrs[i].sh_link>=nsects then
  1570. InternalError(2012062702);
  1571. strndx:=shdrs[i].sh_link;
  1572. if shdrs[strndx].sh_type<>SHT_STRTAB then
  1573. InternalError(2012062703);
  1574. strtablen:=shdrs[strndx].sh_size;
  1575. GetMem(strtab,strtablen);
  1576. FReader.seek(shdrs[strndx].sh_offset);
  1577. FReader.read(strtab^,strtablen);
  1578. symtaboffset:=shdrs[i].sh_offset;
  1579. syms:=shdrs[i].sh_size div sizeof(TElfSymbol);
  1580. localsyms:=shdrs[i].sh_info;
  1581. FLoaded[i]:=True;
  1582. FLoaded[strndx]:=True;
  1583. symtabndx:=i;
  1584. break;
  1585. end;
  1586. if dynobj then
  1587. begin
  1588. if symtabndx=0 then
  1589. InternalError(2012110707);
  1590. { Locate .dynamic and version sections. Expect a single one of a kind. }
  1591. dynndx:=0;
  1592. versymndx:=0;
  1593. verdefndx:=0;
  1594. verneedndx:=0;
  1595. for i:=nsects-1 downto 0 do
  1596. begin
  1597. case shdrs[i].sh_type of
  1598. SHT_DYNAMIC:
  1599. begin
  1600. if dynndx<>0 then
  1601. InternalError(2012102001);
  1602. dynndx:=i;
  1603. if (shdrs[dynndx].sh_link<>strndx) then
  1604. InternalError(2012071402);
  1605. LoadDynamic(shdrs[dynndx],objdata);
  1606. end;
  1607. SHT_GNU_versym:
  1608. begin
  1609. if versymndx<>0 then
  1610. InternalError(2012102002);
  1611. versymndx:=i;
  1612. if shdrs[i].sh_entsize<>sizeof(word) then
  1613. InternalError(2012102003);
  1614. if shdrs[i].sh_link<>symtabndx then
  1615. InternalError(2012102004);
  1616. if shdrs[i].sh_size<>syms*sizeof(word) then
  1617. InternalError(2012102005);
  1618. GetMem(symversions,shdrs[i].sh_size);
  1619. FReader.seek(shdrs[i].sh_offset);
  1620. FReader.read(symversions^,shdrs[i].sh_size);
  1621. if source_info.endian<>target_info.endian then
  1622. for j:=0 to syms-1 do
  1623. symversions[j]:=SwapEndian(symversions[j]);
  1624. end;
  1625. SHT_GNU_verdef:
  1626. begin
  1627. if verdefndx<>0 then
  1628. InternalError(2012102006);
  1629. verdefndx:=i;
  1630. if shdrs[i].sh_link<>strndx then
  1631. InternalError(2012120501);
  1632. vdoffset:=shdrs[i].sh_offset;
  1633. { TODO: can we rely on sh_info, or must read until vd_next=0? }
  1634. for j:=1 to shdrs[i].sh_info do
  1635. begin
  1636. FReader.seek(vdoffset);
  1637. FReader.Read(vd,sizeof(TElfverdef));
  1638. MaybeSwapElfverdef(vd);
  1639. if vd.vd_version<>VER_DEF_CURRENT then
  1640. InternalError(2012120502);
  1641. FReader.seek(vdoffset+vd.vd_aux);
  1642. vdoffset:=vdoffset+vd.vd_next;
  1643. { First verdaux entry holds name of version (unless VER_FLG_BASE flag is set),
  1644. subsequent one(s) point to parent(s). For our purposes, version hierarchy
  1645. looks irrelevant. }
  1646. FReader.Read(vda,sizeof(TElfverdaux));
  1647. MaybeSwapElfverdaux(vda);
  1648. if vda.vda_name>=strtablen then
  1649. InternalError(2012120503);
  1650. if (vd.vd_flags and VER_FLG_BASE)<>0 then
  1651. continue;
  1652. { Assuming verdef indices assigned continuously starting from 2,
  1653. at least BFD produces files that way. }
  1654. if verdefs.count<>vd.vd_ndx then
  1655. InternalError(2012120504);
  1656. TElfVersionDef.Create(verdefs,string(PChar(@strtab[vda.vda_name])));
  1657. end;
  1658. end;
  1659. SHT_GNU_verneed:
  1660. begin
  1661. if verneedndx<>0 then
  1662. InternalError(2012102007);
  1663. verneedndx:=i;
  1664. //sh_link->.dynstr
  1665. //sh_info->number of entries
  1666. end;
  1667. end;
  1668. end;
  1669. if dynndx=0 then
  1670. InternalError(2012071401);
  1671. { load the symtable }
  1672. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1673. LoadSymbols(objdata,syms,localsyms);
  1674. result:=True;
  1675. exit;
  1676. end;
  1677. { assume stack is executable until proven otherwise }
  1678. objdata.ExecStack:=True;
  1679. { Process section headers }
  1680. for i:=1 to nsects-1 do
  1681. if not FLoaded[i] then
  1682. LoadSection(shdrs[i],i,objdata);
  1683. { load the content }
  1684. ReadSectionContent(objdata);
  1685. { load the symtable }
  1686. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1687. LoadSymbols(objdata,syms,localsyms);
  1688. { finish relocations }
  1689. for i:=0 to objdata.ObjSectionList.Count-1 do
  1690. begin
  1691. objsec:=TObjSection(objdata.ObjsectionList[i]);
  1692. { skip debug sections }
  1693. if (oso_debug in objsec.SecOptions) and
  1694. (cs_link_strip in current_settings.globalswitches) and
  1695. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1696. continue;
  1697. if FSecTbl[objsec.index].relocpos>0 then
  1698. LoadRelocations(FSecTbl[objsec.index]);
  1699. end;
  1700. { finish processing section groups, if any }
  1701. if Assigned(objdata.GroupsList) then
  1702. begin
  1703. for i:=0 to objdata.GroupsList.Count-1 do
  1704. begin
  1705. grp:=TObjSectionGroup(objData.GroupsList[i]);
  1706. FReader.Seek(shdrs[i+1].sh_offset);
  1707. { first dword is flags }
  1708. FReader.Read(tmp,sizeof(longword));
  1709. if source_info.endian<>target_info.endian then
  1710. tmp:=SwapEndian(tmp);
  1711. if (tmp and GRP_COMDAT)<>0 then
  1712. grp.IsComdat:=true;
  1713. count:=(shdrs[i+1].sh_size div sizeof(longword))-1;
  1714. SetLength(grp.members,count);
  1715. for j:=0 to count-1 do
  1716. begin
  1717. FReader.Read(tmp,sizeof(longword));
  1718. if source_info.endian<>target_info.endian then
  1719. tmp:=SwapEndian(tmp);
  1720. if (tmp>=nsects) then
  1721. InternalError(2012110805);
  1722. objsec:=FSecTbl[tmp].sec;
  1723. if (objsec=nil) then
  1724. InternalError(2012110806);
  1725. if (TElfObjSection(objsec).shflags and SHF_GROUP)=0 then
  1726. InternalError(2012110807);
  1727. grp.members[j]:=objsec;
  1728. objsec.Group:=grp;
  1729. end;
  1730. end;
  1731. end;
  1732. result:=True;
  1733. end;
  1734. class function TElfObjInput.CanReadObjData(AReader:TObjectreader):boolean;
  1735. var
  1736. header: TElfHeader;
  1737. begin
  1738. result:=false;
  1739. if AReader.Read(header,sizeof(header)) then
  1740. begin;
  1741. if (header.e_ident[EI_MAG0]=ELFMAG0) and (header.e_ident[EI_MAG1]=ELFMAG1) and
  1742. (header.e_ident[EI_MAG2]=ELFMAG2) and (header.e_ident[EI_MAG3]=ELFMAG3) then
  1743. { TODO: check additional fields }
  1744. result:=true;
  1745. end;
  1746. AReader.Seek(0);
  1747. end;
  1748. {*****************************************************************************
  1749. TElfExeOutput
  1750. *****************************************************************************}
  1751. constructor TElfExeOutput.Create;
  1752. begin
  1753. inherited Create;
  1754. CObjData:=TElfObjData;
  1755. CExeSection:=TElfExeSection;
  1756. {$ifdef cpu64}
  1757. MaxMemPos:=Qword($FFFFFFFFFFFFFFFF);
  1758. //MaxMemPos:=$7EFFFFFF; { As specified by SysV AMD64 ABI for small memory model }
  1759. {$else cpu64}
  1760. MaxMemPos:=$7FFFFFFF;
  1761. {$endif cpu64}
  1762. SectionMemAlign:=$20;
  1763. SectionDataAlign:=$20;
  1764. segmentlist:=TFPObjectList.Create(True);
  1765. neededlist:=TFPHashList.Create;
  1766. end;
  1767. destructor TElfExeOutput.Destroy;
  1768. begin
  1769. dyncopysyms.Free;
  1770. neededlist.Free;
  1771. segmentlist.Free;
  1772. dynsymlist.Free;
  1773. dynreloclist.Free;
  1774. if assigned(dynsymnames) then
  1775. FreeMem(dynsymnames);
  1776. stringdispose(FInterpreter);
  1777. inherited Destroy;
  1778. end;
  1779. function TElfExeOutput.AttachSection(objsec:TObjSection):TElfExeSection;
  1780. begin
  1781. objsec.SecOptions:=[oso_keep];
  1782. result:=TElfExeSection(FindExeSection(objsec.name));
  1783. if result=nil then
  1784. result:=TElfExeSection.Create(ExeSectionList,objsec.name);
  1785. result.AddObjSection(objsec);
  1786. end;
  1787. function TElfExeOutput.CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  1788. begin
  1789. result:=TElfSegment.Create(atype,aflags,aalign);
  1790. segmentlist.add(result);
  1791. end;
  1792. procedure TElfExeOutput.WriteHeader;
  1793. var
  1794. header: TElfHeader;
  1795. begin
  1796. FillChar(header,sizeof(header),0);
  1797. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  1798. header.e_ident[EI_MAG1]:=ELFMAG1;
  1799. header.e_ident[EI_MAG2]:=ELFMAG2;
  1800. header.e_ident[EI_MAG3]:=ELFMAG3;
  1801. header.e_ident[EI_CLASS]:=ELFCLASS;
  1802. if target_info.endian=endian_big then
  1803. header.e_ident[EI_DATA]:=ELFDATA2MSB
  1804. else
  1805. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  1806. header.e_ident[EI_VERSION]:=1;
  1807. if IsSharedLibrary then
  1808. header.e_type:=ET_DYN
  1809. else
  1810. header.e_type:=ET_EXEC;
  1811. header.e_machine:=ElfTarget.machine_code;
  1812. header.e_version:=1;
  1813. header.e_phoff:=sizeof(TElfHeader);
  1814. header.e_shoff:=shoffset;
  1815. header.e_shstrndx:=ExeSectionList.IndexOf(shstrtabsect.ExeSection)+1;
  1816. header.e_shnum:=ExeSectionList.Count+1;
  1817. header.e_phnum:=segmentlist.count;
  1818. header.e_ehsize:=sizeof(telfheader);
  1819. if assigned(EntrySym) then
  1820. header.e_entry:=EntrySym.Address;
  1821. header.e_shentsize:=sizeof(telfsechdr);
  1822. header.e_phentsize:=sizeof(telfproghdr);
  1823. MaybeSwapHeader(header);
  1824. FWriter.Write(header,sizeof(header));
  1825. end;
  1826. procedure TElfExeOutput.exesection_write_header(p:TObject;arg:Pointer);
  1827. var
  1828. shdr: TElfsechdr;
  1829. exesec: TElfExeSection absolute p;
  1830. begin
  1831. FillChar(shdr,sizeof(shdr),0);
  1832. shdr.sh_name:=exesec.shstridx;
  1833. if (ExeWriteMode=ewm_dbgonly) and
  1834. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  1835. shdr.sh_type:=SHT_NOBITS
  1836. else
  1837. shdr.sh_type:=exesec.shtype;
  1838. shdr.sh_flags:=exesec.shflags;
  1839. if (oso_load in exesec.SecOptions) then
  1840. shdr.sh_addr:=exesec.MemPos;
  1841. shdr.sh_offset:=exesec.DataPos;
  1842. shdr.sh_size:=exesec.Size;
  1843. shdr.sh_link:=exesec.shlink;
  1844. shdr.sh_info:=exesec.shinfo;
  1845. shdr.sh_addralign:=exesec.SecAlign;
  1846. shdr.sh_entsize:=exesec.shentsize;
  1847. MaybeSwapSecHeader(shdr);
  1848. FWriter.Write(shdr,sizeof(shdr));
  1849. end;
  1850. procedure TElfExeOutput.segment_write_header(p:TObject;arg:Pointer);
  1851. var
  1852. phdr: TElfproghdr;
  1853. seg: TElfSegment absolute p;
  1854. begin
  1855. FillChar(phdr,sizeof(phdr),0);
  1856. phdr.p_type:=seg.ptype;
  1857. phdr.p_flags:=seg.pflags;
  1858. phdr.p_align:=seg.align;
  1859. phdr.p_offset:=seg.DataPos;
  1860. phdr.p_filesz:=seg.DataSize;
  1861. phdr.p_memsz:=seg.MemSize;
  1862. phdr.p_vaddr:=seg.MemPos;
  1863. phdr.p_paddr:=seg.MemPos;
  1864. MaybeSwapHeader(phdr);
  1865. FWriter.Write(phdr,sizeof(phdr));
  1866. end;
  1867. procedure TElfExeOutput.WriteStaticSymtable;
  1868. var
  1869. i: longint;
  1870. sec: TElfExeSection;
  1871. exesym: TExeSymbol;
  1872. begin
  1873. if assigned(tlsseg) then
  1874. symtab.tlsbase:=tlsseg.MemPos;
  1875. for i:=0 to ExeSectionList.Count-1 do
  1876. begin
  1877. sec:=TElfExeSection(ExeSectionList[i]);
  1878. { Must not write symbols for internal sections like .symtab }
  1879. if (sec.shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  1880. continue;
  1881. sec.secsymidx:=symtab.symidx;
  1882. symtab.writeInternalSymbol(sec.mempos,0,STT_SECTION,sec.secshidx);
  1883. end;
  1884. { local symbols first }
  1885. for i:=0 to ExeSymbolList.Count-1 do
  1886. begin
  1887. exesym:=TExeSymbol(ExeSymbolList[i]);
  1888. if (exesym.objsymbol.bind=AB_LOCAL) and (exesym.objsymbol.typ<>AT_LABEL) then
  1889. symtab.WriteSymbol(exesym.objsymbol);
  1890. end;
  1891. { Global Symbols }
  1892. for i:=0 to ExeSymbolList.Count-1 do
  1893. begin
  1894. exesym:=TExeSymbol(ExeSymbolList[i]);
  1895. if (exesym.objsymbol.bind<>AB_LOCAL) then
  1896. symtab.WriteSymbol(exesym.objsymbol);
  1897. end;
  1898. { update exe section properties }
  1899. symtab.ExeSection.size:=symtab.size;
  1900. TElfExeSection(symtab.ExeSection).shinfo:=symtab.shinfo;
  1901. TElfExeSection(symtab.ExeSection).shlink:=ExeSectionList.IndexOf(symtab.fstrsec.ExeSection)+1;
  1902. symtab.fstrsec.ExeSection.Size:=symtab.fstrsec.size;
  1903. end;
  1904. procedure TElfExeOutput.MapSectionsToSegments;
  1905. var
  1906. seg: TElfSegment;
  1907. exesec: TExeSection;
  1908. i: longint;
  1909. begin
  1910. if (not IsSharedLibrary) and assigned(interpobjsec) then
  1911. begin
  1912. phdrseg:=CreateSegment(PT_PHDR,PF_R or PF_X,sizeof(pint));
  1913. seg:=CreateSegment(PT_INTERP,PF_R,1);
  1914. seg.Add(interpobjsec.ExeSection);
  1915. end;
  1916. textseg:=CreateSegment(PT_LOAD,PF_X or PF_R,ElfTarget.max_page_size);
  1917. dataseg:=CreateSegment(PT_LOAD,PF_R or PF_W,ElfTarget.max_page_size);
  1918. for i:=0 to ExeSectionList.Count-1 do
  1919. begin
  1920. exesec:=TExeSection(ExeSectionList[i]);
  1921. if (oso_load in exesec.SecOptions) then
  1922. begin
  1923. if (TElfExeSection(exesec).shflags and SHF_TLS)<>0 then
  1924. begin
  1925. if tlsseg=nil then
  1926. tlsseg:=CreateSegment(PT_TLS,PF_R,sizeof(pint));
  1927. tlsseg.add(exesec);
  1928. end;
  1929. { TODO: at least on Linux, ld seems to drop .note.ABI-tag for static executables.
  1930. (Logic is as follows: there is no .note.ABI-tag section in ld script, so it
  1931. is processed as orphan section. As such, it is placed after .interp.
  1932. For static executables .interp is dropped, and it looks like there's nowhere to
  1933. place .note.ABI-tag in this case)
  1934. Always including it doesn't harm though (except increasing file size). }
  1935. if TElfExeSection(exesec).shtype=SHT_NOTE then
  1936. begin
  1937. if noteseg=nil then
  1938. noteseg:=CreateSegment(PT_NOTE,PF_R,4);
  1939. noteseg.Add(exesec);
  1940. Include(exesec.SecOptions,oso_debug_copy);
  1941. end;
  1942. if (oso_executable in exesec.SecOptions) or
  1943. not (oso_write in exesec.SecOptions) then
  1944. textseg.add(exesec)
  1945. else
  1946. dataseg.add(exesec);
  1947. end;
  1948. end;
  1949. if dynamiclink then
  1950. begin
  1951. seg:=CreateSegment(PT_DYNAMIC,PF_R or PF_W,sizeof(pint));
  1952. seg.add(dynamicsec.ExeSection);
  1953. end;
  1954. { stack flags }
  1955. CreateSegment(PT_GNU_STACK,PF_R or PF_W or (PF_X*ord(ExecStack)),sizeof(pint));
  1956. end;
  1957. procedure TElfExeOutput.make_dynamic_if_undefweak(exesym:TExeSymbol);
  1958. begin
  1959. if (exesym.dynindex=0) and (exesym.state=symstate_undefweak) and
  1960. not (cs_link_staticflag in current_settings.globalswitches) then
  1961. exesym.dynindex:=dynsymlist.add(exesym)+1;
  1962. end;
  1963. function TElfExeOutput.AllocGOTSlot(objsym:TObjSymbol):boolean;
  1964. var
  1965. exesym: TExeSymbol;
  1966. begin
  1967. result:=false;
  1968. exesym:=objsym.exesymbol;
  1969. { Although local symbols should not be accessed through GOT,
  1970. this isn't strictly forbidden. In this case we need to fake up
  1971. the exesym to store the GOT offset in it.
  1972. TODO: name collision; maybe use a different symbol list object? }
  1973. if exesym=nil then
  1974. begin
  1975. exesym:=TExeSymbol.Create(ExeSymbolList,objsym.name+'*local*');
  1976. exesym.objsymbol:=objsym;
  1977. objsym.exesymbol:=exesym;
  1978. end;
  1979. if exesym.GotOffset>0 then
  1980. exit;
  1981. gotobjsec.alloc(sizeof(pint));
  1982. exesym.GotOffset:=gotobjsec.size;
  1983. make_dynamic_if_undefweak(exesym);
  1984. { In shared library, every GOT entry needs a RELATIVE dynamic reloc,
  1985. imported/exported symbols need GLOB_DAT instead. For executables,
  1986. only the latter applies. }
  1987. if IsSharedLibrary or (exesym.dynindex>0) then
  1988. dynrelocsec.alloc(dynrelocsec.shentsize);
  1989. result:=true;
  1990. end;
  1991. procedure TElfExeOutput.PrepareGOT;
  1992. var
  1993. i,j,k: longint;
  1994. objsec: TElfObjSection;
  1995. exesec: TExeSection;
  1996. begin
  1997. for i:=0 to ExeSectionList.Count-1 do
  1998. begin
  1999. exesec:=TExeSection(ExeSectionList[i]);
  2000. for j:=0 to exesec.ObjSectionlist.count-1 do
  2001. begin
  2002. objsec:=TElfObjSection(exesec.ObjSectionlist[j]);
  2003. { ignore linker-generated and debug sections }
  2004. if (objsec.objdata=internalobjdata) or (oso_debug in objsec.SecOptions) then
  2005. continue;
  2006. if not objsec.Used then
  2007. internalerror(2012060901);
  2008. k:=0;
  2009. while k<objsec.ObjRelocations.Count do
  2010. begin
  2011. GOTRelocPass1(objsec,k);
  2012. inc(k);
  2013. end;
  2014. end;
  2015. end;
  2016. { remember sizes for sanity checking }
  2017. gotsize:=gotobjsec.size;
  2018. if assigned(dynrelocsec) then
  2019. dynrelsize:=dynrelocsec.size
  2020. else
  2021. dynrelsize:=0;
  2022. end;
  2023. procedure TElfExeOutput.CreateGOTSection;
  2024. begin
  2025. gotpltobjsec:=TElfObjSection.create_ext(internalObjData,'.got.plt',
  2026. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  2027. gotobjsec:=TElfObjSection.create_ext(internalObjData,'.got',
  2028. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  2029. gotobjsec.SecOptions:=[oso_keep];
  2030. { GOT symbol and reserved .got.plt entries }
  2031. internalObjData.SetSection(gotpltobjsec);
  2032. gotsymbol:=internalObjData.SymbolDefine('_GLOBAL_OFFSET_TABLE_',AB_GLOBAL,AT_DATA);
  2033. gotpltobjsec.writeZeros(3*sizeof(pint));
  2034. end;
  2035. procedure TElfExeOutput.Load_Start;
  2036. begin
  2037. inherited Load_Start;
  2038. dynsymlist:=TFPObjectList.Create(False);
  2039. CreateGOTSection;
  2040. end;
  2041. procedure TElfExeOutput.Load_DynamicObject(objdata:TObjData;asneeded:boolean);
  2042. var
  2043. i: longint;
  2044. exesym: TExeSymbol;
  2045. objsym: TObjSymbol;
  2046. needed: boolean;
  2047. begin
  2048. Comment(v_debug,'Dynamic object: '+objdata.name);
  2049. needed:=false;
  2050. for i:=0 to UnresolvedExeSymbols.Count-1 do
  2051. begin
  2052. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2053. if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
  2054. continue;
  2055. objsym:=TObjSymbol(objdata.ObjSymbolList.Find(exesym.name));
  2056. if assigned(objsym) then
  2057. begin
  2058. exesym.State:=symstate_defined;
  2059. exesym.dynindex:=dynsymlist.Add(exesym)+1;
  2060. { The original binding, value and section of external symbol
  2061. must be preserved, therefore resolving directly to .so symbol
  2062. hurts more than it helps. Copy type and size, and store .so
  2063. symbol in objsym.indsymbol for later use. }
  2064. exesym.ObjSymbol.typ:=objsym.typ;
  2065. if objsym.typ<>AT_FUNCTION then
  2066. exesym.ObjSymbol.size:=objsym.size;
  2067. exesym.ObjSymbol.indsymbol:=objsym;
  2068. objsym.ExeSymbol:=exesym;
  2069. needed:=true;
  2070. end;
  2071. end;
  2072. if (needed or (not asneeded)) and
  2073. (neededlist.Find(objdata.name)=nil) then
  2074. neededlist.Add(objdata.name,objdata);
  2075. end;
  2076. procedure TElfExeOutput.Order_Start;
  2077. begin
  2078. inherited Order_Start;
  2079. dynamiclink:=IsSharedLibrary or (dynsymlist.count>0) or
  2080. (
  2081. (UnresolvedExeSymbols.Count>0) and
  2082. not (cs_link_staticflag in current_settings.globalswitches)
  2083. );
  2084. if dynamiclink then
  2085. InitDynlink;
  2086. if dynamiclink or (IndirectObjSymbols.Count>0) then
  2087. CreatePLT;
  2088. end;
  2089. procedure TElfExeOutput.Order_end;
  2090. procedure set_oso_keep(const s:string);
  2091. var
  2092. exesec:TExeSection;
  2093. objsec:TObjSection;
  2094. i:longint;
  2095. begin
  2096. exesec:=TExeSection(ExeSectionList.Find(s));
  2097. if assigned(exesec) then
  2098. begin
  2099. for i:=0 to exesec.ObjSectionList.Count-1 do
  2100. begin
  2101. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2102. { ignore sections used for symbol definition }
  2103. if oso_data in objsec.SecOptions then
  2104. objsec.SecOptions:=[oso_keep];
  2105. end;
  2106. end;
  2107. end;
  2108. begin
  2109. OrderOrphanSections;
  2110. inherited Order_end;
  2111. set_oso_keep('.init');
  2112. set_oso_keep('.fini');
  2113. set_oso_keep('.jcr');
  2114. set_oso_keep('.ctors');
  2115. set_oso_keep('.dtors');
  2116. set_oso_keep('.preinit_array');
  2117. set_oso_keep('.init_array');
  2118. set_oso_keep('.fini_array');
  2119. set_oso_keep('.eh_frame');
  2120. { let .dynamic reference other dynamic sections so they aren't marked
  2121. for removal as unused }
  2122. if dynamiclink then
  2123. WriteDynamicTags;
  2124. end;
  2125. procedure TElfExeOutput.OrderOrphanSections;
  2126. var
  2127. i,j:longint;
  2128. objdata:TObjData;
  2129. objsec:TObjSection;
  2130. exesec:TExeSection;
  2131. opts:TObjSectionOptions;
  2132. s:string;
  2133. newsections,tmp:TFPHashObjectList;
  2134. allsections:TFPList;
  2135. inserts:array[0..6] of TExeSection;
  2136. idx,inspos:longint;
  2137. begin
  2138. newsections:=TFPHashObjectList.Create(false);
  2139. allsections:=TFPList.Create;
  2140. { copy existing sections }
  2141. for i:=0 to ExeSectionList.Count-1 do
  2142. allsections.add(ExeSectionList[i]);
  2143. inserts[0]:=FindExeSection('.comment');
  2144. inserts[1]:=nil;
  2145. inserts[2]:=FindExeSection('.interp');
  2146. inserts[3]:=FindExeSection('.bss');
  2147. inserts[4]:=FindExeSection('.data');
  2148. inserts[5]:=FindExeSection('.rodata');
  2149. inserts[6]:=FindExeSection('.text');
  2150. for i:=0 to ObjDataList.Count-1 do
  2151. begin
  2152. ObjData:=TObjData(ObjDataList[i]);
  2153. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2154. begin
  2155. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2156. if objsec.Used then
  2157. continue;
  2158. s:=objsec.name;
  2159. exesec:=TExeSection(newsections.Find(s));
  2160. if assigned(exesec) then
  2161. begin
  2162. exesec.AddObjSection(objsec);
  2163. continue;
  2164. end;
  2165. opts:=objsec.SecOptions*[oso_data,oso_load,oso_write,oso_executable];
  2166. if (objsec.SecOptions*[oso_load,oso_debug]=[]) then
  2167. { non-alloc, after .comment
  2168. GNU ld places .comment between stabs and dwarf debug info }
  2169. inspos:=0
  2170. else if not (oso_load in objsec.SecOptions) then
  2171. inspos:=1 { debugging, skip }
  2172. else if (oso_load in objsec.SecOptions) and
  2173. (TElfObjSection(objsec).shtype=SHT_NOTE) then
  2174. inspos:=2 { after .interp }
  2175. else if (opts=[oso_load,oso_write]) then
  2176. inspos:=3 { after .bss }
  2177. else if (opts=[oso_data,oso_load,oso_write]) then
  2178. inspos:=4 { after .data }
  2179. else if (opts=[oso_data,oso_load]) then
  2180. inspos:=5 { rodata, relocs=??? }
  2181. else if (opts=[oso_data,oso_load,oso_executable]) then
  2182. inspos:=6 { text }
  2183. else
  2184. begin
  2185. Comment(v_debug,'Orphan section '+objsec.fullname+' has attributes that are not handled!');
  2186. continue;
  2187. end;
  2188. if (inserts[inspos]=nil) then
  2189. begin
  2190. Comment(v_debug,'Orphan section '+objsec.fullname+': nowhere to insert, ignored');
  2191. continue;
  2192. end;
  2193. idx:=allsections.IndexOf(inserts[inspos]);
  2194. exesec:=CExeSection.Create(newsections,s);
  2195. allsections.Insert(idx+1,exesec);
  2196. inserts[inspos]:=exesec;
  2197. exesec.AddObjSection(objsec);
  2198. end;
  2199. end;
  2200. { Now replace the ExeSectionList with content of allsections }
  2201. if (newsections.count<>0) then
  2202. ReplaceExeSectionList(allsections);
  2203. newsections.Free;
  2204. allsections.Free;
  2205. end;
  2206. procedure TElfExeOutput.AfterUnusedSectionRemoval;
  2207. var
  2208. i:longint;
  2209. exesym:TExeSymbol;
  2210. objsym:TObjSymbol;
  2211. objsec: TObjSection;
  2212. begin
  2213. { Unused section removal sets Used property of referenced exesymbols.
  2214. Remaining ones can be removed. }
  2215. for i:=0 to dynsymlist.count-1 do
  2216. begin
  2217. exesym:=TExeSymbol(dynsymlist[i]);
  2218. if assigned(exesym.ObjSymbol.ObjSection) then // an exported symbol
  2219. continue;
  2220. if not exesym.used then
  2221. begin
  2222. dynsymlist[i]:=nil;
  2223. exesym.dynindex:=0;
  2224. end;
  2225. end;
  2226. dynsymlist.Pack;
  2227. { reindex }
  2228. for i:=0 to dynsymlist.count-1 do
  2229. TExeSymbol(dynsymlist[i]).dynindex:=i+1;
  2230. { Drop unresolved symbols that aren't referenced, assign dynamic
  2231. indices to remaining ones, but not if linking with -Xt.
  2232. TODO: behavior of .so with -Xt ? }
  2233. if (cs_link_staticflag in current_settings.globalswitches) then
  2234. UnresolvedExeSymbols.Clear
  2235. else
  2236. for i:=0 to UnresolvedExeSymbols.Count-1 do
  2237. begin
  2238. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2239. if exesym.used then
  2240. begin
  2241. if exesym.dynindex<>0 then
  2242. InternalError(2012062301);
  2243. { Weak-referenced symbols are changed into dynamic ones
  2244. only if referenced through GOT or PLT (this is BFD-compatible) }
  2245. if exesym.state<>symstate_undefweak then
  2246. exesym.dynindex:=dynsymlist.add(exesym)+1;
  2247. end
  2248. else
  2249. UnresolvedExeSymbols[i]:=nil;
  2250. end;
  2251. UnresolvedExeSymbols.Pack;
  2252. { Scan relocations to determine size of GOT, dynamic reloc section, etc. }
  2253. PrepareGOT;
  2254. { Write required PLT entries }
  2255. for i:=0 to dynsymlist.Count-1 do
  2256. begin
  2257. exesym:=TExeSymbol(dynsymlist[i]);
  2258. if assigned(exesym.ObjSymbol.objsection) then // an exported symbol
  2259. continue;
  2260. if ((exesym.ObjSymbol.refs and symref_plt)<>0) or
  2261. ((exesym.ObjSymbol.typ=AT_FUNCTION) and (not IsSharedLibrary)) then
  2262. begin
  2263. make_dynamic_if_undefweak(exesym);
  2264. { This symbol has a valid address to which relocations are resolved,
  2265. but it remains (weak)external when written to dynamic symtable. }
  2266. objsym:=internalobjdata.CreateSymbol(exesym.name);
  2267. objsym.typ:=AT_FUNCTION;
  2268. objsym.bind:=exesym.ObjSymbol.bind; { AB_EXTERNAL or AB_WEAK_EXTERNAL }
  2269. objsym.indsymbol:=exesym.ObjSymbol.indsymbol;
  2270. objsym.offset:=pltobjsec.size;
  2271. objsym.objsection:=pltobjsec;
  2272. objsym.exesymbol:=exesym;
  2273. exesym.ObjSymbol:=objsym;
  2274. WritePLTEntry(exesym);
  2275. end
  2276. else if ((exesym.ObjSymbol.refs and symref_from_text)<>0) and
  2277. (exesym.ObjSymbol.typ<>AT_FUNCTION) and (not IsSharedLibrary) and
  2278. (exesym.state<>symstate_undefweak) then
  2279. begin
  2280. if exesym.ObjSymbol.size=0 then
  2281. Comment(v_error,'Dynamic variable '+exesym.name+' has zero size');
  2282. internalobjdata.setSection(dynbssobjsec);
  2283. internalobjdata.allocalign(size_2_align(exesym.ObjSymbol.size));
  2284. objsym:=internalobjdata.SymbolDefine(exesym.name,AB_GLOBAL,AT_DATA);
  2285. objsym.size:=exesym.ObjSymbol.size;
  2286. objsym.indsymbol:=exesym.ObjSymbol.indsymbol;
  2287. exesym.ObjSymbol:=objsym;
  2288. objsym.exesymbol:=exesym;
  2289. dynbssobjsec.alloc(objsym.size);
  2290. { allocate space for R_xx_COPY relocation for this symbol;
  2291. we'll create it later, to be consistent with "-z combreloc" semantics }
  2292. dyncopysyms.add(objsym);
  2293. dynrelocsec.alloc(dynrelocsec.shentsize);
  2294. inc(dynrelsize,dynrelocsec.shentsize);
  2295. end;
  2296. end;
  2297. { Handle indirect symbols }
  2298. for i:=0 to IndirectObjSymbols.Count-1 do
  2299. begin
  2300. objsym:=TObjSymbol(IndirectObjSymbols[i]);
  2301. objsec:=objsym.ExeSymbol.ObjSymbol.objsection;
  2302. objsym.bind:=AB_EXTERNAL; { cheat FixupSymbols }
  2303. if (oso_plt in objsec.SecOptions) then
  2304. continue;
  2305. WriteIndirectPLTEntry(objsym.ExeSymbol);
  2306. end;
  2307. FixupSymbols;
  2308. if dynamiclink then
  2309. begin
  2310. WriteVersionSections;
  2311. WriteDynamicSymbolsHash;
  2312. end;
  2313. { Create .shstrtab section, which is needed in both exe and .dbg files }
  2314. shstrtabsect:=TElfObjSection.Create_ext(internalObjData,'.shstrtab',SHT_STRTAB,0,1,0);
  2315. shstrtabsect.SecOptions:=[oso_debug_copy];
  2316. AttachSection(shstrtabsect);
  2317. { Create the static symtable (.symtab and .strtab) }
  2318. if (cs_link_separate_dbg_file in current_settings.globalswitches) or
  2319. not(cs_link_strip in current_settings.globalswitches) then
  2320. begin
  2321. symtab:=TElfSymtab.Create(internalObjData,esk_exe);
  2322. symtab.SecOptions:=[oso_debug];
  2323. symtab.fstrsec.SecOptions:=[oso_debug];
  2324. AttachSection(symtab);
  2325. AttachSection(symtab.fstrsec);
  2326. end;
  2327. { Re-enable sections which end up to contain some data
  2328. (.got, .rel[a].dyn, .rel[a].plt (includes .rel[a].iplt) and .hash }
  2329. if gotobjsec.size<>0 then
  2330. gotobjsec.ExeSection.Disabled:=false;
  2331. if assigned(dynrelocsec) and
  2332. ((dynrelocsec.size<>0) or (dyncopysyms.count<>0)) then
  2333. dynrelocsec.ExeSection.Disabled:=false;
  2334. if assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2335. pltrelocsec.ExeSection.Disabled:=false;
  2336. if assigned(ipltrelocsec) and (ipltrelocsec.size>0) then
  2337. ipltrelocsec.ExeSection.Disabled:=false;
  2338. if assigned(hashobjsec) then
  2339. hashobjsec.ExeSection.Disabled:=false;
  2340. if assigned(symversec) and (symversec.size<>0) then
  2341. symversec.ExeSection.Disabled:=false;
  2342. if assigned(verneedsec) and (verneedsec.size<>0) then
  2343. verneedsec.ExeSection.Disabled:=false;
  2344. if assigned(verdefsec) and (verdefsec.size<>0) then
  2345. verdefsec.ExeSection.Disabled:=false;
  2346. RemoveDisabledSections;
  2347. MapSectionsToSegments;
  2348. if dynamiclink then
  2349. FinishDynamicTags;
  2350. end;
  2351. procedure TElfExeOutput.WriteShStrtab;
  2352. var
  2353. i: longint;
  2354. exesec: TElfExeSection;
  2355. begin
  2356. { Remove any existing .shstrtab contents }
  2357. if (shstrtabsect.size>0) then
  2358. begin
  2359. shstrtabsect.ReleaseData;
  2360. shstrtabsect.Size:=0;
  2361. shstrtabsect.SecOptions:=[oso_data];
  2362. end;
  2363. shstrtabsect.writezeros(1);
  2364. for i:=0 to ExeSectionList.Count-1 do
  2365. begin
  2366. exesec:=TElfExeSection(ExeSectionList[i]);
  2367. exesec.shstridx:=shstrtabsect.writestr(exesec.Name);
  2368. exesec.secshidx:=i+1;
  2369. end;
  2370. end;
  2371. procedure TElfExeOutput.FixupSectionLinks;
  2372. var
  2373. dynstrndx,dynsymndx: longword;
  2374. begin
  2375. if dynamiclink then
  2376. begin
  2377. dynstrndx:=TElfExeSection(dynsymtable.fstrsec.ExeSection).secshidx;
  2378. dynsymndx:=TElfExeSection(dynsymtable.ExeSection).secshidx;
  2379. TElfExeSection(hashobjsec.ExeSection).shlink:=dynsymndx;
  2380. TElfExeSection(dynamicsec.ExeSection).shlink:=dynstrndx;
  2381. TElfExeSection(dynsymtable.ExeSection).shlink:=dynstrndx;
  2382. if assigned(pltrelocsec) then
  2383. begin
  2384. TElfExeSection(pltrelocsec.ExeSection).shlink:=dynsymndx;
  2385. TElfExeSection(pltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2386. end;
  2387. if assigned(dynrelocsec) and assigned(dynrelocsec.ExeSection) then
  2388. TElfExeSection(dynrelocsec.ExeSection).shlink:=dynsymndx;
  2389. if symversec.size>0 then
  2390. TElfExeSection(symversec.ExeSection).shlink:=dynsymndx;
  2391. if verdefsec.size>0 then
  2392. TElfExeSection(verdefsec.ExeSection).shlink:=dynstrndx;
  2393. if verneedsec.size>0 then
  2394. TElfExeSection(verneedsec.ExeSection).shlink:=dynstrndx;
  2395. end
  2396. else if assigned(ipltrelocsec) then
  2397. TElfExeSection(ipltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2398. end;
  2399. procedure TElfExeOutput.Do_Mempos;
  2400. var
  2401. i,j: longint;
  2402. seg: TElfSegment;
  2403. exesec: TElfExeSection;
  2404. objsec: TObjSection;
  2405. tempmempos: qword;
  2406. begin
  2407. if IsSharedLibrary then
  2408. CurrMemPos:=0
  2409. else
  2410. CurrMemPos:=ElfTarget.exe_image_base;
  2411. textseg.MemPos:=CurrMemPos;
  2412. if assigned(phdrseg) then
  2413. begin
  2414. phdrseg.Mempos:=CurrMemPos+sizeof(TElfHeader);
  2415. phdrseg.Memsize:=sizeof(TElfproghdr)*segmentlist.count;
  2416. end;
  2417. CurrMemPos:=CurrMemPos+sizeof(TElfHeader)+segmentlist.count*sizeof(TElfproghdr);
  2418. MemPos_Segment(textseg);
  2419. CurrMemPos:=Align(CurrMemPos,SectionDataAlign); {! Data,not MemAlign}
  2420. CurrMemPos:=CurrMemPos+ElfTarget.max_page_size;
  2421. dataseg.MemPos:=CurrMemPos;
  2422. MemPos_Segment(dataseg);
  2423. { Mempos of unmapped sections is forced to zero, but we have to set positions
  2424. of its objsections and update sizes }
  2425. for i:=0 to ExeSectionList.Count-1 do
  2426. begin
  2427. exesec:=TElfExeSection(ExeSectionList[i]);
  2428. if not (oso_load in exesec.SecOptions) then
  2429. begin
  2430. tempmempos:=0;
  2431. exesec.MemPos:=tempmempos;
  2432. for j:=0 to exesec.ObjSectionList.Count-1 do
  2433. begin
  2434. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2435. tempmempos:=objsec.setmempos(tempmempos);
  2436. end;
  2437. exesec.Size:=tempmempos;
  2438. end;
  2439. end;
  2440. { Update MemPos and MemSize of non-load segments,
  2441. in particular, TLS sizes are needed to resolve relocations }
  2442. for i:=0 to segmentlist.count-1 do
  2443. begin
  2444. seg:=TElfSegment(segmentlist[i]);
  2445. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2446. continue;
  2447. seg.MemPos:=TExeSection(seg.FSectionList.First).MemPos;
  2448. for j:=0 to seg.FSectionList.Count-1 do
  2449. begin
  2450. exesec:=TElfExeSection(seg.FSectionList[j]);
  2451. seg.MemSize:=exesec.MemPos+exesec.Size-seg.MemPos;
  2452. end;
  2453. end;
  2454. end;
  2455. procedure TElfExeOutput.MemPos_Start;
  2456. var
  2457. i: longint;
  2458. begin
  2459. { Assign section indices and fill .shstrtab
  2460. List of sections cannot be modified after this point. }
  2461. WriteShStrtab;
  2462. { fixup sh_link/sh_info members of various dynamic sections }
  2463. FixupSectionLinks;
  2464. { The actual layout }
  2465. Do_Mempos;
  2466. if (not gotwritten) then
  2467. begin
  2468. { Reset size of .got and .rel[a].dyn, they will be refilled while fixing up relocations.
  2469. For .got, consider already written reserved entries. }
  2470. if assigned(gotobjsec) then
  2471. gotobjsec.size:=gotobjsec.data.size;
  2472. if assigned(dynrelocsec) then
  2473. begin
  2474. dynrelocsec.size:=0;
  2475. { write actual .dynsym content (needs valid symbol addresses) }
  2476. if assigned(tlsseg) then
  2477. dynsymtable.tlsbase:=tlsseg.MemPos;
  2478. dynsymtable.size:=sizeof(TElfsymbol);
  2479. for i:=0 to dynsymlist.count-1 do
  2480. dynsymtable.writeSymbol(TExeSymbol(dynsymlist[i]).objsymbol,dynsymnames[i]);
  2481. end;
  2482. end;
  2483. end;
  2484. procedure TElfExeOutput.MemPos_Segment(seg:TElfSegment);
  2485. var
  2486. i: longint;
  2487. exesec: TElfExeSection;
  2488. begin
  2489. for i:=0 to seg.FSectionList.Count-1 do
  2490. begin
  2491. exesec:=TElfExeSection(seg.FSectionList[i]);
  2492. inherited MemPos_ExeSection(exesec);
  2493. { .tbss should not contribute to address space }
  2494. if (exesec.shtype=SHT_NOBITS) and ((exesec.shflags and SHF_TLS)<>0) then
  2495. CurrMemPos:=exesec.MemPos;
  2496. end;
  2497. { calculate size of the segment }
  2498. seg.MemSize:=CurrMemPos-seg.MemPos;
  2499. end;
  2500. procedure TElfExeOutput.MemPos_ExeSection(const aname:string);
  2501. begin
  2502. // Ignore. All layout is done in mempos_start
  2503. end;
  2504. procedure TElfExeOutput.DataPos_Start;
  2505. var
  2506. i,j: longint;
  2507. exesec: TExeSection;
  2508. seg: TElfSegment;
  2509. objreloc: TObjRelocation;
  2510. objsym: TObjSymbol;
  2511. begin
  2512. gotwritten:=true;
  2513. { If target does not support sorted relocations, it is expected to write the
  2514. entire .rel[a].dyn section during FixupRelocations, and leave dynreloclist empty.
  2515. Otherwise, only RELATIVE ones should be written, space for non-relative relocations
  2516. should remain. }
  2517. if assigned(dynrelocsec) then
  2518. begin
  2519. { Append R_xx_COPY relocations }
  2520. for i:=0 to dyncopysyms.count-1 do
  2521. begin
  2522. objsym:=TObjSymbol(dyncopysyms[i]);
  2523. dynreloclist.Add(TObjRelocation.CreateRaw(objsym.address,objsym,ElfTarget.dyn_reloc_codes[dr_copy]));
  2524. end;
  2525. dyncopysyms.Clear;
  2526. if (dynrelocsec.size+(dynreloclist.count*dynrelocsec.shentsize)<>dynrelsize) then
  2527. InternalError(2012110601);
  2528. { Write out non-RELATIVE dynamic relocations
  2529. TODO: additional sorting? }
  2530. for i:=0 to dynreloclist.count-1 do
  2531. begin
  2532. objreloc:=TObjRelocation(dynreloclist[i]);
  2533. WriteDynRelocEntry(objreloc.dataoffset,objreloc.ftype,objreloc.symbol.exesymbol.dynindex,0);
  2534. end;
  2535. end;
  2536. { sanity checks }
  2537. if assigned(gotobjsec) and (gotsize<>gotobjsec.size) then
  2538. InternalError(2012092501);
  2539. if assigned(dynrelocsec) and (dynrelsize<>dynrelocsec.size) then
  2540. InternalError(2012092502);
  2541. if (ExeWriteMode=ewm_dbgonly) or
  2542. (
  2543. (ExeWriteMode=ewm_exefull) and
  2544. not(cs_link_strip in current_settings.globalswitches)
  2545. ) then
  2546. WriteStaticSymtable;
  2547. { first handle primary segments }
  2548. textseg.DataPos:=0;
  2549. CurrDataPos:=sizeof(TElfHeader)+sizeof(TElfproghdr)*segmentlist.count;
  2550. if assigned(phdrseg) then
  2551. begin
  2552. phdrseg.DataPos:=sizeof(TElfHeader);
  2553. phdrseg.DataSize:=sizeof(TElfproghdr)*segmentlist.count;
  2554. end;
  2555. DataPos_Segment(textseg);
  2556. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2557. dataseg.DataPos:=CurrDataPos;
  2558. DataPos_Segment(dataseg);
  2559. { then unmapped sections }
  2560. for i:=0 to ExeSectionList.Count-1 do
  2561. begin
  2562. exesec:=TExeSection(ExeSectionList[i]);
  2563. if not (oso_load in exesec.SecOptions) then
  2564. inherited DataPos_ExeSection(exesec);
  2565. end;
  2566. { finally, update size/position of non-load segments }
  2567. for i:=0 to segmentlist.count-1 do
  2568. begin
  2569. seg:=TElfSegment(segmentlist[i]);
  2570. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2571. continue;
  2572. seg.DataPos:=TExeSection(seg.FSectionList.First).DataPos;
  2573. for j:=0 to seg.FSectionList.Count-1 do
  2574. begin
  2575. exesec:=TExeSection(seg.FSectionList[j]);
  2576. if oso_data in exesec.SecOptions then
  2577. seg.DataSize:=exesec.DataPos+exesec.Size-seg.DataPos;
  2578. end;
  2579. end;
  2580. { place section headers after the data }
  2581. shoffset:=CurrDataPos;
  2582. CurrDataPos:=CurrDataPos+ExeSectionList.Count*sizeof(TElfsechdr);
  2583. end;
  2584. procedure TElfExeOutput.DataPos_Segment(seg:TElfSegment);
  2585. var
  2586. i: longint;
  2587. exesec: TElfExeSection;
  2588. begin
  2589. for i:=0 to seg.FSectionList.Count-1 do
  2590. begin
  2591. exesec:=TElfExeSection(seg.FSectionList[i]);
  2592. { ELF needs DataPos set to 'would-be' value for sections that
  2593. don't have data, and for non-debug sections in .dbg file, too.
  2594. This slightly differs from generic approach. }
  2595. if not (oso_data in exesec.SecOptions) or
  2596. (
  2597. (ExeWriteMode=ewm_dbgonly) and
  2598. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[])
  2599. ) then
  2600. begin
  2601. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2602. exesec.DataPos:=CurrDataPos;
  2603. end
  2604. else
  2605. inherited DataPos_ExeSection(exesec);
  2606. end;
  2607. { calculate size of the segment }
  2608. seg.DataSize:=CurrDataPos-seg.DataPos;
  2609. end;
  2610. procedure TElfExeOutput.DataPos_ExeSection(const aname:string);
  2611. begin
  2612. // Ignore. Work is done entirely in datapos_start.
  2613. end;
  2614. procedure TElfExeOutput.InitDynlink;
  2615. begin
  2616. if not IsSharedLibrary then
  2617. begin
  2618. interpobjsec:=internalObjData.createsection('.interp',1,[oso_data,oso_load,oso_keep]);
  2619. interpobjsec.writestr(interpreter^);
  2620. end;
  2621. hashobjsec:=TElfObjSection.create_ext(internalObjData,'.hash',
  2622. SHT_HASH,SHF_ALLOC,sizeof(pint),4);
  2623. hashobjsec.secoptions:=[oso_keep];
  2624. dynsymtable:=TElfSymtab.create(internalObjData,esk_dyn);
  2625. dynamicsec:=TElfObjSection.create_ext(internalObjData,'.dynamic',
  2626. SHT_DYNAMIC,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(TElfDyn));
  2627. dynamicsec.SecOptions:=[oso_keep];
  2628. dynrelocsec:=TElfObjSection.create_reloc(internalObjData,'.dyn',true);
  2629. dynrelocsec.SecOptions:=[oso_keep];
  2630. dynbssobjsec:=TElfObjSection.create_ext(internalObjData,'.dynbss',
  2631. SHT_NOBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint){16??},0);
  2632. dynbssobjsec.SecOptions:=[oso_keep];
  2633. dynreloclist:=TFPObjectList.Create(true);
  2634. symversec:=TElfObjSection.create_ext(internalObjData,'.gnu.version',
  2635. SHT_GNU_VERSYM,SHF_ALLOC,sizeof(word),sizeof(word));
  2636. symversec.SecOptions:=[oso_keep];
  2637. verdefsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_d',
  2638. SHT_GNU_VERDEF,SHF_ALLOC,sizeof(pint),0);
  2639. verdefsec.SecOptions:=[oso_keep];
  2640. verneedsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_r',
  2641. SHT_GNU_VERNEED,SHF_ALLOC,sizeof(pint),0);
  2642. verneedsec.SecOptions:=[oso_keep];
  2643. dyncopysyms:=TFPObjectList.Create(False);
  2644. end;
  2645. const
  2646. hashbuckets: array[0..15] of longint=(
  2647. 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  2648. 16411, 32771);
  2649. {$push}{$r-,q-}
  2650. function elfhash(const name:string):longword;
  2651. var
  2652. g: longword;
  2653. i: longint;
  2654. begin
  2655. result:=0;
  2656. for i:=1 to length(name) do
  2657. begin
  2658. result:=(result shl 4)+ord(name[i]);
  2659. g:=result and $F0000000;
  2660. if g>0 then
  2661. result:=result xor (g shr 24);
  2662. result:=result and (not g);
  2663. end;
  2664. end;
  2665. {$pop}
  2666. procedure TElfExeOutput.WriteDynamicSymbolsHash;
  2667. var
  2668. nchains,nbuckets: longint;
  2669. i,j: longint;
  2670. hashdata: plongint;
  2671. sym: TExeSymbol;
  2672. begin
  2673. dynsymnames:=AllocMem(dynsymlist.count*sizeof(longword));
  2674. nchains:=dynsymlist.Count+1;
  2675. { determine suitable bucket count }
  2676. i:=high(hashbuckets);
  2677. while (i>=0) and (nchains<hashbuckets[i]) do
  2678. dec(i);
  2679. nbuckets:=hashbuckets[i];
  2680. hashdata:=AllocMem((2+nchains+nbuckets)*sizeof(longint));
  2681. hashdata[0]:=nbuckets;
  2682. hashdata[1]:=nchains;
  2683. { The contents of .dynsym can be written only after mempos pass
  2684. because it needs valid symbol virtual addresses and section indices.
  2685. Here we preset .dynsym size and write names, in order to get
  2686. correct size of .dynstr section. }
  2687. dynsymtable.size:=(dynsymlist.count+1)*sizeof(TElfsymbol);
  2688. for i:=0 to dynsymlist.Count-1 do
  2689. begin
  2690. sym:=TExeSymbol(dynsymlist[i]);
  2691. dynsymnames[i]:=dynsymtable.fstrsec.writestr(sym.objsymbol.name);
  2692. j:=(elfhash(sym.objsymbol.name) mod nbuckets)+2;
  2693. while hashdata[j]<>0 do
  2694. j:=2+nbuckets+hashdata[j];
  2695. hashdata[j]:=i+1;
  2696. end;
  2697. if source_info.endian<>target_info.endian then
  2698. for i:=0 to nchains+nbuckets+1 do
  2699. hashdata[i]:=swapendian(hashdata[i]);
  2700. hashobjsec.write(hashdata^,(2+nchains+nbuckets)*sizeof(longint));
  2701. freemem(hashdata);
  2702. end;
  2703. procedure TElfExeOutput.WriteVersionSections;
  2704. var
  2705. i,j: longint;
  2706. idx,auxidx: longword;
  2707. exesym: TExeSymbol;
  2708. dynobj: TElfDynamicObjData;
  2709. ver: TElfVersionDef;
  2710. vn: TElfverneed;
  2711. vna: TElfvernaux;
  2712. symversions: pword;
  2713. begin
  2714. symversions:=AllocMem((dynsymlist.count+1)*sizeof(word));
  2715. { Assign version indices }
  2716. idx:=VER_NDX_GLOBAL+1;
  2717. for i:=0 to dynsymlist.count-1 do
  2718. begin
  2719. exesym:=TExeSymbol(dynsymlist[i]);
  2720. if (exesym.objsymbol.indsymbol is TVersionedObjSymbol) then
  2721. ver:=TVersionedObjSymbol(exesym.objsymbol.indsymbol).version
  2722. else
  2723. ver:=nil;
  2724. if assigned(ver) then
  2725. begin
  2726. if ver.index=0 then
  2727. begin
  2728. ver.index:=idx;
  2729. inc(idx);
  2730. end;
  2731. symversions[i+1]:=ver.index;
  2732. end
  2733. else if exesym.state in [symstate_undefined,symstate_undefweak] then
  2734. symversions[i+1]:=VER_NDX_LOCAL
  2735. else
  2736. symversions[i+1]:=VER_NDX_GLOBAL;
  2737. end;
  2738. { Count entries to be written }
  2739. verneedcount:=0;
  2740. for i:=0 to neededlist.count-1 do
  2741. begin
  2742. dynobj:=TElfDynamicObjData(neededlist[i]);
  2743. dynobj.vernaux_count:=0;
  2744. for j:=2 to dynobj.versiondefs.count-1 do
  2745. begin
  2746. ver:=TElfVersionDef(dynobj.versiondefs[j]);
  2747. if ver.index>VER_NDX_GLOBAL then
  2748. inc(dynobj.vernaux_count);
  2749. end;
  2750. if (dynobj.vernaux_count>0) then
  2751. inc(verneedcount);
  2752. end;
  2753. { Now write }
  2754. idx:=0;
  2755. for i:=0 to neededlist.count-1 do
  2756. begin
  2757. dynobj:=TElfDynamicObjData(neededlist[i]);
  2758. if dynobj.vernaux_count=0 then
  2759. continue;
  2760. inc(idx);
  2761. vn.vn_version:=VER_NEED_CURRENT;
  2762. vn.vn_cnt:=dynobj.vernaux_count;
  2763. vn.vn_file:=dynobj.soname_strofs;
  2764. vn.vn_aux:=sizeof(TElfverneed);
  2765. vn.vn_next:=ord(idx<verneedcount)*(sizeof(TElfverneed)+vn.vn_cnt*sizeof(TElfvernaux));
  2766. MaybeSwapElfverneed(vn);
  2767. verneedsec.write(vn,sizeof(TElfverneed));
  2768. auxidx:=0;
  2769. for j:=2 to dynobj.versiondefs.count-1 do
  2770. begin
  2771. ver:=TElfVersionDef(dynobj.versiondefs[j]);
  2772. if ver.index<=VER_NDX_GLOBAL then
  2773. continue;
  2774. inc(auxidx);
  2775. vna.vna_hash:=elfhash(ver.name);
  2776. vna.vna_flags:=0; { BFD copies this from verdef.vd_flags?? }
  2777. vna.vna_other:=ver.index;
  2778. vna.vna_name:=dynsymtable.fstrsec.writestr(ver.name);
  2779. vna.vna_next:=ord(auxidx<dynobj.vernaux_count)*sizeof(TElfvernaux);
  2780. MaybeSwapElfvernaux(vna);
  2781. verneedsec.write(vna,sizeof(TElfvernaux));
  2782. end;
  2783. end;
  2784. TElfExeSection(verneedsec.ExeSection).shinfo:=verneedcount;
  2785. { If there are no needed versions, .gnu.version section is not needed }
  2786. if verneedcount>0 then
  2787. begin
  2788. if source_info.endian<>target_info.endian then
  2789. for i:=0 to dynsymlist.count+1 do
  2790. symversions[i]:=swapendian(symversions[i]);
  2791. symversec.write(symversions^,(dynsymlist.count+1)*sizeof(word));
  2792. end;
  2793. FreeMem(symversions);
  2794. end;
  2795. procedure TElfExeOutput.WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  2796. var
  2797. rel:telfreloc;
  2798. begin
  2799. rel.address:=dataofs;
  2800. rel.info:=ELF_R_INFO(symidx,typ);
  2801. {$push}{$r-}
  2802. rel.addend:=addend;
  2803. {$pop}
  2804. MaybeSwapElfReloc(rel);
  2805. dynrelocsec.write(rel,dynrelocsec.shentsize);
  2806. end;
  2807. procedure TElfExeOutput.WriteDynTag(aTag:longword;aValue:longword);
  2808. var
  2809. d: TElfDyn;
  2810. begin
  2811. d.d_tag:=aTag;
  2812. d.d_val:=aValue;
  2813. MaybeSwapElfDyn(d);
  2814. dynamicsec.write(d,sizeof(TElfDyn));
  2815. end;
  2816. procedure TElfExeOutput.WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword);
  2817. var
  2818. d: TElfDyn;
  2819. begin
  2820. d.d_tag:=aTag;
  2821. if source_info.endian<>target_info.endian then
  2822. d.d_tag:=swapendian(d.d_tag);
  2823. dynamicsec.write(d.d_tag,sizeof(d.d_tag));
  2824. { TODO: ignores endianness! }
  2825. dynamicsec.writeReloc_internal(aSection,aOffs,sizeof(d.d_ptr),RELOC_ABSOLUTE);
  2826. end;
  2827. procedure TElfExeOutput.WriteTargetDynamicTags;
  2828. begin
  2829. { to be overridden by CPU-specific descendants }
  2830. end;
  2831. procedure TElfExeOutput.WriteDynamicTags;
  2832. var
  2833. s: aword;
  2834. i: longint;
  2835. sym: TExeSymbol;
  2836. hs:string;
  2837. dynobj: TElfDynamicObjData;
  2838. begin
  2839. for i:=0 to neededlist.Count-1 do
  2840. begin
  2841. dynobj:=TElfDynamicObjData(neededlist[i]);
  2842. s:=dynsymtable.fstrsec.writestr(dynobj.name);
  2843. dynobj.soname_strofs:=s;
  2844. WriteDynTag(DT_NEEDED,s);
  2845. end;
  2846. if IsSharedLibrary then
  2847. begin
  2848. s:=dynsymtable.fstrsec.writestr(ExtractFileName(current_module.sharedlibfilename));
  2849. WriteDynTag(DT_SONAME,s);
  2850. { TODO: names hardcoded here }
  2851. sym:=TExeSymbol(ExeSymbolList.Find('FPC_SHARED_LIB_START'));
  2852. if assigned(sym) then
  2853. WriteDynTag(DT_INIT,sym.objsymbol.objsection,sym.objsymbol.offset);
  2854. sym:=TExeSymbol(ExeSymbolList.Find('FPC_LIB_EXIT'));
  2855. if assigned(sym) then
  2856. WriteDynTag(DT_FINI,sym.objsymbol.objsection,sym.objsymbol.offset);
  2857. end;
  2858. { TODO: we need a dedicated parameter to pass runpath, instead of this hack
  2859. (-Xr is a different thing, it passes "-rpath-link"). }
  2860. if (ParaLinkOptions<>'') then
  2861. begin
  2862. hs:=ParaLinkOptions;
  2863. while (hs<>'') do
  2864. begin
  2865. if (GetToken(hs,' ')='-rpath') then
  2866. begin
  2867. s:=dynsymtable.fstrsec.writestr(GetToken(hs,' '));
  2868. WriteDynTag(DT_RPATH,s);
  2869. end;
  2870. end;
  2871. end;
  2872. writeDynTag(DT_HASH,hashobjsec);
  2873. writeDynTag(DT_STRTAB,dynsymtable.fstrsec);
  2874. writeDynTag(DT_SYMTAB,dynsymtable);
  2875. writeDynTag(DT_SYMENT,sizeof(TElfSymbol));
  2876. if Assigned(gotpltobjsec) then
  2877. writeDynTag(DT_PLTGOT,gotpltobjsec);
  2878. end;
  2879. const
  2880. pltreltags: array[boolean] of longword=(DT_REL,DT_RELA);
  2881. relsztags: array[boolean] of longword=(DT_RELSZ,DT_RELASZ);
  2882. relenttags: array[boolean] of longword=(DT_RELENT,DT_RELAENT);
  2883. relcnttags: array[boolean] of longword=(DT_RELCOUNT,DT_RELACOUNT);
  2884. procedure TElfExeOutput.FinishDynamicTags;
  2885. var
  2886. rela: boolean;
  2887. begin
  2888. if assigned(dynsymtable) then
  2889. writeDynTag(DT_STRSZ,dynsymtable.fstrsec.size);
  2890. if hastextrelocs then
  2891. writeDynTag(DT_TEXTREL,0);
  2892. if Assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2893. begin
  2894. writeDynTag(DT_PLTRELSZ,pltrelocsec.Size);
  2895. writeDynTag(DT_PLTREL,pltreltags[pltrelocsec.shtype=SHT_RELA]);
  2896. writeDynTag(DT_JMPREL,pltrelocsec);
  2897. end;
  2898. if Assigned(dynrelocsec) and (dynrelocsec.size>0) then
  2899. begin
  2900. rela:=(dynrelocsec.shtype=SHT_RELA);
  2901. writeDynTag(pltreltags[rela],dynrelocsec);
  2902. writeDynTag(relsztags[rela],dynrelocsec.Size);
  2903. writeDynTag(relenttags[rela],dynrelocsec.shentsize);
  2904. {$ifndef MIPS}
  2905. if (relative_reloc_count>0) then
  2906. writeDynTag(relcnttags[rela],relative_reloc_count);
  2907. {$endif MIPS}
  2908. end;
  2909. WriteTargetDynamicTags;
  2910. if (verdefcount>0) or (verneedcount>0) then
  2911. begin
  2912. if (verdefcount>0) then
  2913. begin
  2914. writeDynTag(DT_VERDEF,verdefsec);
  2915. writeDynTag(DT_VERDEFNUM,verdefcount);
  2916. end;
  2917. if (verneedcount>0) then
  2918. begin
  2919. writeDynTag(DT_VERNEED,verneedsec);
  2920. writeDynTag(DT_VERNEEDNUM,verneedcount);
  2921. end;
  2922. writeDynTag(DT_VERSYM,symversec);
  2923. end;
  2924. writeDynTag(DT_NULL,0);
  2925. end;
  2926. procedure TElfExeOutput.CreatePLT;
  2927. var
  2928. reloc: TObjRelocation;
  2929. begin
  2930. pltobjsec:=TElfObjSection.create_ext(internalObjData,'.plt',
  2931. SHT_PROGBITS,SHF_ALLOC or SHF_EXECINSTR,4,16);
  2932. pltobjsec.SecOptions:=[oso_keep,oso_plt];
  2933. pltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.plt',true);
  2934. pltrelocsec.SecOptions:=[oso_keep];
  2935. ipltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.iplt',true);
  2936. ipltrelocsec.SecOptions:=[oso_keep];
  2937. { reference .dynamic from .got.plt, this isn't necessary if linking statically }
  2938. { TODO: maybe move writing initial .got.plt entries here completely
  2939. (needs testing --- GOT symbol may get lost if .got.plt is empty)}
  2940. if dynamiclink then
  2941. begin
  2942. reloc:=TObjRelocation.CreateSection(0,dynamicsec,RELOC_ABSOLUTE);
  2943. reloc.size:=sizeof(pint);
  2944. gotpltobjsec.ObjRelocations.Add(reloc);
  2945. end;
  2946. { Initial PLT entry, CPU-specific }
  2947. WriteFirstPLTEntry;
  2948. end;
  2949. procedure TElfExeOutput.WritePLTEntry(exesym:TExeSymbol);
  2950. begin
  2951. // must be implemented by CPU-specific descendant
  2952. InternalError(2012092102);
  2953. end;
  2954. procedure TElfExeOutput.WriteIndirectPLTEntry(exesym:TExeSymbol);
  2955. begin
  2956. // must be implemented by CPU-specific descendant
  2957. InternalError(2012092101);
  2958. end;
  2959. function TElfExeOutput.WriteData:boolean;
  2960. begin
  2961. WriteHeader;
  2962. segmentlist.ForEachCall(@segment_write_header,nil);
  2963. WriteExeSectionContent;
  2964. FWriter.WriteZeros(sizeof(TElfsechdr));
  2965. ExeSectionList.ForEachCall(@exesection_write_header,nil);
  2966. result:=true;
  2967. end;
  2968. procedure TElfExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2969. var
  2970. exportlist: TCmdStrList;
  2971. sym: TExeSymbol;
  2972. begin
  2973. AllowUndefinedSymbols:=IsSharedLibrary;
  2974. { add exported symbols to dynamic list }
  2975. exportlist:=texportlibunix(exportlib).exportedsymnames;
  2976. if not exportlist.empty then
  2977. repeat
  2978. sym:=TExeSymbol(ExeSymbolList.Find(exportlist.getfirst));
  2979. if assigned(sym) then
  2980. begin
  2981. if assigned(sym.objsymbol.objsection) then
  2982. sym.objsymbol.objsection.SecOptions:=[oso_keep];
  2983. sym.dynindex:=dynsymlist.add(sym)+1
  2984. end
  2985. else
  2986. InternalError(2012071801);
  2987. until exportlist.empty;
  2988. end;
  2989. procedure TElfExeOutput.ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2990. begin
  2991. { TODO: include objsec properties into message }
  2992. Comment(v_error,'Relocation '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+''' cannot be used when linking a shared object; recompile with -Cg');
  2993. end;
  2994. procedure TElfExeOutput.ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2995. begin
  2996. { TODO: include objsec properties into message }
  2997. Comment(v_error,'Relocation truncated to fit: '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+'''');
  2998. end;
  2999. {****************************************************************************
  3000. TElfExeSection
  3001. ****************************************************************************}
  3002. procedure TElfExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  3003. begin
  3004. inherited AddObjSection(objsec,ignoreprops);
  3005. if ignoreprops then
  3006. exit;
  3007. if (shtype=SHT_NULL) then
  3008. begin
  3009. shtype:=TElfObjSection(objsec).shtype;
  3010. shflags:=TElfObjSection(objsec).shflags;
  3011. shentsize:=TElfObjSection(objsec).shentsize;
  3012. end;
  3013. end;
  3014. {****************************************************************************
  3015. TElfSegment
  3016. ****************************************************************************}
  3017. constructor TElfSegment.Create(atype,aflags,aalign:longword);
  3018. begin
  3019. ptype:=atype;
  3020. pflags:=aflags;
  3021. align:=aalign;
  3022. FSectionList:=TFPObjectList.Create(false);
  3023. end;
  3024. destructor TElfSegment.Destroy;
  3025. begin
  3026. FSectionList.Free;
  3027. inherited Destroy;
  3028. end;
  3029. procedure TElfSegment.Add(exesec:TExeSection);
  3030. begin
  3031. FSectionList.Add(exesec);
  3032. end;
  3033. end.