ogelf.pas 119 KB

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