ogelf.pas 119 KB

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