ogelf.pas 119 KB

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