ogelf.pas 107 KB

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